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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992
  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. const char *VoiceFromStack()
  475. {
  476. // Use the voice properties from the SSML stack to choose a voice, and switch
  477. // to that voice if it's not the current voice
  478. int ix;
  479. const char *p;
  480. SSML_STACK *sp;
  481. const char *v_id;
  482. int voice_name_specified;
  483. int voice_found;
  484. espeak_VOICE voice_select;
  485. static char voice_name[40];
  486. char language[40];
  487. char buf[80];
  488. strcpy(voice_name, ssml_stack[0].voice_name);
  489. strcpy(language, ssml_stack[0].language);
  490. voice_select.age = ssml_stack[0].voice_age;
  491. voice_select.gender = ssml_stack[0].voice_gender;
  492. voice_select.variant = ssml_stack[0].voice_variant_number;
  493. voice_select.identifier = NULL;
  494. for (ix = 0; ix < n_ssml_stack; ix++) {
  495. sp = &ssml_stack[ix];
  496. voice_name_specified = 0;
  497. if ((sp->voice_name[0] != 0) && (SelectVoiceByName(NULL, sp->voice_name) != NULL)) {
  498. voice_name_specified = 1;
  499. strcpy(voice_name, sp->voice_name);
  500. language[0] = 0;
  501. voice_select.gender = ENGENDER_UNKNOWN;
  502. voice_select.age = 0;
  503. voice_select.variant = 0;
  504. }
  505. if (sp->language[0] != 0) {
  506. strcpy(language, sp->language);
  507. // is this language provided by the base voice?
  508. p = base_voice.languages;
  509. while (*p++ != 0) {
  510. if (strcmp(p, language) == 0) {
  511. // yes, change the language to the main language of the base voice
  512. strcpy(language, &base_voice.languages[1]);
  513. break;
  514. }
  515. p += (strlen(p) + 1);
  516. }
  517. if (voice_name_specified == 0)
  518. voice_name[0] = 0; // forget a previous voice name if a language is specified
  519. }
  520. if (sp->voice_gender != ENGENDER_UNKNOWN)
  521. voice_select.gender = sp->voice_gender;
  522. if (sp->voice_age != 0)
  523. voice_select.age = sp->voice_age;
  524. if (sp->voice_variant_number != 0)
  525. voice_select.variant = sp->voice_variant_number;
  526. }
  527. voice_select.name = voice_name;
  528. voice_select.languages = language;
  529. v_id = SelectVoice(&voice_select, &voice_found);
  530. if (v_id == NULL)
  531. return "default";
  532. if ((strchr(v_id, '+') == NULL) && ((voice_select.gender == ENGENDER_UNKNOWN) || (voice_select.gender == base_voice.gender)) && (base_voice_variant_name[0] != 0)) {
  533. // a voice variant has not been selected, use the original voice variant
  534. sprintf(buf, "%s+%s", v_id, base_voice_variant_name);
  535. strncpy0(voice_name, buf, sizeof(voice_name));
  536. return voice_name;
  537. }
  538. return v_id;
  539. }
  540. static void ProcessParamStack(char *outbuf, int *outix)
  541. {
  542. // Set the speech parameters from the parameter stack
  543. int param;
  544. int ix;
  545. int value;
  546. char buf[20];
  547. int new_parameters[N_SPEECH_PARAM];
  548. static char cmd_letter[N_SPEECH_PARAM] = { 0, 'S', 'A', 'P', 'R', 0, 'C', 0, 0, 0, 0, 0, 'F' }; // embedded command letters
  549. for (param = 0; param < N_SPEECH_PARAM; param++)
  550. new_parameters[param] = -1;
  551. for (ix = 0; ix < n_param_stack; ix++) {
  552. for (param = 0; param < N_SPEECH_PARAM; param++) {
  553. if (param_stack[ix].parameter[param] >= 0)
  554. new_parameters[param] = param_stack[ix].parameter[param];
  555. }
  556. }
  557. for (param = 0; param < N_SPEECH_PARAM; param++) {
  558. if ((value = new_parameters[param]) != speech_parameters[param]) {
  559. buf[0] = 0;
  560. switch (param)
  561. {
  562. case espeakPUNCTUATION:
  563. option_punctuation = value-1;
  564. break;
  565. case espeakCAPITALS:
  566. option_capitals = value;
  567. break;
  568. case espeakRATE:
  569. case espeakVOLUME:
  570. case espeakPITCH:
  571. case espeakRANGE:
  572. case espeakEMPHASIS:
  573. sprintf(buf, "%c%d%c", CTRL_EMBEDDED, value, cmd_letter[param]);
  574. break;
  575. }
  576. speech_parameters[param] = new_parameters[param];
  577. strcpy(&outbuf[*outix], buf);
  578. *outix += strlen(buf);
  579. }
  580. }
  581. }
  582. static PARAM_STACK *PushParamStack(int tag_type)
  583. {
  584. int ix;
  585. PARAM_STACK *sp;
  586. sp = &param_stack[n_param_stack];
  587. if (n_param_stack < (N_PARAM_STACK-1))
  588. n_param_stack++;
  589. sp->type = tag_type;
  590. for (ix = 0; ix < N_SPEECH_PARAM; ix++)
  591. sp->parameter[ix] = -1;
  592. return sp;
  593. }
  594. static void PopParamStack(int tag_type, char *outbuf, int *outix)
  595. {
  596. // unwind the stack up to and including the previous tag of this type
  597. int ix;
  598. int top = 0;
  599. if (tag_type >= SSML_CLOSE)
  600. tag_type -= SSML_CLOSE;
  601. for (ix = 0; ix < n_param_stack; ix++) {
  602. if (param_stack[ix].type == tag_type)
  603. top = ix;
  604. }
  605. if (top > 0)
  606. n_param_stack = top;
  607. ProcessParamStack(outbuf, outix);
  608. }
  609. static wchar_t *GetSsmlAttribute(wchar_t *pw, const char *name)
  610. {
  611. // Gets the value string for an attribute.
  612. // Returns NULL if the attribute is not present
  613. int ix;
  614. static wchar_t empty[1] = { 0 };
  615. while (*pw != 0) {
  616. if (iswspace(pw[-1])) {
  617. ix = 0;
  618. while (*pw == name[ix]) {
  619. pw++;
  620. ix++;
  621. }
  622. if (name[ix] == 0) {
  623. // found the attribute, now get the value
  624. while (iswspace(*pw)) pw++;
  625. if (*pw == '=') pw++;
  626. while (iswspace(*pw)) pw++;
  627. if ((*pw == '"') || (*pw == '\'')) // allow single-quotes ?
  628. return pw+1;
  629. else
  630. return empty;
  631. }
  632. }
  633. pw++;
  634. }
  635. return NULL;
  636. }
  637. static int attr_prosody_value(int param_type, const wchar_t *pw, int *value_out)
  638. {
  639. int sign = 0;
  640. wchar_t *tail;
  641. double value;
  642. while (iswspace(*pw)) pw++;
  643. if (*pw == '+') {
  644. pw++;
  645. sign = 1;
  646. }
  647. if (*pw == '-') {
  648. pw++;
  649. sign = -1;
  650. }
  651. value = (double)wcstod(pw, &tail);
  652. if (tail == pw) {
  653. // failed to find a number, return 100%
  654. *value_out = 100;
  655. return 2;
  656. }
  657. if (*tail == '%') {
  658. if (sign != 0)
  659. value = 100 + (sign * value);
  660. *value_out = (int)value;
  661. return 2; // percentage
  662. }
  663. if ((tail[0] == 's') && (tail[1] == 't')) {
  664. double x;
  665. // convert from semitones to a frequency percentage
  666. x = pow((double)2.0, (double)((value*sign)/12)) * 100;
  667. *value_out = (int)x;
  668. return 2; // percentage
  669. }
  670. if (param_type == espeakRATE) {
  671. if (sign == 0)
  672. *value_out = (int)(value * 100);
  673. else
  674. *value_out = 100 + (int)(sign * value * 100);
  675. return 2; // percentage
  676. }
  677. *value_out = (int)value;
  678. return sign; // -1, 0, or 1
  679. }
  680. static int AddNameData(const char *name, int wide)
  681. {
  682. // Add the name to the namedata and return its position
  683. // (Used by the Windows SAPI wrapper)
  684. int ix;
  685. int len;
  686. void *vp;
  687. if (wide) {
  688. len = (wcslen((const wchar_t *)name)+1)*sizeof(wchar_t);
  689. n_namedata = (n_namedata + sizeof(wchar_t) - 1) % sizeof(wchar_t); // round to wchar_t boundary
  690. } else
  691. len = strlen(name)+1;
  692. if (namedata_ix+len >= n_namedata) {
  693. // allocate more space for marker names
  694. if ((vp = realloc(namedata, namedata_ix+len + 1000)) == NULL)
  695. return -1; // failed to allocate, original data is unchanged but ignore this new name
  696. // !!! Bug?? If the allocated data shifts position, then pointers given to user application will be invalid
  697. namedata = (char *)vp;
  698. n_namedata = namedata_ix+len + 1000;
  699. }
  700. memcpy(&namedata[ix = namedata_ix], name, len);
  701. namedata_ix += len;
  702. return ix;
  703. }
  704. void SetVoiceStack(espeak_VOICE *v, const char *variant_name)
  705. {
  706. SSML_STACK *sp;
  707. sp = &ssml_stack[0];
  708. if (v == NULL) {
  709. memset(sp, 0, sizeof(ssml_stack[0]));
  710. return;
  711. }
  712. if (v->languages != NULL)
  713. strcpy(sp->language, v->languages);
  714. if (v->name != NULL)
  715. strncpy0(sp->voice_name, v->name, sizeof(sp->voice_name));
  716. sp->voice_variant_number = v->variant;
  717. sp->voice_age = v->age;
  718. sp->voice_gender = v->gender;
  719. if (memcmp(variant_name, "!v", 2) == 0)
  720. variant_name += 3; // strip variant directory name, !v plus PATHSEP
  721. strncpy0(base_voice_variant_name, variant_name, sizeof(base_voice_variant_name));
  722. memcpy(&base_voice, &current_voice_selected, sizeof(base_voice));
  723. }
  724. static int GetVoiceAttributes(wchar_t *pw, int tag_type)
  725. {
  726. // Determines whether voice attribute are specified in this tag, and if so, whether this means
  727. // a voice change.
  728. // If it's a closing tag, delete the top frame of the stack and determine whether this implies
  729. // a voice change.
  730. // Returns CLAUSE_TYPE_VOICE_CHANGE if there is a voice change
  731. wchar_t *lang;
  732. wchar_t *gender;
  733. wchar_t *name;
  734. wchar_t *age;
  735. wchar_t *variant;
  736. int value;
  737. const char *new_voice_id;
  738. SSML_STACK *ssml_sp;
  739. static const MNEM_TAB mnem_gender[] = {
  740. { "male", ENGENDER_MALE },
  741. { "female", ENGENDER_FEMALE },
  742. { "neutral", ENGENDER_NEUTRAL },
  743. { NULL, ENGENDER_UNKNOWN }
  744. };
  745. if (tag_type & SSML_CLOSE) {
  746. // delete a stack frame
  747. if (n_ssml_stack > 1)
  748. n_ssml_stack--;
  749. } else {
  750. // add a stack frame if any voice details are specified
  751. lang = GetSsmlAttribute(pw, "xml:lang");
  752. if (tag_type != SSML_VOICE) {
  753. // only expect an xml:lang attribute
  754. name = NULL;
  755. variant = NULL;
  756. age = NULL;
  757. gender = NULL;
  758. } else {
  759. name = GetSsmlAttribute(pw, "name");
  760. variant = GetSsmlAttribute(pw, "variant");
  761. age = GetSsmlAttribute(pw, "age");
  762. gender = GetSsmlAttribute(pw, "gender");
  763. }
  764. if ((tag_type != SSML_VOICE) && (lang == NULL))
  765. return 0; // <s> or <p> without language spec, nothing to do
  766. ssml_sp = &ssml_stack[n_ssml_stack++];
  767. attrcopy_utf8(ssml_sp->language, lang, sizeof(ssml_sp->language));
  768. attrcopy_utf8(ssml_sp->voice_name, name, sizeof(ssml_sp->voice_name));
  769. if ((value = attrnumber(variant, 1, 0)) > 0)
  770. value--; // variant='0' and variant='1' the same
  771. ssml_sp->voice_variant_number = value;
  772. ssml_sp->voice_age = attrnumber(age, 0, 0);
  773. ssml_sp->voice_gender = attrlookup(gender, mnem_gender);
  774. ssml_sp->tag_type = tag_type;
  775. }
  776. new_voice_id = VoiceFromStack();
  777. if (strcmp(new_voice_id, current_voice_id) != 0) {
  778. // add an embedded command to change the voice
  779. strcpy(current_voice_id, new_voice_id);
  780. return CLAUSE_TYPE_VOICE_CHANGE;
  781. }
  782. return 0;
  783. }
  784. static void SetProsodyParameter(int param_type, wchar_t *attr1, PARAM_STACK *sp)
  785. {
  786. int value;
  787. int sign;
  788. static const MNEM_TAB mnem_volume[] = {
  789. { "default", 100 },
  790. { "silent", 0 },
  791. { "x-soft", 30 },
  792. { "soft", 65 },
  793. { "medium", 100 },
  794. { "loud", 150 },
  795. { "x-loud", 230 },
  796. { NULL, -1 }
  797. };
  798. static const MNEM_TAB mnem_rate[] = {
  799. { "default", 100 },
  800. { "x-slow", 60 },
  801. { "slow", 80 },
  802. { "medium", 100 },
  803. { "fast", 125 },
  804. { "x-fast", 160 },
  805. { NULL, -1 }
  806. };
  807. static const MNEM_TAB mnem_pitch[] = {
  808. { "default", 100 },
  809. { "x-low", 70 },
  810. { "low", 85 },
  811. { "medium", 100 },
  812. { "high", 110 },
  813. { "x-high", 120 },
  814. { NULL, -1 }
  815. };
  816. static const MNEM_TAB mnem_range[] = {
  817. { "default", 100 },
  818. { "x-low", 20 },
  819. { "low", 50 },
  820. { "medium", 100 },
  821. { "high", 140 },
  822. { "x-high", 180 },
  823. { NULL, -1 }
  824. };
  825. static const MNEM_TAB *mnem_tabs[5] = {
  826. NULL, mnem_rate, mnem_volume, mnem_pitch, mnem_range
  827. };
  828. if ((value = attrlookup(attr1, mnem_tabs[param_type])) >= 0) {
  829. // mnemonic specifies a value as a percentage of the base pitch/range/rate/volume
  830. sp->parameter[param_type] = (param_stack[0].parameter[param_type] * value)/100;
  831. } else {
  832. sign = attr_prosody_value(param_type, attr1, &value);
  833. if (sign == 0)
  834. sp->parameter[param_type] = value; // absolute value in Hz
  835. else if (sign == 2) {
  836. // change specified as percentage or in semitones
  837. sp->parameter[param_type] = (speech_parameters[param_type] * value)/100;
  838. } else {
  839. // change specified as plus or minus Hz
  840. sp->parameter[param_type] = speech_parameters[param_type] + (value*sign);
  841. }
  842. }
  843. }
  844. static int ReplaceKeyName(char *outbuf, int index, int *outix)
  845. {
  846. // Replace some key-names by single characters, so they can be pronounced in different languages
  847. static MNEM_TAB keynames[] = {
  848. { "space ", 0xe020 },
  849. { "tab ", 0xe009 },
  850. { "underscore ", 0xe05f },
  851. { "double-quote ", '"' },
  852. { NULL, 0 }
  853. };
  854. int ix;
  855. int letter;
  856. char *p;
  857. p = &outbuf[index];
  858. if ((letter = LookupMnem(keynames, p)) != 0) {
  859. ix = utf8_out(letter, p);
  860. *outix = index + ix;
  861. return letter;
  862. }
  863. return 0;
  864. }
  865. static int ProcessSsmlTag(wchar_t *xml_buf, char *outbuf, int *outix, int n_outbuf, bool self_closing)
  866. {
  867. // xml_buf is the tag and attributes with a zero terminator in place of the original '>'
  868. // returns a clause terminator value.
  869. unsigned int ix;
  870. int index;
  871. int c;
  872. int tag_type;
  873. int value;
  874. int value2;
  875. int value3;
  876. int voice_change_flag;
  877. wchar_t *px;
  878. wchar_t *attr1;
  879. wchar_t *attr2;
  880. wchar_t *attr3;
  881. int terminator;
  882. char *uri;
  883. int param_type;
  884. char tag_name[40];
  885. char buf[80];
  886. PARAM_STACK *sp;
  887. SSML_STACK *ssml_sp;
  888. static const MNEM_TAB mnem_phoneme_alphabet[] = {
  889. { "espeak", 1 },
  890. { NULL, -1 }
  891. };
  892. static const MNEM_TAB mnem_punct[] = {
  893. { "none", 1 },
  894. { "all", 2 },
  895. { "some", 3 },
  896. { NULL, -1 }
  897. };
  898. static const MNEM_TAB mnem_capitals[] = {
  899. { "no", 0 },
  900. { "icon", 1 },
  901. { "spelling", 2 },
  902. { "pitch", 20 }, // this is the amount by which to raise the pitch
  903. { NULL, -1 }
  904. };
  905. static const MNEM_TAB mnem_interpret_as[] = {
  906. { "characters", SAYAS_CHARS },
  907. { "tts:char", SAYAS_SINGLE_CHARS },
  908. { "tts:key", SAYAS_KEY },
  909. { "tts:digits", SAYAS_DIGITS },
  910. { "telephone", SAYAS_DIGITS1 },
  911. { NULL, -1 }
  912. };
  913. static const MNEM_TAB mnem_sayas_format[] = {
  914. { "glyphs", 1 },
  915. { NULL, -1 }
  916. };
  917. static const MNEM_TAB mnem_break[] = {
  918. { "none", 0 },
  919. { "x-weak", 1 },
  920. { "weak", 2 },
  921. { "medium", 3 },
  922. { "strong", 4 },
  923. { "x-strong", 5 },
  924. { NULL, -1 }
  925. };
  926. static const MNEM_TAB mnem_emphasis[] = {
  927. { "none", 1 },
  928. { "reduced", 2 },
  929. { "moderate", 3 },
  930. { "strong", 4 },
  931. { "x-strong", 5 },
  932. { NULL, -1 }
  933. };
  934. static const char *prosody_attr[5] = {
  935. NULL, "rate", "volume", "pitch", "range"
  936. };
  937. for (ix = 0; ix < (sizeof(tag_name)-1); ix++) {
  938. if (((c = xml_buf[ix]) == 0) || iswspace(c))
  939. break;
  940. tag_name[ix] = tolower((char)c);
  941. }
  942. tag_name[ix] = 0;
  943. px = &xml_buf[ix]; // the tag's attributes
  944. if (tag_name[0] == '/') {
  945. // closing tag
  946. if ((tag_type = LookupMnem(ssmltags, &tag_name[1])) != HTML_NOSPACE)
  947. outbuf[(*outix)++] = ' ';
  948. tag_type += SSML_CLOSE;
  949. } else {
  950. if ((tag_type = LookupMnem(ssmltags, tag_name)) != HTML_NOSPACE) {
  951. // separate SSML tags from the previous word (but not HMTL tags such as <b> <font> which can occur inside a word)
  952. outbuf[(*outix)++] = ' ';
  953. }
  954. if (self_closing && ignore_if_self_closing[tag_type])
  955. return 0;
  956. }
  957. voice_change_flag = 0;
  958. ssml_sp = &ssml_stack[n_ssml_stack-1];
  959. switch (tag_type)
  960. {
  961. case SSML_STYLE:
  962. sp = PushParamStack(tag_type);
  963. attr1 = GetSsmlAttribute(px, "field");
  964. attr2 = GetSsmlAttribute(px, "mode");
  965. if (attrcmp(attr1, "punctuation") == 0) {
  966. value = attrlookup(attr2, mnem_punct);
  967. sp->parameter[espeakPUNCTUATION] = value;
  968. } else if (attrcmp(attr1, "capital_letters") == 0) {
  969. value = attrlookup(attr2, mnem_capitals);
  970. sp->parameter[espeakCAPITALS] = value;
  971. }
  972. ProcessParamStack(outbuf, outix);
  973. break;
  974. case SSML_PROSODY:
  975. sp = PushParamStack(tag_type);
  976. // look for attributes: rate, volume, pitch, range
  977. for (param_type = espeakRATE; param_type <= espeakRANGE; param_type++) {
  978. if ((attr1 = GetSsmlAttribute(px, prosody_attr[param_type])) != NULL)
  979. SetProsodyParameter(param_type, attr1, sp);
  980. }
  981. ProcessParamStack(outbuf, outix);
  982. break;
  983. case SSML_EMPHASIS:
  984. sp = PushParamStack(tag_type);
  985. value = 3; // default is "moderate"
  986. if ((attr1 = GetSsmlAttribute(px, "level")) != NULL)
  987. value = attrlookup(attr1, mnem_emphasis);
  988. if (translator->langopts.tone_language == 1) {
  989. static unsigned char emphasis_to_pitch_range[] = { 50, 50, 40, 70, 90, 100 };
  990. static unsigned char emphasis_to_volume[] = { 100, 100, 70, 110, 135, 150 };
  991. // tone language (eg.Chinese) do emphasis by increasing the pitch range.
  992. sp->parameter[espeakRANGE] = emphasis_to_pitch_range[value];
  993. sp->parameter[espeakVOLUME] = emphasis_to_volume[value];
  994. } else {
  995. static unsigned char emphasis_to_volume2[] = { 100, 100, 75, 100, 120, 150 };
  996. sp->parameter[espeakVOLUME] = emphasis_to_volume2[value];
  997. sp->parameter[espeakEMPHASIS] = value;
  998. }
  999. ProcessParamStack(outbuf, outix);
  1000. break;
  1001. case SSML_STYLE + SSML_CLOSE:
  1002. case SSML_PROSODY + SSML_CLOSE:
  1003. case SSML_EMPHASIS + SSML_CLOSE:
  1004. PopParamStack(tag_type, outbuf, outix);
  1005. break;
  1006. case SSML_PHONEME:
  1007. attr1 = GetSsmlAttribute(px, "alphabet");
  1008. attr2 = GetSsmlAttribute(px, "ph");
  1009. value = attrlookup(attr1, mnem_phoneme_alphabet);
  1010. if (value == 1) { // alphabet="espeak"
  1011. outbuf[(*outix)++] = '[';
  1012. outbuf[(*outix)++] = '[';
  1013. *outix += attrcopy_utf8(&outbuf[*outix], attr2, n_outbuf-*outix);
  1014. outbuf[(*outix)++] = ']';
  1015. outbuf[(*outix)++] = ']';
  1016. }
  1017. break;
  1018. case SSML_SAYAS:
  1019. attr1 = GetSsmlAttribute(px, "interpret-as");
  1020. attr2 = GetSsmlAttribute(px, "format");
  1021. attr3 = GetSsmlAttribute(px, "detail");
  1022. value = attrlookup(attr1, mnem_interpret_as);
  1023. value2 = attrlookup(attr2, mnem_sayas_format);
  1024. if (value2 == 1)
  1025. value = SAYAS_GLYPHS;
  1026. value3 = attrnumber(attr3, 0, 0);
  1027. if (value == SAYAS_DIGITS) {
  1028. if (value3 <= 1)
  1029. value = SAYAS_DIGITS1;
  1030. else
  1031. value = SAYAS_DIGITS + value3;
  1032. }
  1033. sprintf(buf, "%c%dY", CTRL_EMBEDDED, value);
  1034. strcpy(&outbuf[*outix], buf);
  1035. *outix += strlen(buf);
  1036. sayas_start = *outix;
  1037. sayas_mode = value; // punctuation doesn't end clause during SAY-AS
  1038. break;
  1039. case SSML_SAYAS + SSML_CLOSE:
  1040. if (sayas_mode == SAYAS_KEY) {
  1041. outbuf[*outix] = 0;
  1042. ReplaceKeyName(outbuf, sayas_start, outix);
  1043. }
  1044. outbuf[(*outix)++] = CTRL_EMBEDDED;
  1045. outbuf[(*outix)++] = 'Y';
  1046. sayas_mode = 0;
  1047. break;
  1048. case SSML_SUB:
  1049. if ((attr1 = GetSsmlAttribute(px, "alias")) != NULL) {
  1050. // use the alias rather than the text
  1051. ignore_text = true;
  1052. *outix += attrcopy_utf8(&outbuf[*outix], attr1, n_outbuf-*outix);
  1053. }
  1054. break;
  1055. case SSML_IGNORE_TEXT:
  1056. ignore_text = true;
  1057. break;
  1058. case SSML_SUB + SSML_CLOSE:
  1059. case SSML_IGNORE_TEXT + SSML_CLOSE:
  1060. ignore_text = false;
  1061. break;
  1062. case SSML_MARK:
  1063. if ((attr1 = GetSsmlAttribute(px, "name")) != NULL) {
  1064. // add name to circular buffer of marker names
  1065. attrcopy_utf8(buf, attr1, sizeof(buf));
  1066. if (strcmp(skip_marker, buf) == 0) {
  1067. // This is the marker we are waiting for before starting to speak
  1068. clear_skipping_text = true;
  1069. skip_marker[0] = 0;
  1070. return CLAUSE_NONE;
  1071. }
  1072. if ((index = AddNameData(buf, 0)) >= 0) {
  1073. sprintf(buf, "%c%dM", CTRL_EMBEDDED, index);
  1074. strcpy(&outbuf[*outix], buf);
  1075. *outix += strlen(buf);
  1076. }
  1077. }
  1078. break;
  1079. case SSML_AUDIO:
  1080. sp = PushParamStack(tag_type);
  1081. if ((attr1 = GetSsmlAttribute(px, "src")) != NULL) {
  1082. char fname[256];
  1083. attrcopy_utf8(buf, attr1, sizeof(buf));
  1084. if (uri_callback == NULL) {
  1085. if ((xmlbase != NULL) && (buf[0] != '/')) {
  1086. sprintf(fname, "%s/%s", xmlbase, buf);
  1087. index = LoadSoundFile2(fname);
  1088. } else
  1089. index = LoadSoundFile2(buf);
  1090. if (index >= 0) {
  1091. sprintf(buf, "%c%dI", CTRL_EMBEDDED, index);
  1092. strcpy(&outbuf[*outix], buf);
  1093. *outix += strlen(buf);
  1094. sp->parameter[espeakSILENCE] = 1;
  1095. }
  1096. } else {
  1097. if ((index = AddNameData(buf, 0)) >= 0) {
  1098. uri = &namedata[index];
  1099. if (uri_callback(1, uri, xmlbase) == 0) {
  1100. sprintf(buf, "%c%dU", CTRL_EMBEDDED, index);
  1101. strcpy(&outbuf[*outix], buf);
  1102. *outix += strlen(buf);
  1103. sp->parameter[espeakSILENCE] = 1;
  1104. }
  1105. }
  1106. }
  1107. }
  1108. ProcessParamStack(outbuf, outix);
  1109. if (self_closing)
  1110. PopParamStack(tag_type, outbuf, outix);
  1111. else
  1112. audio_text = true;
  1113. return CLAUSE_NONE;
  1114. case SSML_AUDIO + SSML_CLOSE:
  1115. PopParamStack(tag_type, outbuf, outix);
  1116. audio_text = false;
  1117. return CLAUSE_NONE;
  1118. case SSML_BREAK:
  1119. value = 21;
  1120. terminator = CLAUSE_NONE;
  1121. if ((attr1 = GetSsmlAttribute(px, "strength")) != NULL) {
  1122. static int break_value[6] = { 0, 7, 14, 21, 40, 80 }; // *10mS
  1123. value = attrlookup(attr1, mnem_break);
  1124. if (value < 3) {
  1125. // adjust prepause on the following word
  1126. sprintf(&outbuf[*outix], "%c%dB", CTRL_EMBEDDED, value);
  1127. *outix += 3;
  1128. terminator = 0;
  1129. }
  1130. value = break_value[value];
  1131. }
  1132. if ((attr2 = GetSsmlAttribute(px, "time")) != NULL) {
  1133. value2 = attrnumber(attr2, 0, 1); // pause in mS
  1134. // compensate for speaking speed to keep constant pause length, see function PauseLength()
  1135. // 'value' here is x 10mS
  1136. value = (value2 * 256) / (speed.clause_pause_factor * 10);
  1137. if (value < 200)
  1138. value = (value2 * 256) / (speed.pause_factor * 10);
  1139. if (terminator == 0)
  1140. terminator = CLAUSE_NONE;
  1141. }
  1142. if (terminator) {
  1143. if (value > 0xfff) {
  1144. // scale down the value and set a scaling indicator bit
  1145. value = value / 32;
  1146. if (value > 0xfff)
  1147. value = 0xfff;
  1148. terminator |= CLAUSE_PAUSE_LONG;
  1149. }
  1150. return terminator + value;
  1151. }
  1152. break;
  1153. case SSML_SPEAK:
  1154. if ((attr1 = GetSsmlAttribute(px, "xml:base")) != NULL) {
  1155. attrcopy_utf8(buf, attr1, sizeof(buf));
  1156. if ((index = AddNameData(buf, 0)) >= 0)
  1157. xmlbase = &namedata[index];
  1158. }
  1159. if (GetVoiceAttributes(px, tag_type) == 0)
  1160. return 0; // no voice change
  1161. return CLAUSE_VOICE;
  1162. case SSML_VOICE:
  1163. if (GetVoiceAttributes(px, tag_type) == 0)
  1164. return 0; // no voice change
  1165. return CLAUSE_VOICE;
  1166. case SSML_SPEAK + SSML_CLOSE:
  1167. // unwind stack until the previous <voice> or <speak> tag
  1168. while ((n_ssml_stack > 1) && (ssml_stack[n_ssml_stack-1].tag_type != SSML_SPEAK))
  1169. n_ssml_stack--;
  1170. return CLAUSE_PERIOD + GetVoiceAttributes(px, tag_type);
  1171. case SSML_VOICE + SSML_CLOSE:
  1172. // unwind stack until the previous <voice> or <speak> tag
  1173. while ((n_ssml_stack > 1) && (ssml_stack[n_ssml_stack-1].tag_type != SSML_VOICE))
  1174. n_ssml_stack--;
  1175. terminator = 0; // ?? Sentence intonation, but no pause ??
  1176. return terminator + GetVoiceAttributes(px, tag_type);
  1177. case HTML_BREAK:
  1178. case HTML_BREAK + SSML_CLOSE:
  1179. return CLAUSE_COLON;
  1180. case SSML_SENTENCE:
  1181. if (ssml_sp->tag_type == SSML_SENTENCE) {
  1182. // new sentence implies end-of-sentence
  1183. voice_change_flag = GetVoiceAttributes(px, SSML_SENTENCE+SSML_CLOSE);
  1184. }
  1185. voice_change_flag |= GetVoiceAttributes(px, tag_type);
  1186. return CLAUSE_PARAGRAPH + voice_change_flag;
  1187. case SSML_PARAGRAPH:
  1188. if (ssml_sp->tag_type == SSML_SENTENCE) {
  1189. // new paragraph implies end-of-sentence or end-of-paragraph
  1190. voice_change_flag = GetVoiceAttributes(px, SSML_SENTENCE+SSML_CLOSE);
  1191. }
  1192. if (ssml_sp->tag_type == SSML_PARAGRAPH) {
  1193. // new paragraph implies end-of-sentence or end-of-paragraph
  1194. voice_change_flag |= GetVoiceAttributes(px, SSML_PARAGRAPH+SSML_CLOSE);
  1195. }
  1196. voice_change_flag |= GetVoiceAttributes(px, tag_type);
  1197. return CLAUSE_PARAGRAPH + voice_change_flag;
  1198. case SSML_SENTENCE + SSML_CLOSE:
  1199. if (ssml_sp->tag_type == SSML_SENTENCE) {
  1200. // end of a sentence which specified a language
  1201. voice_change_flag = GetVoiceAttributes(px, tag_type);
  1202. }
  1203. return CLAUSE_PERIOD + voice_change_flag;
  1204. case SSML_PARAGRAPH + SSML_CLOSE:
  1205. if ((ssml_sp->tag_type == SSML_SENTENCE) || (ssml_sp->tag_type == SSML_PARAGRAPH)) {
  1206. // End of a paragraph which specified a language.
  1207. // (End-of-paragraph also implies end-of-sentence)
  1208. return GetVoiceAttributes(px, tag_type) + CLAUSE_PARAGRAPH;
  1209. }
  1210. return CLAUSE_PARAGRAPH;
  1211. }
  1212. return 0;
  1213. }
  1214. static void RemoveChar(char *p)
  1215. {
  1216. // Replace a UTF-8 character by spaces
  1217. int c;
  1218. memset(p, ' ', utf8_in(&c, p));
  1219. }
  1220. static MNEM_TAB xml_char_mnemonics[] = {
  1221. { "gt", '>' },
  1222. { "lt", 0xe000 + '<' }, // private usage area, to avoid confusion with XML tag
  1223. { "amp", '&' },
  1224. { "quot", '"' },
  1225. { "nbsp", ' ' },
  1226. { "apos", '\'' },
  1227. { NULL, -1 }
  1228. };
  1229. int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_buf, int *tone_type, char *voice_change)
  1230. {
  1231. /* Find the end of the current clause.
  1232. Write the clause into buf
  1233. returns: clause type (bits 0-7: pause x10mS, bits 8-11 intonation type)
  1234. Also checks for blank line (paragraph) as end-of-clause indicator.
  1235. Does not end clause for:
  1236. punctuation immediately followed by alphanumeric eg. 1.23 !Speak :path
  1237. repeated punctuation, eg. ... !!!
  1238. */
  1239. int c1 = ' '; // current character
  1240. int c2; // next character
  1241. int cprev = ' '; // previous character
  1242. int cprev2 = ' ';
  1243. int c_next;
  1244. int parag;
  1245. int ix = 0;
  1246. int j;
  1247. int nl_count;
  1248. int linelength = 0;
  1249. int phoneme_mode = 0;
  1250. int n_xml_buf;
  1251. int terminator;
  1252. int found;
  1253. bool any_alnum = false;
  1254. bool self_closing;
  1255. int punct_data = 0;
  1256. bool is_end_clause;
  1257. int announced_punctuation = 0;
  1258. bool stressed_word = false;
  1259. int end_clause_after_tag = 0;
  1260. int end_clause_index = 0;
  1261. wchar_t xml_buf[N_XML_BUF+1];
  1262. #define N_XML_BUF2 20
  1263. char xml_buf2[N_XML_BUF2+2]; // for &<name> and &<number> sequences
  1264. static char ungot_string[N_XML_BUF2+4];
  1265. static int ungot_string_ix = -1;
  1266. if (clear_skipping_text) {
  1267. skipping_text = false;
  1268. clear_skipping_text = false;
  1269. }
  1270. tr->phonemes_repeat_count = 0;
  1271. tr->clause_upper_count = 0;
  1272. tr->clause_lower_count = 0;
  1273. *tone_type = 0;
  1274. *voice_change = 0;
  1275. if (ungot_word != NULL) {
  1276. strcpy(buf, ungot_word);
  1277. ix += strlen(ungot_word);
  1278. ungot_word = NULL;
  1279. }
  1280. if (ungot_char2 != 0)
  1281. c2 = ungot_char2;
  1282. else
  1283. c2 = GetC();
  1284. while (!Eof() || (ungot_char != 0) || (ungot_char2 != 0) || (ungot_string_ix >= 0)) {
  1285. if (!iswalnum(c1)) {
  1286. if ((end_character_position > 0) && (count_characters > end_character_position)) {
  1287. return CLAUSE_EOF;
  1288. }
  1289. if ((skip_characters > 0) && (count_characters >= skip_characters)) {
  1290. // reached the specified start position
  1291. // don't break a word
  1292. clear_skipping_text = true;
  1293. skip_characters = 0;
  1294. UngetC(c2);
  1295. return CLAUSE_NONE;
  1296. }
  1297. }
  1298. cprev2 = cprev;
  1299. cprev = c1;
  1300. c1 = c2;
  1301. if (ungot_string_ix >= 0) {
  1302. if (ungot_string[ungot_string_ix] == 0)
  1303. ungot_string_ix = -1;
  1304. }
  1305. if ((ungot_string_ix == 0) && (ungot_char2 == 0))
  1306. c1 = ungot_string[ungot_string_ix++];
  1307. if (ungot_string_ix >= 0)
  1308. c2 = ungot_string[ungot_string_ix++];
  1309. else {
  1310. c2 = GetC();
  1311. if (Eof())
  1312. c2 = ' ';
  1313. }
  1314. ungot_char2 = 0;
  1315. if ((option_ssml) && (phoneme_mode == 0)) {
  1316. if ((ssml_ignore_l_angle != '&') && (c1 == '&') && ((c2 == '#') || ((c2 >= 'a') && (c2 <= 'z')))) {
  1317. n_xml_buf = 0;
  1318. c1 = c2;
  1319. while (!Eof() && (iswalnum(c1) || (c1 == '#')) && (n_xml_buf < N_XML_BUF2)) {
  1320. xml_buf2[n_xml_buf++] = c1;
  1321. c1 = GetC();
  1322. }
  1323. xml_buf2[n_xml_buf] = 0;
  1324. c2 = GetC();
  1325. sprintf(ungot_string, "%s%c%c", &xml_buf2[0], c1, c2);
  1326. if (c1 == ';') {
  1327. if (xml_buf2[0] == '#') {
  1328. // character code number
  1329. if (xml_buf2[1] == 'x')
  1330. found = sscanf(&xml_buf2[2], "%x", (unsigned int *)(&c1));
  1331. else
  1332. found = sscanf(&xml_buf2[1], "%d", &c1);
  1333. } else {
  1334. if ((found = LookupMnem(xml_char_mnemonics, xml_buf2)) != -1) {
  1335. c1 = found;
  1336. if (c2 == 0)
  1337. c2 = ' ';
  1338. }
  1339. }
  1340. } else
  1341. found = -1;
  1342. if (found <= 0) {
  1343. ungot_string_ix = 0;
  1344. c1 = '&';
  1345. c2 = ' ';
  1346. }
  1347. if ((c1 <= 0x20) && ((sayas_mode == SAYAS_SINGLE_CHARS) || (sayas_mode == SAYAS_KEY)))
  1348. c1 += 0xe000; // move into unicode private usage area
  1349. } else if ((c1 == '<') && (ssml_ignore_l_angle != '<')) {
  1350. if ((c2 == '!') || (c2 == '?')) {
  1351. // a comment, ignore until closing '<' (or <?xml tag )
  1352. while (!Eof() && (c1 != '>'))
  1353. c1 = GetC();
  1354. c2 = ' ';
  1355. } else if ((c2 == '/') || iswalpha(c2)) {
  1356. // check for space in the output buffer for embedded commands produced by the SSML tag
  1357. if (ix > (n_buf - 20)) {
  1358. // Perhaps not enough room, end the clause before the SSML tag
  1359. UngetC(c2);
  1360. ungot_char2 = c1;
  1361. buf[ix] = ' ';
  1362. buf[ix+1] = 0;
  1363. return CLAUSE_NONE;
  1364. }
  1365. // SSML Tag
  1366. n_xml_buf = 0;
  1367. c1 = c2;
  1368. while (!Eof() && (c1 != '>') && (n_xml_buf < N_XML_BUF)) {
  1369. xml_buf[n_xml_buf++] = c1;
  1370. c1 = GetC();
  1371. }
  1372. xml_buf[n_xml_buf] = 0;
  1373. c2 = ' ';
  1374. self_closing = false;
  1375. if (xml_buf[n_xml_buf-1] == '/') {
  1376. // a self-closing tag
  1377. xml_buf[n_xml_buf-1] = ' ';
  1378. self_closing = true;
  1379. }
  1380. terminator = ProcessSsmlTag(xml_buf, buf, &ix, n_buf, self_closing);
  1381. if (terminator != 0) {
  1382. buf[ix] = ' ';
  1383. buf[ix++] = 0;
  1384. if (terminator & CLAUSE_TYPE_VOICE_CHANGE)
  1385. strcpy(voice_change, current_voice_id);
  1386. return terminator;
  1387. }
  1388. c1 = ' ';
  1389. c2 = GetC();
  1390. continue;
  1391. }
  1392. }
  1393. }
  1394. ssml_ignore_l_angle = 0;
  1395. if (ignore_text)
  1396. continue;
  1397. if ((c2 == '\n') && (option_linelength == -1)) {
  1398. // single-line mode, return immediately on NL
  1399. if ((terminator = clause_type_from_codepoint(c1)) == CLAUSE_NONE) {
  1400. charix[ix] = count_characters - clause_start_char;
  1401. *charix_top = ix;
  1402. ix += utf8_out(c1, &buf[ix]);
  1403. terminator = CLAUSE_PERIOD; // line doesn't end in punctuation, assume period
  1404. }
  1405. buf[ix] = ' ';
  1406. buf[ix+1] = 0;
  1407. return terminator;
  1408. }
  1409. if ((c1 == CTRL_EMBEDDED) || (c1 == ctrl_embedded)) {
  1410. // an embedded command. If it's a voice change, end the clause
  1411. if (c2 == 'V') {
  1412. buf[ix++] = 0; // end the clause at this point
  1413. while (!iswspace(c1 = GetC()) && !Eof() && (ix < (n_buf-1)))
  1414. buf[ix++] = c1; // add voice name to end of buffer, after the text
  1415. buf[ix++] = 0;
  1416. return CLAUSE_VOICE;
  1417. } else if (c2 == 'B') {
  1418. // set the punctuation option from an embedded command
  1419. // B0 B1 B<punct list><space>
  1420. strcpy(&buf[ix], " ");
  1421. ix += 3;
  1422. if ((c2 = GetC()) == '0')
  1423. option_punctuation = 0;
  1424. else {
  1425. option_punctuation = 1;
  1426. option_punctlist[0] = 0;
  1427. if (c2 != '1') {
  1428. // a list of punctuation characters to be spoken, terminated by space
  1429. j = 0;
  1430. while (!iswspace(c2) && !Eof()) {
  1431. option_punctlist[j++] = c2;
  1432. c2 = GetC();
  1433. buf[ix++] = ' ';
  1434. }
  1435. option_punctlist[j] = 0; // terminate punctuation list
  1436. option_punctuation = 2;
  1437. }
  1438. }
  1439. c2 = GetC();
  1440. continue;
  1441. }
  1442. }
  1443. linelength++;
  1444. if ((j = lookupwchar2(tr->chars_ignore, c1)) != 0) {
  1445. if (j == 1) {
  1446. // ignore this character (eg. zero-width-non-joiner U+200C)
  1447. continue;
  1448. }
  1449. c1 = j; // replace the character
  1450. }
  1451. if (iswalnum(c1))
  1452. any_alnum = true;
  1453. else {
  1454. if (stressed_word) {
  1455. stressed_word = false;
  1456. c1 = CHAR_EMPHASIS; // indicate this word is stressed
  1457. UngetC(c2);
  1458. c2 = ' ';
  1459. }
  1460. if (c1 == 0xf0b)
  1461. c1 = ' '; // Tibet inter-syllabic mark, ?? replace by space ??
  1462. if (iswspace(c1)) {
  1463. char *p_word;
  1464. if (tr->translator_name == 0x6a626f) {
  1465. // language jbo : lojban
  1466. // treat "i" or ".i" as end-of-sentence
  1467. p_word = &buf[ix-1];
  1468. if (p_word[0] == 'i') {
  1469. if (p_word[-1] == '.')
  1470. p_word--;
  1471. if (p_word[-1] == ' ') {
  1472. ungot_word = "i ";
  1473. UngetC(c2);
  1474. p_word[0] = 0;
  1475. return CLAUSE_PERIOD;
  1476. }
  1477. }
  1478. }
  1479. }
  1480. if (c1 == 0xd4d) {
  1481. // Malayalam virama, check if next character is Zero-width-joiner
  1482. if (c2 == 0x200d)
  1483. c1 = 0xd4e; // use this unofficial code for chillu-virama
  1484. }
  1485. }
  1486. if (iswupper(c1)) {
  1487. tr->clause_upper_count++;
  1488. if ((option_capitals == 2) && (sayas_mode == 0) && !iswupper(cprev)) {
  1489. char text_buf[40];
  1490. char text_buf2[30];
  1491. if (LookupSpecial(tr, "_cap", text_buf2) != NULL) {
  1492. sprintf(text_buf, "%s", text_buf2);
  1493. j = strlen(text_buf);
  1494. if ((ix + j) < n_buf) {
  1495. strcpy(&buf[ix], text_buf);
  1496. ix += j;
  1497. }
  1498. }
  1499. }
  1500. } else if (iswalpha(c1))
  1501. tr->clause_lower_count++;
  1502. if (option_phoneme_input) {
  1503. if (phoneme_mode > 0)
  1504. phoneme_mode--;
  1505. else if ((c1 == '[') && (c2 == '['))
  1506. phoneme_mode = -1; // input is phoneme mnemonics, so don't look for punctuation
  1507. else if ((c1 == ']') && (c2 == ']'))
  1508. phoneme_mode = 2; // set phoneme_mode to zero after the next two characters
  1509. }
  1510. if (c1 == '\n') {
  1511. parag = 0;
  1512. // count consecutive newlines, ignoring other spaces
  1513. while (!Eof() && iswspace(c2)) {
  1514. if (c2 == '\n')
  1515. parag++;
  1516. c2 = GetC();
  1517. }
  1518. if (parag > 0) {
  1519. // 2nd newline, assume paragraph
  1520. UngetC(c2);
  1521. if (end_clause_after_tag)
  1522. RemoveChar(&buf[end_clause_index]); // delete clause-end punctiation
  1523. buf[ix] = ' ';
  1524. buf[ix+1] = 0;
  1525. if (parag > 3)
  1526. parag = 3;
  1527. if (option_ssml) parag = 1;
  1528. return (CLAUSE_PARAGRAPH-30) + 30*parag; // several blank lines, longer pause
  1529. }
  1530. if (linelength <= option_linelength) {
  1531. // treat lines shorter than a specified length as end-of-clause
  1532. UngetC(c2);
  1533. buf[ix] = ' ';
  1534. buf[ix+1] = 0;
  1535. return CLAUSE_COLON;
  1536. }
  1537. linelength = 0;
  1538. }
  1539. announced_punctuation = 0;
  1540. if ((phoneme_mode == 0) && (sayas_mode == 0)) {
  1541. is_end_clause = false;
  1542. if (end_clause_after_tag) {
  1543. // Because of an xml tag, we are waiting for the
  1544. // next non-blank character to decide whether to end the clause
  1545. // i.e. is dot followed by an upper-case letter?
  1546. if (!iswspace(c1)) {
  1547. if (!IsAlpha(c1) || !iswlower(c1)) {
  1548. UngetC(c2);
  1549. ungot_char2 = c1;
  1550. buf[end_clause_index] = ' '; // delete the end-clause punctuation
  1551. buf[end_clause_index+1] = 0;
  1552. return end_clause_after_tag;
  1553. }
  1554. end_clause_after_tag = 0;
  1555. }
  1556. }
  1557. if ((c1 == '.') && (c2 == '.')) {
  1558. while ((c_next = GetC()) == '.') {
  1559. // 3 or more dots, replace by elipsis
  1560. c1 = 0x2026;
  1561. c2 = ' ';
  1562. }
  1563. if (c1 == 0x2026)
  1564. c2 = c_next;
  1565. else
  1566. UngetC(c_next);
  1567. }
  1568. punct_data = 0;
  1569. if ((punct_data = clause_type_from_codepoint(c1)) != CLAUSE_NONE) {
  1570. if (punct_data & CLAUSE_PUNCTUATION_IN_WORD) {
  1571. // Armenian punctuation inside a word
  1572. stressed_word = true;
  1573. *tone_type = punct_data >> 12 & 0xf; // override the end-of-sentence type
  1574. continue;
  1575. }
  1576. if ((iswspace(c2) || (punct_data & CLAUSE_OPTIONAL_SPACE_AFTER) || IsBracket(c2) || (c2 == '?') || Eof() || (c2 == ctrl_embedded))) { // don't check for '-' because it prevents recognizing ':-)'
  1577. // note: (c2='?') is for when a smart-quote has been replaced by '?'
  1578. is_end_clause = true;
  1579. }
  1580. }
  1581. // don't announce punctuation for the alternative text inside inside <audio> ... </audio>
  1582. if (c1 == 0xe000+'<') c1 = '<';
  1583. if (option_punctuation && iswpunct(c1) && (audio_text == false)) {
  1584. // option is set to explicitly speak punctuation characters
  1585. // if a list of allowed punctuation has been set up, check whether the character is in it
  1586. if ((option_punctuation == 1) || (wcschr(option_punctlist, c1) != NULL)) {
  1587. tr->phonemes_repeat_count = 0;
  1588. if ((terminator = AnnouncePunctuation(tr, c1, &c2, buf, &ix, is_end_clause)) >= 0)
  1589. return terminator;
  1590. announced_punctuation = c1;
  1591. }
  1592. }
  1593. if ((punct_data & CLAUSE_SPEAK_PUNCTUATION_NAME) && (announced_punctuation == 0)) {
  1594. // used for elipsis (and 3 dots) if a pronunciation for elipsis is given in *_list
  1595. char *p2;
  1596. p2 = &buf[ix];
  1597. sprintf(p2, "%s", LookupCharName(tr, c1, 1));
  1598. if (p2[0] != 0) {
  1599. ix += strlen(p2);
  1600. announced_punctuation = c1;
  1601. punct_data = punct_data & ~CLAUSE_INTONATION_TYPE; // change intonation type to 0 (full-stop)
  1602. }
  1603. }
  1604. if (is_end_clause) {
  1605. nl_count = 0;
  1606. c_next = c2;
  1607. if (iswspace(c_next)) {
  1608. while (!Eof() && iswspace(c_next)) {
  1609. if (c_next == '\n')
  1610. nl_count++;
  1611. c_next = GetC(); // skip past space(s)
  1612. }
  1613. }
  1614. if ((c1 == '.') && (nl_count < 2))
  1615. punct_data |= CLAUSE_DOT_AFTER_LAST_WORD;
  1616. if (nl_count == 0) {
  1617. if ((c1 == ',') && (cprev == '.') && (tr->translator_name == L('h', 'u')) && iswdigit(cprev2) && (iswdigit(c_next) || (iswlower(c_next)))) {
  1618. // lang=hu, fix for ordinal numbers, eg: "december 2., szerda", ignore ',' after ordinal number
  1619. c1 = CHAR_COMMA_BREAK;
  1620. is_end_clause = false;
  1621. }
  1622. if (c1 == '.') {
  1623. if ((tr->langopts.numbers & NUM_ORDINAL_DOT) &&
  1624. (iswdigit(cprev) || (IsRomanU(cprev) && (IsRomanU(cprev2) || iswspace(cprev2))))) { // lang=hu
  1625. // dot after a number indicates an ordinal number
  1626. if (!iswdigit(cprev))
  1627. is_end_clause = false; // Roman number followed by dot
  1628. else if (iswlower(c_next) || (c_next == '-')) // hyphen is needed for lang-hu (eg. 2.-kal)
  1629. is_end_clause = false; // only if followed by lower-case, (or if there is a XML tag)
  1630. } else if (c_next == '\'')
  1631. is_end_clause = false; // eg. u.s.a.'s
  1632. if (iswlower(c_next)) {
  1633. // next word has no capital letter, this dot is probably from an abbreviation
  1634. is_end_clause = 0;
  1635. }
  1636. if (any_alnum == false) {
  1637. // no letters or digits yet, so probably not a sentence terminator
  1638. // Here, dot is followed by space or bracket
  1639. c1 = ' ';
  1640. is_end_clause = false;
  1641. }
  1642. } else {
  1643. if (any_alnum == false) {
  1644. // no letters or digits yet, so probably not a sentence terminator
  1645. is_end_clause = false;
  1646. }
  1647. }
  1648. if (is_end_clause && (c1 == '.') && (c_next == '<') && option_ssml) {
  1649. // wait until after the end of the xml tag, then look for upper-case letter
  1650. is_end_clause = false;
  1651. end_clause_index = ix;
  1652. end_clause_after_tag = punct_data;
  1653. }
  1654. }
  1655. if (is_end_clause) {
  1656. UngetC(c_next);
  1657. buf[ix] = ' ';
  1658. buf[ix+1] = 0;
  1659. if (iswdigit(cprev) && !IsAlpha(c_next)) // ????
  1660. punct_data &= ~CLAUSE_DOT_AFTER_LAST_WORD;
  1661. if (nl_count > 1) {
  1662. if ((punct_data == CLAUSE_QUESTION) || (punct_data == CLAUSE_EXCLAMATION))
  1663. return punct_data + 35; // with a longer pause
  1664. return CLAUSE_PARAGRAPH;
  1665. }
  1666. return punct_data; // only recognise punctuation if followed by a blank or bracket/quote
  1667. } else if (!Eof()) {
  1668. if (iswspace(c2))
  1669. UngetC(c_next);
  1670. }
  1671. }
  1672. }
  1673. if (speech_parameters[espeakSILENCE] == 1)
  1674. continue;
  1675. if (c1 == announced_punctuation) {
  1676. // This character has already been announced, so delete it so that it isn't spoken a second time.
  1677. // Unless it's a hyphen or apostrophe (which is used by TranslateClause() )
  1678. if (IsBracket(c1))
  1679. c1 = 0xe000 + '('; // Unicode private useage area. So TranslateRules() knows the bracket name has been spoken
  1680. else if (c1 != '-')
  1681. c1 = ' ';
  1682. }
  1683. j = ix+1;
  1684. if (c1 == 0xe000 + '<') c1 = '<';
  1685. ix += utf8_out(c1, &buf[ix]);
  1686. if (!iswspace(c1) && !IsBracket(c1)) {
  1687. charix[ix] = count_characters - clause_start_char;
  1688. while (j < ix)
  1689. charix[j++] = -1; // subsequent bytes of a multibyte character
  1690. }
  1691. *charix_top = ix;
  1692. if (((ix > (n_buf-75)) && !IsAlpha(c1) && !iswdigit(c1)) || (ix >= (n_buf-4))) {
  1693. // clause too long, getting near end of buffer, so break here
  1694. // try to break at a word boundary (unless we actually reach the end of buffer).
  1695. // (n_buf-4) is to allow for 3 bytes of multibyte character plus terminator.
  1696. buf[ix] = ' ';
  1697. buf[ix+1] = 0;
  1698. UngetC(c2);
  1699. return CLAUSE_NONE;
  1700. }
  1701. }
  1702. if (stressed_word)
  1703. ix += utf8_out(CHAR_EMPHASIS, &buf[ix]);
  1704. if (end_clause_after_tag)
  1705. RemoveChar(&buf[end_clause_index]); // delete clause-end punctiation
  1706. buf[ix] = ' ';
  1707. buf[ix+1] = 0;
  1708. return CLAUSE_EOF; // end of file
  1709. }
  1710. void InitNamedata(void)
  1711. {
  1712. namedata_ix = 0;
  1713. if (namedata != NULL) {
  1714. free(namedata);
  1715. namedata = NULL;
  1716. n_namedata = 0;
  1717. }
  1718. }
  1719. void InitText2(void)
  1720. {
  1721. int param;
  1722. ungot_char = 0;
  1723. ungot_char2 = 0;
  1724. n_ssml_stack = 1;
  1725. n_param_stack = 1;
  1726. ssml_stack[0].tag_type = 0;
  1727. for (param = 0; param < N_SPEECH_PARAM; param++)
  1728. speech_parameters[param] = param_stack[0].parameter[param]; // set all speech parameters to defaults
  1729. option_punctuation = speech_parameters[espeakPUNCTUATION];
  1730. option_capitals = speech_parameters[espeakCAPITALS];
  1731. current_voice_id[0] = 0;
  1732. ignore_text = false;
  1733. audio_text = false;
  1734. clear_skipping_text = false;
  1735. count_characters = -1;
  1736. sayas_mode = 0;
  1737. xmlbase = NULL;
  1738. }