-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_ssml.c
1132 lines (1027 loc) · 32.7 KB
/
mod_ssml.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
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* mod_ssml for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2013-2014,2016 Grasshopper
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mod_ssml for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is Grasshopper
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Chris Rienzo <[email protected]>
*
* mod_ssml.c -- SSML audio rendering format
*
*/
#include <switch.h>
#include <iksemel.h>
SWITCH_MODULE_LOAD_FUNCTION(mod_ssml_load);
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_ssml_shutdown);
SWITCH_MODULE_DEFINITION(mod_ssml, mod_ssml_load, mod_ssml_shutdown, NULL);
#define MAX_VOICE_FILES 256
#define MAX_VOICE_PRIORITY 999
#define VOICE_NAME_PRIORITY 1000
#define VOICE_GENDER_PRIORITY 1000
#define VOICE_LANG_PRIORITY 1000000
struct ssml_parser;
/** function to handle tag attributes */
typedef int (* tag_attribs_fn)(struct ssml_parser *, char **);
/** function to handle tag CDATA */
typedef int (* tag_cdata_fn)(struct ssml_parser *, char *, size_t);
/**
* Tag definition
*/
struct tag_def {
tag_attribs_fn attribs_fn;
tag_cdata_fn cdata_fn;
switch_bool_t is_root;
switch_hash_t *children_tags;
};
/**
* Module configuration
*/
static struct {
/** Mapping of mod-name-language-gender to voice */
switch_hash_t *voice_cache;
/** Mapping of voice names */
switch_hash_t *say_voice_map;
/** Synchronizes access to say_voice_map */
switch_mutex_t *say_voice_map_mutex;
/** Mapping of voice names */
switch_hash_t *tts_voice_map;
/** Synchronizes access to tts_voice_map */
switch_mutex_t *tts_voice_map_mutex;
/** Mapping of interpret-as value to macro */
switch_hash_t *interpret_as_map;
/** Mapping of ISO language code to say-module */
switch_hash_t *language_map;
/** Mapping of tag name to definition */
switch_hash_t *tag_defs;
/** module memory pool */
switch_memory_pool_t *pool;
} globals;
/**
* A say language
*/
struct language {
/** The ISO language code */
char *iso;
/** The FreeSWITCH language code */
char *language;
/** The say module name */
char *say_module;
};
/**
* A say macro
*/
struct macro {
/** interpret-as name (cardinal...) */
char *name;
/** language (en-US, en-UK, ...) */
char *language;
/** type (number, items, persons, messages...) */
char *type;
/** method (pronounced, counted, iterated...) */
char *method;
};
/**
* A TTS voice
*/
struct voice {
/** higher priority = more likely to pick */
int priority;
/** voice gender */
char *gender;
/** voice name / macro */
char *name;
/** voice language */
char *language;
/** internal file prefix */
char *prefix;
};
#define TAG_LEN 32
#define NAME_LEN 128
#define LANGUAGE_LEN 6
#define GENDER_LEN 8
/**
* SSML voice state
*/
struct ssml_node {
/** tag name */
char tag_name[TAG_LEN];
/** requested name */
char name[NAME_LEN];
/** requested language */
char language[LANGUAGE_LEN];
/** requested gender */
char gender[GENDER_LEN];
/** voice to use */
struct voice *tts_voice;
/** say macro to use */
struct macro *say_macro;
/** tag handling data */
struct tag_def *tag_def;
/** previous node */
struct ssml_node *parent_node;
};
/**
* A file to play
*/
struct ssml_file {
/** prefix to add to file handle */
char *prefix;
/** the file to play */
const char *name;
};
/**
* SSML parser state
*/
struct ssml_parser {
/** current attribs */
struct ssml_node *cur_node;
/** files to play */
struct ssml_file *files;
/** number of files */
int num_files;
/** max files to play */
int max_files;
/** memory pool to use */
switch_memory_pool_t *pool;
/** desired sample rate */
int sample_rate;
};
/**
* SSML playback state
*/
struct ssml_context {
/** handle to current file */
switch_file_handle_t fh;
/** files to play */
struct ssml_file *files;
/** number of files */
int num_files;
/** current file being played */
int index;
};
/**
* Add a definition for a tag
* @param tag the name
* @param attribs_fn the function to handle the tag attributes
* @param cdata_fn the function to handler the tag CDATA
* @param children_tags comma-separated list of valid child tag names
* @return the definition
*/
static struct tag_def *add_tag_def(const char *tag, tag_attribs_fn attribs_fn, tag_cdata_fn cdata_fn, const char *children_tags)
{
struct tag_def *def = switch_core_alloc(globals.pool, sizeof(*def));
switch_core_hash_init(&def->children_tags);
if (!zstr(children_tags)) {
char *children_tags_dup = switch_core_strdup(globals.pool, children_tags);
char *tags[32] = { 0 };
int tag_count = switch_separate_string(children_tags_dup, ',', tags, sizeof(tags) / sizeof(tags[0]));
if (tag_count) {
int i;
for (i = 0; i < tag_count; i++) {
switch_core_hash_insert(def->children_tags, tags[i], tags[i]);
}
}
}
def->attribs_fn = attribs_fn;
def->cdata_fn = cdata_fn;
def->is_root = SWITCH_FALSE;
switch_core_hash_insert(globals.tag_defs, tag, def);
return def;
}
/**
* Add a definition for a root tag
* @param tag the name
* @param attribs_fn the function to handle the tag attributes
* @param cdata_fn the function to handler the tag CDATA
* @param children_tags comma-separated list of valid child tag names
* @return the definition
*/
static struct tag_def *add_root_tag_def(const char *tag, tag_attribs_fn attribs_fn, tag_cdata_fn cdata_fn, const char *children_tags)
{
struct tag_def *def = add_tag_def(tag, attribs_fn, cdata_fn, children_tags);
def->is_root = SWITCH_TRUE;
return def;
}
/**
* Handle tag attributes
* @param parser the parser
* @param name the tag name
* @param atts the attributes
* @return IKS_OK if OK IKS_BADXML on parse failure
*/
static int process_tag(struct ssml_parser *parser, const char *name, char **atts)
{
struct tag_def *def = switch_core_hash_find(globals.tag_defs, name);
if (def) {
parser->cur_node->tag_def = def;
if (def->is_root && parser->cur_node->parent_node == NULL) {
/* no parent for ROOT tags */
return def->attribs_fn(parser, atts);
} else if (!def->is_root && parser->cur_node->parent_node) {
/* check if this child is allowed by parent node */
struct tag_def *parent_def = parser->cur_node->parent_node->tag_def;
if (switch_core_hash_find(parent_def->children_tags, "ANY") ||
switch_core_hash_find(parent_def->children_tags, name)) {
return def->attribs_fn(parser, atts);
}
}
}
return IKS_BADXML;
}
/**
* Handle CDATA that is ignored
* @param parser the parser
* @param data the CDATA
* @param len the CDATA length
* @return IKS_OK
*/
static int process_cdata_ignore(struct ssml_parser *parser, char *data, size_t len)
{
return IKS_OK;
}
/**
* Handle CDATA that is not allowed
* @param parser the parser
* @param data the CDATA
* @param len the CDATA length
* @return IKS_BADXML
*/
static int process_cdata_bad(struct ssml_parser *parser, char *data, size_t len)
{
int i;
for (i = 0; i < len; i++) {
if (isgraph(data[i])) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Unexpected CDATA for <%s>\n", parser->cur_node->tag_name);
return IKS_BADXML;
}
}
return IKS_OK;
}
/**
* Score the voice on how close it is to desired language, name, and gender
* @param voice the voice to score
* @param cur_node the desired voice attributes
* @param lang_required if true, language must match
* @return the score
*/
static int score_voice(struct voice *voice, struct ssml_node *cur_node, int lang_required)
{
/* language > gender,name > priority */
int score = voice->priority;
if (!zstr_buf(cur_node->gender) && !strcmp(cur_node->gender, voice->gender)) {
score += VOICE_GENDER_PRIORITY;
}
if (!zstr_buf(cur_node->name) && !strcmp(cur_node->name, voice->name)) {
score += VOICE_NAME_PRIORITY;
}
if (!zstr_buf(cur_node->language) && !strcmp(cur_node->language, voice->language)) {
score += VOICE_LANG_PRIORITY;
} else if (lang_required) {
score = 0;
}
return score;
}
/**
* Search for best voice based on attributes
* @param cur_node the desired voice attributes
* @param map the map to search
* @param type "say" or "tts"
* @param lang_required if true, language must match
* @return the voice or NULL
*/
static struct voice *find_voice(struct ssml_node *cur_node, switch_hash_t *map, char *type, int lang_required)
{
switch_hash_index_t *hi = NULL;
struct voice *voice = NULL;
char *lang_name_gender = NULL;
int best_score = 0;
/* check cache */
lang_name_gender = switch_mprintf("%s-%s-%s-%s", type, cur_node->language, cur_node->name, cur_node->gender);
voice = (struct voice *)switch_core_hash_find(globals.voice_cache, lang_name_gender);
if (voice) {
/* that was easy! */
goto done;
}
/* find best language, name, gender match */
for (hi = switch_core_hash_first(map); hi; hi = switch_core_hash_next(&hi)) {
const void *key;
void *val;
struct voice *candidate;
int candidate_score = 0;
switch_core_hash_this(hi, &key, NULL, &val);
candidate = (struct voice *)val;
candidate_score = score_voice(candidate, cur_node, lang_required);
if (candidate_score > 0 && candidate_score > best_score) {
voice = candidate;
best_score = candidate_score;
}
}
/* remember for next time */
if (voice) {
switch_core_hash_insert(globals.voice_cache, lang_name_gender, voice);
}
done:
switch_safe_free(lang_name_gender);
return voice;
}
/**
* Search for best voice based on attributes
* @param cur_node the desired voice attributes
* @return the voice or NULL
*/
static struct voice *find_tts_voice(struct ssml_node *cur_node)
{
struct voice *v;
switch_mutex_lock(globals.tts_voice_map_mutex);
v = find_voice(cur_node, globals.tts_voice_map, "tts", 0);
switch_mutex_unlock(globals.tts_voice_map_mutex);
return v;
}
/**
* Search for best voice based on attributes
* @param cur_node the desired voice attributes
* @return the voice or NULL
*/
static struct voice *find_say_voice(struct ssml_node *cur_node)
{
struct voice *v;
switch_mutex_lock(globals.say_voice_map_mutex);
v = find_voice(cur_node, globals.say_voice_map, "say", 1);
switch_mutex_unlock(globals.say_voice_map_mutex);
return v;
}
/**
* Handle tag attributes that are ignored
* @param parser the parser
* @param atts the attributes
* @return IKS_OK
*/
static int process_attribs_ignore(struct ssml_parser *parsed_data, char **atts)
{
struct ssml_node *cur_node = parsed_data->cur_node;
cur_node->tts_voice = find_tts_voice(cur_node);
return IKS_OK;
}
/**
* open next file for reading
* @param handle the file handle
*/
static switch_status_t next_file(switch_file_handle_t *handle)
{
struct ssml_context *context = handle->private_info;
const char *file;
top:
context->index++;
if (switch_test_flag((&context->fh), SWITCH_FILE_OPEN)) {
switch_core_file_close(&context->fh);
}
if (context->index >= context->num_files) {
return SWITCH_STATUS_FALSE;
}
file = context->files[context->index].name;
context->fh.prefix = context->files[context->index].prefix;
if (switch_test_flag(handle, SWITCH_FILE_FLAG_WRITE)) {
/* unsupported */
return SWITCH_STATUS_FALSE;
}
if (switch_core_file_open(&context->fh, file, handle->channels, handle->samplerate, handle->flags, NULL) != SWITCH_STATUS_SUCCESS) {
goto top;
}
handle->samples = context->fh.samples;
handle->format = context->fh.format;
handle->sections = context->fh.sections;
handle->seekable = context->fh.seekable;
handle->speed = context->fh.speed;
handle->interval = context->fh.interval;
if (switch_test_flag((&context->fh), SWITCH_FILE_NATIVE)) {
switch_set_flag_locked(handle, SWITCH_FILE_NATIVE);
} else {
switch_clear_flag_locked(handle, SWITCH_FILE_NATIVE);
}
return SWITCH_STATUS_SUCCESS;
}
/**
* Process xml:lang attribute
*/
static int process_xml_lang(struct ssml_parser *parsed_data, char **atts)
{
struct ssml_node *cur_node = parsed_data->cur_node;
/* only allow language change in <speak>, <p>, and <s> */
if (atts) {
int i = 0;
while (atts[i]) {
if (!strcmp("xml:lang", atts[i])) {
if (!zstr(atts[i + 1])) {
snprintf(cur_node->language, LANGUAGE_LEN, "%s", atts[i + 1]);
}
}
i += 2;
}
}
cur_node->tts_voice = find_tts_voice(cur_node);
return IKS_OK;
}
/**
* Process <voice>
*/
static int process_voice(struct ssml_parser *parsed_data, char **atts)
{
struct ssml_node *cur_node = parsed_data->cur_node;
if (atts) {
int i = 0;
while (atts[i]) {
if (!strcmp("xml:lang", atts[i])) {
if (!zstr(atts[i + 1])) {
snprintf(cur_node->language, LANGUAGE_LEN, "%s", atts[i + 1]);
}
} else if (!strcmp("name", atts[i])) {
if (!zstr(atts[i + 1])) {
snprintf(cur_node->name, NAME_LEN, "%s", atts[i + 1]);
}
} else if (!strcmp("gender", atts[i])) {
if (!zstr(atts[i + 1])) {
snprintf(cur_node->gender, GENDER_LEN, "%s", atts[i + 1]);
}
}
i += 2;
}
}
cur_node->tts_voice = find_tts_voice(cur_node);
return IKS_OK;
}
/**
* Process <say-as>
*/
static int process_say_as(struct ssml_parser *parsed_data, char **atts)
{
struct ssml_node *cur_node = parsed_data->cur_node;
if (atts) {
int i = 0;
while (atts[i]) {
if (!strcmp("interpret-as", atts[i])) {
char *interpret_as = atts[i + 1];
if (!zstr(interpret_as)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "interpret-as: %s\n", atts[i + 1]);
cur_node->say_macro = (struct macro *)switch_core_hash_find(globals.interpret_as_map, interpret_as);
}
break;
}
i += 2;
}
}
cur_node->tts_voice = find_tts_voice(cur_node);
return IKS_OK;
}
/**
* Process <break>- this is a period of silence
*/
static int process_break(struct ssml_parser *parsed_data, char **atts)
{
if (atts) {
int i = 0;
while (atts[i]) {
if (!strcmp("time", atts[i])) {
char *t = atts[i + 1];
if (!zstr(t) && parsed_data->num_files < parsed_data->max_files) {
int timeout_ms = 0;
char *unit;
if ((unit = strstr(t, "ms"))) {
*unit = '\0';
if (switch_is_number(t)) {
timeout_ms = atoi(t);
}
} else if ((unit = strstr(t, "s"))) {
*unit = '\0';
if (switch_is_number(t)) {
timeout_ms = atoi(t) * 1000;
}
}
if (timeout_ms > 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Adding <break>: \"%s\"\n", t);
parsed_data->files[parsed_data->num_files].name = switch_core_sprintf(parsed_data->pool, "silence_stream://%i", timeout_ms);
parsed_data->files[parsed_data->num_files++].prefix = NULL;
}
}
return IKS_OK;
}
i += 2;
}
}
return IKS_OK;
}
/**
* Process <audio>- this is a URL to play
*/
static int process_audio(struct ssml_parser *parsed_data, char **atts)
{
if (atts) {
int i = 0;
while (atts[i]) {
if (!strcmp("src", atts[i])) {
char *src = atts[i + 1];
ikstack *stack = NULL;
if (!zstr(src) && parsed_data->num_files < parsed_data->max_files) {
/* get the URI */
if (strchr(src, '&')) {
stack = iks_stack_new(256, 0);
src = iks_unescape(stack, src, strlen(src));
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Adding <audio>: \"%s\"\n", src);
parsed_data->files[parsed_data->num_files].name = switch_core_strdup(parsed_data->pool, src);
parsed_data->files[parsed_data->num_files++].prefix = NULL;
if (stack) {
iks_stack_delete(&stack);
}
}
return IKS_OK;
}
i += 2;
}
}
return IKS_OK;
}
/**
* Process a tag
*/
static int tag_hook(void *user_data, char *name, char **atts, int type)
{
int result = IKS_OK;
struct ssml_parser *parsed_data = (struct ssml_parser *)user_data;
struct ssml_node *parent_node = parsed_data->cur_node;
if (type == IKS_OPEN || type == IKS_SINGLE) {
struct ssml_node *new_node = malloc(sizeof *new_node);
switch_assert(new_node);
if (parent_node) {
/* inherit parent attribs */
*new_node = *parent_node;
new_node->parent_node = parent_node;
} else {
new_node->name[0] = '\0';
new_node->language[0] = '\0';
new_node->gender[0] = '\0';
new_node->parent_node = NULL;
}
new_node->tts_voice = NULL;
new_node->say_macro = NULL;
snprintf(new_node->tag_name, TAG_LEN, "%s", name);
parsed_data->cur_node = new_node;
result = process_tag(parsed_data, name, atts);
}
if (type == IKS_CLOSE || type == IKS_SINGLE) {
if (parsed_data->cur_node) {
struct ssml_node *parent_node = parsed_data->cur_node->parent_node;
free(parsed_data->cur_node);
parsed_data->cur_node = parent_node;
}
}
return result;
}
/**
* Try to get file(s) from say module
* @param parsed_data
* @param to_say
* @return 1 if successful
*/
static int get_file_from_macro(struct ssml_parser *parsed_data, char *to_say)
{
struct ssml_node *cur_node = parsed_data->cur_node;
struct macro *say_macro = cur_node->say_macro;
struct voice *say_voice = find_say_voice(cur_node);
struct language *language;
char *file_string = NULL;
char *gender = NULL;
switch_say_interface_t *si;
/* voice is required */
if (!say_voice) {
return 0;
}
language = switch_core_hash_find(globals.language_map, say_voice->language);
/* language is required */
if (!language) {
return 0;
}
/* TODO need to_say gender, not voice gender */
gender = "neuter";
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Trying macro: %s, %s, %s, %s, %s\n", language->language, to_say, say_macro->type, say_macro->method, gender);
if ((si = switch_loadable_module_get_say_interface(language->say_module)) && si->say_string_function) {
switch_say_args_t say_args = {0};
say_args.type = switch_ivr_get_say_type_by_name(say_macro->type);
say_args.method = switch_ivr_get_say_method_by_name(say_macro->method);
say_args.gender = switch_ivr_get_say_gender_by_name(gender);
say_args.ext = "wav";
si->say_string_function(NULL, to_say, &say_args, &file_string);
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Adding macro: \"%s\", prefix=\"%s\"\n", file_string, say_voice->prefix);
if (!zstr(file_string)) {
parsed_data->files[parsed_data->num_files].name = switch_core_strdup(parsed_data->pool, file_string);
parsed_data->files[parsed_data->num_files++].prefix = switch_core_strdup(parsed_data->pool, say_voice->prefix);
return 1;
}
switch_safe_free(file_string);
return 0;
}
/**
* Get TTS file for voice
*/
static int get_file_from_voice(struct ssml_parser *parsed_data, char *to_say)
{
struct ssml_node *cur_node = parsed_data->cur_node;
if (cur_node->tts_voice) {
char *file = switch_core_sprintf(parsed_data->pool, "%s%s", cur_node->tts_voice->prefix, to_say);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Adding <%s>: \"%s\"\n", cur_node->tag_name, file);
parsed_data->files[parsed_data->num_files].name = file;
parsed_data->files[parsed_data->num_files++].prefix = NULL;
return 1;
}
return 0;
}
/**
* Get TTS from CDATA
*/
static int process_cdata_tts(struct ssml_parser *parsed_data, char *data, size_t len)
{
struct ssml_node *cur_node = parsed_data->cur_node;
if (!len) {
return IKS_OK;
}
if (cur_node && parsed_data->num_files < parsed_data->max_files) {
int i = 0;
int empty = 1;
char *to_say;
/* is CDATA empty? */
for (i = 0; i < len && empty; i++) {
empty &= !isgraph(data[i]);
}
if (empty) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Skipping empty tts\n");
return IKS_OK;
}
/* try macro */
to_say = malloc(len + 1);
switch_assert(to_say);
snprintf(to_say, len + 1, "%s", data);
if (!cur_node->say_macro || !get_file_from_macro(parsed_data, to_say)) {
/* use voice instead */
if (!get_file_from_voice(parsed_data, to_say)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "No TTS voices available to render text!\n");
}
}
free(to_say);
return IKS_OK;
}
return IKS_BADXML;
}
/**
* Process <sub>- this is an alias for text to speak
*/
static int process_sub(struct ssml_parser *parsed_data, char **atts)
{
if (atts) {
int i = 0;
while (atts[i]) {
if (!strcmp("alias", atts[i])) {
char *alias = atts[i + 1];
if (!zstr(alias)) {
return process_cdata_tts(parsed_data, alias, strlen(alias));
}
return IKS_BADXML;
}
i += 2;
}
}
return IKS_OK;
}
/**
* Process cdata
*/
static int cdata_hook(void *user_data, char *data, size_t len)
{
struct ssml_parser *parsed_data = (struct ssml_parser *)user_data;
if (!parsed_data) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Missing parser\n");
return IKS_BADXML;
}
if (parsed_data->cur_node) {
struct tag_def *handler = switch_core_hash_find(globals.tag_defs, parsed_data->cur_node->tag_name);
if (handler) {
return handler->cdata_fn(parsed_data, data, len);
}
return IKS_BADXML;
}
return IKS_OK;
}
/**
* Transforms SSML into file_string format and opens file_string.
* @param handle
* @param path the inline SSML
* @return SWITCH_STATUS_SUCCESS if opened
*/
static switch_status_t ssml_file_open(switch_file_handle_t *handle, const char *path)
{
switch_status_t status = SWITCH_STATUS_FALSE;
struct ssml_context *context = switch_core_alloc(handle->memory_pool, sizeof(*context));
struct ssml_parser *parsed_data = switch_core_alloc(handle->memory_pool, sizeof(*parsed_data));
iksparser *parser = iks_sax_new(parsed_data, tag_hook, cdata_hook);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Open: %s\n", path);
parsed_data->cur_node = NULL;
parsed_data->files = switch_core_alloc(handle->memory_pool, sizeof(struct ssml_file) * MAX_VOICE_FILES);
parsed_data->max_files = MAX_VOICE_FILES;
parsed_data->num_files = 0;
parsed_data->pool = handle->memory_pool;
parsed_data->sample_rate = handle->samplerate;
if (iks_parse(parser, path, 0, 1) == IKS_OK) {
if (parsed_data->num_files) {
context->files = parsed_data->files;
context->num_files = parsed_data->num_files;
context->index = -1;
handle->private_info = context;
status = next_file(handle);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "No files to play: %s\n", path);
}
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Parse error: %s, num_files = %i\n", path, parsed_data->num_files);
}
iks_parser_delete(parser);
return status;
}
/**
* Close SSML document.
* @param handle
* @return SWITCH_STATUS_SUCCESS
*/
static switch_status_t ssml_file_close(switch_file_handle_t *handle)
{
struct ssml_context *context = (struct ssml_context *)handle->private_info;
if (switch_test_flag((&context->fh), SWITCH_FILE_OPEN)) {
return switch_core_file_close(&context->fh);
}
return SWITCH_STATUS_SUCCESS;
}
/**
* Read from SSML document
* @param handle
* @param data
* @param len
* @return
*/
static switch_status_t ssml_file_read(switch_file_handle_t *handle, void *data, size_t *len)
{
switch_status_t status;
struct ssml_context *context = (struct ssml_context *)handle->private_info;
size_t llen = *len;
status = switch_core_file_read(&context->fh, data, len);
if (status != SWITCH_STATUS_SUCCESS) {
if ((status = next_file(handle)) != SWITCH_STATUS_SUCCESS) {
return status;
}
*len = llen;
status = switch_core_file_read(&context->fh, data, len);
}
return status;
}
/**
* Seek file
*/
static switch_status_t ssml_file_seek(switch_file_handle_t *handle, unsigned int *cur_sample, int64_t samples, int whence)
{
struct ssml_context *context = handle->private_info;
if (samples == 0 && whence == SWITCH_SEEK_SET) {
/* restart from beginning */
context->index = -1;
return next_file(handle);
}
if (!handle->seekable) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "File is not seekable\n");
return SWITCH_STATUS_NOTIMPL;
}
return switch_core_file_seek(&context->fh, cur_sample, samples, whence);
}
/**
* Configure voices
* @param pool memory pool to use
* @param map voice map to load
* @param type type of voices (for logging)
*/
static void do_config_voices(switch_memory_pool_t *pool, switch_xml_t voices, switch_hash_t *map, const char *type)
{
if (voices) {
int priority = MAX_VOICE_PRIORITY;
switch_xml_t voice;
for (voice = switch_xml_child(voices, "voice"); voice; voice = voice->next) {
const char *name = switch_xml_attr_soft(voice, "name");
const char *language = switch_xml_attr_soft(voice, "language");
const char *gender = switch_xml_attr_soft(voice, "gender");
const char *prefix = switch_xml_attr_soft(voice, "prefix");
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s map (%s, %s, %s) = %s\n", type, name, language, gender, prefix);
if (!zstr(name) && !zstr(prefix)) {
struct voice *v = (struct voice *)switch_core_alloc(pool, sizeof(*v));
v->name = switch_core_strdup(pool, name);
v->language = switch_core_strdup(pool, language);
v->gender = switch_core_strdup(pool, gender);
v->prefix = switch_core_strdup(pool, prefix);
v->priority = priority--;
switch_core_hash_insert(map, name, v);
}
}
}
}
/**
* Set default configuration when no XML configuration is present.
* @param pool memory pool to use
* @return SWITCH_STATUS_SUCCESS if module is configured
*/
static switch_status_t do_default_config(switch_memory_pool_t *pool)
{
struct voice *v = NULL;
struct language *l = NULL;
struct macro *m = NULL;
const char *sounds_dir = switch_core_get_variable("sounds_dir");
/* add TTS voice */
v = switch_core_alloc(pool, sizeof(*v));
v->name = "slt";
v->language = "en-US";
v->gender = "female";
v->prefix = "tts://flite|slt|";
v->priority = MAX_VOICE_PRIORITY;
switch_core_hash_insert(globals.tts_voice_map, "slt", v);
/* add Say voice */
v = switch_core_alloc(pool, sizeof(*v));
v->name = "callie";
v->language = "en-US";
v->gender = "female";
v->prefix = switch_core_sprintf(pool, "%s/en/us/callie/", sounds_dir ? sounds_dir : "");
switch_core_hash_insert(globals.say_voice_map, "callie", v);
/* Add ISO language to Say language mapping */
l = switch_core_alloc(pool, sizeof(*l));
l->iso = "en-US";
l->say_module = "en";
l->language = "en";
switch_core_hash_insert(globals.language_map, "en-US", l);
/* Map interpret-as to Say */
m = switch_core_alloc(pool, sizeof(*m));
m->name = "ordinal";
m->method = "counted";
m->type = "number";
switch_core_hash_insert(globals.interpret_as_map, "ordinal", m);
m = switch_core_alloc(pool, sizeof(*m));
m->name = "cardinal";
m->method = "pronounced";
m->type = "number";
switch_core_hash_insert(globals.interpret_as_map, "cardinal", m);
m = switch_core_alloc(pool, sizeof(*m));
m->name = "characters";
m->method = "pronounced";
m->type = "name_spelled";
switch_core_hash_insert(globals.interpret_as_map, "characters", m);
m = switch_core_alloc(pool, sizeof(*m));
m->name = "telephone";
m->method = "pronounced";
m->type = "telephone_number";
switch_core_hash_insert(globals.interpret_as_map, "telephone", m);
return SWITCH_STATUS_SUCCESS;
}
/**
* Configure module
* @param pool memory pool to use
* @return SWITCH_STATUS_SUCCESS if module is configured
*/
static switch_status_t do_config(switch_memory_pool_t *pool)
{
char *cf = "ssml.conf";
switch_xml_t cfg, xml;