rserota / wad

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

How to add onEnd event listener #133

Closed hajkli closed 3 years ago

hajkli commented 3 years ago

Hi,

Is possible to add some event listener when one sound end?

For example:

this.Signal1 = new Wad({ source: signal_1 }); this,Signal.on('end', ()=> {...})

Thank you.

rserota commented 3 years ago

There isn't an end event, but the play() method returns a promise that resolves when the sound ends.

const signal = new Wad({ source: '/audio/signal.wav' })
signal.play().then(()=>{
    // this function should run when the sound finishes playing
})

There's also an example of this in the docs.

hajkli commented 3 years ago

HI Rserota.

Thank you very much for you answer and help.

First I did something like this:

signal1.soundSource.onended = function () {
   _this.signal2.play();
};

but your solution with .then(...) is better and more elegant I think.

Can I have one more question, please?

This works quite fine in safari and desktop chromium based browsers (chrome, edge, opera). But there is small issue on Firefox and on android mobile. There is "technical silence" between signal1 and signa2 (when signal1 ended => then => signal2 is played), which I can hear with my ears. Do you have any idea how this issue can be resolve?

rserota commented 3 years ago

I'm not able to reproduce this issue on Firefox. Can you post the actual code you're using, that is causing problems?

hajkli commented 3 years ago

Hi Rserota,

thank you for your willingness to look into this issue.

I created very simple example: https://github.com/hajkli/WAD_firefox_test

When you click on button, it plays "Signal 1" then it plays "Signal 2" then "Signal 1" in the infinite loop.
I used the most basic example, only with this kind of signal. In real production it is much more complex and with much better sound :).

But this "Signal" sound is very good for recording the issue, because it is easy to hear and record the silence between sound segments. It works fine on safari and desktop chrome, but on FF and mobile chrome it works kind of weird. You can hear the "technical silence" between signals.

Thank you.

rserota commented 3 years ago

I tested your example on a few different devices, and in a few different browsers, including the ones you listed. I can't hear any differences on any browser or device. Your issue might be due to quirks of the specific browser or OS version you're using, but I probably can't help you debug that any further.