Wav文件直接反映了一个声音在每个时刻的大小值,比如说以下一段波形:
我们按每人0.1秒取一点,得到的wav文件数值就是0,1,1,-1,0,1。因此,假如我们能把许多Wav文件的数据直接相加,你听到的就是所有的声音,这就是混音器的原理。
Step 1, Get the Raw data of the two files, (Example, of the sample 8bit and 8Kh, means one sample is of 8bit)
Step 2 Let the two audio signal be A and B respectively, the range is between 0 and 255. Where A and B are the Sample Values (Each raw data) And store the resultant into the Y
If Both the samples Values are possitv Y = A + B - A * B / 255
Where Y is the resultant signal which contains both signal A and B, merging two audio streams into single
stream by this method solves the problem of overflow and information loss to an extent.
If the range of 8-bit sampling is between -127 to 128
If both A and B are negative Y = A +B - (A * B / (-127))
Else Y = A + B - A * B / 128
Similarly for the nbit (ex 16bit data)
For n-bit sampling audio signal
If both A and B are negative Y = A + B - (A * B / (-(2 pow(n-1) -1)))
Else Y = A + B - (A * B / (2 pow(n-1))
Step 3.
Add the Header to the Resultant (mixed) data and play back.
If some thing is unclear and ambigious let me know.
Regards
Ranjeet Gupta.
还有简单C程序示意代码,但是其中包含了核心算法:
#include
#include
#include
#include
int main(int argc,char *argv[]) {
char mixname[255];
FILE *pcm1, *pcm2, *mix;
char sample1, sample2;
int value;
pcm1 = fopen(argv[1],“r”);
pcm2 = fopen(argv[2],“r”);
strcpy