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

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