user3759750
user3759750

Reputation: 153

Web Audio API - merge two audio buffers

I have audio files and I am using Web Audio API to play them on browser. Now I want to play all the audio files simultaneously.

I got audio buffers for all the audio files using Web Audio API. I can loop through all and can play them but I want to merge all the audio buffers into one single audio buffer and then play it.

Does anyone have an idea to merge two audio buffers in Web Audio API?

Upvotes: 5

Views: 5275

Answers (3)

Raffael Bechara Rameh
Raffael Bechara Rameh

Reputation: 408

If you sum two or more audio PCM arrays, i.e. buffer1[i] + buffer2[i] for each sample, you will have a third audio containing both sounding together.

You can even use a multiply factor over each track to control its volume, for example.

Hope this helps. This is just the begining.

Upvotes: 3

Dima Melnik
Dima Melnik

Reputation: 884

If you need just to play it you should to connect both of sources to destination.

To really merge two buffers into another one, you can use offlineAudioContext or merge arrays yourself like this https://github.com/audiojs/audio-buffer-utils

Upvotes: 2

Raymond Toy
Raymond Toy

Reputation: 6056

If you want to play all of the simultaneously, just connect each of your buffers to context.destination (or most any other node that is connected to the destination) and start them all at the same time.

Upvotes: 3

Related Questions