Programmatically merging two pieces of audio

后端 未结 3 1561
南笙
南笙 2020-12-29 16:54

I have two arrays of samples of two different audio clips. If I just programmatically add them together will this be the equivalent of layering one track over another in an

3条回答
  •  情深已故
    2020-12-29 17:25

    This IS a correct way. Merging is called MIXING in audio jargon.

    BUT:

    If your samples are short (16 bit signed) - you will have to use int (32 bit signed) for addition and then clip the samples manually. If you don't, your values will wrap and you'll have so much fun listening to what you did :)

    Here comes the code:

    short first_array[1024];
    short second_array[1024];
    short final_array[1024];
    for (int i = 0; i < length_of_array; i++)
    {
        int mixed=(int)first_array[i] + (int)second_array[i];
        if (mixed>32767) mixed=32767;
        if (mixed<-32768) mixed=-32768;
        final_array[i] = (short)mixed;
    }
    

    In MOST cases you don't need anything else for normal audio samples, as the clipping will occur in extremely rare conditions. I am talking this from practice, not from theory.

提交回复
热议问题