Re: AudioBufferSourceNode: how to do restart?

Hi Yehonathan,

The answer to your question partly depends on how quickly you need to do this “restart” in response to whatever causes it. If you have enough time in advance to schedule the restart, you can create a new node with the same buffer, schedule the old node to stop at an exact time, and schedule the new node to start at that same exact time. There should be no audible gap with this approach (although there might be a glitch in the sound due to a discontinuity).

You might get better results by scheduling cross-fades by attaching GainNodes to both the old and new nodes, and using their gain attribute's setValueCurveAtTime() function (or other ramp-based function — see the AudioParam API for details) to quickly fade out the old node and fade in the new node, rather than relying on stop and start to cause the audible transition.

Finally, if you are responding to an event triggering the restart immediately in real time, you may need to account for the fact that there is latency in the audio pipeline; things that you schedule for “right now” will not be immediately audible. In that case, you may find it helpful to schedule the switch a short time after the value AudioContext.currentTime. Knowing exactly how far in the future to do this is unfortunately not easy to determine with the current API, since you can’t get reliable latency information yet. You might need to determine some ad hoc latency values through experimentation on various platforms until the API is improved in this respect.

Hope this is helpful.

Best,

.            .       .    .  . ...Joe

Joe Berkovitz
President

Noteflight LLC
Boston, Mass.
phone: +1 978 314 6271
www.noteflight.com
"Your music, everywhere"


On Jul 23, 2014, at 10:08 AM, Yehonathan Sharvit <viebel@gmail.com> wrote:

> I cannot find in the API (http://webaudio.github.io/web-audio-api/) a way to restart  an AudioBufferSourceNode. The “loop” mode is not appropriate as I need to restart the node before it ends.
> 
> I have tried to implement it by doing:
> 1. stop
> 2. recreate the node from the buffer
> 3. start
> 
> But then there is a fraction of second with no sound making the transition not smooth.
> 
> What is the best way to restart an AudioBufferSourceNode?

Received on Friday, 25 July 2014 16:25:30 UTC