Prinzhorn
Prinzhorn

Reputation: 22518

Firefox Web Audio API on-the-fly update AudioBuffer / AudioBufferSourceNode

I'm creating a 1s audio snippet by programmatically filling a AudioBuffer. The AudioBufferSourceNode has looping enabled. It plays back just fine in Chrome and Firefox.

Now I want to dynamically update the AudioBuffer and have the new audio picked up immediately (or at the next loop). In Chrome this works perfectly by simply getting the channel data (getChannelData(0)) and writing to it. Chrome updates the playing audio on-the-fly. Firefox keeps playing the original buffer over and over again. In fact in Firefox the AudioBuffer needs to be written before assigning it to the AudioBufferSourceNode (source.buffer = buffer).

Upvotes: 1

Views: 952

Answers (2)

cwilso
cwilso

Reputation: 13928

This should not be done this way. You're trying to update an object across a thread boundary.

Chrome has a bug where we don't currently implement the memory protection (i.e. you can update the contents of the AudioBuffer and it will change what the looping buffer sounds like). FF currently has a different bug, where it allows you to set the .buffer more than once. These should both get fixed.

To address this scenario, you need to loop each buffer until you get then next one, then cross-fade between them. It's unlikely just looping a 1s buffer is really what you want anyway? (unless it's noise.)

Upvotes: 3

Prinzhorn
Prinzhorn

Reputation: 22518

Simply assigning the same buffer again will update Firefox's internal state. So just do source.buffer = buffer again after updating the buffer. Even though it should be a NOOP, since it's the exact same reference.

Even source.buffer = source.buffer does the trick.

Upvotes: 0

Related Questions