I am trying to mix two WAV files.
The WAV files are available as byte arrays and I am using below code to mix the two.
byte[] byte1 , byte[] byte2
// 44 is header of wav file
for( int i = 44 ; i < byte1.length ; i++){
byte1[i] = byte1[i] + byte2[i];
}
The above code mostly works. But when the result is more than maximum wave (16 bit audio file), it has noise. How can I normalize mixed sound?
this works, thanks all for answers
now it is in short, it won't work because value max is 32768 (max short) and nothing changed
First of all, if, indeed, your audio is 16 bits, adding it byte-by-byte won't work. Other people commented on this. You can see my answer here for how to handle this problem.
using Android's AudioTrack to combine bytes of sound samples produces noise
Secondly, to "normalize" it, you'll have to find the peak first, and then scale all results to that value. That means two loops: one to find the "peak" and one to add the values, scaling to the new peak. Something like this: