Benny Prijono | 69f7312 | 2006-08-04 11:08:00 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 844653c | 2008-12-23 17:27:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
Benny Prijono | 32177c0 | 2008-06-20 22:44:47 +0000 | [diff] [blame] | 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | 69f7312 | 2006-08-04 11:08:00 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * \page page_pjmedia_samples_aectest_c Samples: AEC Test (aectest.c) |
| 24 | * |
| 25 | * Play a file to speaker, run AEC, and record the microphone input |
| 26 | * to see if echo is coming. |
| 27 | * |
| 28 | * This file is pjsip-apps/src/samples/aectest.c |
| 29 | * |
| 30 | * \includelineno aectest.c |
| 31 | */ |
| 32 | #include <pjmedia.h> |
| 33 | #include <pjlib-util.h> /* pj_getopt */ |
| 34 | #include <pjlib.h> |
| 35 | |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 36 | #define THIS_FILE "aectest.c" |
Benny Prijono | 69f7312 | 2006-08-04 11:08:00 +0000 | [diff] [blame] | 37 | #define PTIME 20 |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 38 | #define TAIL_LENGTH 200 |
Benny Prijono | 69f7312 | 2006-08-04 11:08:00 +0000 | [diff] [blame] | 39 | |
| 40 | static const char *desc = |
| 41 | " FILE \n" |
| 42 | " \n" |
| 43 | " aectest.c \n" |
| 44 | " \n" |
| 45 | " PURPOSE \n" |
| 46 | " \n" |
| 47 | " Test the AEC effectiveness. \n" |
| 48 | " \n" |
| 49 | " USAGE \n" |
| 50 | " \n" |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 51 | " aectest [options] <PLAY.WAV> <REC.WAV> <OUTPUT.WAV> \n" |
Benny Prijono | 69f7312 | 2006-08-04 11:08:00 +0000 | [diff] [blame] | 52 | " \n" |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 53 | " <PLAY.WAV> is the signal played to the speaker. \n" |
| 54 | " <REC.WAV> is the signal captured from the microphone. \n" |
| 55 | " <OUTPUT.WAV> is the output file to store the test result \n" |
| 56 | "\n" |
| 57 | " options:\n" |
| 58 | " -d The delay between playback and capture in ms. Default is zero.\n" |
| 59 | " -l Set the echo tail length in ms. Default is 200 ms \n" |
Benny Prijono | a7908d7 | 2008-08-10 16:15:14 +0000 | [diff] [blame] | 60 | " -r Set repeat count (default=1) \n" |
Benny Prijono | 6a23763 | 2008-08-13 13:53:18 +0000 | [diff] [blame] | 61 | " -a Algorithm: 0=default, 1=speex, 3=echo suppress \n" |
| 62 | " -i Interactive \n"; |
Benny Prijono | 69f7312 | 2006-08-04 11:08:00 +0000 | [diff] [blame] | 63 | |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 64 | /* |
| 65 | * Sample session: |
| 66 | * |
| 67 | * -d 100 -a 1 ../bin/orig8.wav ../bin/echo8.wav ../bin/result8.wav |
| 68 | */ |
Benny Prijono | 69f7312 | 2006-08-04 11:08:00 +0000 | [diff] [blame] | 69 | |
| 70 | static void app_perror(const char *sender, const char *title, pj_status_t st) |
| 71 | { |
| 72 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 73 | |
| 74 | pj_strerror(st, errmsg, sizeof(errmsg)); |
| 75 | PJ_LOG(3,(sender, "%s: %s", title, errmsg)); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | /* |
| 80 | * main() |
| 81 | */ |
| 82 | int main(int argc, char *argv[]) |
| 83 | { |
| 84 | pj_caching_pool cp; |
| 85 | pjmedia_endpt *med_endpt; |
| 86 | pj_pool_t *pool; |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 87 | pjmedia_port *wav_play; |
| 88 | pjmedia_port *wav_rec; |
| 89 | pjmedia_port *wav_out; |
Benny Prijono | 69f7312 | 2006-08-04 11:08:00 +0000 | [diff] [blame] | 90 | pj_status_t status; |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 91 | pjmedia_echo_state *ec; |
| 92 | pjmedia_frame play_frame, rec_frame; |
| 93 | unsigned opt = 0; |
| 94 | unsigned latency_ms = 0; |
| 95 | unsigned tail_ms = TAIL_LENGTH; |
| 96 | pj_timestamp t0, t1; |
Benny Prijono | 6a23763 | 2008-08-13 13:53:18 +0000 | [diff] [blame] | 97 | int i, repeat=1, interactive=0, c; |
Benny Prijono | 69f7312 | 2006-08-04 11:08:00 +0000 | [diff] [blame] | 98 | |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 99 | pj_optind = 0; |
Benny Prijono | 6a23763 | 2008-08-13 13:53:18 +0000 | [diff] [blame] | 100 | while ((c=pj_getopt(argc, argv, "d:l:a:r:i")) !=-1) { |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 101 | switch (c) { |
| 102 | case 'd': |
| 103 | latency_ms = atoi(pj_optarg); |
| 104 | break; |
| 105 | case 'l': |
| 106 | tail_ms = atoi(pj_optarg); |
| 107 | break; |
| 108 | case 'a': |
| 109 | { |
| 110 | int alg = atoi(pj_optarg); |
| 111 | switch (alg) { |
| 112 | case 0: |
| 113 | opt = 0; |
| 114 | case 1: |
| 115 | opt = PJMEDIA_ECHO_SPEEX; |
| 116 | break; |
| 117 | case 3: |
| 118 | opt = PJMEDIA_ECHO_SIMPLE; |
| 119 | break; |
| 120 | default: |
| 121 | puts("Invalid algorithm"); |
| 122 | puts(desc); |
| 123 | return 1; |
| 124 | } |
| 125 | } |
| 126 | break; |
Benny Prijono | a7908d7 | 2008-08-10 16:15:14 +0000 | [diff] [blame] | 127 | case 'r': |
| 128 | repeat = atoi(pj_optarg); |
| 129 | if (repeat < 1) { |
| 130 | puts("Invalid algorithm"); |
| 131 | puts(desc); |
| 132 | return 1; |
| 133 | } |
| 134 | break; |
Benny Prijono | 6a23763 | 2008-08-13 13:53:18 +0000 | [diff] [blame] | 135 | case 'i': |
| 136 | interactive = 1; |
| 137 | break; |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
Benny Prijono | 69f7312 | 2006-08-04 11:08:00 +0000 | [diff] [blame] | 140 | |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 141 | if (argc - pj_optind != 3) { |
| 142 | puts("Error: missing argument(s)"); |
Benny Prijono | 69f7312 | 2006-08-04 11:08:00 +0000 | [diff] [blame] | 143 | puts(desc); |
| 144 | return 1; |
| 145 | } |
| 146 | |
Benny Prijono | 69f7312 | 2006-08-04 11:08:00 +0000 | [diff] [blame] | 147 | /* Must init PJLIB first: */ |
| 148 | status = pj_init(); |
| 149 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 150 | |
| 151 | /* Must create a pool factory before we can allocate any memory. */ |
| 152 | pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); |
| 153 | |
| 154 | /* |
| 155 | * Initialize media endpoint. |
| 156 | * This will implicitly initialize PJMEDIA too. |
| 157 | */ |
| 158 | status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt); |
| 159 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 160 | |
| 161 | /* Create memory pool for our file player */ |
| 162 | pool = pj_pool_create( &cp.factory, /* pool factory */ |
| 163 | "wav", /* pool name. */ |
| 164 | 4000, /* init size */ |
| 165 | 4000, /* increment size */ |
| 166 | NULL /* callback on error */ |
| 167 | ); |
| 168 | |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 169 | /* Open wav_play */ |
| 170 | status = pjmedia_wav_player_port_create(pool, argv[pj_optind], PTIME, |
| 171 | PJMEDIA_FILE_NO_LOOP, 0, |
| 172 | &wav_play); |
Benny Prijono | 69f7312 | 2006-08-04 11:08:00 +0000 | [diff] [blame] | 173 | if (status != PJ_SUCCESS) { |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 174 | app_perror(THIS_FILE, "Error opening playback WAV file", status); |
Benny Prijono | 69f7312 | 2006-08-04 11:08:00 +0000 | [diff] [blame] | 175 | return 1; |
| 176 | } |
Benny Prijono | 69f7312 | 2006-08-04 11:08:00 +0000 | [diff] [blame] | 177 | |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 178 | /* Open recorded wav */ |
| 179 | status = pjmedia_wav_player_port_create(pool, argv[pj_optind+1], PTIME, |
| 180 | PJMEDIA_FILE_NO_LOOP, 0, |
| 181 | &wav_rec); |
| 182 | if (status != PJ_SUCCESS) { |
| 183 | app_perror(THIS_FILE, "Error opening recorded WAV file", status); |
| 184 | return 1; |
| 185 | } |
Benny Prijono | 69f7312 | 2006-08-04 11:08:00 +0000 | [diff] [blame] | 186 | |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 187 | /* play and rec WAVs must have the same clock rate */ |
| 188 | if (wav_play->info.clock_rate != wav_rec->info.clock_rate) { |
| 189 | puts("Error: clock rate mismatch in the WAV files"); |
| 190 | return 1; |
| 191 | } |
Benny Prijono | 69f7312 | 2006-08-04 11:08:00 +0000 | [diff] [blame] | 192 | |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 193 | /* .. and channel count */ |
| 194 | if (wav_play->info.channel_count != wav_rec->info.channel_count) { |
| 195 | puts("Error: clock rate mismatch in the WAV files"); |
| 196 | return 1; |
| 197 | } |
| 198 | |
| 199 | /* Create output wav */ |
| 200 | status = pjmedia_wav_writer_port_create(pool, argv[pj_optind+2], |
| 201 | wav_play->info.clock_rate, |
| 202 | wav_play->info.channel_count, |
| 203 | wav_play->info.samples_per_frame, |
| 204 | wav_play->info.bits_per_sample, |
| 205 | 0, 0, &wav_out); |
| 206 | if (status != PJ_SUCCESS) { |
| 207 | app_perror(THIS_FILE, "Error opening output WAV file", status); |
| 208 | return 1; |
| 209 | } |
| 210 | |
| 211 | /* Create echo canceller */ |
| 212 | status = pjmedia_echo_create2(pool, wav_play->info.clock_rate, |
| 213 | wav_play->info.channel_count, |
| 214 | wav_play->info.samples_per_frame, |
| 215 | tail_ms, latency_ms, |
| 216 | opt, &ec); |
| 217 | if (status != PJ_SUCCESS) { |
| 218 | app_perror(THIS_FILE, "Error creating EC", status); |
| 219 | return 1; |
| 220 | } |
| 221 | |
| 222 | |
| 223 | /* Processing loop */ |
| 224 | play_frame.buf = pj_pool_alloc(pool, wav_play->info.samples_per_frame<<1); |
| 225 | rec_frame.buf = pj_pool_alloc(pool, wav_play->info.samples_per_frame<<1); |
| 226 | pj_get_timestamp(&t0); |
Benny Prijono | a7908d7 | 2008-08-10 16:15:14 +0000 | [diff] [blame] | 227 | for (i=0; i < repeat; ++i) { |
| 228 | for (;;) { |
| 229 | play_frame.size = wav_play->info.samples_per_frame << 1; |
| 230 | status = pjmedia_port_get_frame(wav_play, &play_frame); |
| 231 | if (status != PJ_SUCCESS) |
| 232 | break; |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 233 | |
Benny Prijono | a7908d7 | 2008-08-10 16:15:14 +0000 | [diff] [blame] | 234 | status = pjmedia_echo_playback(ec, (short*)play_frame.buf); |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 235 | |
Benny Prijono | a7908d7 | 2008-08-10 16:15:14 +0000 | [diff] [blame] | 236 | rec_frame.size = wav_play->info.samples_per_frame << 1; |
| 237 | status = pjmedia_port_get_frame(wav_rec, &rec_frame); |
| 238 | if (status != PJ_SUCCESS) |
| 239 | break; |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 240 | |
Benny Prijono | a7908d7 | 2008-08-10 16:15:14 +0000 | [diff] [blame] | 241 | status = pjmedia_echo_capture(ec, (short*)rec_frame.buf, 0); |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 242 | |
Benny Prijono | a7908d7 | 2008-08-10 16:15:14 +0000 | [diff] [blame] | 243 | //status = pjmedia_echo_cancel(ec, (short*)rec_frame.buf, |
| 244 | // (short*)play_frame.buf, 0, NULL); |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 245 | |
Benny Prijono | a7908d7 | 2008-08-10 16:15:14 +0000 | [diff] [blame] | 246 | pjmedia_port_put_frame(wav_out, &rec_frame); |
| 247 | } |
| 248 | |
| 249 | pjmedia_wav_player_port_set_pos(wav_play, 0); |
| 250 | pjmedia_wav_player_port_set_pos(wav_rec, 0); |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 251 | } |
| 252 | pj_get_timestamp(&t1); |
| 253 | |
Benny Prijono | 6a23763 | 2008-08-13 13:53:18 +0000 | [diff] [blame] | 254 | i = pjmedia_wav_writer_port_get_pos(wav_out) * 1000 / |
| 255 | (wav_out->info.clock_rate * wav_out->info.channel_count); |
| 256 | PJ_LOG(3,(THIS_FILE, "Processed %3d.%03ds audio", |
| 257 | i / 1000, i % 1000)); |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 258 | PJ_LOG(3,(THIS_FILE, "Completed in %u msec\n", pj_elapsed_msec(&t0, &t1))); |
Benny Prijono | 69f7312 | 2006-08-04 11:08:00 +0000 | [diff] [blame] | 259 | |
| 260 | /* Destroy file port(s) */ |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 261 | status = pjmedia_port_destroy( wav_play ); |
Benny Prijono | 69f7312 | 2006-08-04 11:08:00 +0000 | [diff] [blame] | 262 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 263 | status = pjmedia_port_destroy( wav_rec ); |
| 264 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 265 | status = pjmedia_port_destroy( wav_out ); |
Benny Prijono | 69f7312 | 2006-08-04 11:08:00 +0000 | [diff] [blame] | 266 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 267 | |
Benny Prijono | 95250c7 | 2008-08-09 05:40:22 +0000 | [diff] [blame] | 268 | /* Destroy ec */ |
| 269 | pjmedia_echo_destroy(ec); |
Benny Prijono | 69f7312 | 2006-08-04 11:08:00 +0000 | [diff] [blame] | 270 | |
| 271 | /* Release application pool */ |
| 272 | pj_pool_release( pool ); |
| 273 | |
| 274 | /* Destroy media endpoint. */ |
| 275 | pjmedia_endpt_destroy( med_endpt ); |
| 276 | |
| 277 | /* Destroy pool factory */ |
| 278 | pj_caching_pool_destroy( &cp ); |
| 279 | |
Benny Prijono | af1bb1e | 2006-11-21 12:39:31 +0000 | [diff] [blame] | 280 | /* Shutdown PJLIB */ |
| 281 | pj_shutdown(); |
Benny Prijono | 69f7312 | 2006-08-04 11:08:00 +0000 | [diff] [blame] | 282 | |
Benny Prijono | 6a23763 | 2008-08-13 13:53:18 +0000 | [diff] [blame] | 283 | if (interactive) { |
Benny Prijono | a7908d7 | 2008-08-10 16:15:14 +0000 | [diff] [blame] | 284 | char s[10]; |
| 285 | puts("ENTER to quit"); |
| 286 | fgets(s, sizeof(s), stdin); |
| 287 | } |
Benny Prijono | a7908d7 | 2008-08-10 16:15:14 +0000 | [diff] [blame] | 288 | |
Benny Prijono | 69f7312 | 2006-08-04 11:08:00 +0000 | [diff] [blame] | 289 | /* Done. */ |
| 290 | return 0; |
| 291 | } |
| 292 | |