forked from webitel/mod_amd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod_amd.c
605 lines (508 loc) · 20 KB
/
mod_amd.c
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#include <switch.h>
#define AMD_PARAMS (2)
#define AMD_SYNTAX "<uuid> <command>"
#define BUG_AMD_NAME_READ "amd_read"
#define AMD_EVENT_NAME "amd::info"
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_amd_shutdown);
SWITCH_MODULE_LOAD_FUNCTION(mod_amd_load);
SWITCH_MODULE_DEFINITION(mod_amd, mod_amd_load, mod_amd_shutdown, NULL);
SWITCH_STANDARD_APP(amd_start_function);
typedef struct {
uint32_t initial_silence;
uint32_t greeting;
uint32_t after_greeting_silence;
uint32_t total_analysis_time;
uint32_t minimum_word_length;
uint32_t between_words_silence;
uint32_t maximum_number_of_words;
uint32_t maximum_word_length;
uint32_t silence_threshold;
uint32_t silence_notsure;
} amd_params_t;
static amd_params_t globals;
static switch_xml_config_item_t instructions[] = {
SWITCH_CONFIG_ITEM(
"initial_silence",
SWITCH_CONFIG_INT,
CONFIG_RELOADABLE,
&globals.initial_silence,
(void *) 2500,
NULL, NULL, NULL),
SWITCH_CONFIG_ITEM(
"greeting",
SWITCH_CONFIG_INT,
CONFIG_RELOADABLE,
&globals.greeting,
(void *) 1500,
NULL, NULL, NULL),
SWITCH_CONFIG_ITEM(
"after_greeting_silence",
SWITCH_CONFIG_INT,
CONFIG_RELOADABLE,
&globals.after_greeting_silence,
(void *) 800,
NULL, NULL, NULL),
SWITCH_CONFIG_ITEM(
"total_analysis_time",
SWITCH_CONFIG_INT,
CONFIG_RELOADABLE,
&globals.total_analysis_time,
(void *) 5000,
NULL, NULL, NULL),
SWITCH_CONFIG_ITEM(
"min_word_length",
SWITCH_CONFIG_INT,
CONFIG_RELOADABLE,
&globals.minimum_word_length,
(void *) 100,
NULL, NULL, NULL),
SWITCH_CONFIG_ITEM(
"between_words_silence",
SWITCH_CONFIG_INT,
CONFIG_RELOADABLE,
&globals.between_words_silence,
(void *) 50,
NULL, NULL, NULL),
SWITCH_CONFIG_ITEM(
"maximum_number_of_words",
SWITCH_CONFIG_INT,
CONFIG_RELOADABLE,
&globals.maximum_number_of_words,
(void *) 3,
NULL, NULL, NULL),
SWITCH_CONFIG_ITEM(
"maximum_word_length",
SWITCH_CONFIG_INT,
CONFIG_RELOADABLE,
&globals.maximum_word_length,
(void *)5000,
NULL, NULL, NULL),
SWITCH_CONFIG_ITEM(
"silence_threshold",
SWITCH_CONFIG_INT,
CONFIG_RELOADABLE,
&globals.silence_threshold,
(void *) 256,
NULL, NULL, NULL),
SWITCH_CONFIG_ITEM(
"silence_notsure",
SWITCH_CONFIG_INT,
CONFIG_RELOADABLE,
&globals.silence_notsure,
(void *) 0,
NULL, NULL, NULL),
SWITCH_CONFIG_ITEM_END()
};
static switch_status_t do_config(switch_bool_t reload)
{
memset(&globals, 0, sizeof(globals));
if (switch_xml_config_parse_module_settings("amd.conf", reload, instructions) != SWITCH_STATUS_SUCCESS) {
return SWITCH_STATUS_FALSE;
}
return SWITCH_STATUS_SUCCESS;
}
SWITCH_MODULE_LOAD_FUNCTION(mod_amd_load)
{
switch_application_interface_t *app_interface;
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
do_config(SWITCH_FALSE);
SWITCH_ADD_APP(
app_interface,
"amd",
"Voice activity detection (non-blocking)",
"Asterisk's AMD (Non-blocking)",
amd_start_function,
NULL,
SAF_NONE);
return SWITCH_STATUS_SUCCESS;
}
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_amd_shutdown)
{
switch_xml_config_cleanup(instructions);
return SWITCH_STATUS_SUCCESS;
}
typedef enum {
SILENCE,
VOICED
} amd_frame_classifier;
typedef enum {
VAD_STATE_IN_WORD,
VAD_STATE_IN_SILENCE,
} amd_vad_state_t;
typedef struct amd_vad_c {
switch_core_session_t *session;
switch_channel_t *channel;
switch_codec_implementation_t read_impl;
amd_vad_state_t state;
amd_params_t params;
uint32_t frame_ms;
int32_t sample_count_limit;
uint32_t silence_duration;
uint32_t voice_duration;
uint32_t words;
uint32_t in_initial_silence:1;
uint32_t in_greeting:1;
} amd_vad_t;
static amd_frame_classifier classify_frame(uint32_t silence_threshold, const switch_frame_t *f, const switch_codec_implementation_t *codec)
{
int16_t *audio = f->data;
uint32_t score, count, j;
double energy;
for (energy = 0, j = 0, count = 0; count < f->samples; count++) {
energy += abs(audio[j++]);
}
score = (uint32_t) (energy / (f->samples));
if (score >= silence_threshold) {
return VOICED;
}
return SILENCE;
}
static switch_bool_t amd_handle_silence_frame(amd_vad_t *vad, const switch_frame_t *f)
{
vad->silence_duration += vad->frame_ms;
if (vad->silence_duration >= vad->params.between_words_silence) {
if (vad->state != VAD_STATE_IN_SILENCE) {
switch_log_printf(
SWITCH_CHANNEL_SESSION_LOG(vad->session),
SWITCH_LOG_DEBUG,
"AMD: Changed state to VAD_STATE_IN_SILENCE\n");
}
vad->state = VAD_STATE_IN_SILENCE;
vad->voice_duration = 0;
}
if (vad->in_initial_silence && vad->silence_duration >= vad->params.initial_silence) {
const char *result = (vad->params.silence_notsure > 0) ? "NOTSURE" : "MACHINE";
switch_log_printf(
SWITCH_CHANNEL_SESSION_LOG(vad->session),
SWITCH_LOG_DEBUG,
"AMD: %s (silence_duration: %d, initial_silence: %d, silence_notsure: %d)\n",
result,
vad->silence_duration,
vad->params.initial_silence,
vad->params.silence_notsure);
switch_channel_set_variable(vad->channel, "amd_result", result);
switch_channel_set_variable(vad->channel, "amd_cause", "INITIALSILENCE");
return SWITCH_TRUE;
}
if (vad->silence_duration >= vad->params.after_greeting_silence && vad->in_greeting) {
switch_log_printf(
SWITCH_CHANNEL_SESSION_LOG(vad->session),
SWITCH_LOG_DEBUG,
"AMD: HUMAN (silence_duration: %d, after_greeting_silence: %d)\n",
vad->silence_duration,
vad->params.after_greeting_silence);
switch_channel_set_variable(vad->channel, "amd_result", "HUMAN");
switch_channel_set_variable(vad->channel, "amd_cause", "SILENCEAFTERGREETING");
return SWITCH_TRUE;
}
return SWITCH_FALSE;
}
static switch_bool_t amd_handle_voiced_frame(amd_vad_t *vad, const switch_frame_t *f)
{
vad->voice_duration += vad->frame_ms;
if (vad->voice_duration >= vad->params.minimum_word_length && vad->state == VAD_STATE_IN_SILENCE) {
vad->words++;
switch_log_printf(
SWITCH_CHANNEL_SESSION_LOG(vad->session),
SWITCH_LOG_DEBUG,
"AMD: Word detected (words: %d)\n",
vad->words);
vad->state = VAD_STATE_IN_WORD;
}
if (vad->voice_duration >= vad->params.maximum_word_length) {
switch_log_printf(
SWITCH_CHANNEL_SESSION_LOG(vad->session),
SWITCH_LOG_DEBUG,
"AMD: MACHINE (voice_duration: %d, maximum_word_length: %d)\n",
vad->voice_duration,
vad->params.maximum_word_length);
switch_channel_set_variable(vad->channel, "amd_result", "MACHINE");
switch_channel_set_variable(vad->channel, "amd_cause", "MAXWORDLENGTH");
return SWITCH_TRUE;
}
if (vad->words >= vad->params.maximum_number_of_words) {
switch_log_printf(
SWITCH_CHANNEL_SESSION_LOG(vad->session),
SWITCH_LOG_DEBUG,
"AMD: MACHINE (words: %d, maximum_number_of_words: %d)\n",
vad->words,
vad->params.maximum_number_of_words);
switch_channel_set_variable(vad->channel, "amd_result", "MACHINE");
switch_channel_set_variable(vad->channel, "amd_cause", "MAXWORDS");
return SWITCH_TRUE;
}
if (vad->in_greeting && vad->voice_duration >= vad->params.greeting) {
switch_log_printf(
SWITCH_CHANNEL_SESSION_LOG(vad->session),
SWITCH_LOG_DEBUG,
"AMD: MACHINE (voice_duration: %d, greeting: %d)\n",
vad->voice_duration,
vad->params.greeting);
switch_channel_set_variable(vad->channel, "amd_result", "MACHINE");
switch_channel_set_variable(vad->channel, "amd_cause", "LONGGREETING");
return SWITCH_TRUE;
}
if (vad->voice_duration >= vad->params.minimum_word_length) {
if (vad->silence_duration) {
switch_log_printf(
SWITCH_CHANNEL_SESSION_LOG(vad->session),
SWITCH_LOG_DEBUG,
"AMD: Detected Talk, previous silence duration: %dms\n",
vad->silence_duration);
}
vad->silence_duration = 0;
}
if (vad->voice_duration >= vad->params.minimum_word_length && !vad->in_greeting) {
if (vad->silence_duration) {
switch_log_printf(
SWITCH_CHANNEL_SESSION_LOG(vad->session),
SWITCH_LOG_DEBUG,
"AMD: Before Greeting Time (silence_duration: %d, voice_duration: %d)\n",
vad->silence_duration,
vad->voice_duration);
}
vad->in_initial_silence = 0;
vad->in_greeting = 1;
}
return SWITCH_FALSE;
}
static void amd_fire_event(switch_channel_t *channel) {
switch_event_t *event;
switch_status_t status;
status = switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, AMD_EVENT_NAME);
if (status != SWITCH_STATUS_SUCCESS) {
return;
}
switch_channel_event_set_data(channel, event);
switch_event_fire(&event);
}
static void do_execute(switch_core_session_t *session, switch_channel_t *channel, const char *name) {
char *arg = NULL;
char *p;
int bg = 0;
const char *variable = switch_channel_get_variable(channel, name);
char *expanded = NULL;
char *app = NULL;
if (!variable) {
return;
}
expanded = switch_channel_expand_variables(channel, variable);
app = switch_core_session_strdup(session, expanded);
for(p = app; p && *p; p++) {
if (*p == ' ' || (*p == ':' && (*(p+1) != ':'))) {
*p++ = '\0';
arg = p;
break;
} else if (*p == ':' && (*(p+1) == ':')) {
bg++;
break;
}
}
switch_assert(app != NULL);
if (!strncasecmp(app, "perl", 4)) {
bg++;
}
if (bg) {
switch_core_session_execute_application_async(session, app, arg);
} else {
switch_core_session_execute_application(session, app, arg);
}
}
static switch_bool_t amd_read_audio_callback(switch_media_bug_t *bug, void *user_data, switch_abc_type_t type)
{
struct amd_vad_c *vad = (struct amd_vad_c *) user_data;
switch (type) {
case SWITCH_ABC_TYPE_INIT:
{
switch_core_session_get_read_impl(vad->session, &vad->read_impl);
if (vad->params.total_analysis_time) {
vad->sample_count_limit = (vad->read_impl.actual_samples_per_second / 1000) * vad->params.total_analysis_time;
}
break;
}
case SWITCH_ABC_TYPE_CLOSE:
{
if (switch_channel_ready(vad->channel)) {
const char *result = NULL;
switch_channel_set_variable(vad->channel, "amd_result_microtime", switch_mprintf( "%" SWITCH_TIME_T_FMT, switch_micro_time_now( ) ));
switch_channel_set_variable(vad->channel, "amd_result_epoch", switch_mprintf( "%" SWITCH_TIME_T_FMT, switch_micro_time_now( ) / 1000000 ));
result = switch_channel_get_variable(vad->channel, "amd_result");
if (result == NULL) {
//TODO set error ?
result = "NOTSURE";
switch_log_printf(
SWITCH_CHANNEL_SESSION_LOG(vad->session),
SWITCH_LOG_WARNING,
"No found variable amd_result set amd_result=NOTSURE\n");
switch_channel_set_variable(vad->channel, "amd_result", "NOTSURE");
switch_channel_set_variable(vad->channel, "amd_cause", "TOOLONG");
}
amd_fire_event(vad->channel);
if (!strcasecmp(result, "MACHINE")) {
do_execute(vad->session, vad->channel, "amd_on_machine");
} else if (!strcasecmp(result, "HUMAN")) {
do_execute(vad->session, vad->channel, "amd_on_human");
} else {
do_execute(vad->session, vad->channel, "amd_on_notsure");
}
}
switch_log_printf(
SWITCH_CHANNEL_SESSION_LOG(vad->session),
SWITCH_LOG_DEBUG,
"AMD: close\n");
break;
}
case SWITCH_ABC_TYPE_READ_PING:
{
uint8_t data[SWITCH_RECOMMENDED_BUFFER_SIZE];
switch_frame_t read_frame = { 0 };
switch_status_t status;
read_frame.data = data;
read_frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
status = switch_core_media_bug_read(bug, &read_frame, SWITCH_FALSE);
if (status != SWITCH_STATUS_SUCCESS && status != SWITCH_STATUS_BREAK) {
return SWITCH_TRUE;
}
if (vad->sample_count_limit) {
vad->sample_count_limit -= read_frame.samples;
if (vad->sample_count_limit <= 0) {
switch_channel_set_variable(vad->channel, "amd_result", "NOTSURE");
switch_channel_set_variable(vad->channel, "amd_cause", "TOOLONG");
return SWITCH_FALSE;
}
}
vad->frame_ms = 1000 / (vad->read_impl.actual_samples_per_second / read_frame.samples);
switch (classify_frame(vad->params.silence_threshold, &read_frame, &vad->read_impl)) {
case SILENCE:
switch_log_printf(
SWITCH_CHANNEL_SESSION_LOG(vad->session),
SWITCH_LOG_DEBUG,
"AMD: Silence\n");
if (amd_handle_silence_frame(vad, &read_frame) == SWITCH_TRUE) {
return SWITCH_FALSE;
}
break;
case VOICED:
default:
switch_log_printf(
SWITCH_CHANNEL_SESSION_LOG(vad->session),
SWITCH_LOG_DEBUG,
"AMD: Voiced\n");
if (amd_handle_voiced_frame(vad, &read_frame) == SWITCH_TRUE) {
return SWITCH_FALSE;
}
break;
}
break;
}
default:
break;
}
return SWITCH_TRUE;
}
SWITCH_STANDARD_APP(amd_start_function)
{
switch_channel_t *channel = switch_core_session_get_channel(session);
switch_media_bug_t *bug = NULL;
switch_media_bug_flag_t flags = SMBF_READ_STREAM | SMBF_READ_PING;
char *arg = (char *) data;
char delim = ' ';
amd_vad_t *vad = NULL;
vad = switch_core_session_alloc(session, sizeof(*vad));
if (!session) {
return;
}
vad->params = globals;
vad->channel = channel;
vad->session = session;
vad->state = VAD_STATE_IN_WORD;
vad->silence_duration = 0;
vad->voice_duration = 0;
vad->frame_ms = 0;
vad->in_initial_silence = 1;
vad->in_greeting = 0;
vad->words = 0;
/* Start: parsing argument(s) */
if (!zstr(arg) && *arg == '^' && *(arg+1) == '^') {
arg += 2;
delim = *arg++;
}
if (arg) {
int x, argc;
char *argv[10] = { 0 };
char *param[2] = { 0 };
arg = switch_core_session_strdup(session, arg);
argc = switch_split(arg, delim, argv);
for (x = 0; x < argc; x++) {
if (switch_separate_string(argv[x], '=', param, switch_arraylen(param)) == 2) {
int value = 0;
if (!strcasecmp(param[0], "initial_silence")) {
if ((value = atoi(param[1])) > 0) {
vad->params.initial_silence = value;
}
} else if (!strcasecmp(param[0], "greeting")) {
if ((value = atoi(param[1])) > 0) {
vad->params.greeting = value;
}
} else if (!strcasecmp(param[0], "after_greeting_silence")) {
if ((value = atoi(param[1])) > 0) {
vad->params.after_greeting_silence = value;
}
} else if (!strcasecmp(param[0], "total_analysis_time")) {
if ((value = atoi(param[1])) > 0) {
vad->params.total_analysis_time = value;
}
} else if (!strcasecmp(param[0], "min_word_length")) {
if ((value = atoi(param[1])) > 0) {
vad->params.minimum_word_length = value;
}
} else if (!strcasecmp(param[0], "between_words_silence")) {
if ((value = atoi(param[1])) > 0) {
vad->params.between_words_silence = value;
}
} else if (!strcasecmp(param[0], "maximum_number_of_words")) {
if ((value = atoi(param[1])) > 0) {
vad->params.maximum_number_of_words = value;
}
} else if (!strcasecmp(param[0], "maximum_word_length")) {
if ((value = atoi(param[1])) > 0) {
vad->params.maximum_word_length = value;
}
} else if (!strcasecmp(param[0], "silence_threshold")) {
if ((value = atoi(param[1])) > 0) {
vad->params.silence_threshold = value;
}
} else if (!strcasecmp(param[0], "silence_notsure")) {
if ((value = atoi(param[1])) > 0) {
vad->params.silence_notsure = 1;
}
}
if (value > 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "AMD: Apply [%s]=[%d]\n", param[0], value);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "AMD: Invalid [%s]=[%s]; Value must be positive integer only!\n", param[0], param[1]);
continue;
}
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "AMD: Ignore argument [%s]\n", argv[x]);
}
}
}
/* End: parsing argument(s) */
if (!switch_channel_media_up(channel) || !switch_core_session_get_read_codec(session)) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Can not record session. Media not enabled on channel\n");
return;
}
if (switch_core_media_bug_add(
session,
BUG_AMD_NAME_READ,
NULL,
amd_read_audio_callback,
vad,
0, //TODO
flags,
&bug) != SWITCH_STATUS_SUCCESS ) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Can not add media bug. Media not enabled on channel\n");
return;
}
}