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

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