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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  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 "error.h"
  36. #include "speech.h"
  37. #include "phoneme.h"
  38. #include "voice.h"
  39. #include "synthesize.h"
  40. #include "translate.h"
  41. #include "ssml.h"
  42. #define N_XML_BUF 500
  43. static const char *xmlbase = ""; // base URL from <speak>
  44. static int namedata_ix = 0;
  45. static int n_namedata = 0;
  46. char *namedata = NULL;
  47. static int ungot_char2 = 0;
  48. espeak_ng_TEXT_DECODER *p_decoder = NULL;
  49. static int ungot_char;
  50. static const char *ungot_word = NULL;
  51. static bool ignore_text = false; // set during <sub> ... </sub> to ignore text which has been replaced by an alias
  52. static bool audio_text = false; // set during <audio> ... </audio>
  53. static bool clear_skipping_text = false; // next clause should clear the skipping_text flag
  54. int count_characters = 0;
  55. static int sayas_mode;
  56. static int sayas_start;
  57. static int ssml_ignore_l_angle = 0;
  58. #define N_SSML_STACK 20
  59. static int n_ssml_stack;
  60. static SSML_STACK ssml_stack[N_SSML_STACK];
  61. static espeak_VOICE base_voice;
  62. static char base_voice_variant_name[40] = { 0 };
  63. static char current_voice_id[40] = { 0 };
  64. static int n_param_stack;
  65. PARAM_STACK param_stack[N_PARAM_STACK];
  66. static int speech_parameters[N_SPEECH_PARAM]; // current values, from param_stack
  67. int saved_parameters[N_SPEECH_PARAM]; // Parameters saved on synthesis start
  68. #define ESPEAKNG_CLAUSE_TYPE_PROPERTY_MASK 0xFFF0000000000000ull
  69. int clause_type_from_codepoint(uint32_t c)
  70. {
  71. ucd_category cat = ucd_lookup_category(c);
  72. ucd_property props = ucd_properties(c, cat);
  73. switch (props & ESPEAKNG_CLAUSE_TYPE_PROPERTY_MASK)
  74. {
  75. case ESPEAKNG_PROPERTY_FULL_STOP:
  76. return CLAUSE_PERIOD;
  77. case ESPEAKNG_PROPERTY_FULL_STOP | ESPEAKNG_PROPERTY_OPTIONAL_SPACE_AFTER:
  78. return CLAUSE_PERIOD | CLAUSE_OPTIONAL_SPACE_AFTER;
  79. case ESPEAKNG_PROPERTY_QUESTION_MARK:
  80. return CLAUSE_QUESTION;
  81. case ESPEAKNG_PROPERTY_QUESTION_MARK | ESPEAKNG_PROPERTY_OPTIONAL_SPACE_AFTER:
  82. return CLAUSE_QUESTION | CLAUSE_OPTIONAL_SPACE_AFTER;
  83. case ESPEAKNG_PROPERTY_QUESTION_MARK | ESPEAKNG_PROPERTY_PUNCTUATION_IN_WORD:
  84. return CLAUSE_QUESTION | CLAUSE_PUNCTUATION_IN_WORD;
  85. case ESPEAKNG_PROPERTY_EXCLAMATION_MARK:
  86. return CLAUSE_EXCLAMATION;
  87. case ESPEAKNG_PROPERTY_EXCLAMATION_MARK | ESPEAKNG_PROPERTY_OPTIONAL_SPACE_AFTER:
  88. return CLAUSE_EXCLAMATION | CLAUSE_OPTIONAL_SPACE_AFTER;
  89. case ESPEAKNG_PROPERTY_EXCLAMATION_MARK | ESPEAKNG_PROPERTY_PUNCTUATION_IN_WORD:
  90. return CLAUSE_EXCLAMATION | CLAUSE_PUNCTUATION_IN_WORD;
  91. case ESPEAKNG_PROPERTY_COMMA:
  92. return CLAUSE_COMMA;
  93. case ESPEAKNG_PROPERTY_COMMA | ESPEAKNG_PROPERTY_OPTIONAL_SPACE_AFTER:
  94. return CLAUSE_COMMA | CLAUSE_OPTIONAL_SPACE_AFTER;
  95. case ESPEAKNG_PROPERTY_COLON:
  96. return CLAUSE_COLON;
  97. case ESPEAKNG_PROPERTY_COLON | ESPEAKNG_PROPERTY_OPTIONAL_SPACE_AFTER:
  98. return CLAUSE_COLON | CLAUSE_OPTIONAL_SPACE_AFTER;
  99. case ESPEAKNG_PROPERTY_SEMI_COLON:
  100. case ESPEAKNG_PROPERTY_EXTENDED_DASH:
  101. return CLAUSE_SEMICOLON;
  102. case ESPEAKNG_PROPERTY_SEMI_COLON | ESPEAKNG_PROPERTY_OPTIONAL_SPACE_AFTER:
  103. case ESPEAKNG_PROPERTY_QUESTION_MARK | ESPEAKNG_PROPERTY_OPTIONAL_SPACE_AFTER | ESPEAKNG_PROPERTY_INVERTED_TERMINAL_PUNCTUATION:
  104. case ESPEAKNG_PROPERTY_EXCLAMATION_MARK | ESPEAKNG_PROPERTY_OPTIONAL_SPACE_AFTER | ESPEAKNG_PROPERTY_INVERTED_TERMINAL_PUNCTUATION:
  105. return CLAUSE_SEMICOLON | CLAUSE_OPTIONAL_SPACE_AFTER;
  106. case ESPEAKNG_PROPERTY_ELLIPSIS:
  107. return CLAUSE_SEMICOLON | CLAUSE_SPEAK_PUNCTUATION_NAME | CLAUSE_OPTIONAL_SPACE_AFTER;
  108. case ESPEAKNG_PROPERTY_PARAGRAPH_SEPARATOR:
  109. return CLAUSE_PARAGRAPH;
  110. }
  111. return CLAUSE_NONE;
  112. }
  113. const int param_defaults[N_SPEECH_PARAM] = {
  114. 0, // silence (internal use)
  115. 175, // rate wpm
  116. 100, // volume
  117. 50, // pitch
  118. 50, // range
  119. 0, // punctuation
  120. 0, // capital letters
  121. 0, // wordgap
  122. 0, // options
  123. 0, // intonation
  124. 0,
  125. 0,
  126. 0, // emphasis
  127. 0, // line length
  128. 0, // voice type
  129. };
  130. int towlower2(unsigned int c)
  131. {
  132. // check for non-standard upper to lower case conversions
  133. if (c == 'I' && translator->langopts.dotless_i)
  134. return 0x131; // I -> ı
  135. return ucd_tolower(c);
  136. }
  137. static int IsRomanU(unsigned int c)
  138. {
  139. if ((c == 'I') || (c == 'V') || (c == 'X') || (c == 'L'))
  140. return 1;
  141. return 0;
  142. }
  143. int Eof(void)
  144. {
  145. if (ungot_char != 0)
  146. return 0;
  147. return text_decoder_eof(p_decoder);
  148. }
  149. static int GetC(void)
  150. {
  151. int c1;
  152. if ((c1 = ungot_char) != 0) {
  153. ungot_char = 0;
  154. return c1;
  155. }
  156. count_characters++;
  157. return text_decoder_getc(p_decoder);
  158. }
  159. static void UngetC(int c)
  160. {
  161. ungot_char = c;
  162. }
  163. const char *WordToString2(unsigned int word)
  164. {
  165. // Convert a language mnemonic word into a string
  166. int ix;
  167. static char buf[5];
  168. char *p;
  169. p = buf;
  170. for (ix = 3; ix >= 0; ix--) {
  171. if ((*p = word >> (ix*8)) != 0)
  172. p++;
  173. }
  174. *p = 0;
  175. return buf;
  176. }
  177. static const char *LookupSpecial(Translator *tr, const char *string, char *text_out)
  178. {
  179. unsigned int flags[2];
  180. char phonemes[55];
  181. char phonemes2[55];
  182. char *string1 = (char *)string;
  183. flags[0] = flags[1] = 0;
  184. if (LookupDictList(tr, &string1, phonemes, flags, 0, NULL)) {
  185. SetWordStress(tr, phonemes, flags, -1, 0);
  186. DecodePhonemes(phonemes, phonemes2);
  187. sprintf(text_out, "[\002%s]]", phonemes2);
  188. return text_out;
  189. }
  190. return NULL;
  191. }
  192. static const char *LookupCharName(Translator *tr, int c, int only)
  193. {
  194. // Find the phoneme string (in ascii) to speak the name of character c
  195. // Used for punctuation characters and symbols
  196. int ix;
  197. unsigned int flags[2];
  198. char single_letter[24];
  199. char phonemes[60];
  200. char phonemes2[60];
  201. const char *lang_name = NULL;
  202. char *string;
  203. static char buf[60];
  204. buf[0] = 0;
  205. flags[0] = 0;
  206. flags[1] = 0;
  207. single_letter[0] = 0;
  208. single_letter[1] = '_';
  209. ix = utf8_out(c, &single_letter[2]);
  210. single_letter[2+ix] = 0;
  211. if (only) {
  212. string = &single_letter[2];
  213. LookupDictList(tr, &string, phonemes, flags, 0, NULL);
  214. } else {
  215. string = &single_letter[1];
  216. if (LookupDictList(tr, &string, phonemes, flags, 0, NULL) == 0) {
  217. // try _* then *
  218. string = &single_letter[2];
  219. if (LookupDictList(tr, &string, phonemes, flags, 0, NULL) == 0) {
  220. // now try the rules
  221. single_letter[1] = ' ';
  222. TranslateRules(tr, &single_letter[2], phonemes, sizeof(phonemes), NULL, 0, NULL);
  223. }
  224. }
  225. }
  226. if ((only == 0) && ((phonemes[0] == 0) || (phonemes[0] == phonSWITCH)) && (tr->translator_name != L('e', 'n'))) {
  227. // not found, try English
  228. SetTranslator2("en");
  229. string = &single_letter[1];
  230. single_letter[1] = '_';
  231. if (LookupDictList(translator2, &string, phonemes, flags, 0, NULL) == 0) {
  232. string = &single_letter[2];
  233. LookupDictList(translator2, &string, phonemes, flags, 0, NULL);
  234. }
  235. if (phonemes[0])
  236. lang_name = "en";
  237. else
  238. SelectPhonemeTable(voice->phoneme_tab_ix); // revert to original phoneme table
  239. }
  240. if (phonemes[0]) {
  241. if (lang_name) {
  242. SetWordStress(translator2, phonemes, flags, -1, 0);
  243. DecodePhonemes(phonemes, phonemes2);
  244. sprintf(buf, "[\002_^_%s %s _^_%s]]", "en", phonemes2, WordToString2(tr->translator_name));
  245. SelectPhonemeTable(voice->phoneme_tab_ix); // revert to original phoneme table
  246. } else {
  247. SetWordStress(tr, phonemes, flags, -1, 0);
  248. DecodePhonemes(phonemes, phonemes2);
  249. sprintf(buf, "[\002%s]] ", phonemes2);
  250. }
  251. } else if (only == 0)
  252. strcpy(buf, "[\002(X1)(X1)(X1)]]");
  253. return buf;
  254. }
  255. int Read4Bytes(FILE *f)
  256. {
  257. // Read 4 bytes (least significant first) into a word
  258. int ix;
  259. unsigned char c;
  260. int acc = 0;
  261. for (ix = 0; ix < 4; ix++) {
  262. c = fgetc(f) & 0xff;
  263. acc += (c << (ix*8));
  264. }
  265. return acc;
  266. }
  267. static espeak_ng_STATUS LoadSoundFile(const char *fname, int index, espeak_ng_ERROR_CONTEXT *context)
  268. {
  269. FILE *f;
  270. char *p;
  271. int *ip;
  272. int length;
  273. char fname_temp[100];
  274. char fname2[sizeof(path_home)+13+40];
  275. if (fname == NULL) {
  276. // filename is already in the table
  277. fname = soundicon_tab[index].filename;
  278. }
  279. if (fname == NULL)
  280. return EINVAL;
  281. if (fname[0] != '/') {
  282. // a relative path, look in espeak-ng-data/soundicons
  283. sprintf(fname2, "%s%csoundicons%c%s", path_home, PATHSEP, PATHSEP, fname);
  284. fname = fname2;
  285. }
  286. f = NULL;
  287. if ((f = fopen(fname, "rb")) != NULL) {
  288. int ix;
  289. int fd_temp;
  290. int header[3];
  291. char command[sizeof(fname2)+sizeof(fname2)+40];
  292. if (fseek(f, 20, SEEK_SET) == -1) {
  293. int error = errno;
  294. fclose(f);
  295. return create_file_error_context(context, error, fname);
  296. }
  297. for (ix = 0; ix < 3; ix++)
  298. header[ix] = Read4Bytes(f);
  299. // if the sound file is not mono, 16 bit signed, at the correct sample rate, then convert it
  300. if ((header[0] != 0x10001) || (header[1] != samplerate) || (header[2] != samplerate*2)) {
  301. fclose(f);
  302. f = NULL;
  303. #ifdef HAVE_MKSTEMP
  304. strcpy(fname_temp, "/tmp/espeakXXXXXX");
  305. if ((fd_temp = mkstemp(fname_temp)) >= 0)
  306. close(fd_temp);
  307. #else
  308. strcpy(fname_temp, tmpnam(NULL));
  309. #endif
  310. sprintf(command, "sox \"%s\" -r %d -c1 -t wav %s\n", fname, samplerate, fname_temp);
  311. if (system(command) == 0)
  312. fname = fname_temp;
  313. }
  314. }
  315. if (f == NULL) {
  316. f = fopen(fname, "rb");
  317. if (f == NULL)
  318. return create_file_error_context(context, errno, fname);
  319. }
  320. length = GetFileLength(fname);
  321. if (length < 0) { // length == -errno
  322. fclose(f);
  323. return create_file_error_context(context, -length, fname);
  324. }
  325. if (fseek(f, 0, SEEK_SET) == -1) {
  326. int error = errno;
  327. fclose(f);
  328. return create_file_error_context(context, error, fname);
  329. }
  330. if ((p = (char *)realloc(soundicon_tab[index].data, length)) == NULL) {
  331. fclose(f);
  332. return ENOMEM;
  333. }
  334. if (fread(p, 1, length, f) != length) {
  335. int error = errno;
  336. fclose(f);
  337. remove(fname_temp);
  338. free(p);
  339. return create_file_error_context(context, error, fname);
  340. }
  341. fclose(f);
  342. remove(fname_temp);
  343. ip = (int *)(&p[40]);
  344. soundicon_tab[index].length = (*ip) / 2; // length in samples
  345. soundicon_tab[index].data = p;
  346. return ENS_OK;
  347. }
  348. static int LookupSoundicon(int c)
  349. {
  350. // Find the sound icon number for a punctuation chatacter
  351. int ix;
  352. for (ix = N_SOUNDICON_SLOTS; ix < n_soundicon_tab; ix++) {
  353. if (soundicon_tab[ix].name == c) {
  354. if (soundicon_tab[ix].length == 0) {
  355. if (LoadSoundFile(NULL, ix, NULL) != ENS_OK)
  356. return -1; // sound file is not available
  357. }
  358. return ix;
  359. }
  360. }
  361. return -1;
  362. }
  363. static int LoadSoundFile2(const char *fname)
  364. {
  365. // Load a sound file into one of the reserved slots in the sound icon table
  366. // (if it'snot already loaded)
  367. int ix;
  368. static int slot = -1;
  369. for (ix = 0; ix < n_soundicon_tab; ix++) {
  370. if (((soundicon_tab[ix].filename != NULL) && strcmp(fname, soundicon_tab[ix].filename) == 0))
  371. return ix; // already loaded
  372. }
  373. // load the file into the next slot
  374. slot++;
  375. if (slot >= N_SOUNDICON_SLOTS)
  376. slot = 0;
  377. if (LoadSoundFile(fname, slot, NULL) != ENS_OK)
  378. return -1;
  379. soundicon_tab[slot].filename = (char *)realloc(soundicon_tab[ix].filename, strlen(fname)+1);
  380. strcpy(soundicon_tab[slot].filename, fname);
  381. return slot;
  382. }
  383. static int AnnouncePunctuation(Translator *tr, int c1, int *c2_ptr, char *output, int *bufix, int end_clause)
  384. {
  385. // announce punctuation names
  386. // c1: the punctuation character
  387. // c2: the following character
  388. int punct_count;
  389. const char *punctname = NULL;
  390. int soundicon;
  391. int attributes;
  392. int short_pause;
  393. int c2;
  394. int len;
  395. int bufix1;
  396. char buf[200];
  397. char buf2[80];
  398. char ph_buf[30];
  399. c2 = *c2_ptr;
  400. buf[0] = 0;
  401. if ((soundicon = LookupSoundicon(c1)) >= 0) {
  402. // add an embedded command to play the soundicon
  403. sprintf(buf, "\001%dI ", soundicon);
  404. UngetC(c2);
  405. } else {
  406. if ((c1 == '.') && (end_clause) && (c2 != '.')) {
  407. if (LookupSpecial(tr, "_.p", ph_buf))
  408. punctname = ph_buf; // use word for 'period' instead of 'dot'
  409. }
  410. if (punctname == NULL)
  411. punctname = LookupCharName(tr, c1, 0);
  412. if (punctname == NULL)
  413. return -1;
  414. if ((*bufix == 0) || (end_clause == 0) || (tr->langopts.param[LOPT_ANNOUNCE_PUNCT] & 2)) {
  415. punct_count = 1;
  416. while ((c2 == c1) && (c1 != '<')) { // don't eat extra '<', it can miss XML tags
  417. punct_count++;
  418. c2 = GetC();
  419. }
  420. *c2_ptr = c2;
  421. if (end_clause)
  422. UngetC(c2);
  423. if (punct_count == 1)
  424. sprintf(buf, " %s", punctname); // we need the space before punctname, to ensure it doesn't merge with the previous word (eg. "2.-a")
  425. else if (punct_count < 4) {
  426. buf[0] = 0;
  427. if (embedded_value[EMBED_S] < 300)
  428. sprintf(buf, "\001+10S"); // Speak punctuation name faster, unless we are already speaking fast. It would upset Sonic SpeedUp
  429. while (punct_count-- > 0) {
  430. sprintf(buf2, " %s", punctname);
  431. strcat(buf, buf2);
  432. }
  433. if (embedded_value[EMBED_S] < 300) {
  434. sprintf(buf2, " \001-10S");
  435. strcat(buf, buf2);
  436. }
  437. } else
  438. sprintf(buf, " %s %d %s",
  439. punctname, punct_count, punctname);
  440. } else {
  441. // end the clause now and pick up the punctuation next time
  442. UngetC(c2);
  443. if (option_ssml) {
  444. if ((c1 == '<') || (c1 == '&'))
  445. ssml_ignore_l_angle = c1; // this was &lt; which was converted to <, don't pick it up again as <
  446. }
  447. ungot_char2 = c1;
  448. buf[0] = ' ';
  449. buf[1] = 0;
  450. }
  451. }
  452. bufix1 = *bufix;
  453. len = strlen(buf);
  454. strcpy(&output[*bufix], buf);
  455. *bufix += len;
  456. if (end_clause == 0)
  457. return -1;
  458. if (c1 == '-')
  459. return CLAUSE_NONE; // no pause
  460. attributes = clause_type_from_codepoint(c1);
  461. short_pause = CLAUSE_SHORTFALL;
  462. if ((attributes & CLAUSE_INTONATION_TYPE) == 0x1000)
  463. short_pause = CLAUSE_SHORTCOMMA;
  464. if ((bufix1 > 0) && !(tr->langopts.param[LOPT_ANNOUNCE_PUNCT] & 2)) {
  465. if ((attributes & ~CLAUSE_OPTIONAL_SPACE_AFTER) == CLAUSE_SEMICOLON)
  466. return CLAUSE_SHORTFALL;
  467. return short_pause;
  468. }
  469. if (attributes & CLAUSE_TYPE_SENTENCE)
  470. return attributes;
  471. return short_pause;
  472. }
  473. static int AddNameData(const char *name, int wide)
  474. {
  475. // Add the name to the namedata and return its position
  476. // (Used by the Windows SAPI wrapper)
  477. int ix;
  478. int len;
  479. void *vp;
  480. if (wide) {
  481. len = (wcslen((const wchar_t *)name)+1)*sizeof(wchar_t);
  482. n_namedata = (n_namedata + sizeof(wchar_t) - 1) % sizeof(wchar_t); // round to wchar_t boundary
  483. } else
  484. len = strlen(name)+1;
  485. if (namedata_ix+len >= n_namedata) {
  486. // allocate more space for marker names
  487. if ((vp = realloc(namedata, namedata_ix+len + 1000)) == NULL)
  488. return -1; // failed to allocate, original data is unchanged but ignore this new name
  489. // !!! Bug?? If the allocated data shifts position, then pointers given to user application will be invalid
  490. namedata = (char *)vp;
  491. n_namedata = namedata_ix+len + 1000;
  492. }
  493. memcpy(&namedata[ix = namedata_ix], name, len);
  494. namedata_ix += len;
  495. return ix;
  496. }
  497. void SetVoiceStack(espeak_VOICE *v, const char *variant_name)
  498. {
  499. SSML_STACK *sp;
  500. sp = &ssml_stack[0];
  501. if (v == NULL) {
  502. memset(sp, 0, sizeof(ssml_stack[0]));
  503. return;
  504. }
  505. if (v->languages != NULL)
  506. strcpy(sp->language, v->languages);
  507. if (v->name != NULL)
  508. strncpy0(sp->voice_name, v->name, sizeof(sp->voice_name));
  509. sp->voice_variant_number = v->variant;
  510. sp->voice_age = v->age;
  511. sp->voice_gender = v->gender;
  512. if (memcmp(variant_name, "!v", 2) == 0)
  513. variant_name += 3; // strip variant directory name, !v plus PATHSEP
  514. strncpy0(base_voice_variant_name, variant_name, sizeof(base_voice_variant_name));
  515. memcpy(&base_voice, &current_voice_selected, sizeof(base_voice));
  516. }
  517. static void SetProsodyParameter(int param_type, wchar_t *attr1, PARAM_STACK *sp)
  518. {
  519. int value;
  520. int sign;
  521. static const MNEM_TAB mnem_volume[] = {
  522. { "default", 100 },
  523. { "silent", 0 },
  524. { "x-soft", 30 },
  525. { "soft", 65 },
  526. { "medium", 100 },
  527. { "loud", 150 },
  528. { "x-loud", 230 },
  529. { NULL, -1 }
  530. };
  531. static const MNEM_TAB mnem_rate[] = {
  532. { "default", 100 },
  533. { "x-slow", 60 },
  534. { "slow", 80 },
  535. { "medium", 100 },
  536. { "fast", 125 },
  537. { "x-fast", 160 },
  538. { NULL, -1 }
  539. };
  540. static const MNEM_TAB mnem_pitch[] = {
  541. { "default", 100 },
  542. { "x-low", 70 },
  543. { "low", 85 },
  544. { "medium", 100 },
  545. { "high", 110 },
  546. { "x-high", 120 },
  547. { NULL, -1 }
  548. };
  549. static const MNEM_TAB mnem_range[] = {
  550. { "default", 100 },
  551. { "x-low", 20 },
  552. { "low", 50 },
  553. { "medium", 100 },
  554. { "high", 140 },
  555. { "x-high", 180 },
  556. { NULL, -1 }
  557. };
  558. static const MNEM_TAB *mnem_tabs[5] = {
  559. NULL, mnem_rate, mnem_volume, mnem_pitch, mnem_range
  560. };
  561. if ((value = attrlookup(attr1, mnem_tabs[param_type])) >= 0) {
  562. // mnemonic specifies a value as a percentage of the base pitch/range/rate/volume
  563. sp->parameter[param_type] = (param_stack[0].parameter[param_type] * value)/100;
  564. } else {
  565. sign = attr_prosody_value(param_type, attr1, &value);
  566. if (sign == 0)
  567. sp->parameter[param_type] = value; // absolute value in Hz
  568. else if (sign == 2) {
  569. // change specified as percentage or in semitones
  570. sp->parameter[param_type] = (speech_parameters[param_type] * value)/100;
  571. } else {
  572. // change specified as plus or minus Hz
  573. sp->parameter[param_type] = speech_parameters[param_type] + (value*sign);
  574. }
  575. }
  576. }
  577. static int ProcessSsmlTag(wchar_t *xml_buf, char *outbuf, int *outix, int n_outbuf, bool self_closing)
  578. {
  579. // xml_buf is the tag and attributes with a zero terminator in place of the original '>'
  580. // returns a clause terminator value.
  581. unsigned int ix;
  582. int index;
  583. int c;
  584. int tag_type;
  585. int value;
  586. int value2;
  587. int value3;
  588. int voice_change_flag;
  589. wchar_t *px;
  590. wchar_t *attr1;
  591. wchar_t *attr2;
  592. wchar_t *attr3;
  593. int terminator;
  594. char *uri;
  595. int param_type;
  596. char tag_name[40];
  597. char buf[80];
  598. PARAM_STACK *sp;
  599. SSML_STACK *ssml_sp;
  600. static const MNEM_TAB mnem_phoneme_alphabet[] = {
  601. { "espeak", 1 },
  602. { NULL, -1 }
  603. };
  604. static const MNEM_TAB mnem_punct[] = {
  605. { "none", 1 },
  606. { "all", 2 },
  607. { "some", 3 },
  608. { NULL, -1 }
  609. };
  610. static const MNEM_TAB mnem_capitals[] = {
  611. { "no", 0 },
  612. { "icon", 1 },
  613. { "spelling", 2 },
  614. { "pitch", 20 }, // this is the amount by which to raise the pitch
  615. { NULL, -1 }
  616. };
  617. static const MNEM_TAB mnem_interpret_as[] = {
  618. { "characters", SAYAS_CHARS },
  619. { "tts:char", SAYAS_SINGLE_CHARS },
  620. { "tts:key", SAYAS_KEY },
  621. { "tts:digits", SAYAS_DIGITS },
  622. { "telephone", SAYAS_DIGITS1 },
  623. { NULL, -1 }
  624. };
  625. static const MNEM_TAB mnem_sayas_format[] = {
  626. { "glyphs", 1 },
  627. { NULL, -1 }
  628. };
  629. static const MNEM_TAB mnem_break[] = {
  630. { "none", 0 },
  631. { "x-weak", 1 },
  632. { "weak", 2 },
  633. { "medium", 3 },
  634. { "strong", 4 },
  635. { "x-strong", 5 },
  636. { NULL, -1 }
  637. };
  638. static const MNEM_TAB mnem_emphasis[] = {
  639. { "none", 1 },
  640. { "reduced", 2 },
  641. { "moderate", 3 },
  642. { "strong", 4 },
  643. { "x-strong", 5 },
  644. { NULL, -1 }
  645. };
  646. static const char *prosody_attr[5] = {
  647. NULL, "rate", "volume", "pitch", "range"
  648. };
  649. for (ix = 0; ix < (sizeof(tag_name)-1); ix++) {
  650. if (((c = xml_buf[ix]) == 0) || iswspace(c))
  651. break;
  652. tag_name[ix] = tolower((char)c);
  653. }
  654. tag_name[ix] = 0;
  655. px = &xml_buf[ix]; // the tag's attributes
  656. if (tag_name[0] == '/') {
  657. // closing tag
  658. if ((tag_type = LookupMnem(ssmltags, &tag_name[1])) != HTML_NOSPACE)
  659. outbuf[(*outix)++] = ' ';
  660. tag_type += SSML_CLOSE;
  661. } else {
  662. if ((tag_type = LookupMnem(ssmltags, tag_name)) != HTML_NOSPACE) {
  663. // separate SSML tags from the previous word (but not HMTL tags such as <b> <font> which can occur inside a word)
  664. outbuf[(*outix)++] = ' ';
  665. }
  666. if (self_closing && ignore_if_self_closing[tag_type])
  667. return 0;
  668. }
  669. voice_change_flag = 0;
  670. ssml_sp = &ssml_stack[n_ssml_stack-1];
  671. switch (tag_type)
  672. {
  673. case SSML_STYLE:
  674. sp = PushParamStack(tag_type, &n_param_stack, (PARAM_STACK *) param_stack);
  675. attr1 = GetSsmlAttribute(px, "field");
  676. attr2 = GetSsmlAttribute(px, "mode");
  677. if (attrcmp(attr1, "punctuation") == 0) {
  678. value = attrlookup(attr2, mnem_punct);
  679. sp->parameter[espeakPUNCTUATION] = value;
  680. } else if (attrcmp(attr1, "capital_letters") == 0) {
  681. value = attrlookup(attr2, mnem_capitals);
  682. sp->parameter[espeakCAPITALS] = value;
  683. }
  684. ProcessParamStack(outbuf, outix, n_param_stack, param_stack, speech_parameters);
  685. break;
  686. case SSML_PROSODY:
  687. sp = PushParamStack(tag_type, &n_param_stack, (PARAM_STACK *) param_stack);
  688. // look for attributes: rate, volume, pitch, range
  689. for (param_type = espeakRATE; param_type <= espeakRANGE; param_type++) {
  690. if ((attr1 = GetSsmlAttribute(px, prosody_attr[param_type])) != NULL)
  691. SetProsodyParameter(param_type, attr1, sp);
  692. }
  693. ProcessParamStack(outbuf, outix, n_param_stack, param_stack, speech_parameters);
  694. break;
  695. case SSML_EMPHASIS:
  696. sp = PushParamStack(tag_type, &n_param_stack, (PARAM_STACK *) param_stack);
  697. value = 3; // default is "moderate"
  698. if ((attr1 = GetSsmlAttribute(px, "level")) != NULL)
  699. value = attrlookup(attr1, mnem_emphasis);
  700. if (translator->langopts.tone_language == 1) {
  701. static unsigned char emphasis_to_pitch_range[] = { 50, 50, 40, 70, 90, 100 };
  702. static unsigned char emphasis_to_volume[] = { 100, 100, 70, 110, 135, 150 };
  703. // tone language (eg.Chinese) do emphasis by increasing the pitch range.
  704. sp->parameter[espeakRANGE] = emphasis_to_pitch_range[value];
  705. sp->parameter[espeakVOLUME] = emphasis_to_volume[value];
  706. } else {
  707. static unsigned char emphasis_to_volume2[] = { 100, 100, 75, 100, 120, 150 };
  708. sp->parameter[espeakVOLUME] = emphasis_to_volume2[value];
  709. sp->parameter[espeakEMPHASIS] = value;
  710. }
  711. ProcessParamStack(outbuf, outix, n_param_stack, param_stack, speech_parameters);
  712. break;
  713. case SSML_STYLE + SSML_CLOSE:
  714. case SSML_PROSODY + SSML_CLOSE:
  715. case SSML_EMPHASIS + SSML_CLOSE:
  716. PopParamStack(tag_type, outbuf, outix, &n_param_stack, (PARAM_STACK *) param_stack, (int *) speech_parameters);
  717. break;
  718. case SSML_PHONEME:
  719. attr1 = GetSsmlAttribute(px, "alphabet");
  720. attr2 = GetSsmlAttribute(px, "ph");
  721. value = attrlookup(attr1, mnem_phoneme_alphabet);
  722. if (value == 1) { // alphabet="espeak"
  723. outbuf[(*outix)++] = '[';
  724. outbuf[(*outix)++] = '[';
  725. *outix += attrcopy_utf8(&outbuf[*outix], attr2, n_outbuf-*outix);
  726. outbuf[(*outix)++] = ']';
  727. outbuf[(*outix)++] = ']';
  728. }
  729. break;
  730. case SSML_SAYAS:
  731. attr1 = GetSsmlAttribute(px, "interpret-as");
  732. attr2 = GetSsmlAttribute(px, "format");
  733. attr3 = GetSsmlAttribute(px, "detail");
  734. value = attrlookup(attr1, mnem_interpret_as);
  735. value2 = attrlookup(attr2, mnem_sayas_format);
  736. if (value2 == 1)
  737. value = SAYAS_GLYPHS;
  738. value3 = attrnumber(attr3, 0, 0);
  739. if (value == SAYAS_DIGITS) {
  740. if (value3 <= 1)
  741. value = SAYAS_DIGITS1;
  742. else
  743. value = SAYAS_DIGITS + value3;
  744. }
  745. sprintf(buf, "%c%dY", CTRL_EMBEDDED, value);
  746. strcpy(&outbuf[*outix], buf);
  747. *outix += strlen(buf);
  748. sayas_start = *outix;
  749. sayas_mode = value; // punctuation doesn't end clause during SAY-AS
  750. break;
  751. case SSML_SAYAS + SSML_CLOSE:
  752. if (sayas_mode == SAYAS_KEY) {
  753. outbuf[*outix] = 0;
  754. ReplaceKeyName(outbuf, sayas_start, outix);
  755. }
  756. outbuf[(*outix)++] = CTRL_EMBEDDED;
  757. outbuf[(*outix)++] = 'Y';
  758. sayas_mode = 0;
  759. break;
  760. case SSML_SUB:
  761. if ((attr1 = GetSsmlAttribute(px, "alias")) != NULL) {
  762. // use the alias rather than the text
  763. ignore_text = true;
  764. *outix += attrcopy_utf8(&outbuf[*outix], attr1, n_outbuf-*outix);
  765. }
  766. break;
  767. case SSML_IGNORE_TEXT:
  768. ignore_text = true;
  769. break;
  770. case SSML_SUB + SSML_CLOSE:
  771. case SSML_IGNORE_TEXT + SSML_CLOSE:
  772. ignore_text = false;
  773. break;
  774. case SSML_MARK:
  775. if ((attr1 = GetSsmlAttribute(px, "name")) != NULL) {
  776. // add name to circular buffer of marker names
  777. attrcopy_utf8(buf, attr1, sizeof(buf));
  778. if (strcmp(skip_marker, buf) == 0) {
  779. // This is the marker we are waiting for before starting to speak
  780. clear_skipping_text = true;
  781. skip_marker[0] = 0;
  782. return CLAUSE_NONE;
  783. }
  784. if ((index = AddNameData(buf, 0)) >= 0) {
  785. sprintf(buf, "%c%dM", CTRL_EMBEDDED, index);
  786. strcpy(&outbuf[*outix], buf);
  787. *outix += strlen(buf);
  788. }
  789. }
  790. break;
  791. case SSML_AUDIO:
  792. sp = PushParamStack(tag_type, &n_param_stack, (PARAM_STACK *)param_stack);
  793. if ((attr1 = GetSsmlAttribute(px, "src")) != NULL) {
  794. char fname[256];
  795. attrcopy_utf8(buf, attr1, sizeof(buf));
  796. if (uri_callback == NULL) {
  797. if ((xmlbase != NULL) && (buf[0] != '/')) {
  798. sprintf(fname, "%s/%s", xmlbase, buf);
  799. index = LoadSoundFile2(fname);
  800. } else
  801. index = LoadSoundFile2(buf);
  802. if (index >= 0) {
  803. sprintf(buf, "%c%dI", CTRL_EMBEDDED, index);
  804. strcpy(&outbuf[*outix], buf);
  805. *outix += strlen(buf);
  806. sp->parameter[espeakSILENCE] = 1;
  807. }
  808. } else {
  809. if ((index = AddNameData(buf, 0)) >= 0) {
  810. uri = &namedata[index];
  811. if (uri_callback(1, uri, xmlbase) == 0) {
  812. sprintf(buf, "%c%dU", CTRL_EMBEDDED, index);
  813. strcpy(&outbuf[*outix], buf);
  814. *outix += strlen(buf);
  815. sp->parameter[espeakSILENCE] = 1;
  816. }
  817. }
  818. }
  819. }
  820. ProcessParamStack(outbuf, outix, n_param_stack, param_stack, speech_parameters);
  821. if (self_closing)
  822. PopParamStack(tag_type, outbuf, outix, &n_param_stack, (PARAM_STACK *) param_stack, (int *) speech_parameters);
  823. else
  824. audio_text = true;
  825. return CLAUSE_NONE;
  826. case SSML_AUDIO + SSML_CLOSE:
  827. PopParamStack(tag_type, outbuf, outix, &n_param_stack, (PARAM_STACK *) param_stack, (int *) speech_parameters);
  828. audio_text = false;
  829. return CLAUSE_NONE;
  830. case SSML_BREAK:
  831. value = 21;
  832. terminator = CLAUSE_NONE;
  833. if ((attr1 = GetSsmlAttribute(px, "strength")) != NULL) {
  834. static int break_value[6] = { 0, 7, 14, 21, 40, 80 }; // *10mS
  835. value = attrlookup(attr1, mnem_break);
  836. if (value < 3) {
  837. // adjust prepause on the following word
  838. sprintf(&outbuf[*outix], "%c%dB", CTRL_EMBEDDED, value);
  839. *outix += 3;
  840. terminator = 0;
  841. }
  842. value = break_value[value];
  843. }
  844. if ((attr2 = GetSsmlAttribute(px, "time")) != NULL) {
  845. value2 = attrnumber(attr2, 0, 1); // pause in mS
  846. // compensate for speaking speed to keep constant pause length, see function PauseLength()
  847. // 'value' here is x 10mS
  848. value = (value2 * 256) / (speed.clause_pause_factor * 10);
  849. if (value < 200)
  850. value = (value2 * 256) / (speed.pause_factor * 10);
  851. if (terminator == 0)
  852. terminator = CLAUSE_NONE;
  853. }
  854. if (terminator) {
  855. if (value > 0xfff) {
  856. // scale down the value and set a scaling indicator bit
  857. value = value / 32;
  858. if (value > 0xfff)
  859. value = 0xfff;
  860. terminator |= CLAUSE_PAUSE_LONG;
  861. }
  862. return terminator + value;
  863. }
  864. break;
  865. case SSML_SPEAK:
  866. if ((attr1 = GetSsmlAttribute(px, "xml:base")) != NULL) {
  867. attrcopy_utf8(buf, attr1, sizeof(buf));
  868. if ((index = AddNameData(buf, 0)) >= 0)
  869. xmlbase = &namedata[index];
  870. }
  871. if (GetVoiceAttributes(px, tag_type, ssml_sp, ssml_stack, n_ssml_stack, current_voice_id, &base_voice, base_voice_variant_name) == 0)
  872. return 0; // no voice change
  873. return CLAUSE_VOICE;
  874. case SSML_VOICE:
  875. if (GetVoiceAttributes(px, tag_type, ssml_sp, ssml_stack, n_ssml_stack, current_voice_id, &base_voice, base_voice_variant_name) == 0)
  876. return 0; // no voice change
  877. return CLAUSE_VOICE;
  878. case SSML_SPEAK + SSML_CLOSE:
  879. // unwind stack until the previous <voice> or <speak> tag
  880. while ((n_ssml_stack > 1) && (ssml_stack[n_ssml_stack-1].tag_type != SSML_SPEAK))
  881. n_ssml_stack--;
  882. return CLAUSE_PERIOD + GetVoiceAttributes(px, tag_type, ssml_sp, ssml_stack, n_ssml_stack, current_voice_id, &base_voice, base_voice_variant_name);
  883. case SSML_VOICE + SSML_CLOSE:
  884. // unwind stack until the previous <voice> or <speak> tag
  885. while ((n_ssml_stack > 1) && (ssml_stack[n_ssml_stack-1].tag_type != SSML_VOICE))
  886. n_ssml_stack--;
  887. terminator = 0; // ?? Sentence intonation, but no pause ??
  888. return terminator + GetVoiceAttributes(px, tag_type, ssml_sp, ssml_stack, n_ssml_stack, current_voice_id, &base_voice, base_voice_variant_name);
  889. case HTML_BREAK:
  890. case HTML_BREAK + SSML_CLOSE:
  891. return CLAUSE_COLON;
  892. case SSML_SENTENCE:
  893. if (ssml_sp->tag_type == SSML_SENTENCE) {
  894. // new sentence implies end-of-sentence
  895. voice_change_flag = GetVoiceAttributes(px, SSML_SENTENCE+SSML_CLOSE, ssml_sp, ssml_stack, n_ssml_stack, current_voice_id, &base_voice, base_voice_variant_name);
  896. }
  897. voice_change_flag |= GetVoiceAttributes(px, tag_type, ssml_sp, ssml_stack, n_ssml_stack, current_voice_id, &base_voice, base_voice_variant_name);
  898. return CLAUSE_PARAGRAPH + voice_change_flag;
  899. case SSML_PARAGRAPH:
  900. if (ssml_sp->tag_type == SSML_SENTENCE) {
  901. // new paragraph implies end-of-sentence or end-of-paragraph
  902. voice_change_flag = GetVoiceAttributes(px, SSML_SENTENCE+SSML_CLOSE, ssml_sp, ssml_stack, n_ssml_stack, current_voice_id, &base_voice, base_voice_variant_name);
  903. }
  904. if (ssml_sp->tag_type == SSML_PARAGRAPH) {
  905. // new paragraph implies end-of-sentence or end-of-paragraph
  906. voice_change_flag |= GetVoiceAttributes(px, SSML_PARAGRAPH+SSML_CLOSE, ssml_sp, ssml_stack, n_ssml_stack, current_voice_id, &base_voice, base_voice_variant_name);
  907. }
  908. voice_change_flag |= GetVoiceAttributes(px, tag_type, ssml_sp, ssml_stack, n_ssml_stack, current_voice_id, &base_voice, base_voice_variant_name);
  909. return CLAUSE_PARAGRAPH + voice_change_flag;
  910. case SSML_SENTENCE + SSML_CLOSE:
  911. if (ssml_sp->tag_type == SSML_SENTENCE) {
  912. // end of a sentence which specified a language
  913. voice_change_flag = GetVoiceAttributes(px, tag_type, ssml_sp, ssml_stack, n_ssml_stack, current_voice_id, &base_voice, base_voice_variant_name);
  914. }
  915. return CLAUSE_PERIOD + voice_change_flag;
  916. case SSML_PARAGRAPH + SSML_CLOSE:
  917. if ((ssml_sp->tag_type == SSML_SENTENCE) || (ssml_sp->tag_type == SSML_PARAGRAPH)) {
  918. // End of a paragraph which specified a language.
  919. // (End-of-paragraph also implies end-of-sentence)
  920. return GetVoiceAttributes(px, tag_type, ssml_sp, ssml_stack, n_ssml_stack, current_voice_id, &base_voice, base_voice_variant_name) + CLAUSE_PARAGRAPH;
  921. }
  922. return CLAUSE_PARAGRAPH;
  923. }
  924. return 0;
  925. }
  926. static void RemoveChar(char *p)
  927. {
  928. // Replace a UTF-8 character by spaces
  929. int c;
  930. memset(p, ' ', utf8_in(&c, p));
  931. }
  932. static MNEM_TAB xml_char_mnemonics[] = {
  933. { "gt", '>' },
  934. { "lt", 0xe000 + '<' }, // private usage area, to avoid confusion with XML tag
  935. { "amp", '&' },
  936. { "quot", '"' },
  937. { "nbsp", ' ' },
  938. { "apos", '\'' },
  939. { NULL, -1 }
  940. };
  941. int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_buf, int *tone_type, char *voice_change)
  942. {
  943. /* Find the end of the current clause.
  944. Write the clause into buf
  945. returns: clause type (bits 0-7: pause x10mS, bits 8-11 intonation type)
  946. Also checks for blank line (paragraph) as end-of-clause indicator.
  947. Does not end clause for:
  948. punctuation immediately followed by alphanumeric eg. 1.23 !Speak :path
  949. repeated punctuation, eg. ... !!!
  950. */
  951. int c1 = ' '; // current character
  952. int c2; // next character
  953. int cprev = ' '; // previous character
  954. int cprev2 = ' ';
  955. int c_next;
  956. int parag;
  957. int ix = 0;
  958. int j;
  959. int nl_count;
  960. int linelength = 0;
  961. int phoneme_mode = 0;
  962. int n_xml_buf;
  963. int terminator;
  964. int found;
  965. bool any_alnum = false;
  966. bool self_closing;
  967. int punct_data = 0;
  968. bool is_end_clause;
  969. int announced_punctuation = 0;
  970. bool stressed_word = false;
  971. int end_clause_after_tag = 0;
  972. int end_clause_index = 0;
  973. wchar_t xml_buf[N_XML_BUF+1];
  974. #define N_XML_BUF2 20
  975. char xml_buf2[N_XML_BUF2+2]; // for &<name> and &<number> sequences
  976. static char ungot_string[N_XML_BUF2+4];
  977. static int ungot_string_ix = -1;
  978. if (clear_skipping_text) {
  979. skipping_text = false;
  980. clear_skipping_text = false;
  981. }
  982. tr->phonemes_repeat_count = 0;
  983. tr->clause_upper_count = 0;
  984. tr->clause_lower_count = 0;
  985. *tone_type = 0;
  986. *voice_change = 0;
  987. if (ungot_word != NULL) {
  988. strcpy(buf, ungot_word);
  989. ix += strlen(ungot_word);
  990. ungot_word = NULL;
  991. }
  992. if (ungot_char2 != 0)
  993. c2 = ungot_char2;
  994. else
  995. c2 = GetC();
  996. while (!Eof() || (ungot_char != 0) || (ungot_char2 != 0) || (ungot_string_ix >= 0)) {
  997. if (!iswalnum(c1)) {
  998. if ((end_character_position > 0) && (count_characters > end_character_position)) {
  999. return CLAUSE_EOF;
  1000. }
  1001. if ((skip_characters > 0) && (count_characters >= skip_characters)) {
  1002. // reached the specified start position
  1003. // don't break a word
  1004. clear_skipping_text = true;
  1005. skip_characters = 0;
  1006. UngetC(c2);
  1007. return CLAUSE_NONE;
  1008. }
  1009. }
  1010. cprev2 = cprev;
  1011. cprev = c1;
  1012. c1 = c2;
  1013. if (ungot_string_ix >= 0) {
  1014. if (ungot_string[ungot_string_ix] == 0)
  1015. ungot_string_ix = -1;
  1016. }
  1017. if ((ungot_string_ix == 0) && (ungot_char2 == 0))
  1018. c1 = ungot_string[ungot_string_ix++];
  1019. if (ungot_string_ix >= 0)
  1020. c2 = ungot_string[ungot_string_ix++];
  1021. else {
  1022. c2 = GetC();
  1023. if (Eof())
  1024. c2 = ' ';
  1025. }
  1026. ungot_char2 = 0;
  1027. if ((option_ssml) && (phoneme_mode == 0)) {
  1028. if ((ssml_ignore_l_angle != '&') && (c1 == '&') && ((c2 == '#') || ((c2 >= 'a') && (c2 <= 'z')))) {
  1029. n_xml_buf = 0;
  1030. c1 = c2;
  1031. while (!Eof() && (iswalnum(c1) || (c1 == '#')) && (n_xml_buf < N_XML_BUF2)) {
  1032. xml_buf2[n_xml_buf++] = c1;
  1033. c1 = GetC();
  1034. }
  1035. xml_buf2[n_xml_buf] = 0;
  1036. c2 = GetC();
  1037. sprintf(ungot_string, "%s%c%c", &xml_buf2[0], c1, c2);
  1038. if (c1 == ';') {
  1039. if (xml_buf2[0] == '#') {
  1040. // character code number
  1041. if (xml_buf2[1] == 'x')
  1042. found = sscanf(&xml_buf2[2], "%x", (unsigned int *)(&c1));
  1043. else
  1044. found = sscanf(&xml_buf2[1], "%d", &c1);
  1045. } else {
  1046. if ((found = LookupMnem(xml_char_mnemonics, xml_buf2)) != -1) {
  1047. c1 = found;
  1048. if (c2 == 0)
  1049. c2 = ' ';
  1050. }
  1051. }
  1052. } else
  1053. found = -1;
  1054. if (found <= 0) {
  1055. ungot_string_ix = 0;
  1056. c1 = '&';
  1057. c2 = ' ';
  1058. }
  1059. if ((c1 <= 0x20) && ((sayas_mode == SAYAS_SINGLE_CHARS) || (sayas_mode == SAYAS_KEY)))
  1060. c1 += 0xe000; // move into unicode private usage area
  1061. } else if ((c1 == '<') && (ssml_ignore_l_angle != '<')) {
  1062. if ((c2 == '!') || (c2 == '?')) {
  1063. // a comment, ignore until closing '<' (or <?xml tag )
  1064. while (!Eof() && (c1 != '>'))
  1065. c1 = GetC();
  1066. c2 = ' ';
  1067. } else if ((c2 == '/') || iswalpha(c2)) {
  1068. // check for space in the output buffer for embedded commands produced by the SSML tag
  1069. if (ix > (n_buf - 20)) {
  1070. // Perhaps not enough room, end the clause before the SSML tag
  1071. UngetC(c2);
  1072. ungot_char2 = c1;
  1073. buf[ix] = ' ';
  1074. buf[ix+1] = 0;
  1075. return CLAUSE_NONE;
  1076. }
  1077. // SSML Tag
  1078. n_xml_buf = 0;
  1079. c1 = c2;
  1080. while (!Eof() && (c1 != '>') && (n_xml_buf < N_XML_BUF)) {
  1081. xml_buf[n_xml_buf++] = c1;
  1082. c1 = GetC();
  1083. }
  1084. xml_buf[n_xml_buf] = 0;
  1085. c2 = ' ';
  1086. self_closing = false;
  1087. if (xml_buf[n_xml_buf-1] == '/') {
  1088. // a self-closing tag
  1089. xml_buf[n_xml_buf-1] = ' ';
  1090. self_closing = true;
  1091. }
  1092. terminator = ProcessSsmlTag(xml_buf, buf, &ix, n_buf, self_closing);
  1093. if (terminator != 0) {
  1094. buf[ix] = ' ';
  1095. buf[ix++] = 0;
  1096. if (terminator & CLAUSE_TYPE_VOICE_CHANGE)
  1097. strcpy(voice_change, current_voice_id);
  1098. return terminator;
  1099. }
  1100. c1 = ' ';
  1101. c2 = GetC();
  1102. continue;
  1103. }
  1104. }
  1105. }
  1106. ssml_ignore_l_angle = 0;
  1107. if (ignore_text)
  1108. continue;
  1109. if ((c2 == '\n') && (option_linelength == -1)) {
  1110. // single-line mode, return immediately on NL
  1111. if ((terminator = clause_type_from_codepoint(c1)) == CLAUSE_NONE) {
  1112. charix[ix] = count_characters - clause_start_char;
  1113. *charix_top = ix;
  1114. ix += utf8_out(c1, &buf[ix]);
  1115. terminator = CLAUSE_PERIOD; // line doesn't end in punctuation, assume period
  1116. }
  1117. buf[ix] = ' ';
  1118. buf[ix+1] = 0;
  1119. return terminator;
  1120. }
  1121. if ((c1 == CTRL_EMBEDDED) || (c1 == ctrl_embedded)) {
  1122. // an embedded command. If it's a voice change, end the clause
  1123. if (c2 == 'V') {
  1124. buf[ix++] = 0; // end the clause at this point
  1125. while (!iswspace(c1 = GetC()) && !Eof() && (ix < (n_buf-1)))
  1126. buf[ix++] = c1; // add voice name to end of buffer, after the text
  1127. buf[ix++] = 0;
  1128. return CLAUSE_VOICE;
  1129. } else if (c2 == 'B') {
  1130. // set the punctuation option from an embedded command
  1131. // B0 B1 B<punct list><space>
  1132. strcpy(&buf[ix], " ");
  1133. ix += 3;
  1134. if ((c2 = GetC()) == '0')
  1135. option_punctuation = 0;
  1136. else {
  1137. option_punctuation = 1;
  1138. option_punctlist[0] = 0;
  1139. if (c2 != '1') {
  1140. // a list of punctuation characters to be spoken, terminated by space
  1141. j = 0;
  1142. while (!iswspace(c2) && !Eof()) {
  1143. option_punctlist[j++] = c2;
  1144. c2 = GetC();
  1145. buf[ix++] = ' ';
  1146. }
  1147. option_punctlist[j] = 0; // terminate punctuation list
  1148. option_punctuation = 2;
  1149. }
  1150. }
  1151. c2 = GetC();
  1152. continue;
  1153. }
  1154. }
  1155. linelength++;
  1156. if ((j = lookupwchar2(tr->chars_ignore, c1)) != 0) {
  1157. if (j == 1) {
  1158. // ignore this character (eg. zero-width-non-joiner U+200C)
  1159. continue;
  1160. }
  1161. c1 = j; // replace the character
  1162. }
  1163. if (iswalnum(c1))
  1164. any_alnum = true;
  1165. else {
  1166. if (stressed_word) {
  1167. stressed_word = false;
  1168. c1 = CHAR_EMPHASIS; // indicate this word is stressed
  1169. UngetC(c2);
  1170. c2 = ' ';
  1171. }
  1172. if (c1 == 0xf0b)
  1173. c1 = ' '; // Tibet inter-syllabic mark, ?? replace by space ??
  1174. if (iswspace(c1)) {
  1175. char *p_word;
  1176. if (tr->translator_name == 0x6a626f) {
  1177. // language jbo : lojban
  1178. // treat "i" or ".i" as end-of-sentence
  1179. p_word = &buf[ix-1];
  1180. if (p_word[0] == 'i') {
  1181. if (p_word[-1] == '.')
  1182. p_word--;
  1183. if (p_word[-1] == ' ') {
  1184. ungot_word = "i ";
  1185. UngetC(c2);
  1186. p_word[0] = 0;
  1187. return CLAUSE_PERIOD;
  1188. }
  1189. }
  1190. }
  1191. }
  1192. if (c1 == 0xd4d) {
  1193. // Malayalam virama, check if next character is Zero-width-joiner
  1194. if (c2 == 0x200d)
  1195. c1 = 0xd4e; // use this unofficial code for chillu-virama
  1196. }
  1197. }
  1198. if (iswupper(c1)) {
  1199. tr->clause_upper_count++;
  1200. if ((option_capitals == 2) && (sayas_mode == 0) && !iswupper(cprev)) {
  1201. char text_buf[40];
  1202. char text_buf2[30];
  1203. if (LookupSpecial(tr, "_cap", text_buf2) != NULL) {
  1204. sprintf(text_buf, "%s", text_buf2);
  1205. j = strlen(text_buf);
  1206. if ((ix + j) < n_buf) {
  1207. strcpy(&buf[ix], text_buf);
  1208. ix += j;
  1209. }
  1210. }
  1211. }
  1212. } else if (iswalpha(c1))
  1213. tr->clause_lower_count++;
  1214. if (option_phoneme_input) {
  1215. if (phoneme_mode > 0)
  1216. phoneme_mode--;
  1217. else if ((c1 == '[') && (c2 == '['))
  1218. phoneme_mode = -1; // input is phoneme mnemonics, so don't look for punctuation
  1219. else if ((c1 == ']') && (c2 == ']'))
  1220. phoneme_mode = 2; // set phoneme_mode to zero after the next two characters
  1221. }
  1222. if (c1 == '\n') {
  1223. parag = 0;
  1224. // count consecutive newlines, ignoring other spaces
  1225. while (!Eof() && iswspace(c2)) {
  1226. if (c2 == '\n')
  1227. parag++;
  1228. c2 = GetC();
  1229. }
  1230. if (parag > 0) {
  1231. // 2nd newline, assume paragraph
  1232. UngetC(c2);
  1233. if (end_clause_after_tag)
  1234. RemoveChar(&buf[end_clause_index]); // delete clause-end punctiation
  1235. buf[ix] = ' ';
  1236. buf[ix+1] = 0;
  1237. if (parag > 3)
  1238. parag = 3;
  1239. if (option_ssml) parag = 1;
  1240. return (CLAUSE_PARAGRAPH-30) + 30*parag; // several blank lines, longer pause
  1241. }
  1242. if (linelength <= option_linelength) {
  1243. // treat lines shorter than a specified length as end-of-clause
  1244. UngetC(c2);
  1245. buf[ix] = ' ';
  1246. buf[ix+1] = 0;
  1247. return CLAUSE_COLON;
  1248. }
  1249. linelength = 0;
  1250. }
  1251. announced_punctuation = 0;
  1252. if ((phoneme_mode == 0) && (sayas_mode == 0)) {
  1253. is_end_clause = false;
  1254. if (end_clause_after_tag) {
  1255. // Because of an xml tag, we are waiting for the
  1256. // next non-blank character to decide whether to end the clause
  1257. // i.e. is dot followed by an upper-case letter?
  1258. if (!iswspace(c1)) {
  1259. if (!IsAlpha(c1) || !iswlower(c1)) {
  1260. UngetC(c2);
  1261. ungot_char2 = c1;
  1262. buf[end_clause_index] = ' '; // delete the end-clause punctuation
  1263. buf[end_clause_index+1] = 0;
  1264. return end_clause_after_tag;
  1265. }
  1266. end_clause_after_tag = 0;
  1267. }
  1268. }
  1269. if ((c1 == '.') && (c2 == '.')) {
  1270. while ((c_next = GetC()) == '.') {
  1271. // 3 or more dots, replace by elipsis
  1272. c1 = 0x2026;
  1273. c2 = ' ';
  1274. }
  1275. if (c1 == 0x2026)
  1276. c2 = c_next;
  1277. else
  1278. UngetC(c_next);
  1279. }
  1280. punct_data = 0;
  1281. if ((punct_data = clause_type_from_codepoint(c1)) != CLAUSE_NONE) {
  1282. if (punct_data & CLAUSE_PUNCTUATION_IN_WORD) {
  1283. // Armenian punctuation inside a word
  1284. stressed_word = true;
  1285. *tone_type = punct_data >> 12 & 0xf; // override the end-of-sentence type
  1286. continue;
  1287. }
  1288. if ((iswspace(c2) || (punct_data & CLAUSE_OPTIONAL_SPACE_AFTER) || IsBracket(c2) || (c2 == '?') || Eof() || (c2 == ctrl_embedded))) { // don't check for '-' because it prevents recognizing ':-)'
  1289. // note: (c2='?') is for when a smart-quote has been replaced by '?'
  1290. is_end_clause = true;
  1291. }
  1292. }
  1293. // don't announce punctuation for the alternative text inside inside <audio> ... </audio>
  1294. if (c1 == 0xe000+'<') c1 = '<';
  1295. if (option_punctuation && iswpunct(c1) && (audio_text == false)) {
  1296. // option is set to explicitly speak punctuation characters
  1297. // if a list of allowed punctuation has been set up, check whether the character is in it
  1298. if ((option_punctuation == 1) || (wcschr(option_punctlist, c1) != NULL)) {
  1299. tr->phonemes_repeat_count = 0;
  1300. if ((terminator = AnnouncePunctuation(tr, c1, &c2, buf, &ix, is_end_clause)) >= 0)
  1301. return terminator;
  1302. announced_punctuation = c1;
  1303. }
  1304. }
  1305. if ((punct_data & CLAUSE_SPEAK_PUNCTUATION_NAME) && (announced_punctuation == 0)) {
  1306. // used for elipsis (and 3 dots) if a pronunciation for elipsis is given in *_list
  1307. char *p2;
  1308. p2 = &buf[ix];
  1309. sprintf(p2, "%s", LookupCharName(tr, c1, 1));
  1310. if (p2[0] != 0) {
  1311. ix += strlen(p2);
  1312. announced_punctuation = c1;
  1313. punct_data = punct_data & ~CLAUSE_INTONATION_TYPE; // change intonation type to 0 (full-stop)
  1314. }
  1315. }
  1316. if (is_end_clause) {
  1317. nl_count = 0;
  1318. c_next = c2;
  1319. if (iswspace(c_next)) {
  1320. while (!Eof() && iswspace(c_next)) {
  1321. if (c_next == '\n')
  1322. nl_count++;
  1323. c_next = GetC(); // skip past space(s)
  1324. }
  1325. }
  1326. if ((c1 == '.') && (nl_count < 2))
  1327. punct_data |= CLAUSE_DOT_AFTER_LAST_WORD;
  1328. if (nl_count == 0) {
  1329. if ((c1 == ',') && (cprev == '.') && (tr->translator_name == L('h', 'u')) && iswdigit(cprev2) && (iswdigit(c_next) || (iswlower(c_next)))) {
  1330. // lang=hu, fix for ordinal numbers, eg: "december 2., szerda", ignore ',' after ordinal number
  1331. c1 = CHAR_COMMA_BREAK;
  1332. is_end_clause = false;
  1333. }
  1334. if (c1 == '.') {
  1335. if ((tr->langopts.numbers & NUM_ORDINAL_DOT) &&
  1336. (iswdigit(cprev) || (IsRomanU(cprev) && (IsRomanU(cprev2) || iswspace(cprev2))))) { // lang=hu
  1337. // dot after a number indicates an ordinal number
  1338. if (!iswdigit(cprev))
  1339. is_end_clause = false; // Roman number followed by dot
  1340. else if (iswlower(c_next) || (c_next == '-')) // hyphen is needed for lang-hu (eg. 2.-kal)
  1341. is_end_clause = false; // only if followed by lower-case, (or if there is a XML tag)
  1342. } else if (c_next == '\'')
  1343. is_end_clause = false; // eg. u.s.a.'s
  1344. if (iswlower(c_next)) {
  1345. // next word has no capital letter, this dot is probably from an abbreviation
  1346. is_end_clause = 0;
  1347. }
  1348. if (any_alnum == false) {
  1349. // no letters or digits yet, so probably not a sentence terminator
  1350. // Here, dot is followed by space or bracket
  1351. c1 = ' ';
  1352. is_end_clause = false;
  1353. }
  1354. } else {
  1355. if (any_alnum == false) {
  1356. // no letters or digits yet, so probably not a sentence terminator
  1357. is_end_clause = false;
  1358. }
  1359. }
  1360. if (is_end_clause && (c1 == '.') && (c_next == '<') && option_ssml) {
  1361. // wait until after the end of the xml tag, then look for upper-case letter
  1362. is_end_clause = false;
  1363. end_clause_index = ix;
  1364. end_clause_after_tag = punct_data;
  1365. }
  1366. }
  1367. if (is_end_clause) {
  1368. UngetC(c_next);
  1369. buf[ix] = ' ';
  1370. buf[ix+1] = 0;
  1371. if (iswdigit(cprev) && !IsAlpha(c_next)) // ????
  1372. punct_data &= ~CLAUSE_DOT_AFTER_LAST_WORD;
  1373. if (nl_count > 1) {
  1374. if ((punct_data == CLAUSE_QUESTION) || (punct_data == CLAUSE_EXCLAMATION))
  1375. return punct_data + 35; // with a longer pause
  1376. return CLAUSE_PARAGRAPH;
  1377. }
  1378. return punct_data; // only recognise punctuation if followed by a blank or bracket/quote
  1379. } else if (!Eof()) {
  1380. if (iswspace(c2))
  1381. UngetC(c_next);
  1382. }
  1383. }
  1384. }
  1385. if (speech_parameters[espeakSILENCE] == 1)
  1386. continue;
  1387. if (c1 == announced_punctuation) {
  1388. // This character has already been announced, so delete it so that it isn't spoken a second time.
  1389. // Unless it's a hyphen or apostrophe (which is used by TranslateClause() )
  1390. if (IsBracket(c1))
  1391. c1 = 0xe000 + '('; // Unicode private useage area. So TranslateRules() knows the bracket name has been spoken
  1392. else if (c1 != '-')
  1393. c1 = ' ';
  1394. }
  1395. j = ix+1;
  1396. if (c1 == 0xe000 + '<') c1 = '<';
  1397. ix += utf8_out(c1, &buf[ix]);
  1398. if (!iswspace(c1) && !IsBracket(c1)) {
  1399. charix[ix] = count_characters - clause_start_char;
  1400. while (j < ix)
  1401. charix[j++] = -1; // subsequent bytes of a multibyte character
  1402. }
  1403. *charix_top = ix;
  1404. if (((ix > (n_buf-75)) && !IsAlpha(c1) && !iswdigit(c1)) || (ix >= (n_buf-4))) {
  1405. // clause too long, getting near end of buffer, so break here
  1406. // try to break at a word boundary (unless we actually reach the end of buffer).
  1407. // (n_buf-4) is to allow for 3 bytes of multibyte character plus terminator.
  1408. buf[ix] = ' ';
  1409. buf[ix+1] = 0;
  1410. UngetC(c2);
  1411. return CLAUSE_NONE;
  1412. }
  1413. }
  1414. if (stressed_word)
  1415. ix += utf8_out(CHAR_EMPHASIS, &buf[ix]);
  1416. if (end_clause_after_tag)
  1417. RemoveChar(&buf[end_clause_index]); // delete clause-end punctiation
  1418. buf[ix] = ' ';
  1419. buf[ix+1] = 0;
  1420. return CLAUSE_EOF; // end of file
  1421. }
  1422. void InitNamedata(void)
  1423. {
  1424. namedata_ix = 0;
  1425. if (namedata != NULL) {
  1426. free(namedata);
  1427. namedata = NULL;
  1428. n_namedata = 0;
  1429. }
  1430. }
  1431. void InitText2(void)
  1432. {
  1433. int param;
  1434. ungot_char = 0;
  1435. ungot_char2 = 0;
  1436. n_ssml_stack = 1;
  1437. n_param_stack = 1;
  1438. ssml_stack[0].tag_type = 0;
  1439. for (param = 0; param < N_SPEECH_PARAM; param++)
  1440. speech_parameters[param] = param_stack[0].parameter[param]; // set all speech parameters to defaults
  1441. option_punctuation = speech_parameters[espeakPUNCTUATION];
  1442. option_capitals = speech_parameters[espeakCAPITALS];
  1443. current_voice_id[0] = 0;
  1444. ignore_text = false;
  1445. audio_text = false;
  1446. clear_skipping_text = false;
  1447. count_characters = -1;
  1448. sayas_mode = 0;
  1449. xmlbase = NULL;
  1450. }