Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

web audio buffer strange behaviour

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?

like image 798
ja. Avatar asked Feb 03 '16 09:02

ja.


1 Answers

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);
}
like image 55
ktec Avatar answered Oct 28 '22 05:10

ktec