metabolic
metabolic

Reputation: 689

First couple AudioChunks are played simultaneously

I'm using the WebAudio api to play a stream from the server. I do that the following way:

    var d2 = new DataView(evt.data);

    var data = new Float32Array(d2.byteLength / Float32Array.BYTES_PER_ELEMENT);
    for (var jj = 0; jj < data.length; ++jj) {
        data[jj] = d2.getFloat32(jj * Float32Array.BYTES_PER_ELEMENT, true);
    }

    var buffer = context.createBuffer(1, data.length, 44100);
    buffer.getChannelData(0).set(data);

    source = context.createBufferSource();
    source.buffer = buffer;
    source.start(startTime);
    source.connect(context.destination);

    startTime += buffer.duration;

startime is initialized with 0. This works fine but it seams to me, that the first couple chunks are played simultaneously... This takes about 1sec and after that the audio is played normal.

Does anybody have an idea why this is happening? Thanks, metabolic!

Upvotes: 1

Views: 27

Answers (1)

cwilso
cwilso

Reputation: 13908

startTime shouldn't be initialized with 0, it should be initialized with context.currentTime. Actually, probably currentTime + some slight delay for slop.

The context clock starts as soon as the context is created. I'm guessing there's a bit of a gap between when the context is created and when your first chunk is played - so it's trying to play it in the past, and it just plays it as soon as possible. The next chunk or two that comes in, same thing - after that, I expect it settles down to a predictable pattern, or (more likely) you're always trying to start() with a time that's in the past, so it just plays it as soon as possible, and you're just not hearing the glitching at boundaries.

Upvotes: 1

Related Questions