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
Your above merging method will work if the sample rates and desired mix level of the two audio clips are identical. If the desired mix levels are different, then a slightly more general form of your mixer would be something like:
mixed_result[i] = rescale_and_clip_fix( volume1 * input1[i] + volume2 * input2[i] );
Where rescale_and_clip_fix() might be a limiter or compressor, following making sure the scale after multiplication is correct for the result's data type. If the result array is an integer data type, then you may also want to do rounding or noise filtering while scaling.
If the sample rates are different, then you will need to do a sample rate conversion on one of the input channels first and/or the result afterwards.
In general, this will get you what you want - however, watch for clipping. That is, be careful not to end up with integer overflow; and don't avoid this by just limiting the value to the max/minimum of the type in question. You may need to apply a compressor to bring the values back into range after adding them.
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.