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 30KB

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