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