I have two raw sound streams that I need to add together. For the purposes of this question, we can assume they are the same bitrate and bit depth (say 16 bit sample, 44.1k
I found a new way to add samples in a way in which they can never exceed a given range. The basic Idea is to convert values in a range between -1 to 1 to a range between approximately -Infinity to +Infinity, add everything together and reverse the initial transformation. I came up with the following formulas for this:
I tried it out and it does work, but for multiple loud sounds the resulting audio sounds worse than just adding the samples together and clipping every value which is too big. I used the following code to test this:
#include
#include
#include
#include
#include
#include
#include
#include
// fabs wasn't accurate enough
long double ldabs(long double x){
return x < 0 ? -x : x;
}
// -Inf 1. )
ret = 1.;
return ret;
}
// -1<=input<=+1, -Inf