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

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