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

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