talha khalid
talha khalid

Reputation: 301

merging two channels data from audiobuffer in javascript

how can we merge data from two channels of audio buffer to an arrayBuffer so we can convert that arrayBuffer to Blob?

var floatarray1 = new Float32Array();
audioBuffer.copyFromChannel(floatarray1,0);

var floatarray2 = new Float32Array();
audioBuffer.copyFromChannel(floatarray2,1);

how can we merge "floatarray1" and "floatarray2" which are being copied from an "audioBuffer" into a single "arrayBuffer"?

Upvotes: 1

Views: 604

Answers (1)

tgarm
tgarm

Reputation: 493

To "merge" two channels of audio into a single channel. This can be called audio "mix". A very simple mix is just to add each corresponding sample together, like this:

var mixed = new Float32Array(floatarray1.length)

for(i=0;i<floatarray1.length;i++){
    mixed[i] = floatarray1[i]+floatarray2[i]
}

If the audio volume is pretty high, and is possibly "overflow", you may need to pick a factor (<1.0) to multiplied with each sample, to reduce the risk of overflow:

const voladj = 0.6
var mixed = new Float32Array(floatarray1.length)

for(i=0;i<floatarray1.length;i++){
    mixed[i] = (floatarray1[i]+floatarray2[i])*voladj
}

I think this simple algorithm is already good enough to calculate it in Javascript.

Hope this helps.

Upvotes: 1

Related Questions