Web Audio API - merge two audio buffers
Asked Answered
B

3

5

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?

Berserker answered 20/1, 2016 at 10:51 Comment(0)
W
3

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.

Waylan answered 21/1, 2016 at 16:55 Comment(0)
C
3

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.

Capote answered 2/2, 2023 at 14:13 Comment(1)
This should be the right answer. The Buffer is just the sine wave and you can just add Buffers to one anotherBallyhoo
C
2

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

Crochet answered 18/8, 2019 at 20:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.