Mercurial > hg > audiostuff
annotate 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 |
rev | line source |
---|---|
2 | 1 /* aec_test.cpp |
2 * | |
3 * Copyright (C) DFS Deutsche Flugsicherung (2004). All Rights Reserved. | |
4 * | |
5 * Test stub for Acoustic Echo Cancellation NLMS-pw algorithm | |
6 * Author: Andre Adrian, DFS Deutsche Flugsicherung | |
7 * <Andre.Adrian@dfs.de> | |
8 * | |
9 * fortune says: | |
10 * It's never as good as it feels, and it's never as bad as it seems. | |
11 * | |
12 * compile | |
13 c++ -DWIDEB=2 -O2 -o aec_test aec_test.cpp aec.cpp tcp.cpp -lm | |
14 * | |
15 * Version 1.3 set/get ambient in dB | |
16 */ | |
17 | |
18 #include <stdio.h> | |
19 #include <stdlib.h> | |
20 #include <math.h> | |
21 #include <string.h> | |
22 | |
23 #include "tcp.h" | |
24 #include "aec.h" | |
25 | |
26 #define TAPS (40*WIDEB*8) | |
27 | |
28 typedef signed short MONO; | |
29 | |
30 float dB2q(float dB) | |
31 { | |
32 /* Dezibel to Ratio */ | |
33 return powf(10.0f, dB / 20.0f); | |
34 } | |
35 | |
36 float q2dB(float q) | |
37 { | |
38 /* Ratio to Dezibel */ | |
39 return 20.0f * log10f(q); | |
40 } | |
41 | |
6
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
42 /* Read a raw audio files (8KHz sample frequency, 16bit PCM, mono) |
2 | 43 * from stdin, echo cancel it and write it to stdout |
44 */ | |
45 int main(int argc, char *argv[]) | |
46 { | |
6
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
47 MONO inbuf_speaker[TAPS], inbuf_mic[TAPS], outbuf[TAPS]; |
2 | 48 float visualize; |
49 | |
6
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
50 fprintf(stderr, "usage: aec_test [ambient in dB] speaker.raw mic.raw out.raw\n"); |
2 | 51 |
52 AEC aec; | |
53 | |
6
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
54 // ambient in dB |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
55 aec.setambient(MAXPCM*dB2q(0.0)); |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
56 |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
57 FILE *fspeaker = fopen("speaker.raw", "rb"); |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
58 if (!fspeaker) { |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
59 fprintf(stderr, "Failed to open 'speaker.raw', exit.\n"); |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
60 exit(EXIT_FAILURE); |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
61 } |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
62 |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
63 FILE *fmic = fopen("mic.raw", "rb"); |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
64 if (!fmic) { |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
65 fprintf(stderr, "Failed to open 'mic.raw', exit.\n"); |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
66 exit(EXIT_FAILURE); |
2 | 67 } |
68 | |
6
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
69 FILE *fout = fopen("out.raw", "wb"); |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
70 if (!fout) { |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
71 fprintf(stderr, "Failed to open 'out.raw', exit.\n"); |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
72 exit(EXIT_FAILURE); |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
73 } |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
74 |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
75 int taps_speaker, taps_mic; |
2 | 76 float ambient; |
6
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
77 while (true) { |
2 | 78 int i; |
6
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
79 |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
80 taps_speaker = fread(inbuf_speaker, sizeof(MONO), TAPS, fspeaker); |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
81 taps_mic = fread(inbuf_mic, sizeof(MONO), TAPS, fmic); |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
82 |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
83 if (taps_speaker != TAPS || taps_mic != TAPS) |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
84 break; |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
85 |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
86 |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
87 for (i = 0; i < TAPS; ++i) { |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
88 int s0 = inbuf_mic[i]; /* left channel microphone */ |
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
89 int s1 = inbuf_speaker[i]; /* right channel speaker */ |
2 | 90 |
91 /* and do NLMS */ | |
92 s0 = aec.doAEC(s0, s1); | |
93 | |
94 /* output one internal variable */ | |
95 // visualize = 16 * aec.hangover; | |
96 visualize = 32000 * aec.stepsize; | |
97 | |
6
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
98 outbuf[i] = s0; /* right channel echo cancelled mic */ |
2 | 99 } |
100 | |
6
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
101 fwrite(outbuf, sizeof(MONO), TAPS, fout); |
2 | 102 } |
103 ambient = aec.getambient(); | |
104 float ambientdB = q2dB(ambient / 32767.0f); | |
105 fprintf(stderr, "Ambient = %2.0f dB\n", ambientdB); | |
106 | |
6
22a74b01a099
implement more meaningful test program
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
2
diff
changeset
|
107 return EXIT_SUCCESS; |
2 | 108 } |