How do I tell the AudioBufferSourceNode to start and end in the middle of the buffer?

I am using HTML5 audio app to incorporate sounds into my game. How do I create an AudioBufferSourceNode that starts and stops playback in the middle of an AudioBuffer?

I didn't find anything in the AudioBufferSourceNode interface for creating an AudioBufferSourceNode that starts in the middle of the buffer: https://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html#AudioBufferSourceNode

I suppose I could create an AudioBufferSourceNode with a snippet of the original AudioBuffer, but I want to save memory since my game is already using a lot of javascript heap of memory, so I really don't want to duplicate AudioBuffers or create parts of it. I just want to make multiple AudioBufferSourceNodes, each playing parts of a single AudioBuffer.

How do I do it?

+3


source to share


1 answer


Just use additional (optional) parameters for AudioBufferSourceNode#start

// begin playback immediately, starting at second 10 of the buffer 
// and lasting 20 seconds
sourceNode.start( audioContext.currentTime, 10, 20 );

      

The first parameter when

that you are probably familiar with. It simply determines when the sound will start playing using the coordinate system AudioContext#currentTime

.



Second parameter offset

. This is the number of seconds in your instance AudioBuffer

. So if you want to start playing 10 seconds later to the buffer, you must pass 10

here.

Third parameter duration

. Also in seconds, and hopefully pretty self-explanatory. Otherwise, it determines how long the sound will play.

+5


source







All Articles