www.pudn.com > aec_fix_zhang.rar > aec_test.c, change:2008-11-05,size:2341b


#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 
#include <string.h> 
 
#include "aec.h" 
 
#define WIDEB   1		// 1:8k		2:16k 
#define TAPS  	(30*WIDEB*8) 
 
typedef signed short MONO; 
 
extern AECSTATDEF aecdata; 
 
//typedef struct { 
//	signed short l; 
//	signed short r; 
//} STEREO; 
 
float dB2q(float dB) 
{ 
	/* Dezibel to Ratio */ 
	return pow(10.0f, dB / 20.0f); 
} 
 
float q2dB(float q) 
{ 
	/* Ratio to Dezibel */ 
	return 20.0f * log10(q); 
} 
 
 
/* Read a raw audio file (8KHz sample frequency, 16bit PCM, stereo) 
* from stdin, echo cancel it and write it to stdout 
*/ 
int main(int argc, char *argv[]) 
{ 
	int taps1, taps2; 
	float ambient;	 
	float ambientdB; 
	FILE * fin1 = NULL;  
	FILE * fin2 = NULL;  
	FILE * fout1 = NULL;  
  
	short inbuf1[TAPS], inbuf2[TAPS], outbuf[TAPS]; 
 
	fprintf(stderr, "usage: aec_test [ambient in dB] micin.pcm speaker.pcm micout.pcm\n"); 
	 
	AEC_init(&aecdata, WIDEB); 
	 
	if (argc > 3)  
	{ 
		AEC_setambient(&aecdata, MAXPCM*dB2q(atof(argv[1])));     
	} 
	 
    if((fin1 = fopen("micin.pcm", "rb")) == NULL ) 
	{ 
		printf("cannot open mic signal file. \n"); 
		return -1; 
	} 
 
    if((fin2 = fopen("speaker.pcm", "rb")) == NULL ) 
	{ 
		printf("cannot open speaker signal file. \n"); 
		return -1; 
	} 
 
    if((fout1 = fopen("micout.pcm", "wb")) == NULL ) 
	{ 
		printf("cannot open output mic signal file. \n"); 
		return -1; 
	} 
 
	taps1 = fread(inbuf1, sizeof(short), TAPS, fin1); 
	taps2 = fread(inbuf2, sizeof(short), TAPS, fin2); 
    if( taps1 != taps2 ) 
	{ 
		printf("Not enough data to process.\n"); 
		return -1; 
	} 
 
	while (taps1 == taps2 )  
	{ 
		int i; 
		for (i = 0; i < taps1; ++i)  
		{ 
			int s0 = inbuf1[i];      /* left channel microphone */ 
			int s1 = inbuf2[i];      /* right channel speaker */ 
			 
			/* and do NLMS */ 
			s0 = AEC_doAEC(&aecdata, s0, s1, WIDEB);			 
		 
			outbuf[i] = s0;         /* right channel echo cancelled mic */ 
		} 
		 
		fwrite(outbuf, sizeof(short), taps1, fout1); 
 
		taps1 = fread(inbuf1, sizeof(short), TAPS, fin1); 
		taps2 = fread(inbuf2, sizeof(short), TAPS, fin2); 
 	} 
 
	fclose(fin1); 
	fclose(fin2); 
	fclose(fout1); 
 
	ambient = AEC_getambient(&aecdata); 
	ambientdB = q2dB(ambient / 32767.0f); 
	fprintf(stderr, "Ambient = %2.0f dB\n", ambientdB); 
	fflush(NULL); 
	 
	return 0; 
}