Debian Codegraph @

external/espeak/ssml.cpp

raw
1 /*
2 * Copyright (C) 2005 to 2015 by Jonathan Duddington
3 * email: [email protected]
4 * Copyright (C) 2015-2017 Reece H. Dunn
5 * Copyright (C) 2018 Juho Hiltunen
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see: <http://www.gnu.org/licenses/>.
19 */
20
21
22 #include "espeak__config.h"
23
24 #include <ctype.h>
25 #include <errno.h>
26 #include <locale.h>
27 #include <math.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <wchar.h>
34 #include "wctype_portable.h"
35
36
37 #include "espeak_ng.h"
38 #include "speak_lib.h"
39 #include "encoding.h"
40 #include "ucd.h"
41
42 #include "ssml.h"
43 #include "common.h" // for strncpy0
44 #include "mnemonics.h" // for LookupMnemName, MNEM_TAB,
45 #include "readclause.h" // for PARAM_STACK, param_stack, AddNameData
46 #include "soundicon.h" // for LoadSoundFile2
47 #include "synthesize.h" // for SPEED_FACTORS, speed
48 #include "translate.h" // for CTRL_EMBEDDED
49 #include "voice.h" // for SelectVoice, SelectVoiceByName
50 #include "speech.h" // for MAKE_MEM_UNDEFINED
51
52 static const MNEM_TAB ssmltags[] = {
53 { "speak", SSML_SPEAK },
54 { "voice", SSML_VOICE },
55 { "prosody", SSML_PROSODY },
56 { "say-as", SSML_SAYAS },
57 { "mark", SSML_MARK },
58 { "s", SSML_SENTENCE },
59 { "p", SSML_PARAGRAPH },
60 { "phoneme", SSML_PHONEME },
61 { "sub", SSML_SUB },
62 { "tts:style", SSML_STYLE },
63 { "audio", SSML_AUDIO },
64 { "emphasis", SSML_EMPHASIS },
65 { "break", SSML_BREAK },
66 { "metadata", SSML_IGNORE_TEXT },
67
68 { "br", HTML_BREAK },
69 { "li", HTML_BREAK },
70 { "dd", HTML_BREAK },
71 { "img", HTML_BREAK },
72 { "td", HTML_BREAK },
73 { "h1", SSML_PARAGRAPH },
74 { "h2", SSML_PARAGRAPH },
75 { "h3", SSML_PARAGRAPH },
76 { "h4", SSML_PARAGRAPH },
77 { "hr", SSML_PARAGRAPH },
78 { "script", SSML_IGNORE_TEXT },
79 { "style", SSML_IGNORE_TEXT },
80 { "font", HTML_NOSPACE },
81 { "b", HTML_NOSPACE },
82 { "i", HTML_NOSPACE },
83 { "strong", HTML_NOSPACE },
84 { "em", HTML_NOSPACE },
85 { "code", HTML_NOSPACE },
86
87 { NULL, 0 }
88 };
89
90 static int (*uri_callback)(int, const char *, const char *) = NULL;
91
92 static int attrcmp(const wchar_t *string1, const char *string2)
93 {
94 int ix;
95
96 if (string1 == NULL)
97 return 1;
98
99 for (ix = 0; (string1[ix] == string2[ix]) && (string1[ix] != 0); ix++)
100 ;
101 if (((string1[ix] == '"') || (string1[ix] == '\'')) && (string2[ix] == 0))
102 return 0;
103 return 1;
104 }
105
106
107 static int attrlookup(const wchar_t *string1, const MNEM_TAB *mtab)
108 {
109 int ix;
110
111 for (ix = 0; mtab[ix].mnem != NULL; ix++) {
112 if (attrcmp(string1, mtab[ix].mnem) == 0)
113 return mtab[ix].value;
114 }
115 return mtab[ix].value;
116 }
117
118 static int attrnumber(const wchar_t *pw, int default_value, int type)
119 {
120 int value = 0;
121
122 if ((pw == NULL) || !IsDigit09(*pw))
123 return default_value;
124
125 while (IsDigit09(*pw))
126 value = value*10 + *pw++ - '0';
127 if ((type == 1) && (ucd_tolower(*pw) == 's')) {
128 // time: seconds rather than ms
129 value *= 1000;
130 }
131 return value;
132 }
133
134 static int attrcopy_utf8(char *buf, const wchar_t *pw, int len)
135 {
136 // Convert attribute string into utf8, write to buf, and return its utf8 length
137 int ix = 0;
138
139 if (pw != NULL) {
140 wchar_t quote = pw[-1];
141 if ((quote != '"') && (quote != '\'')) quote = 0;
142
143 unsigned int c;
144 int prev_c = 0;
145 while ((ix < (len-4)) && ((c = *pw++) != 0)) {
146 if ((quote == 0) && (isspace(c) || (c == '/')))
147 break;
148 if ((quote != 0) && (c == quote) && (prev_c != '\\'))
149 break; // " indicates end of attribute, unless preceded by backstroke
150
151 int n = utf8_out(c, &buf[ix]);
152 ix += n;
153 prev_c = c;
154 }
155 }
156 buf[ix] = 0;
157 return ix;
158 }
159
160 static int attr_prosody_value(int param_type, const wchar_t *pw, int *value_out)
161 {
162 int sign = 0;
163 wchar_t *tail;
164 double value;
165
166 while (iswspace_portable(*pw)) pw++;
167 if (*pw == '+') {
168 pw++;
169 sign = 1;
170 }
171 if (*pw == '-') {
172 pw++;
173 sign = -1;
174 }
175 value = (double)wcstod(pw, &tail);
176 if (tail == pw) {
177 // failed to find a number, return 100%
178 *value_out = 100;
179 return 2;
180 }
181
182 if (*tail == '%') {
183 if (sign != 0)
184 value = 100 + (sign * value);
185 *value_out = (int)value;
186 return 2; // percentage
187 }
188
189 if ((tail[0] == 's') && (tail[1] == 't')) {
190 double x;
191 // convert from semitones to a frequency percentage
192 x = pow((double)2.0, (double)((value*sign)/12)) * 100;
193 *value_out = (int)x;
194 return 2; // percentage
195 }
196
197 if (param_type == espeakRATE) {
198 if (sign == 0)
199 *value_out = (int)(value * 100);
200 else
201 *value_out = 100 + (int)(sign * value * 100);
202 return 2; // percentage
203 }
204
205 *value_out = (int)value;
206 return sign; // -1, 0, or 1
207 }
208
209 static const char *VoiceFromStack(SSML_STACK *ssml_stack, int n_ssml_stack, espeak_VOICE *base_voice, char base_voice_variant_name[40])
210 {
211 // Use the voice properties from the SSML stack to choose a voice, and switch
212 // to that voice if it's not the current voice
213
214 int ix;
215 const char *p;
216 SSML_STACK *sp;
217 const char *v_id;
218 int voice_found;
219 espeak_VOICE voice_select;
220 static char voice_name[40];
221 char language[40];
222
223 MAKE_MEM_UNDEFINED(&voice_name, sizeof(voice_name));
224
225 strcpy(voice_name, ssml_stack[0].voice_name);
226 strcpy(language, ssml_stack[0].language);
227 voice_select.age = ssml_stack[0].voice_age;
228 voice_select.gender = ssml_stack[0].voice_gender;
229 voice_select.variant = ssml_stack[0].voice_variant_number;
230 voice_select.identifier = NULL;
231
232 for (ix = 0; ix < n_ssml_stack; ix++) {
233 sp = &ssml_stack[ix];
234 int voice_name_specified = 0;
235
236 if ((sp->voice_name[0] != 0) && (SelectVoiceByName(NULL, sp->voice_name) != NULL)) {
237 voice_name_specified = 1;
238 strcpy(voice_name, sp->voice_name);
239 language[0] = 0;
240 voice_select.gender = ENGENDER_UNKNOWN;
241 voice_select.age = 0;
242 voice_select.variant = 0;
243 }
244 if (sp->language[0] != 0) {
245 strcpy(language, sp->language);
246
247 // is this language provided by the base voice?
248 p = base_voice->languages;
249 while (*p++ != 0) {
250 if (strcmp(p, language) == 0) {
251 // yes, change the language to the main language of the base voice
252 strcpy(language, &base_voice->languages[1]);
253 break;
254 }
255 p += (strlen(p) + 1);
256 }
257
258 if (voice_name_specified == 0)
259 voice_name[0] = 0; // forget a previous voice name if a language is specified
260 }
261 if (sp->voice_gender != ENGENDER_UNKNOWN)
262 voice_select.gender = sp->voice_gender;
263
264 if (sp->voice_age != 0)
265 voice_select.age = sp->voice_age;
266 if (sp->voice_variant_number != 0)
267 voice_select.variant = sp->voice_variant_number;
268 }
269
270 voice_select.name = voice_name;
271 voice_select.languages = language;
272 v_id = SelectVoice(&voice_select, &voice_found);
273 if (v_id == NULL)
274 return "default";
275
276 if ((strchr(v_id, '+') == NULL) && ((voice_select.gender == ENGENDER_UNKNOWN) || (voice_select.gender == base_voice->gender)) && (base_voice_variant_name[0] != 0)) {
277 // a voice variant has not been selected, use the original voice variant
278 char buf[80];
279 sprintf(buf, "%s+%s", v_id, base_voice_variant_name);
280 strncpy0(voice_name, buf, sizeof(voice_name));
281 return voice_name;
282 }
283 return v_id;
284 }
285
286
287 static const wchar_t *GetSsmlAttribute(wchar_t *pw, const char *name)
288 {
289 // Gets the value string for an attribute.
290 // Returns NULL if the attribute is not present
291
292 int ix;
293 static const wchar_t empty[1] = { 0 };
294
295 while (*pw != 0) {
296 if (iswspace_portable(pw[-1])) {
297 ix = 0;
298 while (*pw == name[ix]) {
299 pw++;
300 ix++;
301 }
302 if (name[ix] == 0) {
303 // found the attribute, now get the value
304 while (iswspace_portable(*pw)) pw++;
305 if (*pw == '=') pw++;
306 while (iswspace_portable(*pw)) pw++;
307 if ((*pw == '"') || (*pw == '\'')) // allow single-quotes ?
308 return pw+1;
309 else if (iswspace_portable(*pw) || (*pw == '/')) // end of attribute
310 return empty;
311 else
312 return pw;
313 }
314 }
315 pw++;
316 }
317 return NULL;
318 }
319
320
321 static int GetVoiceAttributes(wchar_t *pw, int tag_type, SSML_STACK *ssml_sp, SSML_STACK *ssml_stack, int n_ssml_stack, char current_voice_id[40], espeak_VOICE *base_voice, char *base_voice_variant_name)
322 {
323 // Determines whether voice attribute are specified in this tag, and if so, whether this means
324 // a voice change.
325 // If it's a closing tag, delete the top frame of the stack and determine whether this implies
326 // a voice change.
327 // Returns CLAUSE_TYPE_VOICE_CHANGE if there is a voice change
328
329 const char *new_voice_id;
330
331 static const MNEM_TAB mnem_gender[] = {
332 { "male", ENGENDER_MALE },
333 { "female", ENGENDER_FEMALE },
334 { "neutral", ENGENDER_NEUTRAL },
335 { NULL, ENGENDER_UNKNOWN }
336 };
337
338 if (tag_type & SSML_CLOSE) {
339 // delete a stack frame
340 if (n_ssml_stack > 1)
341 n_ssml_stack--;
342 } else {
343 const wchar_t *lang;
344 const wchar_t *gender;
345 const wchar_t *name;
346 const wchar_t *age;
347 const wchar_t *variant;
348
349 // add a stack frame if any voice details are specified
350 lang = GetSsmlAttribute(pw, "xml:lang");
351
352 if (tag_type != SSML_VOICE) {
353 // only expect an xml:lang attribute
354 name = NULL;
355 variant = NULL;
356 age = NULL;
357 gender = NULL;
358 } else {
359 name = GetSsmlAttribute(pw, "name");
360 variant = GetSsmlAttribute(pw, "variant");
361 age = GetSsmlAttribute(pw, "age");
362 gender = GetSsmlAttribute(pw, "gender");
363 }
364
365 if ((tag_type != SSML_VOICE) && (lang == NULL))
366 return 0; // <s> or <p> without language spec, nothing to do
367
368 ssml_sp = &ssml_stack[n_ssml_stack++];
369
370 int value;
371
372 attrcopy_utf8(ssml_sp->language, lang, sizeof(ssml_sp->language));
373 attrcopy_utf8(ssml_sp->voice_name, name, sizeof(ssml_sp->voice_name));
374 if ((value = attrnumber(variant, 1, 0)) > 0)
375 value--; // variant='0' and variant='1' the same
376 ssml_sp->voice_variant_number = value;
377 ssml_sp->voice_age = attrnumber(age, 0, 0);
378 ssml_sp->voice_gender = attrlookup(gender, mnem_gender);
379 ssml_sp->tag_type = tag_type;
380 }
381
382 new_voice_id = VoiceFromStack(ssml_stack, n_ssml_stack, base_voice, base_voice_variant_name);
383 if (strcmp(new_voice_id, current_voice_id) != 0) {
384 // add an embedded command to change the voice
385 strcpy(current_voice_id, new_voice_id);
386 return CLAUSE_TYPE_VOICE_CHANGE;
387 }
388
389 return 0;
390 }
391
392 static void ProcessParamStack(char *outbuf, int *outix, int n_param_stack, PARAM_STACK *paramStack, int *speech_parameters)
393 {
394 // Set the speech parameters from the parameter stack
395 int param;
396 int ix;
397 char buf[20];
398 int new_parameters[N_SPEECH_PARAM];
399 static const char cmd_letter[N_SPEECH_PARAM] = { 0, 'S', 'A', 'P', 'R', 0, 'C', 0, 0, 0, 0, 0, 'F' }; // embedded command letters
400
401 for (param = 0; param < N_SPEECH_PARAM; param++)
402 new_parameters[param] = -1;
403
404 for (ix = 0; ix < n_param_stack; ix++) {
405 for (param = 0; param < N_SPEECH_PARAM; param++) {
406 if (paramStack[ix].parameter[param] >= 0)
407 new_parameters[param] = paramStack[ix].parameter[param];
408 }
409 }
410
411 for (param = 0; param < N_SPEECH_PARAM; param++) {
412 int value;
413 if ((value = new_parameters[param]) != speech_parameters[param]) {
414 buf[0] = 0;
415
416 switch (param)
417 {
418 case espeakPUNCTUATION:
419 option_punctuation = value-1;
420 break;
421 case espeakCAPITALS:
422 option_capitals = value;
423 break;
424 case espeakRATE:
425 case espeakVOLUME:
426 case espeakPITCH:
427 case espeakRANGE:
428 case espeakEMPHASIS:
429 sprintf(buf, "%c%d%c", CTRL_EMBEDDED, value, cmd_letter[param]);
430 break;
431 }
432
433 speech_parameters[param] = new_parameters[param];
434 strcpy(&outbuf[*outix], buf);
435 *outix += strlen(buf);
436 }
437 }
438 }
439
440 static PARAM_STACK *PushParamStack(int tag_type, int *n_param_stack, PARAM_STACK *paramStack)
441 {
442 int ix;
443 PARAM_STACK *sp;
444
445 sp = &paramStack[*n_param_stack];
446 if (*n_param_stack < (N_PARAM_STACK-1))
447 (*n_param_stack)++;
448
449 sp->type = tag_type;
450 for (ix = 0; ix < N_SPEECH_PARAM; ix++)
451 sp->parameter[ix] = -1;
452 return sp;
453 }
454
455 static void PopParamStack(int tag_type, char *outbuf, int *outix, int *n_param_stack, PARAM_STACK *paramStack, int *speech_parameters)
456 {
457 // unwind the stack up to and including the previous tag of this type
458 int ix;
459 int top = 0;
460
461 if (tag_type >= SSML_CLOSE)
462 tag_type -= SSML_CLOSE;
463
464 for (ix = 0; ix < *n_param_stack; ix++) {
465 if (paramStack[ix].type == tag_type)
466 top = ix;
467 }
468 if (top > 0)
469 *n_param_stack = top;
470 ProcessParamStack(outbuf, outix, *n_param_stack, paramStack, speech_parameters);
471 }
472
473 static int ReplaceKeyName(char *outbuf, int index, int *outix)
474 {
475 // Replace some key-names by single characters, so they can be pronounced in different languages
476 static const MNEM_TAB keynames[] = {
477 { "space ", 0xe020 },
478 { "tab ", 0xe009 },
479 { "underscore ", 0xe05f },
480 { "double-quote ", '"' },
481 { NULL, 0 }
482 };
483
484 int letter;
485 char *p;
486
487 p = &outbuf[index];
488
489 if ((letter = LookupMnem(keynames, p)) != 0) {
490 int ix;
491 ix = utf8_out(letter, p);
492 *outix = index + ix;
493 return letter;
494 }
495 return 0;
496 }
497
498 static void SetProsodyParameter(int param_type, const wchar_t *attr1, PARAM_STACK *sp, PARAM_STACK *paramStack, int *speech_parameters)
499 {
500 int value;
501
502
503 static const MNEM_TAB mnem_volume[] = {
504 { "default", 100 },
505 { "silent", 0 },
506 { "x-soft", 30 },
507 { "soft", 65 },
508 { "medium", 100 },
509 { "loud", 150 },
510 { "x-loud", 230 },
511 { NULL, -1 }
512 };
513
514 static const MNEM_TAB mnem_rate[] = {
515 { "default", 100 },
516 { "x-slow", 60 },
517 { "slow", 80 },
518 { "medium", 100 },
519 { "fast", 125 },
520 { "x-fast", 160 },
521 { NULL, -1 }
522 };
523
524 static const MNEM_TAB mnem_pitch[] = {
525 { "default", 100 },
526 { "x-low", 70 },
527 { "low", 85 },
528 { "medium", 100 },
529 { "high", 110 },
530 { "x-high", 120 },
531 { NULL, -1 }
532 };
533
534 static const MNEM_TAB mnem_range[] = {
535 { "default", 100 },
536 { "x-low", 20 },
537 { "low", 50 },
538 { "medium", 100 },
539 { "high", 140 },
540 { "x-high", 180 },
541 { NULL, -1 }
542 };
543
544 static const MNEM_TAB * const mnem_tabs[5] = {
545 NULL, mnem_rate, mnem_volume, mnem_pitch, mnem_range
546 };
547
548 if ((value = attrlookup(attr1, mnem_tabs[param_type])) >= 0) {
549 // mnemonic specifies a value as a percentage of the base pitch/range/rate/volume
550 sp->parameter[param_type] = (paramStack[0].parameter[param_type] * value)/100;
551 } else {
552 int sign = attr_prosody_value(param_type, attr1, &value);
553
554 if (sign == 0)
555 sp->parameter[param_type] = value; // absolute value in Hz
556 else if (sign == 2) {
557 // change specified as percentage or in semitones
558 sp->parameter[param_type] = (speech_parameters[param_type] * value)/100;
559 } else {
560 // change specified as plus or minus Hz
561 sp->parameter[param_type] = speech_parameters[param_type] + (value*sign);
562 }
563 }
564 }
565
566 int ProcessSsmlTag(wchar_t *xml_buf, char *outbuf, int *outix, int n_outbuf, const char *xmlbase, bool *audio_text, char *current_voice_id, espeak_VOICE *base_voice, char *base_voice_variant_name, bool *ignore_text, bool *clear_skipping_text, int *sayas_mode, int *sayas_start, SSML_STACK *ssml_stack, int *n_ssml_stack, int *n_param_stack, int *speech_parameters)
567 {
568 // xml_buf is the tag and attributes with a zero terminator in place of the original '>'
569 // returns a clause terminator value.
570
571 unsigned int ix;
572 int index;
573 int tag_type;
574 int value;
575 int value2;
576 int value3;
577 int voice_change_flag;
578 wchar_t *px;
579 const wchar_t *attr1;
580 const wchar_t *attr2;
581 const wchar_t *attr3;
582 int terminator;
583 int param_type;
584 char tag_name[40];
585 char buf[160];
586 PARAM_STACK *sp;
587 SSML_STACK *ssml_sp;
588
589 // don't process comments and xml declarations
590 if (wcsncmp(xml_buf, (wchar_t *) "!--", 3) == 0 || wcsncmp(xml_buf, (wchar_t *) "?xml", 4) == 0) {
591 return 0;
592 }
593
594 // these tags have no effect if they are self-closing, eg. <voice />
595 static const char ignore_if_self_closing[] = { 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0 };
596
597 bool self_closing = false;
598 int len;
599 len = wcslen(xml_buf);
600 if (xml_buf[len - 1] == '/') {
601 // a self-closing tag
602 xml_buf[len - 1] = ' ';
603 self_closing = true;
604 }
605
606 static const MNEM_TAB mnem_phoneme_alphabet[] = {
607 { "espeak", 1 },
608 { NULL, -1 }
609 };
610
611 static const MNEM_TAB mnem_punct[] = {
612 { "none", 1 },
613 { "all", 2 },
614 { "some", 3 },
615 { NULL, -1 }
616 };
617
618 static const MNEM_TAB mnem_capitals[] = {
619 { "no", 0 },
620 { "icon", 1 },
621 { "spelling", 2 },
622 { "pitch", 20 }, // this is the amount by which to raise the pitch
623 { NULL, -1 }
624 };
625
626 static const MNEM_TAB mnem_interpret_as[] = {
627 { "characters", SAYAS_CHARS },
628 { "tts:char", SAYAS_SINGLE_CHARS },
629 { "tts:key", SAYAS_KEY },
630 { "tts:digits", SAYAS_DIGITS },
631 { "telephone", SAYAS_DIGITS1 },
632 { NULL, -1 }
633 };
634
635 static const MNEM_TAB mnem_sayas_format[] = {
636 { "glyphs", 1 },
637 { NULL, -1 }
638 };
639
640 static const MNEM_TAB mnem_break[] = {
641 { "none", 0 },
642 { "x-weak", 1 },
643 { "weak", 2 },
644 { "medium", 3 },
645 { "strong", 4 },
646 { "x-strong", 5 },
647 { NULL, -1 }
648 };
649
650 static const MNEM_TAB mnem_emphasis[] = {
651 { "none", 1 },
652 { "reduced", 2 },
653 { "moderate", 3 },
654 { "strong", 4 },
655 { "x-strong", 5 },
656 { NULL, -1 }
657 };
658
659 static const char * const prosody_attr[5] = {
660 NULL, "rate", "volume", "pitch", "range"
661 };
662
663 for (ix = 0; ix < (sizeof(tag_name)-1); ix++) {
664 int c;
665 if (((c = xml_buf[ix]) == 0) || iswspace_portable(c))
666 break;
667 tag_name[ix] = tolower((char)c);
668 }
669 tag_name[ix] = 0;
670
671 px = &xml_buf[ix]; // the tag's attributes
672
673 if (tag_name[0] == '/') {
674 // closing tag
675 if ((tag_type = LookupMnem(ssmltags, &tag_name[1])) != HTML_NOSPACE)
676 outbuf[(*outix)++] = ' ';
677 tag_type += SSML_CLOSE;
678 } else {
679 if ((tag_type = LookupMnem(ssmltags, tag_name)) != HTML_NOSPACE) {
680 // separate SSML tags from the previous word (but not HMTL tags such as <b> <font> which can occur inside a word)
681 outbuf[(*outix)++] = ' ';
682 }
683
684 if (self_closing && ignore_if_self_closing[tag_type])
685 return 0;
686 }
687
688 voice_change_flag = 0;
689 ssml_sp = &ssml_stack[*n_ssml_stack-1];
690
691 switch (tag_type)
692 {
693 case SSML_STYLE:
694 sp = PushParamStack(tag_type, n_param_stack, (PARAM_STACK *) param_stack);
695 attr1 = GetSsmlAttribute(px, "field");
696 attr2 = GetSsmlAttribute(px, "mode");
697
698
699 if (attrcmp(attr1, "punctuation") == 0) {
700 value = attrlookup(attr2, mnem_punct);
701 sp->parameter[espeakPUNCTUATION] = value;
702 } else if (attrcmp(attr1, "capital_letters") == 0) {
703 value = attrlookup(attr2, mnem_capitals);
704 sp->parameter[espeakCAPITALS] = value;
705 }
706 ProcessParamStack(outbuf, outix, *n_param_stack, param_stack, speech_parameters);
707 break;
708 case SSML_PROSODY:
709 sp = PushParamStack(tag_type, n_param_stack, (PARAM_STACK *) param_stack);
710
711 // look for attributes: rate, volume, pitch, range
712 for (param_type = espeakRATE; param_type <= espeakRANGE; param_type++) {
713 if ((attr1 = GetSsmlAttribute(px, prosody_attr[param_type])) != NULL)
714 SetProsodyParameter(param_type, attr1, sp, param_stack, speech_parameters);
715 }
716
717 ProcessParamStack(outbuf, outix, *n_param_stack, param_stack, speech_parameters);
718 break;
719 case SSML_EMPHASIS:
720 sp = PushParamStack(tag_type, n_param_stack, (PARAM_STACK *) param_stack);
721 value = 3; // default is "moderate"
722 if ((attr1 = GetSsmlAttribute(px, "level")) != NULL)
723 value = attrlookup(attr1, mnem_emphasis);
724
725 if (translator->langopts.tone_language == 1) {
726 static const unsigned char emphasis_to_pitch_range[] = { 50, 50, 40, 70, 90, 100 };
727 static const unsigned char emphasis_to_volume[] = { 100, 100, 70, 110, 135, 150 };
728 // tone language (eg.Chinese) do emphasis by increasing the pitch range.
729 sp->parameter[espeakRANGE] = emphasis_to_pitch_range[value];
730 sp->parameter[espeakVOLUME] = emphasis_to_volume[value];
731 } else {
732 static const unsigned char emphasis_to_volume2[] = { 100, 100, 75, 100, 120, 150 };
733 sp->parameter[espeakVOLUME] = emphasis_to_volume2[value];
734 sp->parameter[espeakEMPHASIS] = value;
735 }
736 ProcessParamStack(outbuf, outix, *n_param_stack, param_stack, speech_parameters);
737 break;
738 case SSML_STYLE + SSML_CLOSE:
739 case SSML_PROSODY + SSML_CLOSE:
740 case SSML_EMPHASIS + SSML_CLOSE:
741 PopParamStack(tag_type, outbuf, outix, n_param_stack, (PARAM_STACK *) param_stack, (int *) speech_parameters);
742 break;
743 case SSML_PHONEME:
744 attr1 = GetSsmlAttribute(px, "alphabet");
745 attr2 = GetSsmlAttribute(px, "ph");
746 value = attrlookup(attr1, mnem_phoneme_alphabet);
747 if (value == 1) { // alphabet="espeak"
748 outbuf[(*outix)++] = '[';
749 outbuf[(*outix)++] = '[';
750 *outix += attrcopy_utf8(&outbuf[*outix], attr2, n_outbuf-*outix);
751 outbuf[(*outix)++] = ']';
752 outbuf[(*outix)++] = ']';
753 }
754 break;
755 case SSML_SAYAS:
756 attr1 = GetSsmlAttribute(px, "interpret-as");
757 attr2 = GetSsmlAttribute(px, "format");
758 attr3 = GetSsmlAttribute(px, "detail");
759 value = attrlookup(attr1, mnem_interpret_as);
760 value2 = attrlookup(attr2, mnem_sayas_format);
761 if (value2 == 1)
762 value = SAYAS_GLYPHS;
763
764 value3 = attrnumber(attr3, 0, 0);
765
766 if (value == SAYAS_DIGITS) {
767 if (value3 <= 1)
768 value = SAYAS_DIGITS1;
769 else
770 value = SAYAS_DIGITS + value3;
771 }
772
773 sprintf(buf, "%c%dY", CTRL_EMBEDDED, value);
774 strcpy(&outbuf[*outix], buf);
775 *outix += strlen(buf);
776
777 *sayas_start = *outix;
778 *sayas_mode = value; // punctuation doesn't end clause during SAY-AS
779 break;
780 case SSML_SAYAS + SSML_CLOSE:
781 if (*sayas_mode == SAYAS_KEY) {
782 outbuf[*outix] = 0;
783 ReplaceKeyName(outbuf, *sayas_start, outix);
784 }
785
786 outbuf[(*outix)++] = CTRL_EMBEDDED;
787 outbuf[(*outix)++] = 'Y';
788 *sayas_mode = 0;
789 break;
790 case SSML_SUB:
791 if ((attr1 = GetSsmlAttribute(px, "alias")) != NULL) {
792 // use the alias rather than the text
793 *ignore_text = true;
794 *outix += attrcopy_utf8(&outbuf[*outix], attr1, n_outbuf-*outix);
795 }
796 break;
797 case SSML_IGNORE_TEXT:
798 *ignore_text = true;
799 break;
800 case SSML_SUB + SSML_CLOSE:
801 case SSML_IGNORE_TEXT + SSML_CLOSE:
802 *ignore_text = false;
803 break;
804 case SSML_MARK:
805 if ((attr1 = GetSsmlAttribute(px, "name")) != NULL) {
806 // add name to circular buffer of marker names
807 attrcopy_utf8(buf, attr1, sizeof(buf));
808
809 if ((buf[0] != 0) && (strcmp(skip_marker, buf) == 0)) {
810 // This is the marker we are waiting for before starting to speak
811 *clear_skipping_text = true;
812 skip_marker[0] = 0;
813 return CLAUSE_NONE;
814 }
815
816 if ((index = AddNameData(buf, 0)) >= 0) {
817 sprintf(buf, "%c%dM", CTRL_EMBEDDED, index);
818 strcpy(&outbuf[*outix], buf);
819 *outix += strlen(buf);
820 }
821 }
822 break;
823 case SSML_AUDIO:
824 sp = PushParamStack(tag_type, n_param_stack, (PARAM_STACK *)param_stack);
825
826 if ((attr1 = GetSsmlAttribute(px, "src")) != NULL) {
827 attrcopy_utf8(buf, attr1, sizeof(buf));
828
829 if (uri_callback == NULL) {
830 if ((xmlbase != NULL) && (buf[0] != '/')) {
831 char fname[256];
832 sprintf(fname, "%s/%s", xmlbase, buf);
833 index = LoadSoundFile2(fname);
834 } else
835 index = LoadSoundFile2(buf);
836 if (index >= 0) {
837 sprintf(buf, "%c%dI", CTRL_EMBEDDED, index);
838 strcpy(&outbuf[*outix], buf);
839 *outix += strlen(buf);
840 sp->parameter[espeakSILENCE] = 1;
841 }
842 } else {
843 if ((index = AddNameData(buf, 0)) >= 0) {
844 char *uri;
845 uri = &namedata[index];
846 if (uri_callback(1, uri, xmlbase) == 0) {
847 sprintf(buf, "%c%dU", CTRL_EMBEDDED, index);
848 strcpy(&outbuf[*outix], buf);
849 *outix += strlen(buf);
850 sp->parameter[espeakSILENCE] = 1;
851 }
852 }
853 }
854 }
855 ProcessParamStack(outbuf, outix, *n_param_stack, param_stack, speech_parameters);
856
857 if (self_closing)
858 PopParamStack(tag_type, outbuf, outix, n_param_stack, (PARAM_STACK *) param_stack, (int *) speech_parameters);
859 else
860 *audio_text = true;
861 return CLAUSE_NONE;
862 case SSML_AUDIO + SSML_CLOSE:
863 PopParamStack(tag_type, outbuf, outix, n_param_stack, (PARAM_STACK *) param_stack, (int *) speech_parameters);
864 *audio_text = false;
865 return CLAUSE_NONE;
866 case SSML_BREAK:
867 value = 21;
868 terminator = CLAUSE_NONE;
869
870 if ((attr1 = GetSsmlAttribute(px, "strength")) != NULL) {
871 static const int break_value[6] = { 0, 7, 14, 21, 40, 80 }; // *10mS
872 value = attrlookup(attr1, mnem_break);
873 if (value < 0) value = 2;
874 if (value < 3) {
875 // adjust prepause on the following word
876 sprintf(&outbuf[*outix], "%c%dB", CTRL_EMBEDDED, value);
877 *outix += 3;
878 terminator = 0;
879 }
880 value = break_value[value];
881 }
882 if ((attr2 = GetSsmlAttribute(px, "time")) != NULL) {
883 value2 = attrnumber(attr2, 0, 1); // pause in mS
884
885 value2 = value2 * speech_parameters[espeakSSML_BREAK_MUL] / 100;
886
887 int wpm = speech_parameters[espeakRATE];
888 espeak_SetParameter(espeakRATE, wpm, 0);
889
890 #if USE_LIBSONIC
891 if (wpm >= espeakRATE_MAXIMUM) {
892 // Compensate speedup with libsonic, see function SetSpeed()
893 double sonic = ((double)wpm)/espeakRATE_NORMAL;
894 value2 = value2 * sonic;
895 }
896 #endif
897
898 // compensate for speaking speed to keep constant pause length, see function PauseLength()
899 // 'value' here is x 10mS
900 value = (value2 * 256) / (speed.clause_pause_factor * 10);
901 if (value < 200)
902 value = (value2 * 256) / (speed.pause_factor * 10);
903
904 if (terminator == 0)
905 terminator = CLAUSE_NONE;
906 }
907 if (terminator) {
908 if (value > 0xfff) {
909 // scale down the value and set a scaling indicator bit
910 value = value / 32;
911 if (value > 0xfff)
912 value = 0xfff;
913 terminator |= CLAUSE_PAUSE_LONG;
914 }
915 return terminator + value;
916 }
917 break;
918 case SSML_SPEAK:
919 if ((attr1 = GetSsmlAttribute(px, "xml:base")) != NULL) {
920 attrcopy_utf8(buf, attr1, sizeof(buf));
921 if ((index = AddNameData(buf, 0)) >= 0)
922 xmlbase = &namedata[index];
923 }
924 if (GetVoiceAttributes(px, tag_type, ssml_sp, ssml_stack, *n_ssml_stack, current_voice_id, base_voice, base_voice_variant_name) == 0)
925 return 0; // no voice change
926 return CLAUSE_VOICE;
927 case SSML_VOICE:
928 if (GetVoiceAttributes(px, tag_type, ssml_sp, ssml_stack, *n_ssml_stack, current_voice_id, base_voice, base_voice_variant_name) == 0)
929 return 0; // no voice change
930 return CLAUSE_VOICE;
931 case SSML_SPEAK + SSML_CLOSE:
932 // unwind stack until the previous <voice> or <speak> tag
933 while ((*n_ssml_stack > 1) && (ssml_stack[*n_ssml_stack-1].tag_type != SSML_SPEAK))
934 (*n_ssml_stack)--;
935 return CLAUSE_PERIOD + GetVoiceAttributes(px, tag_type, ssml_sp, ssml_stack, *n_ssml_stack, current_voice_id, base_voice, base_voice_variant_name);
936 case SSML_VOICE + SSML_CLOSE:
937 // unwind stack until the previous <voice> or <speak> tag
938 while ((*n_ssml_stack > 1) && (ssml_stack[*n_ssml_stack-1].tag_type != SSML_VOICE))
939 (*n_ssml_stack)--;
940
941 terminator = 0; // ?? Sentence intonation, but no pause ??
942 return terminator + GetVoiceAttributes(px, tag_type, ssml_sp, ssml_stack, *n_ssml_stack, current_voice_id, base_voice, base_voice_variant_name);
943 case HTML_BREAK:
944 case HTML_BREAK + SSML_CLOSE:
945 return CLAUSE_COLON;
946 case SSML_SENTENCE:
947 if (ssml_sp->tag_type == SSML_SENTENCE) {
948 // new sentence implies end-of-sentence
949 voice_change_flag = GetVoiceAttributes(px, SSML_SENTENCE+SSML_CLOSE, ssml_sp, ssml_stack, *n_ssml_stack, current_voice_id, base_voice, base_voice_variant_name);
950 }
951 voice_change_flag |= GetVoiceAttributes(px, tag_type, ssml_sp, ssml_stack, *n_ssml_stack, current_voice_id, base_voice, base_voice_variant_name);
952 return CLAUSE_PARAGRAPH + voice_change_flag;
953 case SSML_PARAGRAPH:
954 if (ssml_sp->tag_type == SSML_SENTENCE) {
955 // new paragraph implies end-of-sentence or end-of-paragraph
956 voice_change_flag = GetVoiceAttributes(px, SSML_SENTENCE+SSML_CLOSE, ssml_sp, ssml_stack, *n_ssml_stack, current_voice_id, base_voice, base_voice_variant_name);
957 }
958 if (ssml_sp->tag_type == SSML_PARAGRAPH) {
959 // new paragraph implies end-of-sentence or end-of-paragraph
960 voice_change_flag |= GetVoiceAttributes(px, SSML_PARAGRAPH+SSML_CLOSE, ssml_sp, ssml_stack, *n_ssml_stack, current_voice_id, base_voice, base_voice_variant_name);
961 }
962 voice_change_flag |= GetVoiceAttributes(px, tag_type, ssml_sp, ssml_stack, *n_ssml_stack, current_voice_id, base_voice, base_voice_variant_name);
963 return CLAUSE_PARAGRAPH + voice_change_flag;
964 case SSML_SENTENCE + SSML_CLOSE:
965 if (ssml_sp->tag_type == SSML_SENTENCE) {
966 // end of a sentence which specified a language
967 voice_change_flag = GetVoiceAttributes(px, tag_type, ssml_sp, ssml_stack, *n_ssml_stack, current_voice_id, base_voice, base_voice_variant_name);
968 }
969 return CLAUSE_PERIOD + voice_change_flag;
970 case SSML_PARAGRAPH + SSML_CLOSE:
971 if ((ssml_sp->tag_type == SSML_SENTENCE) || (ssml_sp->tag_type == SSML_PARAGRAPH)) {
972 // End of a paragraph which specified a language.
973 // (End-of-paragraph also implies end-of-sentence)
974 return GetVoiceAttributes(px, tag_type, ssml_sp, ssml_stack, *n_ssml_stack, current_voice_id, base_voice, base_voice_variant_name) + CLAUSE_PARAGRAPH;
975 }
976 return CLAUSE_PARAGRAPH;
977 }
978 return 0;
979 }
980
981 #pragma GCC visibility push(default)
982 ESPEAK_API void espeak_SetUriCallback(int (*UriCallback)(int, const char *, const char *))
983 {
984 uri_callback = UriCallback;
985 }
986 #pragma GCC visibility pop
987
988 static const MNEM_TAB xml_entity_mnemonics[] = {
989 { "gt", '>' },
990 { "lt", 0xe000 + '<' }, // private usage area, to avoid confusion with XML tag
991 { "amp", '&' },
992 { "quot", '"' },
993 { "nbsp", ' ' },
994 { "apos", '\'' },
995 { NULL, -1 }
996 };
997
998 int ParseSsmlReference(char *ref, int *c1, int *c2) {
999 // Check if buffer *ref contains an XML character or entity reference
1000 // if found, set *c1 to the replacement char
1001 // change *c2 for entity references
1002 // returns >= 0 on success
1003
1004 if (ref[0] == '#') {
1005 // character reference
1006 if (ref[1] == 'x')
1007 return sscanf(&ref[2], "%x", c1);
1008 else
1009 return sscanf(&ref[1], "%d", c1);
1010 } else {
1011 // entity reference
1012 int found;
1013 if ((found = LookupMnem(xml_entity_mnemonics, ref)) != -1) {
1014 *c1 = found;
1015 if (*c2 == 0)
1016 *c2 = ' ';
1017 return found;
1018 }
1019 }
1020 return -1;
1021 }