Mercurial > hg > audiostuff
comparison intercom/aec_test.cpp @ 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 | 13be24d74cd2 |
| children |
comparison
equal
deleted
inserted
replaced
| 5:f762bf195c4b | 6:22a74b01a099 |
|---|---|
| 25 | 25 |
| 26 #define TAPS (40*WIDEB*8) | 26 #define TAPS (40*WIDEB*8) |
| 27 | 27 |
| 28 typedef signed short MONO; | 28 typedef signed short MONO; |
| 29 | 29 |
| 30 typedef struct { | |
| 31 signed short l; | |
| 32 signed short r; | |
| 33 } STEREO; | |
| 34 | |
| 35 float dB2q(float dB) | 30 float dB2q(float dB) |
| 36 { | 31 { |
| 37 /* Dezibel to Ratio */ | 32 /* Dezibel to Ratio */ |
| 38 return powf(10.0f, dB / 20.0f); | 33 return powf(10.0f, dB / 20.0f); |
| 39 } | 34 } |
| 42 { | 37 { |
| 43 /* Ratio to Dezibel */ | 38 /* Ratio to Dezibel */ |
| 44 return 20.0f * log10f(q); | 39 return 20.0f * log10f(q); |
| 45 } | 40 } |
| 46 | 41 |
| 47 | 42 /* Read a raw audio files (8KHz sample frequency, 16bit PCM, mono) |
| 48 /* Read a raw audio file (8KHz sample frequency, 16bit PCM, stereo) | |
| 49 * from stdin, echo cancel it and write it to stdout | 43 * from stdin, echo cancel it and write it to stdout |
| 50 */ | 44 */ |
| 51 int main(int argc, char *argv[]) | 45 int main(int argc, char *argv[]) |
| 52 { | 46 { |
| 53 STEREO inbuf[TAPS], outbuf[TAPS]; | 47 MONO inbuf_speaker[TAPS], inbuf_mic[TAPS], outbuf[TAPS]; |
| 54 float visualize; | 48 float visualize; |
| 55 | 49 |
| 56 fprintf(stderr, "usage: aec_test [ambient in dB] <in.raw >out.raw\n"); | 50 fprintf(stderr, "usage: aec_test [ambient in dB] speaker.raw mic.raw out.raw\n"); |
| 57 | 51 |
| 58 AEC aec; | 52 AEC aec; |
| 59 | 53 |
| 60 if (argc >= 2) { | 54 // ambient in dB |
| 61 aec.setambient(MAXPCM*dB2q(atof(argv[1]))); | 55 aec.setambient(MAXPCM*dB2q(0.0)); |
| 56 | |
| 57 FILE *fspeaker = fopen("speaker.raw", "rb"); | |
| 58 if (!fspeaker) { | |
| 59 fprintf(stderr, "Failed to open 'speaker.raw', exit.\n"); | |
| 60 exit(EXIT_FAILURE); | |
| 61 } | |
| 62 | |
| 63 FILE *fmic = fopen("mic.raw", "rb"); | |
| 64 if (!fmic) { | |
| 65 fprintf(stderr, "Failed to open 'mic.raw', exit.\n"); | |
| 66 exit(EXIT_FAILURE); | |
| 62 } | 67 } |
| 63 | 68 |
| 64 int taps; | 69 FILE *fout = fopen("out.raw", "wb"); |
| 70 if (!fout) { | |
| 71 fprintf(stderr, "Failed to open 'out.raw', exit.\n"); | |
| 72 exit(EXIT_FAILURE); | |
| 73 } | |
| 74 | |
| 75 int taps_speaker, taps_mic; | |
| 65 float ambient; | 76 float ambient; |
| 66 while (taps = fread(inbuf, sizeof(STEREO), TAPS, stdin)) { | 77 while (true) { |
| 67 int i; | 78 int i; |
| 68 for (i = 0; i < taps; ++i) { | 79 |
| 69 int s0 = inbuf[i].l; /* left channel microphone */ | 80 taps_speaker = fread(inbuf_speaker, sizeof(MONO), TAPS, fspeaker); |
| 70 int s1 = inbuf[i].r; /* right channel speaker */ | 81 taps_mic = fread(inbuf_mic, sizeof(MONO), TAPS, fmic); |
| 82 | |
| 83 if (taps_speaker != TAPS || taps_mic != TAPS) | |
| 84 break; | |
| 85 | |
| 86 | |
| 87 for (i = 0; i < TAPS; ++i) { | |
| 88 int s0 = inbuf_mic[i]; /* left channel microphone */ | |
| 89 int s1 = inbuf_speaker[i]; /* right channel speaker */ | |
| 71 | 90 |
| 72 /* and do NLMS */ | 91 /* and do NLMS */ |
| 73 s0 = aec.doAEC(s0, s1); | 92 s0 = aec.doAEC(s0, s1); |
| 74 | 93 |
| 75 /* output one internal variable */ | 94 /* output one internal variable */ |
| 76 // visualize = 16 * aec.hangover; | 95 // visualize = 16 * aec.hangover; |
| 77 visualize = 32000 * aec.stepsize; | 96 visualize = 32000 * aec.stepsize; |
| 78 | 97 |
| 79 outbuf[i].l = (short)(visualize); /* left channel */ | 98 outbuf[i] = s0; /* right channel echo cancelled mic */ |
| 80 outbuf[i].r = s0; /* right channel echo cancelled mic */ | |
| 81 } | 99 } |
| 82 | 100 |
| 83 fwrite(outbuf, sizeof(STEREO), taps, stdout); | 101 fwrite(outbuf, sizeof(MONO), TAPS, fout); |
| 84 } | 102 } |
| 85 ambient = aec.getambient(); | 103 ambient = aec.getambient(); |
| 86 float ambientdB = q2dB(ambient / 32767.0f); | 104 float ambientdB = q2dB(ambient / 32767.0f); |
| 87 fprintf(stderr, "Ambient = %2.0f dB\n", ambientdB); | 105 fprintf(stderr, "Ambient = %2.0f dB\n", ambientdB); |
| 88 fflush(NULL); | |
| 89 | 106 |
| 90 return 0; | 107 return EXIT_SUCCESS; |
| 91 } | 108 } |
