| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
#include <pjmedia.h> |
| 40 |
#include <pjmedia-audiodev/audiodev.h> |
| 41 |
#include <pjlib.h> |
| 42 |
#include <pjlib-util.h> |
| 43 |
|
| 44 |
#include <stdio.h> |
| 45 |
#include <stdlib.h> |
| 46 |
|
| 47 |
#define THIS_FILE "vaddemo.c" |
| 48 |
|
| 49 |
static struct app_t |
| 50 |
{ |
| 51 |
pj_caching_pool cp; |
| 52 |
pj_pool_t *pool; |
| 53 |
|
| 54 |
pjmedia_silence_det *vad; |
| 55 |
pjmedia_aud_stream *strm; |
| 56 |
|
| 57 |
unsigned clock_rate; |
| 58 |
unsigned ptime; |
| 59 |
unsigned channel_count; |
| 60 |
unsigned samples_per_frame; |
| 61 |
pj_int32_t fixed_threshold; |
| 62 |
int log_level; |
| 63 |
|
| 64 |
|
| 65 |
unsigned frame_count; |
| 66 |
unsigned voiced_count; |
| 67 |
unsigned silence_count; |
| 68 |
pj_int32_t level_min; |
| 69 |
pj_int32_t level_max; |
| 70 |
pj_int64_t level_sum; |
| 71 |
|
| 72 |
|
| 73 |
pj_bool_t prev_silence; |
| 74 |
unsigned state_frames; |
| 75 |
unsigned sil_run; |
| 76 |
|
| 77 |
volatile pj_bool_t running; |
| 78 |
} app; |
| 79 |
|
| 80 |
|
| 81 |
static pj_status_t rec_cb(void *user_data, pjmedia_frame *frame) |
| 82 |
{ |
| 83 |
pj_int32_t level; |
| 84 |
pj_bool_t is_silence; |
| 85 |
const pj_int16_t *samples = (const pj_int16_t *)frame->buf; |
| 86 |
unsigned sample_count; |
| 87 |
|
| 88 |
PJ_UNUSED_ARG(user_data); |
| 89 |
|
| 90 |
if (frame->type != PJMEDIA_FRAME_TYPE_AUDIO || !app.running) |
| 91 |
return PJ_SUCCESS; |
| 92 |
|
| 93 |
sample_count = (unsigned)(frame->size / 2); |
| 94 |
|
| 95 |
|
| 96 |
is_silence = pjmedia_silence_det_detect(app.vad, samples, |
| 97 |
sample_count, &level); |
| 98 |
|
| 99 |
|
| 100 |
app.frame_count++; |
| 101 |
if (is_silence) |
| 102 |
app.silence_count++; |
| 103 |
else |
| 104 |
app.voiced_count++; |
| 105 |
|
| 106 |
app.level_sum += level; |
| 107 |
if (level < app.level_min) |
| 108 |
app.level_min = level; |
| 109 |
if (level > app.level_max) |
| 110 |
app.level_max = level; |
| 111 |
|
| 112 |
|
| 113 |
{ |
| 114 |
unsigned ms = app.frame_count * app.ptime; |
| 115 |
pj_bool_t is_transition; |
| 116 |
|
| 117 |
|
| 118 |
is_transition = (app.frame_count == 1 || |
| 119 |
is_silence != app.prev_silence); |
| 120 |
if (is_transition) { |
| 121 |
|
| 122 |
if (app.sil_run > 3 && app.prev_silence) { |
| 123 |
unsigned sil_end_ms = (app.frame_count - 1) * app.ptime; |
| 124 |
printf("[%3u.%03u] ... (%u silence frames, %ums)\n", |
| 125 |
sil_end_ms / 1000, sil_end_ms % 1000, |
| 126 |
app.sil_run, app.sil_run * app.ptime); |
| 127 |
} |
| 128 |
app.state_frames = 0; |
| 129 |
app.sil_run = 0; |
| 130 |
} |
| 131 |
app.state_frames++; |
| 132 |
app.prev_silence = is_silence; |
| 133 |
|
| 134 |
if (is_silence) { |
| 135 |
app.sil_run++; |
| 136 |
|
| 137 |
if (app.sil_run <= 3) { |
| 138 |
char line[120]; |
| 139 |
int len; |
| 140 |
|
| 141 |
len = pj_ansi_snprintf(line, sizeof(line), |
| 142 |
"[%3u.%03u] lvl=%5d SIL%s\n", |
| 143 |
ms / 1000, ms % 1000, (int)level, |
| 144 |
(is_transition && app.frame_count > 1) ? |
| 145 |
" << SILENCE" : ""); |
| 146 |
if (len > 0) |
| 147 |
fwrite(line, 1, len, stdout); |
| 148 |
} |
| 149 |
} else { |
| 150 |
|
| 151 |
char line[120]; |
| 152 |
char bar[53]; |
| 153 |
int bar_len = (int)(level * 50 / 5000); |
| 154 |
int len, i; |
| 155 |
|
| 156 |
if (bar_len > 50) bar_len = 50; |
| 157 |
bar[0] = '|'; |
| 158 |
for (i = 0; i < 50; i++) |
| 159 |
bar[i + 1] = (i < bar_len) ? '#' : ' '; |
| 160 |
bar[51] = '|'; |
| 161 |
bar[52] = '\0'; |
| 162 |
|
| 163 |
len = pj_ansi_snprintf(line, sizeof(line), |
| 164 |
"[%3u.%03u] lvl=%5d VOX %s%s\n", |
| 165 |
ms / 1000, ms % 1000, (int)level, bar, |
| 166 |
(is_transition && app.frame_count > 1) ? |
| 167 |
" << VOICED" : ""); |
| 168 |
if (len > 0) |
| 169 |
fwrite(line, 1, len, stdout); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
return PJ_SUCCESS; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
static void print_usage(void) |
| 178 |
{ |
| 179 |
puts("vaddemo - Silence Detector (VAD) Diagnostic Tool"); |
| 180 |
puts(""); |
| 181 |
puts("Usage: vaddemo [options]"); |
| 182 |
puts(""); |
| 183 |
puts("Options:"); |
| 184 |
puts(" -r N Clock rate in Hz (default: 16000)"); |
| 185 |
puts(" -p N Ptime in ms (default: 20)"); |
| 186 |
puts(" -t N Fixed threshold (0 = adaptive, default: 0)"); |
| 187 |
puts(" -L N Log level (default: 3)"); |
| 188 |
puts(" -h Show this help"); |
| 189 |
puts(""); |
| 190 |
puts("Output format:"); |
| 191 |
puts(" VOX frames: [time] lvl=N VOX |#### visual bar|"); |
| 192 |
puts(" SIL frames: [time] lvl=N SIL (first 3 shown, rest compressed)"); |
| 193 |
puts(" << marks state transitions"); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
int main(int argc, char *argv[]) |
| 198 |
{ |
| 199 |
pjmedia_aud_param param; |
| 200 |
int c; |
| 201 |
pj_status_t status; |
| 202 |
|
| 203 |
|
| 204 |
pj_bzero(&app, sizeof(app)); |
| 205 |
app.clock_rate = 16000; |
| 206 |
app.ptime = 20; |
| 207 |
app.channel_count = 1; |
| 208 |
app.fixed_threshold = 0; |
| 209 |
app.log_level = 3; |
| 210 |
app.level_min = 0x7FFFFFFF; |
| 211 |
app.level_max = 0; |
| 212 |
|
| 213 |
|
| 214 |
while ((c = pj_getopt(argc, argv, "r:p:t:L:h")) != -1) { |
| 215 |
switch (c) { |
| 216 |
case 'r': |
| 217 |
app.clock_rate = (unsigned)atoi(pj_optarg); |
| 218 |
break; |
| 219 |
case 'p': |
| 220 |
app.ptime = (unsigned)atoi(pj_optarg); |
| 221 |
break; |
| 222 |
case 't': |
| 223 |
app.fixed_threshold = atoi(pj_optarg); |
| 224 |
break; |
| 225 |
case 'L': |
| 226 |
app.log_level = atoi(pj_optarg); |
| 227 |
break; |
| 228 |
case 'h': |
| 229 |
print_usage(); |
| 230 |
return 0; |
| 231 |
default: |
| 232 |
print_usage(); |
| 233 |
return 1; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
if (app.clock_rate == 0 || app.ptime == 0) { |
| 238 |
puts("Error: clock rate and ptime must be > 0"); |
| 239 |
return 1; |
| 240 |
} |
| 241 |
|
| 242 |
app.samples_per_frame = app.clock_rate * app.ptime / 1000; |
| 243 |
|
| 244 |
|
| 245 |
status = pj_init(); |
| 246 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 247 |
|
| 248 |
pj_log_set_level(app.log_level); |
| 249 |
|
| 250 |
|
| 251 |
status = pjlib_util_init(); |
| 252 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 253 |
|
| 254 |
|
| 255 |
pj_caching_pool_init(&app.cp, &pj_pool_factory_default_policy, 0); |
| 256 |
app.pool = pj_pool_create(&app.cp.factory, "vaddemo", 4096, 4096, |
| 257 |
NULL); |
| 258 |
|
| 259 |
|
| 260 |
status = pjmedia_aud_subsys_init(&app.cp.factory); |
| 261 |
if (status != PJ_SUCCESS) { |
| 262 |
PJ_PERROR(1, (THIS_FILE, status, "Error init audio subsystem")); |
| 263 |
goto on_error; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
status = pjmedia_silence_det_create(app.pool, app.clock_rate, |
| 268 |
app.samples_per_frame, &app.vad); |
| 269 |
if (status != PJ_SUCCESS) { |
| 270 |
PJ_PERROR(1, (THIS_FILE, status, "Error creating VAD")); |
| 271 |
goto on_error; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
if (app.fixed_threshold > 0) { |
| 276 |
pjmedia_silence_det_set_fixed(app.vad, app.fixed_threshold); |
| 277 |
PJ_LOG(3, (THIS_FILE, "Using fixed threshold: %d", |
| 278 |
app.fixed_threshold)); |
| 279 |
} else { |
| 280 |
pjmedia_silence_det_set_adaptive(app.vad, -1); |
| 281 |
PJ_LOG(3, (THIS_FILE, "Using adaptive threshold")); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
status = pjmedia_aud_dev_default_param(PJMEDIA_AUD_DEFAULT_CAPTURE_DEV, |
| 286 |
¶m); |
| 287 |
if (status != PJ_SUCCESS) { |
| 288 |
PJ_PERROR(1, (THIS_FILE, status, "Error getting default param")); |
| 289 |
goto on_error; |
| 290 |
} |
| 291 |
|
| 292 |
param.dir = PJMEDIA_DIR_CAPTURE; |
| 293 |
param.clock_rate = app.clock_rate; |
| 294 |
param.channel_count = app.channel_count; |
| 295 |
param.samples_per_frame = app.samples_per_frame; |
| 296 |
param.bits_per_sample = 16; |
| 297 |
|
| 298 |
status = pjmedia_aud_stream_create(¶m, &rec_cb, NULL, NULL, |
| 299 |
&app.strm); |
| 300 |
if (status != PJ_SUCCESS) { |
| 301 |
PJ_PERROR(1, (THIS_FILE, status, "Error opening capture device")); |
| 302 |
goto on_error; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
app.running = PJ_TRUE; |
| 307 |
|
| 308 |
status = pjmedia_aud_stream_start(app.strm); |
| 309 |
if (status != PJ_SUCCESS) { |
| 310 |
PJ_PERROR(1, (THIS_FILE, status, "Error starting capture")); |
| 311 |
goto on_error; |
| 312 |
} |
| 313 |
|
| 314 |
PJ_LOG(3, (THIS_FILE, "VAD demo started: rate=%uHz, ptime=%ums, " |
| 315 |
"spf=%u", |
| 316 |
app.clock_rate, app.ptime, |
| 317 |
app.samples_per_frame)); |
| 318 |
PJ_LOG(3, (THIS_FILE, "Speak into your microphone. " |
| 319 |
"Press ENTER to stop.")); |
| 320 |
puts(""); |
| 321 |
printf("[ time ] lvl= N STA " |
| 322 |
"|visual bar for voiced frames |\n"); |
| 323 |
printf("---------------------------------------------" |
| 324 |
"--------------------------------------------------\n"); |
| 325 |
|
| 326 |
|
| 327 |
{ |
| 328 |
char tmp[10]; |
| 329 |
if (fgets(tmp, sizeof(tmp), stdin) == NULL) { |
| 330 |
|
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
app.running = PJ_FALSE; |
| 336 |
pj_thread_sleep(100); |
| 337 |
|
| 338 |
|
| 339 |
if (app.sil_run > 3) { |
| 340 |
unsigned sil_end_ms = app.frame_count * app.ptime; |
| 341 |
printf("[%3u.%03u] ... (%u silence frames, %ums)\n", |
| 342 |
sil_end_ms / 1000, sil_end_ms % 1000, |
| 343 |
app.sil_run, app.sil_run * app.ptime); |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
puts(""); |
| 348 |
puts("=== Summary ==="); |
| 349 |
printf("Total frames : %u (%.1fs)\n", |
| 350 |
app.frame_count, |
| 351 |
(float)app.frame_count * app.ptime / 1000.0f); |
| 352 |
printf("Voiced frames : %u (%.1f%%)\n", |
| 353 |
app.voiced_count, |
| 354 |
app.frame_count ? (float)app.voiced_count * 100.0f / |
| 355 |
app.frame_count : 0.0f); |
| 356 |
printf("Silence frames: %u (%.1f%%)\n", |
| 357 |
app.silence_count, |
| 358 |
app.frame_count ? (float)app.silence_count * 100.0f / |
| 359 |
app.frame_count : 0.0f); |
| 360 |
printf("Level min/max/avg: %d / %d / %d\n", |
| 361 |
(int)app.level_min, (int)app.level_max, |
| 362 |
app.frame_count ? (int)(app.level_sum / app.frame_count) : 0); |
| 363 |
|
| 364 |
on_error: |
| 365 |
if (app.strm) { |
| 366 |
pjmedia_aud_stream_stop(app.strm); |
| 367 |
pjmedia_aud_stream_destroy(app.strm); |
| 368 |
} |
| 369 |
|
| 370 |
pjmedia_aud_subsys_shutdown(); |
| 371 |
|
| 372 |
if (app.pool) |
| 373 |
pj_pool_release(app.pool); |
| 374 |
|
| 375 |
pj_caching_pool_destroy(&app.cp); |
| 376 |
pj_shutdown(); |
| 377 |
|
| 378 |
return 0; |
| 379 |
} |