DSP

DSP-C6416-录音混音算法工程

2019-07-13 10:16发布

//record.c record/play input using external memory


#include "dsk6416_aic23.h" //codec support
Uint32 fs=DSK6416_AIC23_FREQ_8KHZ; //set sampling rate
//#define DSK6416_AIC23_INPUT_MIC 0x0015
//#define DSK6416_AIC23_INPUT_LINE 0x0011
//Uint16 inputsource=DSK6416_AIC23_INPUT_MIC; // select input
#define N 2400000    //large buffer size 300 secs
#define n 32                //for 32-bit input
long i,j,buffer_size;
short buffer[N];
short buffer_mix[N];
short buffer_voice[N];
char sourseFile[10][2];  
#pragma DATA_SECTION(buffer,".EXT_RAM0") //buffer ->external memory
#pragma DATA_SECTION(buffer_voice,".EXT_RAM1")
#pragma DATA_SECTION(buffer_mix,".EXT_RAM2")
#pragma DATA_SECTION(sourseFile,".EXT_RAM3")
short date_mix;   




void main()
{
short recording = 0;//录音状态变量
short playing = 0;//播放状态变量
for (i=0 ; i//buffer[]用于存放第一段录音(伴奏),buffer_voice[]用于存放第二段录音(原唱)
{
buffer[i] = 0;
buffer_voice[i] = 0;
}
DSK6416_DIP_init();
DSK6416_LED_init();
comm_poll();                   //init DSK, codec, McBSP
while(1)                           //infinite loop
{
  if(DSK6416_DIP_get(3) == 0)      //if SW#3 is pressed
{
i=0;
recording = 1;
while (recording == 1)
{
DSK6416_LED_on(3);           //turn on LED#3
buffer[i++] = input_left_sample(); //input data
        if (i>N||DSK6416_DIP_get(3) == 1)
  {
  recording = 0;
  DSK6416_LED_off(3);//LED#3 off when buffer full
  }            

buffer_size=i;// 记录录音数据的大小

//***************************//
//***************************//
  if(DSK6416_DIP_get(0)==0)//if SW#0 pressed
{
i=0;
playing = 1;
recording = 1;
while (playing == 1&&(recording==1))
{
DSK6416_LED_on(0);             //turn on LED#0
        for (i=0 ; (i< buffer_size)&&(DSK6416_DIP_get(0) == 0) ; i++)//一旦按键,立即结束录音。
        {
        output_left_sample(buffer[i]); //在伴奏播放时开始录音
        buffer_voice[i] = input_right_sample();//input data
}

if (i>N||DSK6416_DIP_get(0) == 1)
  {
  playing = 0;
  recording = 0;
  DSK6416_LED_off(0);
for(j=0;j<=buffer_size;j++)//音频混音算法 1、线性叠加后求平均
{
int t=buffer_voice[j]+buffer[j];
buffer_mix[j]=(short)(t/2);
}
}
} // 2.PCM脉冲编码混音编码算法,效果不咋地
//if(buffer[j]<0&&buffer_voice[j]<0)
// buffer_mix[j]=buffer[j]+buffer_voice[j]-((buffer[j]*buffer_voice[j])/(-(2*pow(n-1)-1)));
//else
// buffer_mix[j]=buffer[j]+buffer_voice[j]-((buffer[j]*buffer_voice[j])/(2*pow(n-1)));


}  // while (playing == 1)
 
//***************************//
//***************************//
if(DSK6416_DIP_get(1)==0) //if SW#1 pressed
 {
i=0;
playing = 1;
while (playing == 1)
{
DSK6416_LED_on(1);             //turn on LED#0
for (i=0 ; (i{
        output_left_sample(buffer_mix[i]); //输出混音后的音频数据,这里采用的是线性叠加算法。
// output_right_sample(buffer_voice[i]);
}
    if (i>N||DSK6416_DIP_get(1) == 1)
  {
  playing = 0;
  DSK6416_LED_off(1);//LED#1 off when buffer full
  }            
} // while (playing == 1)
 }
}
}
/***********3.自适应加权混音算法,目前正在调试。
void Mix_music(char sourseFile[10][SIZE_AUDIO_FRAME],int number,char *objectFile)  
{  
    //归一化混音  
    int const MAX=2147483647;  
    int const MIN=-2147483648;  
   
    double f=1;  
    int output;  
    int i = 0,j = 0;  
    for (i=0;i     {  
        int temp=0;  
        for (j=0;j         {  
            temp+=*(short*)(sourseFile[j]+i*2);  
        }                  
        output=(int)(temp*f);  
        if (output>MAX)  
        {  
            f=(double)MAX/(double)(output);  
            output=MAX;  
        }  
        if (output         {  
            f=(double)MIN/(double)(output);  
            output=MIN;  
        }  
        if (f<1)  
        {  
            f+=((double)1-f)/(double)64;  
        }  
        *(short*)(objectFile+i*2)=(short)output;  
    }     
}
*************/