Mercurial > hg > audiostuff
changeset 6:22a74b01a099 default tip
implement more meaningful test program
author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
---|---|
date | Fri, 25 Jun 2010 16:14:50 +0200 |
parents | f762bf195c4b |
children | |
files | intercom/aec_test.cpp |
diffstat | 1 files changed, 38 insertions(+), 21 deletions(-) [+] |
line wrap: on
line diff
--- a/intercom/aec_test.cpp Fri Jun 25 16:00:21 2010 +0200 +++ b/intercom/aec_test.cpp Fri Jun 25 16:14:50 2010 +0200 @@ -27,11 +27,6 @@ typedef signed short MONO; -typedef struct { - signed short l; - signed short r; -} STEREO; - float dB2q(float dB) { /* Dezibel to Ratio */ @@ -44,30 +39,54 @@ return 20.0f * log10f(q); } - -/* Read a raw audio file (8KHz sample frequency, 16bit PCM, stereo) +/* Read a raw audio files (8KHz sample frequency, 16bit PCM, mono) * from stdin, echo cancel it and write it to stdout */ int main(int argc, char *argv[]) { - STEREO inbuf[TAPS], outbuf[TAPS]; + MONO inbuf_speaker[TAPS], inbuf_mic[TAPS], outbuf[TAPS]; float visualize; - fprintf(stderr, "usage: aec_test [ambient in dB] <in.raw >out.raw\n"); + fprintf(stderr, "usage: aec_test [ambient in dB] speaker.raw mic.raw out.raw\n"); AEC aec; - if (argc >= 2) { - aec.setambient(MAXPCM*dB2q(atof(argv[1]))); + // ambient in dB + aec.setambient(MAXPCM*dB2q(0.0)); + + FILE *fspeaker = fopen("speaker.raw", "rb"); + if (!fspeaker) { + fprintf(stderr, "Failed to open 'speaker.raw', exit.\n"); + exit(EXIT_FAILURE); + } + + FILE *fmic = fopen("mic.raw", "rb"); + if (!fmic) { + fprintf(stderr, "Failed to open 'mic.raw', exit.\n"); + exit(EXIT_FAILURE); } - int taps; + FILE *fout = fopen("out.raw", "wb"); + if (!fout) { + fprintf(stderr, "Failed to open 'out.raw', exit.\n"); + exit(EXIT_FAILURE); + } + + int taps_speaker, taps_mic; float ambient; - while (taps = fread(inbuf, sizeof(STEREO), TAPS, stdin)) { + while (true) { int i; - for (i = 0; i < taps; ++i) { - int s0 = inbuf[i].l; /* left channel microphone */ - int s1 = inbuf[i].r; /* right channel speaker */ + + taps_speaker = fread(inbuf_speaker, sizeof(MONO), TAPS, fspeaker); + taps_mic = fread(inbuf_mic, sizeof(MONO), TAPS, fmic); + + if (taps_speaker != TAPS || taps_mic != TAPS) + break; + + + for (i = 0; i < TAPS; ++i) { + int s0 = inbuf_mic[i]; /* left channel microphone */ + int s1 = inbuf_speaker[i]; /* right channel speaker */ /* and do NLMS */ s0 = aec.doAEC(s0, s1); @@ -76,16 +95,14 @@ // visualize = 16 * aec.hangover; visualize = 32000 * aec.stepsize; - outbuf[i].l = (short)(visualize); /* left channel */ - outbuf[i].r = s0; /* right channel echo cancelled mic */ + outbuf[i] = s0; /* right channel echo cancelled mic */ } - fwrite(outbuf, sizeof(STEREO), taps, stdout); + fwrite(outbuf, sizeof(MONO), TAPS, fout); } ambient = aec.getambient(); float ambientdB = q2dB(ambient / 32767.0f); fprintf(stderr, "Ambient = %2.0f dB\n", ambientdB); - fflush(NULL); - return 0; + return EXIT_SUCCESS; }