PCM Mix

I am not sure weather I have fully understood your question or not, I persume that you are asking 
"How can we mix two or more audio stream", If this is the question then I am explaning below the 
mixing of the two audio stream (You Can Mix More Audio Stream),

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 possitve

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 (mixname, argv[1]);
  strcat (mixname, "_temp.wav");
  mix = fopen(mixname, "w");

  while(!feof(pcm1)) {

    sample1 = fgetc(pcm1);
    sample2 = fgetc(pcm2);
    
    if ((sample1 < 0) && (sample2 < 0)) {
      value = sample1 + sample2 - (sample1 * sample2 / -(pow(2,16-1)-1));
    }else{
      value = sample1 + sample2 - (sample1 * sample2 / (pow(2,16-1)-1));
    }

    fputc(value, mix);
  }


  fclose(pcm1);
  fclose(pcm2);
  fclose(mix);

  return 0;
}


另一个算法

C = A + B - (A * B >> 0x10)

  A和B就是两路不同的音频数据,C就是混音后的音频数据,当然,处理后,还需要对C进行防止数据溢出的处理,否则,可能会有爆音。

如果是16bit音频数据,就是:

if (C > 32767) C = 32767;
else if (C < -32768) C = -32768;

如果是float音频数据,就是:

if (C > 1) C = 1;
else if (C < -1) C = -1;
  这个算法针对的是16bit的音频采样数据,我实验的结果是:对float音频采样数 据,同样有不错的效果
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值