1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| #include <stdio.h> #include <stdlib.h> #include <math.h>
#define IN_FILE1 "1.wav" #define IN_FILE2 "2.wav" #define OUT_FILE "remix.pcm"
#define SIZE_AUDIO_FRAME (2)
void Mix(char sourseFile[10][SIZE_AUDIO_FRAME],int number,char *objectFile) { int const MAX=32767; int const MIN=-32768;
double f=1; int output; int i = 0,j = 0; for (i=0;i<SIZE_AUDIO_FRAME/2;i++) { int temp=0; for (j=0;j<number;j++) temp+=*(short*)(sourseFile[j]+i*2); output=(int)(temp*f); if (output>MAX) { f=(double)MAX/(double)(output); output=MAX; } if (output<MIN) { f=(double)MIN/(double)(output); output=MIN; } if (f<1) f+=((double)1-f)/(double)32;
*(short*)(objectFile+i*2)=(short)output; } }
int main() { FILE * fp1,*fp2,*fpm; fp1 = fopen(IN_FILE1,"rb"); fp2 = fopen(IN_FILE2,"rb"); fpm = fopen(OUT_FILE,"wb");
short data1,data2,date_mix; int ret1,ret2; char sourseFile[10][2];
while(1) { ret1 = fread(&data1,2,1,fp1); ret2 = fread(&data2,2,1,fp2); *(short*) sourseFile[0] = data1; *(short*) sourseFile[1] = data2;
if(ret1>0 && ret2>0) { Mix(sourseFile,2,(char *)&date_mix);
if(date_mix > pow(2,16-1) || date_mix < -pow(2,16-1)) printf("mix error\n"); } else if( (ret1 > 0) && (ret2==0)) date_mix = data1; else if( (ret2 > 0) && (ret1==0)) date_mix = data2; else if( (ret1 == 0) && (ret2 == 0)) break; fwrite(&date_mix,2,1,fpm); } fclose(fp1); fclose(fp2); fclose(fpm); printf("Done!\n"); }
|