forked from darrensessions/app_swift
-
Notifications
You must be signed in to change notification settings - Fork 6
/
app_swift.c
860 lines (754 loc) · 22.7 KB
/
app_swift.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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
/*
* app_swift -- A Cepstral Swift TTS engine interface
*
* Copyright (C) 2006 - 2012, Darren Sessions
* Portions Copyright (C) 2012, Cepstral LLC.
* Portions Copyright (C) 2013 - 2016, Jeremy Kister
*
* All rights reserved.
*
*
* This program is free software, distributed under the
* terms of the GNU General Public License Version 2. See
* the LICENSE file at the top of the source tree for more
* information.
*
*/
/*!
* \file
* \author Darren Sessions <[email protected]>
* \brief Cepstral Swift text-to-speech engine interface
*
* \ingroup applications
* \note This module requires the Cepstral engine to be installed in
* it's default location (/opt/swift)
*/
/*** MODULEINFO
<support_level>extended</support_level>
<defaultenabled>no</defaultenabled>
<depend>swift</depend>
***/
#define AST_MODULE_SELF_SYM __app_swift_sym
#include "asterisk.h"
#if (_AST_MAJ_VER <= 13)
ASTERISK_FILE_VERSION(__FILE__, "$Revision: 305000 $")
#endif
#include <swift.h>
#if defined _SWIFT_VER_6
#include <swift_asterisk_interface.h>
#endif
#include <math.h>
#if (_AST_MAJ_VER <= 15)
#include "asterisk/astobj.h"
#else
#include "asterisk/astobj2.h"
#endif
#include "asterisk/channel.h"
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/app.h"
#include "asterisk/file.h"
#if (_AST_MAJ_VER >= 13)
#include "asterisk/format_cache.h"
#endif
/*** DOCUMENTATION
<application name="Swift" language="en_US">
<synopsis>
Swift TTS engine and optionally listen for DTMF.
</synopsis>
<syntax>
<parameter name="text" required="true">
<para>Text to Speak.</para>
</parameter>
<parameter name="options">
<optionlist>
<option name="timeout">
<para>Timeout in milliseconds.</para>
</option>
<option name="digits">
<para>Maxiumum digits.</para>
</option>
</optionlist>
</parameter>
</syntax>
<description>
<para>This application streams tts audio from the Cepstral swift engine and will additionally read DTMF into the ${SWIFT_DTMF} variable if the timeout and digits options are used. You may change the voice dynamically by setting the channel variable SWIFT_VOICE.</para>
<para></para>
<para>in extensions.conf:</para>
<para>exten => s,1,Answer</para>
<para>exten => s,n,Swift(This is Swift talking in the default voice from swift.conf)</para>
<para>exten => s,n,Set(SWIFT_VOICE=Callie-8kHz)</para>
<para>exten => s,n,Swift(This is Swift talking in the Callie voice)</para>
<para>exten => s,n,Swift(Please enter three digits,5000,3)</para>
<para>exten => s,n,Swift(You entered ${SWIFT_DTMF}. Goodbye)</para>
<para>exten => s,n,Hangup</para>
</description>
</application>
***/
static char *app = "Swift";
#if (defined _AST_VER_1_4 || defined _AST_VER_1_6)
static char *synopsis = "Speak text through the Cepstral Swift text-to-speech engine.";
static char *descrip =
"This application streams tts audio from the Cepstral swift engine and\n"
"will alternatively read DTMF into the ${SWIFT_DTMF} variable if the timeout\n"
"and digits options are used. You may change the voice dynamically by\n"
"setting the channel variable SWIFT_VOICE.\n\n"
" Syntax: Swift(text[|timeout in ms][|maximum digits])\n";
#endif
#define FRAMESIZE 20
const int framesize = FRAMESIZE;
#define AST_MODULE "app_swift"
#define SWIFT_CONFIG_FILE "swift.conf"
#define dtmf_codes 12
/* following lead from other dev's: https://github.com/asterisk/asterisk/commit/e04607f8a327363aed79e794bd5dc1cdbb9a54e8#diff-d6e336fe459624be3a7b782f27b3acddR52 */
#define STUFF_NAME_LEN 80
static unsigned int cfg_buffer_size;
static int cfg_goto_exten;
static int samplerate;
static char cfg_voice[20];
struct stuff {
#if (_AST_MAJ_VER <= 15)
ASTOBJ_COMPONENTS(struct stuff);
#else
char name[STUFF_NAME_LEN];
#endif
int generating_done;
char *q;
char *pq_r; /* queue read position */
char *pq_w; /* queue write position */
int qc;
int immediate_exit;
};
struct dtmf_lookup {
long ast_res;
char* dtmf_res;
};
static struct dtmf_lookup ast_dtmf_table[dtmf_codes] = {
{35, "#"},
{42, "*"},
{48, "0"},
{49, "1"},
{50, "2"},
{51, "3"},
{52, "4"},
{53, "5"},
{54, "6"},
{55, "7"},
{56, "8"},
{57, "9"}
};
static void swift_init_stuff(struct stuff *ps)
{
#if (_AST_MAJ_VER <= 15)
ASTOBJ_INIT(ps);
#else
ao2_alloc(sizeof(*ps), NULL);
#endif
ps->generating_done = 0;
ps->q = ast_malloc(cfg_buffer_size);
ps->pq_r = ps->q;
ps->pq_w = ps->q;
ps->qc = 0;
ps->immediate_exit = 0;
}
static int swift_generator_running(struct stuff *ps)
{
int r;
#if (_AST_MAJ_VER <= 15)
ASTOBJ_RDLOCK(ps);
#else
ao2_rdlock(ps);
#endif
r = !ps->immediate_exit && (!ps->generating_done || ps->qc);
#if (_AST_MAJ_VER <= 15)
ASTOBJ_UNLOCK(ps);
#else
ao2_unlock(ps);
#endif
return r;
}
static int swift_bytes_available(struct stuff *ps)
{
int r;
#if (_AST_MAJ_VER <= 15)
ASTOBJ_RDLOCK(ps);
#else
ao2_rdlock(ps);
#endif
r = ps->qc;
#if (_AST_MAJ_VER <= 15)
ASTOBJ_UNLOCK(ps);
#else
ao2_unlock(ps);
#endif
return r;
}
static swift_result_t swift_cb(swift_event *event, swift_event_t type, void *udata)
{
void *buf;
int len, spacefree;
unsigned long sleepfor;
swift_event_t rv = SWIFT_SUCCESS;
struct stuff *ps = udata;
if (type == SWIFT_EVENT_AUDIO) {
rv = swift_event_get_audio(event, &buf, &len);
if (!SWIFT_FAILED(rv) && len > 0) {
ast_log(LOG_DEBUG, "audio callback\n");
#if (_AST_MAJ_VER <= 15)
ASTOBJ_WRLOCK(ps);
#else
ao2_wrlock(ps);
#endif
/* Sleep while waiting for some queue space to become available */
while (len + ps->qc > cfg_buffer_size && !ps->immediate_exit) {
/* Each byte is 125us of time, so assume queue space will become available
at that rate and guess when we'll have enough space available.
+ another (125 usec/sample * framesize samples) (1 frame) for fudge */
sleepfor = ((unsigned long)(len - (cfg_buffer_size - ps->qc)) * 125UL) + (125UL * (unsigned long)framesize);
/* ast_log(LOG_DEBUG, "generator: %d bytes to write but only %d space avail, sleeping %ldus\n", len, cfg_buffer_size - ps->qc, sleepfor); */
#if (_AST_MAJ_VER <= 15)
ASTOBJ_UNLOCK(ps);
#else
ao2_unlock(ps);
#endif
usleep(sleepfor);
#if (_AST_MAJ_VER <= 15)
ASTOBJ_WRLOCK(ps);
#else
ao2_wrlock(ps);
#endif
}
if (ps->immediate_exit) {
#if (_AST_MAJ_VER <= 15)
ASTOBJ_UNLOCK(ps);
#else
ao2_unlock(ps);
#endif
return SWIFT_SUCCESS;
}
spacefree = cfg_buffer_size - ((uintptr_t) ps->pq_w - (uintptr_t)ps->q);
if (len > spacefree) {
ast_log(LOG_DEBUG, "audio fancy write; %d bytes but only %d avail to end %d totalavail\n", len, spacefree, cfg_buffer_size - ps->qc);
/* write #1 to end of mem */
memcpy(ps->pq_w, buf, spacefree);
ps->pq_w = ps->q;
ps->qc += spacefree;
/* write #2 and beg of mem */
memcpy(ps->pq_w, buf + spacefree, len - spacefree);
ps->pq_w += len - spacefree;
ps->qc += len - spacefree;
} else {
ast_log(LOG_DEBUG, "audio easy write, %d avail to end %d totalavail\n", spacefree, cfg_buffer_size - ps->qc);
memcpy(ps->pq_w, buf, len);
ps->pq_w += len;
ps->qc += len;
}
#if (_AST_MAJ_VER <= 15)
ASTOBJ_UNLOCK(ps);
#else
ao2_unlock(ps);
#endif
} else {
ast_log(LOG_DEBUG, "got audio callback but get_audio call failed\n");
}
} else if (type == SWIFT_EVENT_END) {
ast_log(LOG_DEBUG, "got END callback; done generating audio\n");
#if (_AST_MAJ_VER <= 15)
ASTOBJ_WRLOCK(ps);
#else
ao2_wrlock(ps);
#endif
ps->generating_done = 1;
#if (_AST_MAJ_VER <= 15)
ASTOBJ_UNLOCK(ps);
#else
ao2_unlock(ps);
#endif
#if defined _SWIFT_VER_6
} else if (type == SWIFT_EVENT_ERROR) {
/*
* Error events are used to communicate to app_swift that there are no more swift_ports available.
* So check to make sure that is the cause of the error signal, then terminate.
* Termination may not be the best behavior, but any queuing should be managed on the Asterisk side.
*/
swift_result_t error_code;
if ((swift_event_get_error(event, &error_code, NULL)==SWIFT_SUCCESS) && (error_code == SWIFT_PORT_UNAVAILABLE)) {
ast_log(LOG_WARNING, "Received SWIFT_EVENT_ERROR with code: SWIFT_PORT_UNAVAILABLE. There are no ports available for simultaneous synthesis. All licensed ports are already in use.\n");
#if (_AST_MAJ_VER <= 15)
ASTOBJ_WRLOCK(ps);
#else
ao2_wrlock(ps);
#endif
ps->generating_done = 1;
#if (_AST_MAJ_VER <= 15)
ASTOBJ_UNLOCK(ps);
#else
ao2_unlock(ps);
#endif
}
#endif
} else {
ast_log(LOG_DEBUG, "UNKNOWN callback\n");
}
return rv;
}
static int dtmf_conv(int dtmf)
{
char *res = (char *) ast_malloc(100);
int dtmf_search_counter = 0, dtmf_search_match = 0;
memset(res, 0, 100);
while ((dtmf_search_counter < dtmf_codes) && (dtmf_search_match == 0)) {
if (dtmf == ast_dtmf_table[dtmf_search_counter].ast_res) {
dtmf_search_match += 1;
sprintf(res, "%s", ast_dtmf_table[dtmf_search_counter].dtmf_res);
}
dtmf_search_counter = dtmf_search_counter + 1;
}
return *res;
}
static char *listen_for_dtmf(struct ast_channel *chan, int timeout, int max_digits)
{
char *dtmf_conversion = (char *) ast_malloc(100);
char cnv[2];
int dtmf = 0, i = 0;
memset(dtmf_conversion, 0, 100);
memset(cnv, 0, 2);
while (i < max_digits) {
dtmf = ast_waitfordigit(chan, timeout);
if (!dtmf) {
break;
}
sprintf(cnv, "%c", dtmf_conv(dtmf));
strcat(dtmf_conversion, cnv);
i += 1;
}
return ast_strdup(dtmf_conversion);
}
#if (defined _AST_VER_1_4 || defined _AST_VER_1_6)
static int app_exec(struct ast_channel *chan, void *data)
#elif (defined _AST_VER_1_8 || _AST_MAJ_VER >= 10)
static int app_exec(struct ast_channel *chan, const char *data)
#endif
{
int res = 0, max_digits = 0, timeout = 0, alreadyran = 0;
int ms = -1, len = 0, availatend = 0;
char *argv[3], *text = NULL, *rc = NULL;
char tmp_exten[2], results[20];
struct ast_module_user *u;
struct ast_frame *f;
struct timeval next;
struct stuff *ps;
char *parse;
#if (defined _AST_VER_10 || defined _AST_VER_11 || defined _AST_VER_12)
struct ast_format old_writeformat;
#elif (_AST_MAJ_VER >= 13)
RAII_VAR(struct ast_format *, old_writeformat, NULL, ao2_cleanup);
#else
int old_writeformat = 0;
#endif
parse = ast_strdupa(data);
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(text);
AST_APP_ARG(timeout);
AST_APP_ARG(max_digits);
);
AST_STANDARD_APP_ARGS(args, parse);
struct myframe {
struct ast_frame f;
char offset[AST_FRIENDLY_OFFSET];
short frdata[FRAMESIZE];
} myf = {
.f = { 0, },
};
swift_engine *engine;
swift_port *port = NULL;
swift_voice *voice;
swift_params *params;
swift_result_t sresult;
swift_background_t tts_stream;
unsigned int event_mask;
const char *vvoice = NULL;
memset(results, 0 ,20);
memset(tmp_exten, 0, 2);
memset(argv, 0, 3);
u = ast_module_user_add(chan);
if (!ast_strlen_zero(args.timeout)) {
timeout = strtol(args.timeout, NULL, 0);
}
if (!ast_strlen_zero(args.max_digits)) {
max_digits = strtol(args.max_digits, NULL, 0);
}
text = args.text;
if (ast_strlen_zero(text)) {
ast_log(LOG_WARNING, "%s requires text to speak!\n", app);
return -1;
}else{
ast_log(LOG_DEBUG, "Text to Speak : %s\n", text);
}
if (timeout > 0) {
ast_log(LOG_DEBUG, "Timeout : %d\n", timeout);
}
if (max_digits > 0) {
ast_log(LOG_DEBUG, "Max Digits : %d\n", max_digits);
}
if( (ps = ast_malloc(sizeof(struct stuff))) == NULL ){
ast_log(LOG_WARNING, "malloc fail! - memory problem?\n");
goto exception;
}
swift_init_stuff(ps);
/* Setup synthesis */
if ((engine = swift_engine_open(NULL)) == NULL) {
ast_log(LOG_ERROR, "Failed to open Swift Engine.\n");
goto exception;
}
params = swift_params_new(NULL);
swift_params_set_string(params, "audio/encoding", "ulaw");
swift_params_set_string(params, "audio/sampling-rate", "8000");
swift_params_set_string(params, "audio/output-format", "raw");
swift_params_set_string(params, "tts/text-encoding", "utf-8");
/* Additional swift parameters
*
* swift_params_set_float(params, "speech/pitch/shift", 1.0);
* swift_params_set_int(params, "speech/rate", 150);
* swift_params_set_int(params, "audio/volume", 110);
* swift_params_set_int(params, "audio/deadair", 0);
*/
if ((port = swift_port_open(engine, params)) == NULL) {
ast_log(LOG_ERROR, "Failed to open Swift Port.\n");
goto exception;
}
#if defined _SWIFT_VER_6
if (port!=NULL) {
/*
* This registers a chan with swift, otherwise through repeated DTMF+synth requests
* a single call could consume all available concurrent synthesis ports.
*/
swift_register_ast_chan(port, chan);
}
#endif
ast_log(LOG_DEBUG, "Config voice is %s via %s\n", cfg_voice, SWIFT_CONFIG_FILE);
/* allow exten => x,n,Set(SWIFT_VOICE=Callie) */
if ((vvoice = pbx_builtin_getvar_helper(chan, "SWIFT_VOICE"))) {
ast_copy_string(cfg_voice, vvoice, sizeof(cfg_voice));
ast_log(LOG_DEBUG, "Config voice override to %s via SWIFT_VOICE\n", cfg_voice);
}
if ((voice = swift_port_set_voice_by_name(port, cfg_voice)) == NULL) {
ast_log(LOG_ERROR, "Failed to set voice.\n");
goto exception;
}
#if defined _SWIFT_VER_6
event_mask = SWIFT_EVENT_AUDIO | SWIFT_EVENT_END | SWIFT_EVENT_ERROR;
#elif defined _SWIFT_VER_5
event_mask = SWIFT_EVENT_AUDIO | SWIFT_EVENT_END;
#endif
swift_port_set_callback(port, &swift_cb, event_mask, ps);
if (SWIFT_FAILED(swift_port_speak_text(port, text, 0, NULL, &tts_stream, NULL))) {
ast_log(LOG_ERROR, "Failed to speak.\n");
goto exception;
}
#if (_AST_MAJ_VER >= 11)
if (ast_channel_state(chan) != AST_STATE_UP) {
#else
if (chan->_state != AST_STATE_UP) {
#endif
ast_answer(chan);
}
ast_stopstream(chan);
#if (defined _AST_VER_1_4 || defined _AST_VER_1_6 || defined _AST_VER_1_8)
old_writeformat = chan->writeformat;
if (ast_set_write_format(chan, AST_FORMAT_ULAW) < 0) {
#elif (defined _AST_VER_10)
ast_format_copy(&old_writeformat, &chan->writeformat);
if (ast_set_write_format_by_id(chan, AST_FORMAT_ULAW) < 0) {
#elif (defined _AST_VER_11 || defined _AST_VER_12)
ast_format_copy(&old_writeformat, ast_channel_writeformat(chan));
if (ast_set_write_format_by_id(chan, AST_FORMAT_ULAW) < 0) {
#elif (_AST_MAJ_VER >= 13)
old_writeformat = ao2_bump(ast_channel_writeformat(chan));
if (ast_set_write_format(chan, ast_format_ulaw) < 0) {
#endif
ast_log(LOG_WARNING, "Unable to set write format.\n");
goto exception;
}
res = 0;
/* Wait 100ms first for synthesis to start crankin'; if that's not
* enough the
*/
next = ast_tvadd(ast_tvnow(), ast_tv(0, 100000));
while (swift_generator_running(ps)) {
ms = ast_tvdiff_ms(next, ast_tvnow());
if (ms <= 0) {
if (swift_bytes_available(ps) > 0) {
#if (_AST_MAJ_VER <= 15)
ASTOBJ_WRLOCK(ps);
#else
ao2_wrlock(ps);
#endif
len = fmin(framesize, ps->qc);
availatend = cfg_buffer_size - (ps->pq_r - ps->q);
if (len > availatend) {
/* read #1: to end of q buf */
memcpy(myf.frdata, ps->pq_r, availatend);
ps->qc -= availatend;
/* read #2: reset to start of q buf and get rest */
ps->pq_r = ps->q;
memcpy(myf.frdata + availatend, ps->pq_r, len - availatend);
ps->qc -= len - availatend;
ps->pq_r += len - availatend;
} else {
ast_log(LOG_DEBUG, "Easy read; %d bytes and %d at end, %d free\n", len, availatend, cfg_buffer_size - ps->qc);
memcpy(myf.frdata, ps->pq_r, len);
ps->qc -= len;
ps->pq_r += len;
}
myf.f.frametype = AST_FRAME_VOICE;
#if (defined _AST_VER_1_6 || defined _AST_VER_1_4)
myf.f.subclass = AST_FORMAT_ULAW;
#elif defined _AST_VER_1_8
myf.f.subclass.codec = AST_FORMAT_ULAW;
#elif (defined _AST_VER_10 || defined _AST_VER_11 || defined _AST_VER_12)
ast_format_set(&myf.f.subclass.format, AST_FORMAT_ULAW, 0);
#elif (_AST_MAJ_VER >= 13)
myf.f.subclass.format = ast_format_ulaw;
#endif
myf.f.datalen = len;
myf.f.samples = len;
#if defined _AST_VER_1_4
myf.f.data = myf.frdata;
#else
myf.f.data.ptr = myf.frdata;
#endif
myf.f.mallocd = 0;
myf.f.offset = AST_FRIENDLY_OFFSET;
myf.f.src = __PRETTY_FUNCTION__;
myf.f.delivery.tv_sec = 0;
myf.f.delivery.tv_usec = 0;
if (ast_write(chan, &myf.f) < 0) {
res = -1;
ast_log(LOG_DEBUG, "ast_write failed\n");
}
ast_log(LOG_DEBUG, "wrote a frame of %d\n", len);
if (ps->qc < 0) {
ast_log(LOG_DEBUG, "queue claims to contain negative bytes. Huh? qc < 0\n");
}
#if (_AST_MAJ_VER <= 15)
ASTOBJ_UNLOCK(ps);
#else
ao2_unlock(ps);
#endif
next = ast_tvadd(next, ast_samp2tv(myf.f.samples, samplerate));
} else {
next = ast_tvadd(next, ast_samp2tv(framesize / 2, samplerate));
ast_log(LOG_DEBUG, "Whoops, writer starved for audio\n");
}
} else {
ms = ast_waitfor(chan, ms);
if (ms < 0) {
ast_log(LOG_DEBUG, "Hangup detected\n");
res = -1;
#if (_AST_MAJ_VER <= 15)
ASTOBJ_WRLOCK(ps);
#else
ao2_wrlock(ps);
#endif
ps->immediate_exit = 1;
#if (_AST_MAJ_VER <= 15)
ASTOBJ_UNLOCK(ps);
#else
ao2_unlock(ps);
#endif
} else if (ms) {
f = ast_read(chan);
if (!f) {
ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
res = -1;
#if (_AST_MAJ_VER <= 15)
ASTOBJ_WRLOCK(ps);
#else
ao2_wrlock(ps);
#endif
ps->immediate_exit = 1;
#if (_AST_MAJ_VER <= 15)
ASTOBJ_UNLOCK(ps);
#else
ao2_unlock(ps);
#endif
} else {
if (f->frametype == AST_FRAME_DTMF && timeout > 0 && max_digits > 0) {
#if (defined _AST_VER_1_6 || defined _AST_VER_1_4)
char originalDTMF = f->subclass;
#elif (defined _AST_VER_1_8 || _AST_MAJ_VER >= 10)
char originalDTMF = f->subclass.integer;
#endif
alreadyran = 1;
res = 0;
#if (_AST_MAJ_VER <= 15)
ASTOBJ_WRLOCK(ps);
#else
ao2_wrlock(ps);
#endif
ps->immediate_exit = 1;
#if (_AST_MAJ_VER <= 15)
ASTOBJ_UNLOCK(ps);
#else
ao2_unlock(ps);
#endif
if (max_digits > 1) {
rc = listen_for_dtmf(chan, timeout, max_digits - 1);
}
if (rc) {
sprintf(results, "%c%s", originalDTMF, rc);
} else {
sprintf(results, "%c", originalDTMF);
}
ast_log(LOG_NOTICE, "DTMF = %s\n", results);
pbx_builtin_setvar_helper(chan, "SWIFT_DTMF", results);
}
if (f!=NULL) {
ast_frfree(f);
}
}
}
}
#if (_AST_MAJ_VER <= 15)
ASTOBJ_RDLOCK(ps);
#else
ao2_rdlock(ps);
#endif
if (ps->immediate_exit && !ps->generating_done) {
if (SWIFT_FAILED(sresult = swift_port_stop(port, tts_stream, SWIFT_EVENT_NOW))) {
ast_log(LOG_NOTICE, "Early top of swift port failed\n");
}
}
#if (_AST_MAJ_VER <= 15)
ASTOBJ_UNLOCK(ps);
#else
ao2_unlock(ps);
#endif
}
if (alreadyran == 0 && timeout > 0 && max_digits > 0) {
rc = listen_for_dtmf(chan, timeout, max_digits);
if (rc != NULL) {
sprintf(results, "%s", rc);
ast_log(LOG_NOTICE, "DTMF = %s\n", results);
pbx_builtin_setvar_helper(chan, "SWIFT_DTMF", results);
}
}
if (max_digits >= 1 && results != NULL) {
if (cfg_goto_exten) {
#if (_AST_MAJ_VER >= 11)
ast_log(LOG_NOTICE, "GoTo(%s|%s|%d) : ", ast_channel_context(chan), results, 1);
#else
ast_log(LOG_NOTICE, "GoTo(%s|%s|%d) : ", chan->context, results, 1);
#endif
#if (defined _AST_VER_1_6 || defined _AST_VER_1_4)
if (ast_exists_extension (chan, chan->context, results, 1, chan->cid.cid_num)) {
#elif (defined _AST_VER_1_8 || defined _AST_VER_10)
if (ast_exists_extension (chan, chan->context, results, 1, chan->caller.id.number.str)) {
#elif (_AST_MAJ_VER >= 11)
if (ast_exists_extension (chan, ast_channel_context(chan), results, 1, ast_channel_caller(chan)->id.number.str)) {
#endif
ast_log(LOG_NOTICE, "OK\n");
#if (_AST_MAJ_VER >= 11)
ast_channel_exten_set(chan, results);
ast_channel_priority_set(chan, 0);
#else
ast_copy_string(chan->exten, results, sizeof(chan->exten) - 1);
chan->priority = 0;
#endif
} else {
ast_log(LOG_NOTICE, "FAILED\n");
}
}
}
exception:
if (port != NULL) {
swift_port_close(port);
}
if (engine != NULL) {
swift_engine_close(engine);
}
if (ps && ps->q) {
ast_free(ps->q);
ps->q = NULL;
}
if (ps) {
#if (_AST_MAJ_VER <= 15)
ast_free(ps);
#else
ao2_ref(ps, -1);
#endif
ps = NULL;
}
#if (defined _AST_VER_1_6 || defined _AST_VER_1_4 || defined _AST_VER_1_8)
if (!res && old_writeformat) {
ast_set_write_format(chan, old_writeformat);
}
#elif (defined _AST_VER_10 || defined _AST_VER_11 || defined _AST_VER_12)
if (!res) {
ast_set_write_format(chan, &old_writeformat);
}
#elif (_AST_MAJ_VER >= 13)
if (!res) {
ast_set_write_format(chan, old_writeformat);
}
ao2_cleanup(old_writeformat);
#endif
ast_module_user_remove(u);
return res;
}
static int unload_module(void)
{
int res;
res = ast_unregister_application(app);
ast_module_user_hangup_all();
return res;
}
static int load_module(void)
{
int res = 0;
const char *val = NULL;
struct ast_config *cfg;
#if (defined _AST_VER_1_6 || defined _AST_VER_1_8 || _AST_MAJ_VER >= 10)
struct ast_flags config_flags = { CONFIG_FLAG_NOCACHE };
#endif
/* Set some defaults */
cfg_buffer_size = 65535;
cfg_goto_exten = 0;
samplerate = 8000; /* G711a/G711u */
ast_copy_string(cfg_voice, "Allison-8kHz", sizeof(cfg_voice));
#if (defined _AST_VER_1_6 || defined _AST_VER_1_4)
res = ast_register_application(app, app_exec, synopsis, descrip) ?
#elif (defined _AST_VER_1_8 || _AST_MAJ_VER >= 10)
res = ast_register_application_xml(app, app_exec) ?
#endif
AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
#if defined _AST_VER_1_4
cfg = ast_config_load(SWIFT_CONFIG_FILE);
#elif (defined _AST_VER_1_6 || defined _AST_VER_1_8 || _AST_MAJ_VER >= 10)
cfg = ast_config_load(SWIFT_CONFIG_FILE, config_flags);
#endif
if (cfg) {
if ((val = ast_variable_retrieve(cfg, "general", "buffer_size"))) {
cfg_buffer_size = atoi(val);
ast_log(LOG_DEBUG, "Config buffer_size is %d\n", cfg_buffer_size);
}
if ((val = ast_variable_retrieve(cfg, "general", "goto_exten"))) {
if (!strcmp(val, "yes")) {
cfg_goto_exten = 1;
} else {
cfg_goto_exten = 0;
ast_log(LOG_DEBUG, "Config goto_exten is %d\n", cfg_goto_exten);
}
}
if ((val = ast_variable_retrieve(cfg, "general", "voice"))) {
ast_copy_string(cfg_voice, val, sizeof(cfg_voice));
ast_log(LOG_DEBUG, "Config voice is %s\n", cfg_voice);
}
ast_config_destroy(cfg);
} else {
ast_log(LOG_NOTICE, "Failed to load config\n");
}
return res;
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Cepstral Swift TTS Application");