eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readclause.c 29KB

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