2 Wav file mixing

Does anybody know how to mix together two Wav files to playy at the same time.
closed account (Dy7SLyTq)
get an audio libraby like openAL or sfml-audio
You can either:

1. use an API like DTSCode suggests and just play two streams at the same time. This works well if you are mixing during runtime.

or

2. Simply load both WAVE files into memory and then "add" the signals togeather. This is an easy way to do it if you are "preprocessing"

1
2
3
4
5
6
7
8
9
10
short* wav1data;
short* wav2data;
short* wavOutData;
// Load wav1, wav2 and allocate the size of wavOut

float gain1 = 1.f; // Tune these to your liking
float gain2 = 1.f;

for (int &i : wavOutData)
    wavOutData[i] = clip(gain1 * wav1data[i] + gain2 * wav2data[i]);


Where clip will "clip" your signal to avoid overflows:
1
2
3
4
5
6
7
8
short clip(int input)
{
    if (input > 0x7FFF)
        return 0x7FFFF;
    if (input < - 0x7FFF)
        return -0x7FFF;
    return input;
}


Last edited on
Topic archived. No new replies allowed.