Debian Codegraph @

external/espeak/readclause.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 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see: <http://www.gnu.org/licenses/>.
18 */
19
20 #include "espeak__config.h"
21
22 #include <ctype.h>
23 #include <errno.h>
24 #include <locale.h>
25 #include <math.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <wchar.h>
32 #include "wctype_portable.h"
33
34 #include "espeak_ng.h"
35 #include "speak_lib.h"
36 #include "encoding.h"
37 #include "ucd.h"
38
39 #include "readclause.h"
40 #include "common.h" // for GetFileLength, strncpy0
41 #include "dictionary.h" // for LookupDictList, DecodePhonemes, Set...
42 #include "error.h" // for create_file_error_context
43 #include "phoneme.h" // for phonSWITCH
44 #include "soundicon.h" // for LookupSoundIcon
45 #include "speech.h" // for LookupMnem, PATHSEP
46 #include "ssml.h" // for SSML_STACK, ProcessSsmlTag, N_PARAM...
47 #include "synthdata.h" // for SelectPhonemeTable
48 #include "translate.h" // for Translator, utf8_out, CLAUSE_OPTION...
49 #include "voice.h" // for voice, voice_t, espeak_GetCurrentVoice
50
51 #define N_XML_BUF 500
52
53 static void DecodeWithPhonemeMode(char *buf, char *phonemes, Translator *tr, Translator *tr2, unsigned int flags[]);
54 static void TerminateBufWithSpaceAndZero(char *buf, int index, int *ungetc);
55
56 static const char *xmlbase = ""; // base URL from <speak>
57
58 static int namedata_ix = 0;
59 static int n_namedata = 0;
60 char *namedata = NULL;
61
62 static int ungot_char2 = 0;
63 espeak_ng_TEXT_DECODER *p_decoder = NULL;
64 static int ungot_char;
65
66 static bool ignore_text = false; // set during <sub> ... </sub> to ignore text which has been replaced by an alias
67 static bool audio_text = false; // set during <audio> ... </audio>
68 static bool clear_skipping_text = false; // next clause should clear the skipping_text flag
69 int count_characters = 0;
70 static int sayas_mode;
71 static int sayas_start;
72
73 #define N_SSML_STACK 20
74 static int n_ssml_stack;
75 static SSML_STACK ssml_stack[N_SSML_STACK];
76
77 static espeak_VOICE base_voice;
78 static char base_voice_variant_name[40] = { 0 };
79 static char current_voice_id[40] = { 0 };
80
81 static int n_param_stack;
82 PARAM_STACK param_stack[N_PARAM_STACK];
83
84 static int speech_parameters[N_SPEECH_PARAM]; // current values, from param_stack
85 int saved_parameters[N_SPEECH_PARAM]; // Parameters saved on synthesis start
86
87 #define ESPEAKNG_CLAUSE_TYPE_PROPERTY_MASK 0xFFF0000000000000ull
88
89 int clause_type_from_codepoint(uint32_t c)
90 {
91 ucd_category cat = ucd_lookup_category(c);
92 ucd_property props = ucd_properties(c, cat);
93
94 switch (props & ESPEAKNG_CLAUSE_TYPE_PROPERTY_MASK)
95 {
96 case ESPEAKNG_PROPERTY_FULL_STOP:
97 return CLAUSE_PERIOD;
98 case ESPEAKNG_PROPERTY_FULL_STOP | ESPEAKNG_PROPERTY_OPTIONAL_SPACE_AFTER:
99 return CLAUSE_PERIOD | CLAUSE_OPTIONAL_SPACE_AFTER;
100 case ESPEAKNG_PROPERTY_QUESTION_MARK:
101 return CLAUSE_QUESTION;
102 case ESPEAKNG_PROPERTY_QUESTION_MARK | ESPEAKNG_PROPERTY_OPTIONAL_SPACE_AFTER:
103 return CLAUSE_QUESTION | CLAUSE_OPTIONAL_SPACE_AFTER;
104 case ESPEAKNG_PROPERTY_QUESTION_MARK | ESPEAKNG_PROPERTY_PUNCTUATION_IN_WORD:
105 return CLAUSE_QUESTION | CLAUSE_PUNCTUATION_IN_WORD;
106 case ESPEAKNG_PROPERTY_EXCLAMATION_MARK:
107 return CLAUSE_EXCLAMATION;
108 case ESPEAKNG_PROPERTY_EXCLAMATION_MARK | ESPEAKNG_PROPERTY_OPTIONAL_SPACE_AFTER:
109 return CLAUSE_EXCLAMATION | CLAUSE_OPTIONAL_SPACE_AFTER;
110 case ESPEAKNG_PROPERTY_EXCLAMATION_MARK | ESPEAKNG_PROPERTY_PUNCTUATION_IN_WORD:
111 return CLAUSE_EXCLAMATION | CLAUSE_PUNCTUATION_IN_WORD;
112 case ESPEAKNG_PROPERTY_COMMA:
113 return CLAUSE_COMMA;
114 case ESPEAKNG_PROPERTY_COMMA | ESPEAKNG_PROPERTY_OPTIONAL_SPACE_AFTER:
115 return CLAUSE_COMMA | CLAUSE_OPTIONAL_SPACE_AFTER;
116 case ESPEAKNG_PROPERTY_COLON:
117 return CLAUSE_COLON;
118 case ESPEAKNG_PROPERTY_COLON | ESPEAKNG_PROPERTY_OPTIONAL_SPACE_AFTER:
119 return CLAUSE_COLON | CLAUSE_OPTIONAL_SPACE_AFTER;
120 case ESPEAKNG_PROPERTY_SEMI_COLON:
121 case ESPEAKNG_PROPERTY_EXTENDED_DASH:
122 return CLAUSE_SEMICOLON;
123 case ESPEAKNG_PROPERTY_SEMI_COLON | ESPEAKNG_PROPERTY_OPTIONAL_SPACE_AFTER:
124 case ESPEAKNG_PROPERTY_QUESTION_MARK | ESPEAKNG_PROPERTY_OPTIONAL_SPACE_AFTER | ESPEAKNG_PROPERTY_INVERTED_TERMINAL_PUNCTUATION:
125 case ESPEAKNG_PROPERTY_EXCLAMATION_MARK | ESPEAKNG_PROPERTY_OPTIONAL_SPACE_AFTER | ESPEAKNG_PROPERTY_INVERTED_TERMINAL_PUNCTUATION:
126 return CLAUSE_SEMICOLON | CLAUSE_OPTIONAL_SPACE_AFTER;
127 case ESPEAKNG_PROPERTY_ELLIPSIS:
128 return CLAUSE_SEMICOLON | CLAUSE_SPEAK_PUNCTUATION_NAME | CLAUSE_OPTIONAL_SPACE_AFTER;
129 case ESPEAKNG_PROPERTY_PARAGRAPH_SEPARATOR:
130 return CLAUSE_PARAGRAPH;
131 }
132
133 return CLAUSE_NONE;
134 }
135
136 static int IsRomanU(unsigned int c)
137 {
138 if ((c == 'I') || (c == 'V') || (c == 'X') || (c == 'L'))
139 return 1;
140 return 0;
141 }
142
143 int Eof(void)
144 {
145 if (ungot_char != 0)
146 return 0;
147
148 return text_decoder_eof(p_decoder);
149 }
150
151 static int GetC(void)
152 {
153 int c1;
154
155 if ((c1 = ungot_char) != 0) {
156 ungot_char = 0;
157 return c1;
158 }
159
160 count_characters++;
161 return text_decoder_getc(p_decoder);
162 }
163
164 static void UngetC(int c)
165 {
166 ungot_char = c;
167 }
168
169 const char *WordToString2(char buf[5], unsigned int word)
170 {
171 // Convert a language mnemonic word into a string
172 int ix;
173 char *p;
174
175 p = buf;
176 for (ix = 3; ix >= 0; ix--) {
177 if ((*p = word >> (ix*8)) != 0)
178 p++;
179 }
180 *p = 0;
181 return buf;
182 }
183
184 static const char *LookupSpecial(Translator *tr, const char *string, char *text_out)
185 {
186 unsigned int flags[2];
187 char phonemes[55];
188 char *string1 = (char *)string;
189
190 flags[0] = flags[1] = 0;
191 if (LookupDictList(tr, &string1, phonemes, flags, 0, NULL)) {
192 DecodeWithPhonemeMode(text_out, phonemes, tr, NULL, flags);
193 return text_out;
194 }
195 return NULL;
196 }
197
198 static const char *LookupCharName(char buf[60], Translator *tr, int c, bool only)
199 {
200 // Find the phoneme string (in ascii) to speak the name of character c
201 // Used for punctuation characters and symbols
202
203 int ix;
204 unsigned int flags[2];
205 char single_letter[24];
206 char phonemes[60];
207 const char *lang_name = NULL;
208 char *string;
209
210 buf[0] = 0;
211 flags[0] = 0;
212 flags[1] = 0;
213 single_letter[0] = 0;
214 single_letter[1] = '_';
215 ix = utf8_out(c, &single_letter[2]);
216 single_letter[2+ix] = 0;
217
218 if (only == true) {
219 string = &single_letter[2];
220 LookupDictList(tr, &string, phonemes, flags, 0, NULL);
221 }
222
223 if (only == false) {
224 string = &single_letter[1];
225 if (LookupDictList(tr, &string, phonemes, flags, 0, NULL) == 0) {
226 // try _* then *
227 string = &single_letter[2];
228 if (LookupDictList(tr, &string, phonemes, flags, 0, NULL) == 0) {
229 // now try the rules
230 single_letter[1] = ' ';
231 TranslateRules(tr, &single_letter[2], phonemes, sizeof(phonemes), NULL, 0, NULL);
232 }
233 }
234
235 if (((phonemes[0] == 0) || (phonemes[0] == phonSWITCH)) && (tr->translator_name != L('e', 'n'))) {
236 // not found, try English
237 SetTranslator2(ESPEAKNG_DEFAULT_VOICE);
238 string = &single_letter[1];
239 single_letter[1] = '_';
240 if (LookupDictList(translator2, &string, phonemes, flags, 0, NULL) == 0) {
241 string = &single_letter[2];
242 LookupDictList(translator2, &string, phonemes, flags, 0, NULL);
243 }
244 if (phonemes[0])
245 lang_name = ESPEAKNG_DEFAULT_VOICE;
246 else
247 SelectPhonemeTable(voice->phoneme_tab_ix); // revert to original phoneme table
248 }
249 }
250
251 if (phonemes[0]) {
252 if (lang_name) {
253 DecodeWithPhonemeMode(buf, phonemes, tr, translator2, flags);
254 SelectPhonemeTable(voice->phoneme_tab_ix); // revert to original phoneme table
255 } else {
256 DecodeWithPhonemeMode(buf, phonemes, tr, NULL, flags);
257 }
258 } else if (only == false)
259 strcpy(buf, "[\002(X1)(X1)(X1)]]");
260
261 return buf;
262 }
263
264 static int AnnouncePunctuation(Translator *tr, int c1, int *c2_ptr, char *output, int *bufix, int end_clause)
265 {
266 // announce punctuation names
267 // c1: the punctuation character
268 // c2: the following character
269
270
271 const char *punctname = NULL;
272 int soundicon;
273 int attributes;
274 int short_pause;
275 int c2;
276 int len;
277 int bufix1;
278 char buf[200];
279 char ph_buf[30];
280 char cn_buf[60];
281
282 c2 = *c2_ptr;
283 buf[0] = 0;
284
285 if ((soundicon = LookupSoundicon(c1)) >= 0) {
286 // add an embedded command to play the soundicon
287 sprintf(buf, "\001%dI ", soundicon);
288 UngetC(c2);
289 } else {
290 if ((c1 == '.') && (end_clause) && (c2 != '.')) {
291 if (LookupSpecial(tr, "_.p", ph_buf))
292 punctname = ph_buf; // use word for 'period' instead of 'dot'
293 }
294 if (punctname == NULL)
295 punctname = LookupCharName(cn_buf, tr, c1, false);
296
297 if (punctname == NULL)
298 return -1;
299
300
301 if ((*bufix == 0) || (end_clause == 0) || (tr->langopts.param[LOPT_ANNOUNCE_PUNCT] & 2)) {
302 int punct_count = 1;
303 while (!Eof() && (c2 == c1) && (c1 != '<')) { // don't eat extra '<', it can miss XML tags
304 punct_count++;
305 c2 = GetC();
306 }
307 *c2_ptr = c2;
308 if (end_clause)
309 UngetC(c2);
310
311 if (punct_count == 1)
312 sprintf(buf, " %s", punctname); // we need the space before punctname, to ensure it doesn't merge with the previous word (eg. "2.-a")
313 else if (punct_count < 4) {
314 buf[0] = 0;
315 if (embedded_value[EMBED_S] < 300)
316 sprintf(buf, "\001+10S"); // Speak punctuation name faster, unless we are already speaking fast. It would upset Sonic SpeedUp
317
318 char buf2[80];
319 while (punct_count-- > 0) {
320 sprintf(buf2, " %s", punctname);
321 strcat(buf, buf2);
322 }
323
324 if (embedded_value[EMBED_S] < 300) {
325 sprintf(buf2, " \001-10S");
326 strcat(buf, buf2);
327 }
328 } else
329 sprintf(buf, " %s %d %s",
330 punctname, punct_count, punctname);
331 } else {
332 // end the clause now and pick up the punctuation next time
333 ungot_char2 = c1;
334 TerminateBufWithSpaceAndZero(buf, 0, &c2);
335 }
336 }
337
338 bufix1 = *bufix;
339 len = strlen(buf);
340 strcpy(&output[*bufix], buf);
341 *bufix += len;
342
343 if (end_clause == 0)
344 return -1;
345
346 if (c1 == '-')
347 return CLAUSE_NONE; // no pause
348
349 attributes = clause_type_from_codepoint(c1);
350
351 short_pause = CLAUSE_SHORTFALL;
352 if ((attributes & CLAUSE_INTONATION_TYPE) == 0x1000)
353 short_pause = CLAUSE_SHORTCOMMA;
354
355 if ((bufix1 > 0) && !(tr->langopts.param[LOPT_ANNOUNCE_PUNCT] & 2)) {
356 if ((attributes & ~CLAUSE_OPTIONAL_SPACE_AFTER) == CLAUSE_SEMICOLON)
357 return CLAUSE_SHORTFALL;
358 return short_pause;
359 }
360
361 if (attributes & CLAUSE_TYPE_SENTENCE)
362 return attributes;
363
364 return short_pause;
365 }
366
367 int AddNameData(const char *name, int wide)
368 {
369 // Add the name to the namedata and return its position
370 // (Used by the Windows SAPI wrapper)
371
372 int ix;
373 int len;
374
375 if (wide) {
376 len = (wcslen((const wchar_t *)name)+1)*sizeof(wchar_t);
377 n_namedata = (n_namedata + sizeof(wchar_t) - 1) % sizeof(wchar_t); // round to wchar_t boundary
378 } else
379 len = strlen(name)+1;
380
381 if (namedata_ix+len >= n_namedata) {
382 // allocate more space for marker names
383 void *vp;
384 if ((vp = realloc(namedata, namedata_ix+len + 1000)) == NULL)
385 return -1; // failed to allocate, original data is unchanged but ignore this new name
386 // !!! Bug?? If the allocated data shifts position, then pointers given to user application will be invalid
387
388 namedata = (char *)vp;
389 n_namedata = namedata_ix+len + 1000;
390 }
391 memcpy(&namedata[ix = namedata_ix], name, len);
392 namedata_ix += len;
393 return ix;
394 }
395
396 void SetVoiceStack(espeak_VOICE *v, const char *variant_name)
397 {
398 SSML_STACK *sp;
399 sp = &ssml_stack[0];
400
401 if (v == NULL) {
402 memset(sp, 0, sizeof(ssml_stack[0]));
403 return;
404 }
405 if (v->languages != NULL)
406 strcpy(sp->language, v->languages);
407 if (v->name != NULL)
408 strncpy0(sp->voice_name, v->name, sizeof(sp->voice_name));
409 sp->voice_variant_number = v->variant;
410 sp->voice_age = v->age;
411 sp->voice_gender = v->gender;
412
413 if (variant_name[0] == '!' && variant_name[1] == 'v' && variant_name[2] == PATHSEP)
414 variant_name += 3; // strip variant directory name, !v plus PATHSEP
415 strncpy0(base_voice_variant_name, variant_name, sizeof(base_voice_variant_name));
416 memcpy(&base_voice, espeak_GetCurrentVoice(), sizeof(base_voice));
417 }
418
419 static void RemoveChar(char *p)
420 {
421 // Replace a UTF-8 character by spaces
422 int c;
423
424 memset(p, ' ', utf8_in(&c, p));
425 }
426
427 static int lookupwchar2(const unsigned short *list, int c)
428 {
429 // Replace character c by another character.
430 // Returns 0 = not found, 1 = delete character
431
432 int ix;
433
434 for (ix = 0; list[ix] != 0; ix += 2) {
435 if (list[ix] == c)
436 return list[ix+1];
437 }
438 return 0;
439 }
440
441 static bool IgnoreOrReplaceChar(Translator *tr, int *c1) {
442 int i;
443 if ((i = lookupwchar2(tr->chars_ignore, *c1)) != 0) {
444 if (i == 1) {
445 // ignore this character
446 return true;
447 }
448 *c1 = i; // replace current character with the result
449 }
450 return false;
451 }
452
453 static int CheckPhonemeMode(int optionPhonemeInput, int phoneme_mode, int c1, int c2) {
454 if (optionPhonemeInput) {
455 if (phoneme_mode > 0)
456 phoneme_mode--;
457 else if ((c1 == '[') && (c2 == '['))
458 phoneme_mode = -1; // input is phoneme mnemonics, so don't look for punctuation
459 else if ((c1 == ']') && (c2 == ']'))
460 phoneme_mode = 2; // set phoneme_mode to zero after the next two characters
461
462 }
463 return phoneme_mode;
464 }
465
466 int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_buf, int *tone_type, char *voice_change)
467 {
468 /* Find the end of the current clause.
469 Write the clause into buf
470
471 returns: clause type (bits 0-7: pause x10mS, bits 8-11 intonation type)
472
473 Also checks for blank line (paragraph) as end-of-clause indicator.
474
475 Does not end clause for:
476 punctuation immediately followed by alphanumeric eg. 1.23 !Speak :path
477 repeated punctuation, eg. ... !!!
478 */
479
480 int c1 = ' '; // current character
481 int c2; // next character
482 int cprev = ' '; // previous character
483 int c_next = 0;
484 int parag;
485 int ix = 0;
486 int j;
487 int nl_count;
488 int linelength = 0;
489 int phoneme_mode = 0;
490 int n_xml_buf;
491 int terminator;
492 bool any_alnum = false;
493 int punct_data = 0;
494 bool is_end_clause;
495 int announced_punctuation = 0;
496 bool stressed_word = false;
497 int end_clause_after_tag = 0;
498 int end_clause_index = 0;
499 wchar_t xml_buf[N_XML_BUF+1];
500
501 #define N_XML_BUF2 20
502 char xml_buf2[N_XML_BUF2+2]; // for &<name> and &<number> sequences
503 static char ungot_string[N_XML_BUF2+4];
504 static int ungot_string_ix = -1;
505
506 if (clear_skipping_text) {
507 skipping_text = false;
508 clear_skipping_text = false;
509 }
510
511 tr->phonemes_repeat_count = 0;
512 tr->clause_upper_count = 0;
513 tr->clause_lower_count = 0;
514 *tone_type = 0;
515 *voice_change = 0;
516
517 if (ungot_char2 != 0) {
518 c2 = ungot_char2;
519 } else if (Eof()) {
520 c2 = 0;
521 } else {
522 c2 = GetC();
523 }
524
525 while (!Eof() || (ungot_char != 0) || (ungot_char2 != 0) || (ungot_string_ix >= 0)) {
526 if (!iswalnum_portable(c1)) {
527 if ((end_character_position > 0) && (count_characters > end_character_position)) {
528 return CLAUSE_EOF;
529 }
530
531 if ((skip_characters > 0) && (count_characters >= skip_characters)) {
532 // reached the specified start position
533 // don't break a word
534 clear_skipping_text = true;
535 skip_characters = 0;
536 UngetC(c2);
537 return CLAUSE_NONE;
538 }
539 }
540 int cprev2 = cprev;
541 cprev = c1;
542 c1 = c2;
543
544 if (ungot_string_ix >= 0) {
545 if (ungot_string[ungot_string_ix] == 0) {
546 MAKE_MEM_UNDEFINED(&ungot_string, sizeof(ungot_string));
547 ungot_string_ix = -1;
548 }
549 }
550
551 if ((ungot_string_ix == 0) && (ungot_char2 == 0))
552 c1 = ungot_string[ungot_string_ix++];
553 if (ungot_string_ix >= 0) {
554 c2 = ungot_string[ungot_string_ix++];
555 } else if (Eof()) {
556 c2 = ' ';
557 } else {
558 c2 = GetC();
559 }
560
561 ungot_char2 = 0;
562
563 if ((option_ssml) && (phoneme_mode == 0)) {
564 if ((c1 == '&') && ((c2 == '#') || ((c2 >= 'a') && (c2 <= 'z')))) {
565 n_xml_buf = 0;
566 c1 = c2;
567 while (!Eof() && (iswalnum_portable(c1) || (c1 == '#')) && (n_xml_buf < N_XML_BUF2)) {
568 xml_buf2[n_xml_buf++] = c1;
569 c1 = GetC();
570 }
571 xml_buf2[n_xml_buf] = 0;
572 if (Eof()) {
573 c2 = '\0';
574 } else {
575 c2 = GetC();
576 }
577 sprintf(ungot_string, "%s%c%c", &xml_buf2[0], c1, c2);
578
579 int found = -1;
580 if (c1 == ';') {
581 found = ParseSsmlReference(xml_buf2, &c1, &c2);
582 }
583
584 if (found <= 0) {
585 ungot_string_ix = 0;
586 c1 = '&';
587 c2 = ' ';
588 }
589
590 if ((c1 <= 0x20) && ((sayas_mode == SAYAS_SINGLE_CHARS) || (sayas_mode == SAYAS_KEY)))
591 c1 += 0xe000; // move into unicode private usage area
592 } else if (c1 == '<') {
593 if ((c2 == '/') || iswalpha_portable(c2) || c2 == '!' || c2 == '?') {
594 // check for space in the output buffer for embedded commands produced by the SSML tag
595 if (ix > (n_buf - 20)) {
596 // Perhaps not enough room, end the clause before the SSML tag
597 ungot_char2 = c1;
598 TerminateBufWithSpaceAndZero(buf, ix, &c2);
599 return CLAUSE_NONE;
600 }
601
602 // SSML Tag
603 n_xml_buf = 0;
604 c1 = c2;
605 while (!Eof() && (c1 != '>') && (n_xml_buf < N_XML_BUF)) {
606 xml_buf[n_xml_buf++] = c1;
607 c1 = GetC();
608 }
609 xml_buf[n_xml_buf] = 0;
610 c2 = ' ';
611
612 terminator = ProcessSsmlTag(xml_buf, buf, &ix, n_buf, xmlbase, &audio_text, current_voice_id, &base_voice, base_voice_variant_name, &ignore_text, &clear_skipping_text, &sayas_mode, &sayas_start, ssml_stack, &n_ssml_stack, &n_param_stack, (int *)speech_parameters);
613
614 if (terminator != 0) {
615 TerminateBufWithSpaceAndZero(buf, ix, NULL);
616
617 if (terminator & CLAUSE_TYPE_VOICE_CHANGE)
618 strcpy(voice_change, current_voice_id);
619 return terminator;
620 }
621 c1 = ' ';
622 if (!Eof()) {
623 c2 = GetC();
624 }
625 continue;
626 }
627 }
628 }
629
630 if (ignore_text)
631 continue;
632
633 if ((c2 == '\n') && (option_linelength == -1)) {
634 // single-line mode, return immediately on NL
635 if ((terminator = clause_type_from_codepoint(c1)) == CLAUSE_NONE) {
636 charix[ix] = count_characters - clause_start_char;
637 *charix_top = ix;
638 ix += utf8_out(c1, &buf[ix]);
639 terminator = CLAUSE_PERIOD; // line doesn't end in punctuation, assume period
640 }
641 TerminateBufWithSpaceAndZero(buf, ix, NULL);
642 return terminator;
643 }
644
645 if (c1 == CTRL_EMBEDDED) {
646 // an embedded command. If it's a voice change, end the clause
647 if (c2 == 'V') {
648 buf[ix++] = 0; // end the clause at this point
649 while (!Eof() && !iswspace_portable(c1 = GetC()) && (ix < (n_buf-1)))
650 buf[ix++] = c1; // add voice name to end of buffer, after the text
651 buf[ix++] = 0;
652 return CLAUSE_VOICE;
653 } else if (c2 == 'B') {
654 // set the punctuation option from an embedded command
655 // B0 B1 B<punct list><space>
656 strcpy(&buf[ix], " ");
657 ix += 3;
658
659 if (!Eof() && (c2 = GetC()) == '0')
660 option_punctuation = 0;
661 else {
662 option_punctuation = 1;
663 option_punctlist[0] = 0;
664 if (c2 != '1') {
665 // a list of punctuation characters to be spoken, terminated by space
666 j = 0;
667 while (!Eof() && !iswspace_portable(c2) && (j < N_PUNCTLIST-1)) {
668 option_punctlist[j++] = c2;
669 c2 = GetC();
670 buf[ix++] = ' ';
671 }
672 option_punctlist[j] = 0; // terminate punctuation list
673 option_punctuation = 2;
674 }
675 }
676 if (!Eof())
677 c2 = GetC();
678 continue;
679 }
680 }
681
682 linelength++;
683
684 if (IgnoreOrReplaceChar(tr, &c1) == true)
685 continue;
686
687
688 if (iswalnum_portable(c1))
689 any_alnum = true;
690 else {
691 if (stressed_word) {
692 stressed_word = false;
693 c1 = CHAR_EMPHASIS; // indicate this word is stressed
694 UngetC(c2);
695 c2 = ' ';
696 }
697
698 if (c1 == 0xf0b)
699 c1 = ' '; // Tibet inter-syllabic mark, ?? replace by space ??
700
701 if (c1 == 0xd4d) {
702 // Malayalam virama, check if next character is Zero-width-joiner
703 if (c2 == 0x200d)
704 c1 = 0xd4e; // use this unofficial code for chillu-virama
705 }
706 }
707
708 if (iswupper_portable(c1)) {
709 tr->clause_upper_count++;
710
711 if ((option_capitals == 2) && (sayas_mode == 0) && !iswupper_portable(cprev)) {
712 char text_buf[30];
713 if (LookupSpecial(tr, "_cap", text_buf) != NULL) {
714 j = strlen(text_buf);
715 if ((ix + j) < n_buf) {
716 strcpy(&buf[ix], text_buf);
717 ix += j;
718 }
719 }
720 }
721 } else if (iswalpha_portable(c1))
722 tr->clause_lower_count++;
723
724 phoneme_mode = CheckPhonemeMode(option_phoneme_input, phoneme_mode, c1, c2);
725
726 if (c1 == '\n') {
727 parag = 0;
728
729 // count consecutive newlines, ignoring other spaces
730 while (!Eof() && iswspace_portable(c2)) {
731 if (c2 == '\n')
732 parag++;
733 c2 = GetC();
734 }
735 if (parag > 0) {
736 // 2nd newline, assume paragraph
737 if (end_clause_after_tag)
738 RemoveChar(&buf[end_clause_index]); // delete clause-end punctiation
739 TerminateBufWithSpaceAndZero(buf, ix, &c2);
740 if (parag > 3)
741 parag = 3;
742 if (option_ssml) parag = 1;
743 return (CLAUSE_PARAGRAPH-30) + 30*parag; // several blank lines, longer pause
744 }
745
746 if (linelength <= option_linelength) {
747 // treat lines shorter than a specified length as end-of-clause
748 TerminateBufWithSpaceAndZero(buf, ix, &c2);
749 return CLAUSE_COLON;
750 }
751
752 linelength = 0;
753 }
754
755 announced_punctuation = 0;
756
757 if ((phoneme_mode == 0) && (sayas_mode == 0)) {
758 is_end_clause = false;
759
760 if (end_clause_after_tag) {
761 // Because of an xml tag, we are waiting for the
762 // next non-blank character to decide whether to end the clause
763 // i.e. is dot followed by an upper-case letter?
764
765 if (!iswspace_portable(c1)) {
766 if (!IsAlpha(c1) || !iswlower_portable(c1)) {
767 ungot_char2 = c1;
768 TerminateBufWithSpaceAndZero(buf, end_clause_index, &c2);
769 return end_clause_after_tag;
770 }
771 end_clause_after_tag = 0;
772 }
773 }
774
775 if ((c1 == '.') && (c2 == '.')) {
776 while (!Eof() && (c_next = GetC()) == '.') {
777 // 3 or more dots, replace by elipsis
778 c1 = 0x2026;
779 c2 = ' ';
780 }
781 if (c1 == 0x2026)
782 c2 = c_next;
783 else
784 UngetC(c_next);
785 }
786
787 punct_data = 0;
788 if ((punct_data = clause_type_from_codepoint(c1)) != CLAUSE_NONE) {
789
790 // Handling of sequences of ? and ! like ??!?, !!??!, ?!! etc
791 // Use only first char as determinant
792 if(punct_data & (CLAUSE_QUESTION | CLAUSE_EXCLAMATION)) {
793 while(!Eof() && clause_type_from_codepoint(c2) & (CLAUSE_QUESTION | CLAUSE_EXCLAMATION)) {
794 c_next = GetC();
795 c2 = c_next;
796 }
797 }
798
799 if (punct_data & CLAUSE_PUNCTUATION_IN_WORD) {
800 // Armenian punctuation inside a word
801 stressed_word = true;
802 *tone_type = punct_data >> 12 & 0xf; // override the end-of-sentence type
803 continue;
804 }
805
806 if (iswspace_portable(c2) || (punct_data & CLAUSE_OPTIONAL_SPACE_AFTER) || IsBracket(c2) || (c2 == '?') || Eof() || c2 == CTRL_EMBEDDED) { // don't check for '-' because it prevents recognizing ':-)'
807 // note: (c2='?') is for when a smart-quote has been replaced by '?'
808 is_end_clause = true;
809 }
810 }
811
812 // don't announce punctuation for the alternative text inside inside <audio> ... </audio>
813 if (c1 == 0xe000+'<') c1 = '<';
814 if (option_punctuation && iswpunct_portable(c1) && (audio_text == false)) {
815 // option is set to explicitly speak punctuation characters
816 // if a list of allowed punctuation has been set up, check whether the character is in it
817 if ((option_punctuation == 1) || (wcschr(option_punctlist, c1) != NULL)) {
818 tr->phonemes_repeat_count = 0;
819 if ((terminator = AnnouncePunctuation(tr, c1, &c2, buf, &ix, is_end_clause)) >= 0)
820 return terminator;
821 announced_punctuation = c1;
822 }
823 }
824
825 if ((punct_data & CLAUSE_SPEAK_PUNCTUATION_NAME) && (announced_punctuation == 0)) {
826 // used for elipsis (and 3 dots) if a pronunciation for elipsis is given in *_list
827 char *p2;
828
829 p2 = &buf[ix];
830 char cn_buf[60];
831 sprintf(p2, "%s", LookupCharName(cn_buf, tr, c1, true));
832 if (p2[0] != 0) {
833 ix += strlen(p2);
834 announced_punctuation = c1;
835 punct_data = punct_data & ~CLAUSE_INTONATION_TYPE; // change intonation type to 0 (full-stop)
836 }
837 }
838
839 if (is_end_clause) {
840 nl_count = 0;
841 c_next = c2;
842
843 if (iswspace_portable(c_next)) {
844 while (!Eof() && iswspace_portable(c_next)) {
845 if (c_next == '\n')
846 nl_count++;
847 c_next = GetC(); // skip past space(s)
848 }
849 }
850
851 if ((c1 == '.') && (nl_count < 2))
852 punct_data |= CLAUSE_DOT_AFTER_LAST_WORD;
853
854 if (nl_count == 0) {
855 if ((c1 == ',') && (cprev == '.') && (tr->translator_name == L('h', 'u')) && iswdigit_portable(cprev2) && (iswdigit_portable(c_next) || (iswlower_portable(c_next)))) {
856 // lang=hu, fix for ordinal numbers, eg: "december 2., szerda", ignore ',' after ordinal number
857 c1 = CHAR_COMMA_BREAK;
858 is_end_clause = false;
859 }
860
861 if (c1 == '.' && c_next == '\'' && text_decoder_peekc(p_decoder) == 's') {
862 // A special case to handle english acronym + genitive, eg. u.s.a.'s
863 // But avoid breaking clause handling if anything else follows the apostrophe.
864 is_end_clause = false;
865 }
866
867 if (c1 == '.') {
868 if ((tr->langopts.numbers & NUM_ORDINAL_DOT) &&
869 (iswdigit_portable(cprev) || (IsRomanU(cprev) && (IsRomanU(cprev2) || iswspace_portable(cprev2))))) { // lang=hu
870 // dot after a number indicates an ordinal number
871 if (!iswdigit_portable(cprev))
872 is_end_clause = false; // Roman number followed by dot
873 else if (iswlower_portable(c_next) || (c_next == '-')) // hyphen is needed for lang-hu (eg. 2.-kal)
874 is_end_clause = false; // only if followed by lower-case, (or if there is a XML tag)
875 }
876 if (iswlower_portable(c_next) && tr->langopts.lowercase_sentence == false) {
877 // next word has no capital letter, this dot is probably from an abbreviation
878 is_end_clause = false;
879 }
880 if (any_alnum == false) {
881 // no letters or digits yet, so probably not a sentence terminator
882 // Here, dot is followed by space or bracket
883 c1 = ' ';
884 is_end_clause = false;
885 }
886 } else {
887 if (any_alnum == false) {
888 // no letters or digits yet, so probably not a sentence terminator
889 is_end_clause = false;
890 }
891 }
892
893 if (is_end_clause && (c1 == '.') && (c_next == '<') && option_ssml) {
894 // wait until after the end of the xml tag, then look for upper-case letter
895 is_end_clause = false;
896 end_clause_index = ix;
897 end_clause_after_tag = punct_data;
898 }
899 }
900
901 if (is_end_clause) {
902 TerminateBufWithSpaceAndZero(buf, ix, &c_next);
903
904 if (iswdigit_portable(cprev) && !IsAlpha(c_next)) // ????
905 punct_data &= ~CLAUSE_DOT_AFTER_LAST_WORD;
906 if (nl_count > 1) {
907 if ((punct_data == CLAUSE_QUESTION) || (punct_data == CLAUSE_EXCLAMATION))
908 return punct_data + 35; // with a longer pause
909 return CLAUSE_PARAGRAPH;
910 }
911 return punct_data; // only recognise punctuation if followed by a blank or bracket/quote
912 } else if (!Eof()) {
913 if (iswspace_portable(c2))
914 UngetC(c_next);
915 }
916 }
917 }
918
919 if (speech_parameters[espeakSILENCE] == 1)
920 continue;
921
922 if (c1 == announced_punctuation) {
923 // This character has already been announced, so delete it so that it isn't spoken a second time.
924 // Unless it's a hyphen or apostrophe (which is used by TranslateClause() )
925 if (IsBracket(c1))
926 c1 = 0xe000 + '('; // Unicode private useage area. So TranslateRules() knows the bracket name has been spoken
927 else if (c1 != '-')
928 c1 = ' ';
929 }
930
931 j = ix+1;
932
933 if (c1 == 0xe000 + '<') c1 = '<';
934
935 ix += utf8_out(c1, &buf[ix]);
936 if (!iswspace_portable(c1) && !IsBracket(c1)) {
937 charix[ix] = count_characters - clause_start_char;
938 while (j < ix)
939 charix[j++] = -1; // subsequent bytes of a multibyte character
940 }
941 *charix_top = ix;
942
943 if (((ix > (n_buf-75)) && !IsAlpha(c1) && !iswdigit_portable(c1)) || (ix >= (n_buf-4))) {
944 // clause too long, getting near end of buffer, so break here
945 // try to break at a word boundary (unless we actually reach the end of buffer).
946 // (n_buf-4) is to allow for 3 bytes of multibyte character plus terminator.
947 TerminateBufWithSpaceAndZero(buf, ix, &c2);
948 return CLAUSE_NONE;
949 }
950 }
951
952 if (stressed_word)
953 ix += utf8_out(CHAR_EMPHASIS, &buf[ix]);
954 if (end_clause_after_tag)
955 RemoveChar(&buf[end_clause_index]); // delete clause-end punctiation
956 TerminateBufWithSpaceAndZero(buf, ix, NULL);
957 return CLAUSE_EOF; // end of file
958 }
959
960 void InitNamedata(void)
961 {
962 namedata_ix = 0;
963 if (namedata != NULL) {
964 free(namedata);
965 namedata = NULL;
966 n_namedata = 0;
967 }
968 }
969
970 void InitText2(void)
971 {
972 int param;
973
974 ungot_char = 0;
975 ungot_char2 = 0;
976
977 n_ssml_stack = 1;
978 MAKE_MEM_UNDEFINED(&ssml_stack[1], sizeof(ssml_stack) - sizeof(ssml_stack[0]));
979 n_param_stack = 1;
980 MAKE_MEM_UNDEFINED(&param_stack[1], sizeof(param_stack) - sizeof(param_stack[0]));
981 ssml_stack[0].tag_type = 0;
982
983 for (param = 0; param < N_SPEECH_PARAM; param++)
984 speech_parameters[param] = param_stack[0].parameter[param]; // set all speech parameters to defaults
985
986 option_punctuation = speech_parameters[espeakPUNCTUATION];
987 option_capitals = speech_parameters[espeakCAPITALS];
988
989 current_voice_id[0] = 0;
990
991 ignore_text = false;
992 audio_text = false;
993 clear_skipping_text = false;
994 count_characters = -1;
995 sayas_mode = 0;
996
997 xmlbase = NULL;
998 }
999
1000 static void TerminateBufWithSpaceAndZero(char *buf, int index, int *ungetc) {
1001 buf[index] = ' ';
1002 buf[index+1] = 0;
1003
1004 if (ungetc != NULL) {
1005 UngetC(*ungetc);
1006 }
1007 }
1008
1009 static void DecodeWithPhonemeMode(char *buf, char *phonemes, Translator *tr, Translator *tr2, unsigned int flags[]) {
1010 char phonemes2[55];
1011 if (tr2 == NULL) {
1012 SetWordStress(tr, phonemes, flags, -1, 0);
1013 DecodePhonemes(phonemes, phonemes2);
1014 sprintf(buf, "[\002%s]]", phonemes2);
1015 } else {
1016 SetWordStress(tr2, phonemes, flags, -1, 0);
1017 DecodePhonemes(phonemes, phonemes2);
1018 char wbuf[5];
1019 sprintf(buf, "[\002_^_%s %s _^_%s]]", ESPEAKNG_DEFAULT_VOICE, phonemes2, WordToString2(wbuf, tr->translator_name));
1020 }
1021 }