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

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