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

Is there a way to know when sound has been loaded? #21

Closed warpdesign closed 9 years ago

warpdesign commented 9 years ago

I'd like to know if there is a way to be notified when a sound has loaded properly, through an "onLoad" mechanism, or maybe another way? (like you would have when using

I didn't see anything into the documentation.

rserota commented 9 years ago

There are a couple of things you can do in this case. Neither of them are documented, because I wasn't sure what would actually be the most user-friendly/practical solution.

Probably the more straightforward option is using a callback. When you create a Wad or PolyWad, you can pass in a callback function, which will be called with the newly created Wad as an argument. For example,

Var sound = new Wad({ source : 'http://path/to/sound.wav', callback : function(wad){ wad.play(); } })

Also, Wads and PolyWads have a 'playable' attribute, which is used internally. It is initially set to 1. If the Wad needs to request an external audio file (or do some other asynchronous operation during setup), then 'playable' gets decremented. When those asynchronous actions complete, 'playable' gets incremented. So, if you ever check the 'playable' attribute, and it is less than 1, that Wad is still waiting for something.

I hope that helps. Let me know if you have any further questions.

On Sun, Nov 16, 2014 at 2:36 AM, Nicolas Ramz notifications@github.com wrote:

I'd like to know if there is a way to be notified when a sound has loaded properly, through an "onLoad" mechanism, or maybe another way? (like you would have when using tag for example)

I didn't see anything into the documentation.

— Reply to this email directly or view it on GitHub https://github.com/rserota/wad/issues/21.

warpdesign commented 9 years ago

I tried your piece of code with a simple wave file. The callback gets called, but arguments list is empty: is it normal ? (looking at network resources, it seems my audio file has correctly been loaded).

warpdesign commented 9 years ago

I also have another (unrelated) question: is it possible to set loop after instanciating a sound object ? (maybe when playing or with a setLoop() method ?)

rserota commented 9 years ago

You're right that no arguments were being passed in. That was an error in the code, but I just fixed it. If you try this again after pulling down fresh code, you should find that your newly created Wad is the argument to your callback. For the simple case of needing to play an audio file as soon as it's loaded, you don't actually need a callback for that. If you try to play an audio clip before the audio file has finished loading, Wad will wait until the file has loaded, and THEN call play.

For your second question, even if you don't set 'loop' on the constructor, you can still set 'loop : true,' when you call play. There is an example in the docs of passing this parameter to the 'play()' method, but I suppose it's easy to miss.