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

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