rserota / wad

Web Audio DAW. Use the Web Audio API for dynamic sound synthesis. It's like jQuery for your ears.
MIT License
1.9k stars 160 forks source link

What's the best way to play the same sound multiple times #22

Closed warpdesign closed 9 years ago

warpdesign commented 9 years ago

I'm working on a game engine and the same sound may be played simultaneously multiple times: should I create new Wad objects for each time it is played, or will calling play() several times on the created instance play the sound several times?

Btw, if I only have to create an instance: will doing myWad.setVolume() impact current playing sounds, or only future calls to myWad.play() ?

rserota commented 9 years ago

You only need one Wad object. If you call play() multiple times, you will hear multiple, independent instances of that sound. Calling myWad.setVolume() will change the volume of the most recent call to play() (if one or more instances of the sound are still playing), as well as future calls to play().

If you need to keep control over all currently sustained sounds, not just the most recent, you might need to use 'play labels'. Check the docs, but the basic idea is that when you call play(), you can pass in a string to label that play() call, so you can stop it later, even if you called play() several more times from the same Wad. The setVolume method doesn't work with play labels the way that the stop method does, but it really should. If that sounds like something you need to do (play a wad simultaneously multiple times, and maintain volume control over each instance individually), let me know, and I can work on implementing it soon.