Something strange is happening here. I create a audio buffer store it in a variable and try to re-use it several times - but it seems to be corrupted
I make some buttons
<button onclick="play();">play(0)</button>
<button onclick="playsection();">play section</button>
<button onclick="stop();">stop()</button>
Get some audio data
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() multiple times without an error
function play(){
p = context.createBufferSource();
p.buffer = wavbuf;
p.connect(context.destination);
p.start(0);
}
playsection seems to work only once - or occasionally more than once if I press stop before stop(10) evaluates
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();
}
Seems like p.buffer = wavbuf creates a pointer into the buffer rather than a copy
Is this a bug or a feature?
So this is interesting, it will play the section consistently either without the stop:
function playsection(){
p = context.createBufferSource();
p.buffer = wavbuf;
p.connect(context.destination);
p.start(0, 6); // start after 6 seconds
}
or without the 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 declaring the offset and duration within the start:
function playsection(){
p = context.createBufferSource();
p.buffer = wavbuf;
p.connect(context.destination);
p.start(0,6,10);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With