5

I'm looking to append data to an already existing AudioBuffer that is being played using web audio.


Upon receiving audio data to play, I create an AudioBuffer and then assign the data to the ArrayBuffer response.

    var audioBuffer = context.createBuffer(1, audioToPlay.length, 16000);
    audioBuffer.getChannelData(0).set(audioToPlay);

I then create a buffer source node and hook up the audio buffer to the web audio context.

    var source = context.createBufferSource();
    source.buffer = audioBuffer;
    source.connect(context.destination);
    source.start(0);

Now, what I would like to do is append more data (another ArrayBuffer) to the currently playing buffer, while avoiding having to use the onended callback to create a new buffer..

Is there any way to do this? From what I can tell, this does not appear to be supported.

Nirvana Tikku
  • 4,105
  • 1
  • 20
  • 28
  • Have you looked at http://stackoverflow.com/questions/14143652/web-audio-api-append-concatenate-different-audiobuffers-and-play-them-as-one-son – Rob M. Sep 26 '14 at 21:05
  • Yes - however, in this case, Im dealing with a stream (I don't have all the buffers up front) – Nirvana Tikku Sep 26 '14 at 21:06

1 Answers1

7

You cannot do this. Once a playing buffer has been handed off to the audio thread via start(), you can't modify it.

However, that said, you absolutely should not be using onended, either. What you should do is keep track of the last audiobuffer scheduled and the currentTime at which it was started, and create and play a NEW audio buffer at time = lastCurrentTimeStart+lastBufferDuration. You should be able to get a smooth, uninterrupted playback.

cwilso
  • 13,610
  • 1
  • 30
  • 35
  • 2
    Thanks for the validation. So it sounds like I will also have to track the state of the player? (i.e. if the user paused, if the playback rate was diff etc) – Nirvana Tikku Sep 27 '14 at 16:11