c# - Playing 30 sound effects at the same time repeatedly -
i'm trying play 30 piano notes @ same time in xna application on windows phone 7. have imported , loaded wave files below
protected override void loadcontent() { spritebatch = new spritebatch(graphicsdevice); sound3 = content.load<soundeffect>("1"); sound5 = content.load<soundeffect>("3"); sound6 = content.load<soundeffect>("4"); sound7 = content.load<soundeffect>("5"); sound8 = content.load<soundeffect>("6"); }
every sound effect file less second, i'm trying play of them @ same time. play them using loop runs every second.(so @ every loop 30 sounds played goes on , plays same 30 sounds each second) works fine few seconds stops playing sound (the loop still working) again starts working once or twice , again stops working . makes bad noises if audio system cant support many sounds play @ time.
i'm not sure how can solve problem , if buffer problem or threads.
nico schertler right! i've faced same problem , fixed managing instances. when play soundeffect instance! default have hope xna/monogame care it, doesn't. when have many live instances (even stopped, breaks, noise, silence.
i hold rectangular array sound effects 4 instance each, , if have instances of sound want play instead of creating stop oldest (by timestamp), play , remember current timestamp.
see following code:
private const int max_inst_of_one_sound = 4; private soundeffectinstance[][] sndinstarray = null; private float[][] sndinsttimes = null; public init() { sndarray = new soundeffect[soundsschema.sounds.length]; sndinstarray = new soundeffectinstance[soundsschema.sounds.length][]; sndinsttimes = new float[soundsschema.sounds.length][]; (int = 0; < soundsschema.sounds.length; i++) { try { sndarray[i] = content.load<soundeffect>(soundsschema.sounds[i]);//soundsschema string list holder class } catch (system.exception) { } sndinstarray[i] = new soundeffectinstance[max_inst_of_one_sound]; sndinsttimes[i] = new float[max_inst_of_one_sound]; } } private soundeffectinstance getvalidinstance(int sound) { if (sound < 0 || sound > sndinstarray.length) return null; soundeffectinstance inst = null; (int = 0; < max_inst_of_one_sound; i++) { if (sndinstarray[sound][i] == null || (sndinstarray[sound][i] != null && sndinstarray[sound][i].isdisposed)) { sndinstarray[sound][i] = sndarray[sound].createinstance(); sndinsttimes[sound][i] = myengine.curtime; inst = sndinstarray[sound][i]; break; } } if (inst == null) { float min_time = float.maxvalue; int ind = -1; (int = 0; < max_inst_of_one_sound; i++) { if (sndinstarray[sound][i] != null && sndinsttimes[sound][i] < min_time) { min_time = sndinsttimes[sound][i]; ind = i; } } if (ind == -1) ind = 0; if (sndinstarray[sound][ind].isdisposed) sndinstarray[sound][ind] = sndarray[sound].createinstance(); else { try { sndinstarray[sound][ind].stop(); } catch {} } sndinsttimes[sound][ind] = myengine.curtime; inst = sndinstarray[sound][ind]; } return inst; } public virtual void playsound(int sound, float volume, float panoram, bool loop) { if (sound < 0 || sound > sndarray.length) return null; if (!mmuted && mvolume > 0) { soundeffectinstance sndinst = getvalidinstance(sound); if (sndinst == null) return null; try { sndinst.islooped = loop; sndinst.pan = panoram; sndinst.volume = mvolume * vol; sndinst.play(); } catch { } } }
Comments
Post a Comment