Strange buffer behavior

There is something strange here. I create an audio buffer that stores it in a variable and try to reuse it several times - but it seems to be damaged

I make a few buttons

<button onclick="play();">play(0)</button> <button onclick="playsection();">play section</button> <button onclick="stop();">stop()</button> 

Get some audio

 context = new AudioContext(); var getWav = new XMLHttpRequest(); var wavbuf; getWav.open("GET", "/wav/test.wav", true); getWav.responseType = "arraybuffer"; getWav.onload = function() { context.decodeAudioData(getWav.response, function(buffer){ wavbuf = buffer; }); } getWav.send(); var p; 

I can evaluate play () several times without error

 function play(){ p = context.createBufferSource(); p.buffer = wavbuf; p.connect(context.destination); p.start(0); } 

reproduced seems to work only once - or sometimes more than once if I stop before stopping (10), evaluating

 function playsection(){ p = context.createBufferSource(); p.buffer = wavbuf; p.connect(context.destination); p.start(0, 6); // start after 6 seconds p.stop(10); // stop after 10 seconds } function stop(){ p.stop(); } 

It looks like p.buffer = wavbuf is creating a pointer to the buffer, not a copy

Is this a bug or function?

+6
source share
1 answer

So, this is interesting, it will play the section sequentially without stopping:

 function playsection(){ p = context.createBufferSource(); p.buffer = wavbuf; p.connect(context.destination); p.start(0, 6); // start after 6 seconds } 

or without offset:

 function playsection(){ p = context.createBufferSource(); p.buffer = wavbuf; p.connect(context.destination); p.start(0); p.stop(10); // stop after 10 seconds } 

and even declare the offset and duration at the beginning:

 function playsection(){ p = context.createBufferSource(); p.buffer = wavbuf; p.connect(context.destination); p.start(0,6,10); } 
+1
source

All Articles