/
mod_pocketsphinx.c
841 lines (651 loc) · 26.7 KB
/
mod_pocketsphinx.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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
/*
* (C) 2016 Unicall
*
* Description: IVR speech recognition with ifly iat
*
* Author: chenbingfeng
*
*/
#include <switch.h>
#include <pocketsphinx.h>
#include <sphinxbase/err.h>
#include <sphinxbase/logmath.h>
#include "ifly/qisr.h"
#include "ifly/msp_cmn.h"
#include "ifly/msp_errors.h"
#define IFLY_BUFFER_SIZE 4096
#define IFLY_FRAME_LEN 640
#define IFLY_HINTS_SIZE 100
SWITCH_MODULE_LOAD_FUNCTION(mod_pocketsphinx_load);
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_pocketsphinx_shutdown);
SWITCH_MODULE_DEFINITION(mod_pocketsphinx, mod_pocketsphinx_load, mod_pocketsphinx_shutdown, NULL);
static switch_mutex_t *MUTEX = NULL;
static switch_event_node_t *NODE = NULL;
static struct {
char *model8k;
char *model16k;
char *dictionary;
char *language_weight;
uint32_t thresh;
int no_input_timeout;
int speech_timeout;
switch_bool_t start_input_timers;
int confidence_threshold;
uint32_t silence_hits;
uint32_t listen_hits;
int auto_reload;
switch_memory_pool_t *pool;
} globals;
typedef enum {
PSFLAG_HAS_TEXT = (1 << 0),
PSFLAG_READY = (1 << 1),
PSFLAG_BARGE = (1 << 2),
PSFLAG_ALLOCATED = (1 << 3),
PSFLAG_INPUT_TIMERS = (1 << 4),
PSFLAG_START_OF_SPEECH = (1 << 5),
PSFLAG_NOINPUT_TIMEOUT = (1 << 6),
PSFLAG_SPEECH_TIMEOUT = (1 << 7),
PSFLAG_NOINPUT = (1 << 8),
PSFLAG_NOMATCH = (1 << 9)
} psflag_t;
typedef struct {
ps_decoder_t *ps;
uint32_t flags;
switch_mutex_t *flag_mutex;
uint32_t org_silence_hits;
uint32_t thresh;
uint32_t silence_hits;
uint32_t listen_hits;
uint32_t listening;
uint32_t countdown;
int no_input_timeout;
int speech_timeout;
switch_bool_t start_input_timers;
switch_time_t silence_time;
int confidence_threshold;
char *hyp;
char *grammar;
int32_t score;
int32_t confidence;
char const *uttid;
cmd_ln_t *config;
const char* ifly_session_id;
char ifly_rec_result[IFLY_BUFFER_SIZE];
char ifly_hints[IFLY_HINTS_SIZE];
unsigned int ifly_total_len;
int ifly_aud_stat;
int ifly_ep_stat;
int ifly_rec_stat;
int ifly_errcode;
switch_bool_t ifly_wait_result;
} pocketsphinx_t;
/*! function to open the asr interface */
static switch_status_t pocketsphinx_asr_open(switch_asr_handle_t *ah, const char *codec, int rate, const char *dest, switch_asr_flag_t *flags)
{
pocketsphinx_t *ps;
const char* session_begin_params = "sub = iat, domain = iat, language = zh_ch, accent = mandarin, sample_rate = 16000, result_type = plain, result_encoding = utf8";
int errcode;
/* chen */
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>>pocketsphinx_asr_open<<<<<<<<<\n");
if (!(ps = (pocketsphinx_t *) switch_core_alloc(ah->memory_pool, sizeof(*ps)))) {
return SWITCH_STATUS_MEMERR;
}
switch_mutex_init(&ps->flag_mutex, SWITCH_MUTEX_NESTED, ah->memory_pool);
ah->private_info = ps;
if (rate == 8000) {
ah->rate = 8000;
} else if (rate == 16000) {
ah->rate = 16000;
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid rate %d. Only 8000 and 16000 are supported.\n", rate);
}
codec = "L16";
ah->codec = switch_core_strdup(ah->memory_pool, codec);
ps->thresh = globals.thresh;
ps->silence_hits = globals.silence_hits;
ps->listen_hits = globals.listen_hits;
ps->org_silence_hits = ps->silence_hits;
ps->start_input_timers = globals.start_input_timers;
ps->no_input_timeout = globals.no_input_timeout;
ps->speech_timeout = globals.speech_timeout;
ps->confidence_threshold = globals.confidence_threshold;
/* ify login*/
ps->ifly_session_id = NULL;
//ps->ifly_rec_result = {NULL};
//ps->ifly_hints = {NULL}; //hints为结束本次会话的原因描述,由用户自定义
ps->ifly_total_len = 0;
ps->ifly_aud_stat = MSP_AUDIO_SAMPLE_CONTINUE ; //音频状态
ps->ifly_ep_stat = MSP_EP_LOOKING_FOR_SPEECH; //端点检测
ps->ifly_rec_stat = MSP_REC_STATUS_SUCCESS ; //识别状态
ps->ifly_wait_result = SWITCH_FALSE;
/* ifly open session */
ps->ifly_session_id = QISRSessionBegin(NULL, session_begin_params, &errcode);
if (MSP_SUCCESS != errcode) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>>QISRSessionBegin fail error !!!<<<<<<<<<\n");
}
return SWITCH_STATUS_SUCCESS;
}
/*! function to load a grammar to the asr interface */
static switch_status_t pocketsphinx_asr_load_grammar(switch_asr_handle_t *ah, const char *grammar, const char *name)
{
char *jsgf, *dic, *model, *rate = NULL;
pocketsphinx_t *ps = (pocketsphinx_t *) ah->private_info;
switch_status_t status = SWITCH_STATUS_FALSE;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>>pocketsphinx_asr_load_grammar<<<<<<<<<\n");
if (switch_test_flag(ps, PSFLAG_READY)) {
ps_end_utt(ps->ps);
switch_clear_flag(ps, PSFLAG_READY);
}
if (switch_is_file_path(grammar)) {
char *dot = strrchr(grammar, '.');
if (dot && !strcmp(dot, ".gram")) {
jsgf = strdup(grammar);
} else {
jsgf = switch_mprintf("%s.gram", grammar);
}
} else {
jsgf = switch_mprintf("%s%s%s.gram", SWITCH_GLOBAL_dirs.grammar_dir, SWITCH_PATH_SEPARATOR, grammar);
}
if (ah->rate == 8000) {
model = switch_mprintf("%s%smodel%s%s", SWITCH_GLOBAL_dirs.grammar_dir, SWITCH_PATH_SEPARATOR, SWITCH_PATH_SEPARATOR, globals.model8k);
} else {
model = switch_mprintf("%s%smodel%s%s", SWITCH_GLOBAL_dirs.grammar_dir, SWITCH_PATH_SEPARATOR, SWITCH_PATH_SEPARATOR, globals.model16k);
}
dic = switch_mprintf("%s%s%s", SWITCH_GLOBAL_dirs.grammar_dir, SWITCH_PATH_SEPARATOR, globals.dictionary);
if (switch_file_exists(dic, ah->memory_pool) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't open dictionary %s.\n", dic);
goto end;
}
if (switch_file_exists(model, ah->memory_pool) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Can't open speech model %s.\n", model);
goto end;
}
if (switch_file_exists(jsgf, ah->memory_pool) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Can't open grammar file %s.\n", jsgf);
goto end;
}
rate = switch_mprintf("%d", ah->rate);
switch_assert(jsgf && dic && model);
ps->config = cmd_ln_init(ps->config, ps_args(), FALSE,
"-samprate", rate,
"-hmm", model, "-jsgf", jsgf, "-lw", globals.language_weight, "-dict", dic, "-frate", "50", "-silprob", "0.005", NULL);
if (ps->config == NULL) {
status = SWITCH_STATUS_GENERR;
goto end;
}
switch_mutex_lock(ps->flag_mutex);
if (switch_test_flag(ps, PSFLAG_ALLOCATED)) {
ps_reinit(ps->ps, ps->config);
} else {
if (!(ps->ps = ps_init(ps->config))) {
switch_mutex_unlock(ps->flag_mutex);
goto end;
}
switch_set_flag(ps, PSFLAG_ALLOCATED);
}
switch_mutex_unlock(ps->flag_mutex);
ps_start_utt(ps->ps, NULL);
ps->silence_time = switch_micro_time_now();
switch_clear_flag(ps, PSFLAG_START_OF_SPEECH);
switch_clear_flag(ps, PSFLAG_NOINPUT_TIMEOUT);
switch_clear_flag(ps, PSFLAG_NOINPUT);
switch_clear_flag(ps, PSFLAG_NOMATCH);
switch_clear_flag(ps, PSFLAG_SPEECH_TIMEOUT);
if (ps->start_input_timers) {
switch_set_flag(ps, PSFLAG_INPUT_TIMERS);
} else {
switch_clear_flag(ps, PSFLAG_INPUT_TIMERS);
}
switch_set_flag(ps, PSFLAG_READY);
switch_safe_free(ps->grammar);
ps->grammar = strdup(grammar);
status = SWITCH_STATUS_SUCCESS;
end:
switch_safe_free(rate);
switch_safe_free(jsgf);
switch_safe_free(dic);
switch_safe_free(model);
return status;
}
/*! function to unload a grammar to the asr interface */
static switch_status_t pocketsphinx_asr_unload_grammar(switch_asr_handle_t *ah, const char *name)
{
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>>pocketsphinx_asr_unload_grammar<<<<<<<<<\n");
return SWITCH_STATUS_SUCCESS;
}
/*! function to close the asr interface */
static switch_status_t pocketsphinx_asr_close(switch_asr_handle_t *ah, switch_asr_flag_t *flags)
{
pocketsphinx_t *ps = (pocketsphinx_t *) ah->private_info;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>>pocketsphinx_asr_close<<<<<<<<<\n");
switch_mutex_lock(ps->flag_mutex);
if (switch_test_flag(ps, PSFLAG_ALLOCATED)) {
if (switch_test_flag(ps, PSFLAG_READY)) {
ps_end_utt(ps->ps);
}
ps_free(ps->ps);
ps->ps = NULL;
}
switch_safe_free(ps->grammar);
switch_mutex_unlock(ps->flag_mutex);
switch_clear_flag(ps, PSFLAG_HAS_TEXT);
switch_clear_flag(ps, PSFLAG_READY);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Port Closed.\n");
switch_set_flag(ah, SWITCH_ASR_FLAG_CLOSED);
/* ifly end session */
QISRSessionEnd(ps->ifly_session_id, ps->ifly_hints);
return SWITCH_STATUS_SUCCESS;
}
/* detect if should stop */
static switch_bool_t stop_detect(pocketsphinx_t *ps, int16_t *data, unsigned int samples)
{
uint32_t score, count = 0, j = 0;
double energy = 0;
if (ps->countdown) {
if (!--ps->countdown) {
/* when countdown ==0 */
ps->silence_hits = ps->org_silence_hits;
ps->listening = 0;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>>stop_detect:stop by countdown=0<<<<<<<<<\n");
ps->ifly_wait_result = SWITCH_TRUE;
return SWITCH_TRUE;
}
return SWITCH_FALSE;
}
/* Do simple energy threshold for VAD */
for (count = 0; count < samples; count++) {
energy += abs(data[j]);
}
score = (uint32_t) (energy / samples);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
">>>>>>>>detect score = %d listening=%d status=%d ep=%d<<<<<<<<<\n",
(int)score,
ps->listening,
ps->ifly_rec_stat,
ps->ifly_ep_stat);
if (score >= ps->thresh) {
if (++ps->listening == 1) {
switch_mutex_lock(ps->flag_mutex);
switch_set_flag(ps, PSFLAG_BARGE);
switch_set_flag(ps, PSFLAG_START_OF_SPEECH);
switch_mutex_unlock(ps->flag_mutex);
}
ps->silence_time = 0;
} else if (!ps->silence_time) {
ps->silence_time = switch_micro_time_now();
}
/* Check silence timeouts */
if (ps->silence_time && switch_test_flag(ps, PSFLAG_INPUT_TIMERS)) {
switch_time_t elapsed_ms = (switch_micro_time_now() - ps->silence_time) / 1000;
if (switch_test_flag(ps, PSFLAG_START_OF_SPEECH)) {
if (ps->speech_timeout > 0 && !switch_test_flag(ps, PSFLAG_SPEECH_TIMEOUT) && elapsed_ms >= ps->speech_timeout) {
switch_set_flag_locked(ps, PSFLAG_SPEECH_TIMEOUT);
ps->listening = 0;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>>stop_detect:stop by speech_timeout<<<<<<<<<\n");
return SWITCH_TRUE;
}
} else {
if (ps->no_input_timeout > 0 && !switch_test_flag(ps, PSFLAG_NOINPUT_TIMEOUT) && elapsed_ms >= ps->no_input_timeout) {
switch_mutex_lock(ps->flag_mutex);
switch_set_flag(ps, PSFLAG_NOINPUT_TIMEOUT);
switch_set_flag(ps, PSFLAG_NOINPUT);
switch_mutex_unlock(ps->flag_mutex);
ps->listening = 0;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>>stop_detect:stop by no_input_timeout<<<<<<<<<\n");
return SWITCH_TRUE;
}
}
}
if (ps->listening > ps->listen_hits && score < ps->thresh) {
if (!--ps->silence_hits) {
ps->countdown = 12;
}
} else {
ps->silence_hits = ps->org_silence_hits;
}
return SWITCH_FALSE;
}
/*! function to feed audio to the ASR */
static switch_status_t pocketsphinx_asr_feed(switch_asr_handle_t *ah, void *data, unsigned int len, switch_asr_flag_t *flags)
{
pocketsphinx_t *ps = (pocketsphinx_t *) ah->private_info;
int rv = 0;
int ret = MSP_SUCCESS;
int errcode = MSP_SUCCESS;
//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>>pocketsphinx_asr_feed<<<<<<<<<\n");
if (switch_test_flag(ah, SWITCH_ASR_FLAG_CLOSED))
return SWITCH_STATUS_BREAK;
if (!switch_test_flag(ps, PSFLAG_NOMATCH) && !switch_test_flag(ps, PSFLAG_NOINPUT) && !switch_test_flag(ps, PSFLAG_HAS_TEXT) && switch_test_flag(ps, PSFLAG_READY)) {
if (stop_detect(ps, (int16_t *) data, len / 2)) {
char const *hyp;
switch_mutex_lock(ps->flag_mutex);
QISRAudioWrite(ps->ifly_session_id, NULL, 0, MSP_AUDIO_SAMPLE_LAST, &(ps->ifly_ep_stat), &(ps->ifly_rec_stat));
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>> tell zero <<<<<<<<<\n");
hyp = QISRGetResult(ps->ifly_session_id, &(ps->ifly_rec_stat), 0, &errcode);
if (hyp || ps->ifly_rec_stat != MSP_REC_STATUS_INCOMPLETE) {
ps->ifly_wait_result = SWITCH_FALSE;
}
if (hyp) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>> k0<<<<<<<<<\n");
if (errcode == MSP_SUCCESS && !zstr(hyp)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>> k2<<<<<<<<<\n");
switch_clear_flag(ps, PSFLAG_READY);
ps->hyp = switch_core_strdup(ah->memory_pool, hyp);
switch_set_flag(ps, PSFLAG_HAS_TEXT);
}
}
if (switch_test_flag(ps, PSFLAG_SPEECH_TIMEOUT) && !switch_test_flag(ps, PSFLAG_HAS_TEXT)) {
/* heard something, but doesn't match anything */
switch_clear_flag(ps, PSFLAG_READY);
switch_set_flag(ps, PSFLAG_NOMATCH);
}
switch_mutex_unlock(ps->flag_mutex);
} else if ( ps->ifly_wait_result == SWITCH_TRUE ) {
char const *hyp;
switch_mutex_lock(ps->flag_mutex);
hyp = QISRGetResult(ps->ifly_session_id, &(ps->ifly_rec_stat), 0, &errcode);
if (hyp || ps->ifly_rec_stat != MSP_REC_STATUS_INCOMPLETE) {
ps->ifly_wait_result = SWITCH_FALSE;
}
if (hyp) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>> k0<<<<<<<<<\n");
if (errcode == MSP_SUCCESS && !zstr(hyp)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>> k2<<<<<<<<<\n");
switch_clear_flag(ps, PSFLAG_READY);
ps->hyp = switch_core_strdup(ah->memory_pool, hyp);
switch_set_flag(ps, PSFLAG_HAS_TEXT);
}
}
switch_mutex_unlock(ps->flag_mutex);
}
/* only feed ps_process_raw when we are listening */
if (ps->listening) {
switch_mutex_lock(ps->flag_mutex);
//rv = ps_process_raw(ps->ps, (int16 *) data, len / 2, FALSE, FALSE);
/* chenbingfeng*/
ret = QISRAudioWrite(ps->ifly_session_id, (const void *)data, len, ps->ifly_aud_stat, &(ps->ifly_ep_stat), &(ps->ifly_rec_stat));
if (MSP_SUCCESS != ret)
{
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "\nQISRAudioWrite failed! error code:%d\n", ret);
}
switch_mutex_unlock(ps->flag_mutex);
}
if (rv < 0) {
return SWITCH_STATUS_FALSE;
}
} else if (switch_test_flag(ps, PSFLAG_NOINPUT_TIMEOUT)) {
/* never heard anything */
switch_clear_flag_locked(ps, PSFLAG_READY);
}
return SWITCH_STATUS_SUCCESS;
}
/*! function to pause recognizer */
static switch_status_t pocketsphinx_asr_pause(switch_asr_handle_t *ah)
{
pocketsphinx_t *ps = (pocketsphinx_t *) ah->private_info;
switch_status_t status = SWITCH_STATUS_FALSE;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>>pocketsphinx_asr_pause<<<<<<<<<\n");
switch_mutex_lock(ps->flag_mutex);
if (switch_test_flag(ps, PSFLAG_READY)) {
ps_end_utt(ps->ps);
switch_clear_flag(ps, PSFLAG_READY);
status = SWITCH_STATUS_SUCCESS;
}
switch_mutex_unlock(ps->flag_mutex);
return status;
}
/*! function to resume recognizer */
static switch_status_t pocketsphinx_asr_resume(switch_asr_handle_t *ah)
{
pocketsphinx_t *ps = (pocketsphinx_t *) ah->private_info;
switch_status_t status = SWITCH_STATUS_FALSE;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>>pocketsphinx_asr_resume<<<<<<<<<\n");
switch_mutex_lock(ps->flag_mutex);
switch_clear_flag(ps, PSFLAG_HAS_TEXT);
ps->silence_time = switch_micro_time_now();
if (!switch_test_flag(ps, PSFLAG_READY)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Manually Resuming\n");
if (ps_start_utt(ps->ps, NULL)) {
status = SWITCH_STATUS_GENERR;
} else {
switch_set_flag(ps, PSFLAG_READY);
}
}
switch_mutex_unlock(ps->flag_mutex);
return status;
}
/*! function to read results from the ASR*/
static switch_status_t pocketsphinx_asr_check_results(switch_asr_handle_t *ah, switch_asr_flag_t *flags)
{
pocketsphinx_t *ps = (pocketsphinx_t *) ah->private_info;
//int errcode = MSP_SUCCESS;
//const char *rslt;
//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>>pocketsphinx_asr_check_results<<<<<<<<<\n");
/* chenbingfeng */
/*
rslt = QISRGetResult(ps->ifly_session_id, &(ps->ifly_rec_stat), 0, &errcode);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>>result !!!>> %s <<<<<<<<<<<<\n", rslt);
*/
return (switch_test_flag(ps, PSFLAG_NOINPUT) || switch_test_flag(ps, PSFLAG_NOMATCH) || switch_test_flag(ps, PSFLAG_HAS_TEXT) || switch_test_flag(ps, PSFLAG_BARGE)) ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
}
/*! function to read results from the ASR */
static switch_status_t pocketsphinx_asr_get_results(switch_asr_handle_t *ah, char **xmlstr, switch_asr_flag_t *flags)
{
pocketsphinx_t *ps = (pocketsphinx_t *) ah->private_info;
switch_status_t status = SWITCH_STATUS_SUCCESS;
//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>>pocketsphinx_asr_get_results<<<<<<<<<\n");
if (switch_test_flag(ps, PSFLAG_BARGE)) {
switch_clear_flag_locked(ps, PSFLAG_BARGE);
status = SWITCH_STATUS_BREAK;
}
if (switch_test_flag(ps, PSFLAG_HAS_TEXT)) {
switch_mutex_lock(ps->flag_mutex);
switch_clear_flag(ps, PSFLAG_HAS_TEXT);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Recognized: %s, Confidence: %d, Confidence-Threshold: %d\n", ps->hyp, ps->confidence, ps->confidence_threshold);
switch_mutex_unlock(ps->flag_mutex);
*xmlstr = switch_mprintf("%s", ps->hyp);
if (!switch_test_flag(ps, PSFLAG_INPUT_TIMERS) && switch_test_flag(ah, SWITCH_ASR_FLAG_AUTO_RESUME)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Auto Resuming\n");
switch_set_flag(ps, PSFLAG_READY);
ps_start_utt(ps->ps, NULL);
}
status = SWITCH_STATUS_SUCCESS;
} else if (switch_test_flag(ps, PSFLAG_NOINPUT)) {
switch_clear_flag_locked(ps, PSFLAG_NOINPUT);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "NO INPUT\n");
*xmlstr = switch_mprintf("");
status = SWITCH_STATUS_SUCCESS;
} else if (switch_test_flag(ps, PSFLAG_NOMATCH)) {
switch_clear_flag_locked(ps, PSFLAG_NOMATCH);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "NO MATCH\n");
*xmlstr = switch_mprintf("");
status = SWITCH_STATUS_SUCCESS;
}
return status;
}
/*! function to start input timeouts */
static switch_status_t pocketsphinx_asr_start_input_timers(switch_asr_handle_t *ah)
{
pocketsphinx_t *ps = (pocketsphinx_t *) ah->private_info;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>>pocketsphinx_asr_start_input_timers<<<<<<<<<\n");
switch_set_flag_locked(ps, PSFLAG_INPUT_TIMERS);
return SWITCH_STATUS_SUCCESS;
}
/*! set text parameter */
static void pocketsphinx_asr_text_param(switch_asr_handle_t *ah, char *param, const char *val)
{
pocketsphinx_t *ps = (pocketsphinx_t *) ah->private_info;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>>pocketsphinx_asr_text_param<<<<<<<<<\n");
if (!zstr(param) && !zstr(val)) {
if (!strcasecmp("no-input-timeout", param) && switch_is_number(val)) {
ps->no_input_timeout = atoi(val);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "no-input-timeout = %d\n", ps->no_input_timeout);
} else if (!strcasecmp("speech-timeout", param) && switch_is_number(val)) {
ps->speech_timeout = atoi(val);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "speech-timeout = %d\n", ps->speech_timeout);
} else if (!strcasecmp("start-input-timers", param)) {
ps->start_input_timers = switch_true(val);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "start-input-timers = %d\n", ps->start_input_timers);
} else if (!strcasecmp("confidence-threshold", param) && switch_is_number(val)) {
ps->confidence_threshold = atoi(val);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "confidence-threshold = %d\n", ps->confidence_threshold);
}
}
}
/*! set numeric parameter */
static void pocketsphinx_asr_numeric_param(switch_asr_handle_t *ah, char *param, int val)
{
char *val_str = switch_mprintf("%d", val);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>>pocketsphinx_asr_numeric_param<<<<<<<<<\n");
pocketsphinx_asr_text_param(ah, param, val_str);
switch_safe_free(val_str);
}
/*! set float parameter */
static void pocketsphinx_asr_float_param(switch_asr_handle_t *ah, char *param, double val)
{
char *val_str = switch_mprintf("%f", val);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>>pocketsphinx_asr_float_param<<<<<<<<<\n");
pocketsphinx_asr_text_param(ah, param, val_str);
switch_safe_free(val_str);
}
static switch_status_t load_config(void)
{
char *cf = "pocketsphinx.conf";
switch_xml_t cfg, xml = NULL, param, settings;
switch_status_t status = SWITCH_STATUS_SUCCESS;
int ret = MSP_SUCCESS;
const char* login_params = "appid = 56f37a90, work_dir = ."; //登录参数,appid与msc库绑定,请勿随意改动
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>>load_config<<<<<<<<<\n");
/* Set defaults */
globals.thresh = 400;
globals.silence_hits = 15;
globals.listen_hits = 1;
globals.auto_reload = 1;
globals.start_input_timers = SWITCH_FALSE;
globals.no_input_timeout = 1000; // stop when silence last THIS time
globals.speech_timeout = 10000; // totcal speech length
globals.confidence_threshold = 0;
if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Open of %s failed\n", cf);
status = SWITCH_STATUS_FALSE;
goto done;
}
if ((settings = switch_xml_child(cfg, "settings"))) {
for (param = switch_xml_child(settings, "param"); param; param = param->next) {
char *var = (char *) switch_xml_attr_soft(param, "name");
char *val = (char *) switch_xml_attr_soft(param, "value");
if (!strcasecmp(var, "threshold")) {
globals.thresh = atoi(val);
} else if (!strcasecmp(var, "start-input-timers")) {
globals.start_input_timers = switch_true(val);
} else if (!strcasecmp(var, "no-input-timeout")) {
globals.no_input_timeout = atoi(val);
} else if (!strcasecmp(var, "speech-timeout")) {
globals.speech_timeout = atoi(val);
} else if (!strcasecmp(var, "confidence_threshold")) {
globals.confidence_threshold = atoi(val);
} else if (!strcasecmp(var, "silence-hits")) {
//globals.silence_hits = atoi(val);
} else if (!strcasecmp(var, "language-weight")) {
globals.language_weight = switch_core_strdup(globals.pool, val);
} else if (!strcasecmp(var, "listen-hits")) {
globals.listen_hits = atoi(val);
} else if (!strcasecmp(var, "auto-reload")) {
globals.auto_reload = switch_true(val);
} else if (!strcasecmp(var, "narrowband-model")) {
globals.model8k = switch_core_strdup(globals.pool, val);
} else if (!strcasecmp(var, "wideband-model")) {
globals.model16k = switch_core_strdup(globals.pool, val);
} else if (!strcasecmp(var, "dictionary")) {
globals.dictionary = switch_core_strdup(globals.pool, val);
}
}
}
if (!globals.model8k) {
globals.model8k = switch_core_strdup(globals.pool, "communicator");
}
if (!globals.model16k) {
globals.model16k = switch_core_strdup(globals.pool, "wsj1");
}
if (!globals.dictionary) {
globals.dictionary = switch_core_strdup(globals.pool, "default.dic");
}
if (!globals.language_weight) {
globals.language_weight = switch_core_strdup(globals.pool, "6.5");
}
done:
if (xml) {
switch_xml_free(xml);
}
/* log key configuration chenbingfeng */
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "--->thresh=%d\n", globals.thresh);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "--->no_input_timeout=%d\n", globals.no_input_timeout);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "--->speech_timeout=%d\n", globals.speech_timeout);
ret = MSPLogin(NULL, NULL, login_params); //第一个参数是用户名,第二个参数是密码,均传NULL即可,第三个参数是登录参数
if (MSP_SUCCESS != ret) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>>MSPLogin fail error!!!<<<<<<<<<\n");
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>>MSPLogin success !!!<<<<<<<<<\n");
}
return status;
}
static void do_load(void)
{
switch_mutex_lock(MUTEX);
load_config();
switch_mutex_unlock(MUTEX);
}
static void event_handler(switch_event_t *event)
{
if (globals.auto_reload) {
do_load();
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "PocketSphinx Reloaded\n");
}
}
SWITCH_MODULE_LOAD_FUNCTION(mod_pocketsphinx_load)
{
switch_asr_interface_t *asr_interface;
switch_mutex_init(&MUTEX, SWITCH_MUTEX_NESTED, pool);
globals.pool = pool;
if ((switch_event_bind_removable(modname, SWITCH_EVENT_RELOADXML, NULL, event_handler, NULL, &NODE) != SWITCH_STATUS_SUCCESS)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't bind!\n");
}
do_load();
/* connect my internal structure to the blank pointer passed to me */
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
asr_interface = switch_loadable_module_create_interface(*module_interface, SWITCH_ASR_INTERFACE);
asr_interface->interface_name = "pocketsphinx";
asr_interface->asr_open = pocketsphinx_asr_open;
asr_interface->asr_load_grammar = pocketsphinx_asr_load_grammar;
asr_interface->asr_unload_grammar = pocketsphinx_asr_unload_grammar;
asr_interface->asr_close = pocketsphinx_asr_close;
asr_interface->asr_feed = pocketsphinx_asr_feed;
asr_interface->asr_resume = pocketsphinx_asr_resume;
asr_interface->asr_pause = pocketsphinx_asr_pause;
asr_interface->asr_check_results = pocketsphinx_asr_check_results;
asr_interface->asr_get_results = pocketsphinx_asr_get_results;
asr_interface->asr_start_input_timers = pocketsphinx_asr_start_input_timers;
asr_interface->asr_text_param = pocketsphinx_asr_text_param;
asr_interface->asr_numeric_param = pocketsphinx_asr_numeric_param;
asr_interface->asr_float_param = pocketsphinx_asr_float_param;
err_set_logfp(NULL);
/* indicate that the module should continue to be loaded */
return SWITCH_STATUS_SUCCESS;
}
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_pocketsphinx_shutdown)
{
MSPLogout();
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, ">>>>>>>>MSPLogout<<<<<<<<<\n");
switch_event_unbind(&NODE);
return SWITCH_STATUS_UNLOAD;
}
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
*/