readclause.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116
  1. /*
  2. * Copyright (C) 2005 to 2015 by Jonathan Duddington
  3. * email: jonsd@users.sourceforge.net
  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 <ctype.h>
  20. #include <errno.h>
  21. #include <locale.h>
  22. #include <math.h>
  23. #include <stdint.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <wchar.h>
  29. #include <wctype.h>
  30. #include "espeak_ng.h"
  31. #include "encoding.h"
  32. #include "ucd.h"
  33. #include "error.h"
  34. #include "speech.h"
  35. #include "synthesize.h"
  36. #include "translate.h"
  37. #define N_XML_BUF 500
  38. static const char *xmlbase = ""; // base URL from <speak>
  39. static int namedata_ix = 0;
  40. static int n_namedata = 0;
  41. char *namedata = NULL;
  42. static int ungot_char2 = 0;
  43. espeak_ng_TEXT_DECODER *p_decoder = NULL;
  44. static int ungot_char;
  45. static const char *ungot_word = NULL;
  46. static int ignore_text = 0; // set during <sub> ... </sub> to ignore text which has been replaced by an alias
  47. static int audio_text = 0; // set during <audio> ... </audio>
  48. static int clear_skipping_text = 0; // next clause should clear the skipping_text flag
  49. int count_characters = 0;
  50. static int sayas_mode;
  51. static int sayas_start;
  52. static int ssml_ignore_l_angle = 0;
  53. // stack for language and voice properties
  54. // frame 0 is for the defaults, before any ssml tags.
  55. typedef struct {
  56. int tag_type;
  57. int voice_variant_number;
  58. int voice_gender;
  59. int voice_age;
  60. char voice_name[40];
  61. char language[20];
  62. } SSML_STACK;
  63. #define N_SSML_STACK 20
  64. static int n_ssml_stack;
  65. static SSML_STACK ssml_stack[N_SSML_STACK];
  66. static espeak_VOICE base_voice;
  67. static char base_voice_variant_name[40] = { 0 };
  68. static char current_voice_id[40] = { 0 };
  69. #define N_PARAM_STACK 20
  70. static int n_param_stack;
  71. PARAM_STACK param_stack[N_PARAM_STACK];
  72. static int speech_parameters[N_SPEECH_PARAM]; // current values, from param_stack
  73. int saved_parameters[N_SPEECH_PARAM]; // Parameters saved on synthesis start
  74. #define ESPEAKNG_CLAUSE_TYPE_PROPERTY_MASK 0xFFF0000000000000ull
  75. int clause_type_from_codepoint(uint32_t c)
  76. {
  77. ucd_category cat = ucd_lookup_category(c);
  78. ucd_property props = ucd_properties(c, cat);
  79. switch (props & ESPEAKNG_CLAUSE_TYPE_PROPERTY_MASK)
  80. {
  81. case ESPEAKNG_PROPERTY_FULL_STOP:
  82. return CLAUSE_PERIOD;
  83. case ESPEAKNG_PROPERTY_FULL_STOP | ESPEAKNG_PROPERTY_OPTIONAL_SPACE_AFTER:
  84. return CLAUSE_PERIOD | CLAUSE_OPTIONAL_SPACE_AFTER;
  85. case ESPEAKNG_PROPERTY_QUESTION_MARK:
  86. return CLAUSE_QUESTION;
  87. case ESPEAKNG_PROPERTY_QUESTION_MARK | ESPEAKNG_PROPERTY_OPTIONAL_SPACE_AFTER:
  88. return CLAUSE_QUESTION | CLAUSE_OPTIONAL_SPACE_AFTER;
  89. case ESPEAKNG_PROPERTY_QUESTION_MARK | ESPEAKNG_PROPERTY_PUNCTUATION_IN_WORD:
  90. return CLAUSE_QUESTION | CLAUSE_PUNCTUATION_IN_WORD;
  91. case ESPEAKNG_PROPERTY_EXCLAMATION_MARK:
  92. return CLAUSE_EXCLAMATION;
  93. case ESPEAKNG_PROPERTY_EXCLAMATION_MARK | ESPEAKNG_PROPERTY_OPTIONAL_SPACE_AFTER:
  94. return CLAUSE_EXCLAMATION | CLAUSE_OPTIONAL_SPACE_AFTER;
  95. case ESPEAKNG_PROPERTY_EXCLAMATION_MARK | ESPEAKNG_PROPERTY_PUNCTUATION_IN_WORD:
  96. return CLAUSE_EXCLAMATION | CLAUSE_PUNCTUATION_IN_WORD;
  97. case ESPEAKNG_PROPERTY_COMMA:
  98. return CLAUSE_COMMA;
  99. case ESPEAKNG_PROPERTY_COMMA | ESPEAKNG_PROPERTY_OPTIONAL_SPACE_AFTER:
  100. return CLAUSE_COMMA | CLAUSE_OPTIONAL_SPACE_AFTER;
  101. case ESPEAKNG_PROPERTY_COLON:
  102. return CLAUSE_COLON;
  103. case ESPEAKNG_PROPERTY_COLON | ESPEAKNG_PROPERTY_OPTIONAL_SPACE_AFTER:
  104. return CLAUSE_COLON | CLAUSE_OPTIONAL_SPACE_AFTER;
  105. case ESPEAKNG_PROPERTY_SEMI_COLON:
  106. case ESPEAKNG_PROPERTY_EXTENDED_DASH:
  107. return CLAUSE_SEMICOLON;
  108. case ESPEAKNG_PROPERTY_SEMI_COLON | ESPEAKNG_PROPERTY_OPTIONAL_SPACE_AFTER:
  109. case ESPEAKNG_PROPERTY_QUESTION_MARK | ESPEAKNG_PROPERTY_OPTIONAL_SPACE_AFTER | ESPEAKNG_PROPERTY_INVERTED_TERMINAL_PUNCTUATION:
  110. case ESPEAKNG_PROPERTY_EXCLAMATION_MARK | ESPEAKNG_PROPERTY_OPTIONAL_SPACE_AFTER | ESPEAKNG_PROPERTY_INVERTED_TERMINAL_PUNCTUATION:
  111. return CLAUSE_SEMICOLON | CLAUSE_OPTIONAL_SPACE_AFTER;
  112. case ESPEAKNG_PROPERTY_ELLIPSIS:
  113. return CLAUSE_SEMICOLON | CLAUSE_SPEAK_PUNCTUATION_NAME | CLAUSE_OPTIONAL_SPACE_AFTER;
  114. case ESPEAKNG_PROPERTY_PARAGRAPH_SEPARATOR:
  115. return CLAUSE_PARAGRAPH;
  116. }
  117. return CLAUSE_NONE;
  118. }
  119. const int param_defaults[N_SPEECH_PARAM] = {
  120. 0, // silence (internal use)
  121. 175, // rate wpm
  122. 100, // volume
  123. 50, // pitch
  124. 50, // range
  125. 0, // punctuation
  126. 0, // capital letters
  127. 0, // wordgap
  128. 0, // options
  129. 0, // intonation
  130. 0,
  131. 0,
  132. 0, // emphasis
  133. 0, // line length
  134. 0, // voice type
  135. };
  136. int towlower2(unsigned int c)
  137. {
  138. // check for non-standard upper to lower case conversions
  139. if (c == 'I' && translator->langopts.dotless_i)
  140. return 0x131; // I -> ı
  141. return tolower(c);
  142. }
  143. static int IsRomanU(unsigned int c)
  144. {
  145. if ((c == 'I') || (c == 'V') || (c == 'X') || (c == 'L'))
  146. return 1;
  147. return 0;
  148. }
  149. int Eof(void)
  150. {
  151. if (ungot_char != 0)
  152. return 0;
  153. return text_decoder_eof(p_decoder);
  154. }
  155. static int GetC(void)
  156. {
  157. int c1;
  158. if ((c1 = ungot_char) != 0) {
  159. ungot_char = 0;
  160. return c1;
  161. }
  162. count_characters++;
  163. return text_decoder_getc(p_decoder);
  164. }
  165. static void UngetC(int c)
  166. {
  167. ungot_char = c;
  168. }
  169. const char *WordToString2(unsigned int word)
  170. {
  171. // Convert a language mnemonic word into a string
  172. int ix;
  173. static char buf[5];
  174. char *p;
  175. p = buf;
  176. for (ix = 3; ix >= 0; ix--) {
  177. if ((*p = word >> (ix*8)) != 0)
  178. p++;
  179. }
  180. *p = 0;
  181. return buf;
  182. }
  183. static const char *LookupSpecial(Translator *tr, const char *string, char *text_out)
  184. {
  185. unsigned int flags[2];
  186. char phonemes[55];
  187. char phonemes2[55];
  188. char *string1 = (char *)string;
  189. flags[0] = flags[1] = 0;
  190. if (LookupDictList(tr, &string1, phonemes, flags, 0, NULL)) {
  191. SetWordStress(tr, phonemes, flags, -1, 0);
  192. DecodePhonemes(phonemes, phonemes2);
  193. sprintf(text_out, "[\002%s]]", phonemes2);
  194. return text_out;
  195. }
  196. return NULL;
  197. }
  198. static const char *LookupCharName(Translator *tr, int c, int only)
  199. {
  200. // Find the phoneme string (in ascii) to speak the name of character c
  201. // Used for punctuation characters and symbols
  202. int ix;
  203. unsigned int flags[2];
  204. char single_letter[24];
  205. char phonemes[60];
  206. char phonemes2[60];
  207. const char *lang_name = NULL;
  208. char *string;
  209. static char buf[60];
  210. buf[0] = 0;
  211. flags[0] = 0;
  212. flags[1] = 0;
  213. single_letter[0] = 0;
  214. single_letter[1] = '_';
  215. ix = utf8_out(c, &single_letter[2]);
  216. single_letter[2+ix] = 0;
  217. if (only) {
  218. string = &single_letter[2];
  219. LookupDictList(tr, &string, phonemes, flags, 0, NULL);
  220. } else {
  221. string = &single_letter[1];
  222. if (LookupDictList(tr, &string, phonemes, flags, 0, NULL) == 0) {
  223. // try _* then *
  224. string = &single_letter[2];
  225. if (LookupDictList(tr, &string, phonemes, flags, 0, NULL) == 0) {
  226. // now try the rules
  227. single_letter[1] = ' ';
  228. TranslateRules(tr, &single_letter[2], phonemes, sizeof(phonemes), NULL, 0, NULL);
  229. }
  230. }
  231. }
  232. if ((only == 0) && ((phonemes[0] == 0) || (phonemes[0] == phonSWITCH)) && (tr->translator_name != L('e', 'n'))) {
  233. // not found, try English
  234. SetTranslator2("en");
  235. string = &single_letter[1];
  236. single_letter[1] = '_';
  237. if (LookupDictList(translator2, &string, phonemes, flags, 0, NULL) == 0) {
  238. string = &single_letter[2];
  239. LookupDictList(translator2, &string, phonemes, flags, 0, NULL);
  240. }
  241. if (phonemes[0])
  242. lang_name = "en";
  243. else
  244. SelectPhonemeTable(voice->phoneme_tab_ix); // revert to original phoneme table
  245. }
  246. if (phonemes[0]) {
  247. if (lang_name) {
  248. SetWordStress(translator2, phonemes, flags, -1, 0);
  249. DecodePhonemes(phonemes, phonemes2);
  250. sprintf(buf, "[\002_^_%s %s _^_%s]]", "en", phonemes2, WordToString2(tr->translator_name));
  251. SelectPhonemeTable(voice->phoneme_tab_ix); // revert to original phoneme table
  252. } else {
  253. SetWordStress(tr, phonemes, flags, -1, 0);
  254. DecodePhonemes(phonemes, phonemes2);
  255. sprintf(buf, "[\002%s]] ", phonemes2);
  256. }
  257. } else if (only == 0)
  258. strcpy(buf, "[\002(X1)(X1)(X1)]]");
  259. return buf;
  260. }
  261. int Read4Bytes(FILE *f)
  262. {
  263. // Read 4 bytes (least significant first) into a int32
  264. char p4 [4];
  265. fread (p4, 1, 4, f);
  266. return get_int32_le (p4);
  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 static_cast<espeak_ng_STATUS> (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 header[3];
  291. char command[sizeof(fname2)+sizeof(fname2)+40];
  292. if (fseek(f, 20, SEEK_SET) == -1) {
  293. int error = errno;
  294. fclose(f);
  295. return create_file_error_context(context, static_cast<espeak_ng_STATUS> (error), fname);
  296. }
  297. for (ix = 0; ix < 3; ix++)
  298. header[ix] = Read4Bytes(f);
  299. // if the sound file is not mono, 16 bit signed, at the correct sample rate, then convert it
  300. if ((header[0] != 0x10001) || (header[1] != samplerate) || (header[2] != samplerate*2)) {
  301. fclose(f);
  302. f = NULL;
  303. #ifdef HAVE_MKSTEMP
  304. int fd_temp;
  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, static_cast<espeak_ng_STATUS> (errno), fname);
  320. }
  321. length = GetFileLength(fname);
  322. if (length < 0) { // length == -errno
  323. fclose(f);
  324. return create_file_error_context(context, static_cast<espeak_ng_STATUS> (-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, static_cast<espeak_ng_STATUS> (error), fname);
  330. }
  331. if ((p = (char *)realloc(soundicon_tab[index].data, length)) == NULL) {
  332. fclose(f);
  333. // return static_cast<espeak_ng_STATUS> (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, static_cast<espeak_ng_STATUS> (error), fname);
  341. }
  342. fclose(f);
  343. remove(fname_temp);
  344. int i32 = get_int32_le (p + 40); // ip = (int *)(&p[40]);
  345. soundicon_tab[index].length = i32 / 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. #define SSML_SPEAK 1
  475. #define SSML_VOICE 2
  476. #define SSML_PROSODY 3
  477. #define SSML_SAYAS 4
  478. #define SSML_MARK 5
  479. #define SSML_SENTENCE 6
  480. #define SSML_PARAGRAPH 7
  481. #define SSML_PHONEME 8
  482. #define SSML_SUB 9
  483. #define SSML_STYLE 10
  484. #define SSML_AUDIO 11
  485. #define SSML_EMPHASIS 12
  486. #define SSML_BREAK 13
  487. #define SSML_IGNORE_TEXT 14
  488. #define HTML_BREAK 15
  489. #define HTML_NOSPACE 16 // don't insert a space for this element, so it doesn't break a word
  490. #define SSML_CLOSE 0x20 // for a closing tag, OR this with the tag type
  491. // these tags have no effect if they are self-closing, eg. <voice />
  492. static char ignore_if_self_closing[] = { 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0 };
  493. static MNEM_TAB ssmltags[] = {
  494. { "speak", SSML_SPEAK },
  495. { "voice", SSML_VOICE },
  496. { "prosody", SSML_PROSODY },
  497. { "say-as", SSML_SAYAS },
  498. { "mark", SSML_MARK },
  499. { "s", SSML_SENTENCE },
  500. { "p", SSML_PARAGRAPH },
  501. { "phoneme", SSML_PHONEME },
  502. { "sub", SSML_SUB },
  503. { "tts:style", SSML_STYLE },
  504. { "audio", SSML_AUDIO },
  505. { "emphasis", SSML_EMPHASIS },
  506. { "break", SSML_BREAK },
  507. { "metadata", SSML_IGNORE_TEXT },
  508. { "br", HTML_BREAK },
  509. { "li", HTML_BREAK },
  510. { "dd", HTML_BREAK },
  511. { "img", HTML_BREAK },
  512. { "td", HTML_BREAK },
  513. { "h1", SSML_PARAGRAPH },
  514. { "h2", SSML_PARAGRAPH },
  515. { "h3", SSML_PARAGRAPH },
  516. { "h4", SSML_PARAGRAPH },
  517. { "hr", SSML_PARAGRAPH },
  518. { "script", SSML_IGNORE_TEXT },
  519. { "style", SSML_IGNORE_TEXT },
  520. { "font", HTML_NOSPACE },
  521. { "b", HTML_NOSPACE },
  522. { "i", HTML_NOSPACE },
  523. { "strong", HTML_NOSPACE },
  524. { "em", HTML_NOSPACE },
  525. { "code", HTML_NOSPACE },
  526. { NULL, 0 }
  527. };
  528. static const char *VoiceFromStack()
  529. {
  530. // Use the voice properties from the SSML stack to choose a voice, and switch
  531. // to that voice if it's not the current voice
  532. int ix;
  533. const char *p;
  534. SSML_STACK *sp;
  535. const char *v_id;
  536. int voice_name_specified;
  537. int voice_found;
  538. espeak_VOICE voice_select;
  539. static char voice_name[40];
  540. char language[40];
  541. char buf[80];
  542. strcpy(voice_name, ssml_stack[0].voice_name);
  543. strcpy(language, ssml_stack[0].language);
  544. voice_select.age = ssml_stack[0].voice_age;
  545. voice_select.gender = ssml_stack[0].voice_gender;
  546. voice_select.variant = ssml_stack[0].voice_variant_number;
  547. voice_select.identifier = NULL;
  548. for (ix = 0; ix < n_ssml_stack; ix++) {
  549. sp = &ssml_stack[ix];
  550. voice_name_specified = 0;
  551. if ((sp->voice_name[0] != 0) && (SelectVoiceByName(NULL, sp->voice_name) != NULL)) {
  552. voice_name_specified = 1;
  553. strcpy(voice_name, sp->voice_name);
  554. language[0] = 0;
  555. voice_select.gender = ENGENDER_UNKNOWN;
  556. voice_select.age = 0;
  557. voice_select.variant = 0;
  558. }
  559. if (sp->language[0] != 0) {
  560. strcpy(language, sp->language);
  561. // is this language provided by the base voice?
  562. p = base_voice.languages;
  563. while (*p++ != 0) {
  564. if (strcmp(p, language) == 0) {
  565. // yes, change the language to the main language of the base voice
  566. strcpy(language, &base_voice.languages[1]);
  567. break;
  568. }
  569. p += (strlen(p) + 1);
  570. }
  571. if (voice_name_specified == 0)
  572. voice_name[0] = 0; // forget a previous voice name if a language is specified
  573. }
  574. if (sp->voice_gender != ENGENDER_UNKNOWN)
  575. voice_select.gender = sp->voice_gender;
  576. if (sp->voice_age != 0)
  577. voice_select.age = sp->voice_age;
  578. if (sp->voice_variant_number != 0)
  579. voice_select.variant = sp->voice_variant_number;
  580. }
  581. voice_select.name = voice_name;
  582. voice_select.languages = language;
  583. v_id = SelectVoice(&voice_select, &voice_found);
  584. if (v_id == NULL)
  585. return "default";
  586. if ((strchr(v_id, '+') == NULL) && ((voice_select.gender == ENGENDER_UNKNOWN) || (voice_select.gender == base_voice.gender)) && (base_voice_variant_name[0] != 0)) {
  587. // a voice variant has not been selected, use the original voice variant
  588. sprintf(buf, "%s+%s", v_id, base_voice_variant_name);
  589. strncpy0(voice_name, buf, sizeof(voice_name));
  590. return voice_name;
  591. }
  592. return v_id;
  593. }
  594. static void ProcessParamStack(char *outbuf, int *outix)
  595. {
  596. // Set the speech parameters from the parameter stack
  597. int param;
  598. int ix;
  599. int value;
  600. char buf[20];
  601. int new_parameters[N_SPEECH_PARAM];
  602. static char cmd_letter[N_SPEECH_PARAM] = { 0, 'S', 'A', 'P', 'R', 0, 'C', 0, 0, 0, 0, 0, 'F' }; // embedded command letters
  603. for (param = 0; param < N_SPEECH_PARAM; param++)
  604. new_parameters[param] = -1;
  605. for (ix = 0; ix < n_param_stack; ix++) {
  606. for (param = 0; param < N_SPEECH_PARAM; param++) {
  607. if (param_stack[ix].parameter[param] >= 0)
  608. new_parameters[param] = param_stack[ix].parameter[param];
  609. }
  610. }
  611. for (param = 0; param < N_SPEECH_PARAM; param++) {
  612. if ((value = new_parameters[param]) != speech_parameters[param]) {
  613. buf[0] = 0;
  614. switch (param)
  615. {
  616. case espeakPUNCTUATION:
  617. option_punctuation = value-1;
  618. break;
  619. case espeakCAPITALS:
  620. option_capitals = value;
  621. break;
  622. case espeakRATE:
  623. case espeakVOLUME:
  624. case espeakPITCH:
  625. case espeakRANGE:
  626. case espeakEMPHASIS:
  627. sprintf(buf, "%c%d%c", CTRL_EMBEDDED, value, cmd_letter[param]);
  628. break;
  629. }
  630. speech_parameters[param] = new_parameters[param];
  631. strcpy(&outbuf[*outix], buf);
  632. *outix += strlen(buf);
  633. }
  634. }
  635. }
  636. static PARAM_STACK *PushParamStack(int tag_type)
  637. {
  638. int ix;
  639. PARAM_STACK *sp;
  640. sp = &param_stack[n_param_stack];
  641. if (n_param_stack < (N_PARAM_STACK-1))
  642. n_param_stack++;
  643. sp->type = tag_type;
  644. for (ix = 0; ix < N_SPEECH_PARAM; ix++)
  645. sp->parameter[ix] = -1;
  646. return sp;
  647. }
  648. static void PopParamStack(int tag_type, char *outbuf, int *outix)
  649. {
  650. // unwind the stack up to and including the previous tag of this type
  651. int ix;
  652. int top = 0;
  653. if (tag_type >= SSML_CLOSE)
  654. tag_type -= SSML_CLOSE;
  655. for (ix = 0; ix < n_param_stack; ix++) {
  656. if (param_stack[ix].type == tag_type)
  657. top = ix;
  658. }
  659. if (top > 0)
  660. n_param_stack = top;
  661. ProcessParamStack(outbuf, outix);
  662. }
  663. static wchar_t *GetSsmlAttribute(wchar_t *pw, const char *name)
  664. {
  665. // Gets the value string for an attribute.
  666. // Returns NULL if the attribute is not present
  667. int ix;
  668. static wchar_t empty[1] = { 0 };
  669. while (*pw != 0) {
  670. if (iswspace(pw[-1])) {
  671. ix = 0;
  672. while (*pw == name[ix]) {
  673. pw++;
  674. ix++;
  675. }
  676. if (name[ix] == 0) {
  677. // found the attribute, now get the value
  678. while (iswspace(*pw)) pw++;
  679. if (*pw == '=') pw++;
  680. while (iswspace(*pw)) pw++;
  681. if ((*pw == '"') || (*pw == '\'')) // allow single-quotes ?
  682. return pw+1;
  683. else
  684. return empty;
  685. }
  686. }
  687. pw++;
  688. }
  689. return NULL;
  690. }
  691. static int attrcmp(const wchar_t *string1, const char *string2)
  692. {
  693. int ix;
  694. if (string1 == NULL)
  695. return 1;
  696. for (ix = 0; (string1[ix] == string2[ix]) && (string1[ix] != 0); ix++)
  697. ;
  698. if (((string1[ix] == '"') || (string1[ix] == '\'')) && (string2[ix] == 0))
  699. return 0;
  700. return 1;
  701. }
  702. static int attrlookup(const wchar_t *string1, const MNEM_TAB *mtab)
  703. {
  704. int ix;
  705. for (ix = 0; mtab[ix].mnem != NULL; ix++) {
  706. if (attrcmp(string1, mtab[ix].mnem) == 0)
  707. return mtab[ix].value;
  708. }
  709. return mtab[ix].value;
  710. }
  711. static int attrnumber(const wchar_t *pw, int default_value, int type)
  712. {
  713. int value = 0;
  714. if ((pw == NULL) || !IsDigit09(*pw))
  715. return default_value;
  716. while (IsDigit09(*pw))
  717. value = value*10 + *pw++ - '0';
  718. if ((type == 1) && (tolower(*pw) == 's')) {
  719. // time: seconds rather than ms
  720. value *= 1000;
  721. }
  722. return value;
  723. }
  724. static int attrcopy_utf8(char *buf, const wchar_t *pw, int len)
  725. {
  726. // Convert attribute string into utf8, write to buf, and return its utf8 length
  727. unsigned int c;
  728. int ix = 0;
  729. int n;
  730. int prev_c = 0;
  731. if (pw != NULL) {
  732. while ((ix < (len-4)) && ((c = *pw++) != 0)) {
  733. if ((c == '"') && (prev_c != '\\'))
  734. break; // " indicates end of attribute, unless preceded by backstroke
  735. n = utf8_out(c, &buf[ix]);
  736. ix += n;
  737. prev_c = c;
  738. }
  739. }
  740. buf[ix] = 0;
  741. return ix;
  742. }
  743. static int attr_prosody_value(int param_type, const wchar_t *pw, int *value_out)
  744. {
  745. int sign = 0;
  746. wchar_t *tail;
  747. double value;
  748. while (iswspace(*pw)) pw++;
  749. if (*pw == '+') {
  750. pw++;
  751. sign = 1;
  752. }
  753. if (*pw == '-') {
  754. pw++;
  755. sign = -1;
  756. }
  757. value = (double)wcstod(pw, &tail);
  758. if (tail == pw) {
  759. // failed to find a number, return 100%
  760. *value_out = 100;
  761. return 2;
  762. }
  763. if (*tail == '%') {
  764. if (sign != 0)
  765. value = 100 + (sign * value);
  766. *value_out = (int)value;
  767. return 2; // percentage
  768. }
  769. if ((tail[0] == 's') && (tail[1] == 't')) {
  770. double x;
  771. // convert from semitones to a frequency percentage
  772. x = pow((double)2.0, (double)((value*sign)/12)) * 100;
  773. *value_out = (int)x;
  774. return 2; // percentage
  775. }
  776. if (param_type == espeakRATE) {
  777. if (sign == 0)
  778. *value_out = (int)(value * 100);
  779. else
  780. *value_out = 100 + (int)(sign * value * 100);
  781. return 2; // percentage
  782. }
  783. *value_out = (int)value;
  784. return sign; // -1, 0, or 1
  785. }
  786. static int AddNameData(const char *name, int wide)
  787. {
  788. // Add the name to the namedata and return its position
  789. // (Used by the Windows SAPI wrapper)
  790. int ix;
  791. int len;
  792. void *vp;
  793. if (wide) {
  794. len = (wcslen((const wchar_t *)name)+1)*sizeof(wchar_t);
  795. n_namedata = (n_namedata + sizeof(wchar_t) - 1) % sizeof(wchar_t); // round to wchar_t boundary
  796. } else
  797. len = strlen(name)+1;
  798. if (namedata_ix+len >= n_namedata) {
  799. // allocate more space for marker names
  800. if ((vp = realloc(namedata, namedata_ix+len + 1000)) == NULL)
  801. return -1; // failed to allocate, original data is unchanged but ignore this new name
  802. // !!! Bug?? If the allocated data shifts position, then pointers given to user application will be invalid
  803. namedata = (char *)vp;
  804. n_namedata = namedata_ix+len + 1000;
  805. }
  806. memcpy(&namedata[ix = namedata_ix], name, len);
  807. namedata_ix += len;
  808. return ix;
  809. }
  810. void SetVoiceStack(espeak_VOICE *v, const char *variant_name)
  811. {
  812. SSML_STACK *sp;
  813. sp = &ssml_stack[0];
  814. if (v == NULL) {
  815. memset(sp, 0, sizeof(ssml_stack[0]));
  816. return;
  817. }
  818. if (v->languages != NULL)
  819. strcpy(sp->language, v->languages);
  820. if (v->name != NULL)
  821. strncpy0(sp->voice_name, v->name, sizeof(sp->voice_name));
  822. sp->voice_variant_number = v->variant;
  823. sp->voice_age = v->age;
  824. sp->voice_gender = v->gender;
  825. if (memcmp(variant_name, "!v", 2) == 0)
  826. variant_name += 3; // strip variant directory name, !v plus PATHSEP
  827. strncpy0(base_voice_variant_name, variant_name, sizeof(base_voice_variant_name));
  828. memcpy(&base_voice, &current_voice_selected, sizeof(base_voice));
  829. }
  830. static int GetVoiceAttributes(wchar_t *pw, int tag_type)
  831. {
  832. // Determines whether voice attribute are specified in this tag, and if so, whether this means
  833. // a voice change.
  834. // If it's a closing tag, delete the top frame of the stack and determine whether this implies
  835. // a voice change.
  836. // Returns CLAUSE_TYPE_VOICE_CHANGE if there is a voice change
  837. wchar_t *lang;
  838. wchar_t *gender;
  839. wchar_t *name;
  840. wchar_t *age;
  841. wchar_t *variant;
  842. int value;
  843. const char *new_voice_id;
  844. SSML_STACK *ssml_sp;
  845. static const MNEM_TAB mnem_gender[] = {
  846. { "male", ENGENDER_MALE },
  847. { "female", ENGENDER_FEMALE },
  848. { "neutral", ENGENDER_NEUTRAL },
  849. { NULL, ENGENDER_UNKNOWN }
  850. };
  851. if (tag_type & SSML_CLOSE) {
  852. // delete a stack frame
  853. if (n_ssml_stack > 1)
  854. n_ssml_stack--;
  855. } else {
  856. // add a stack frame if any voice details are specified
  857. lang = GetSsmlAttribute(pw, "xml:lang");
  858. if (tag_type != SSML_VOICE) {
  859. // only expect an xml:lang attribute
  860. name = NULL;
  861. variant = NULL;
  862. age = NULL;
  863. gender = NULL;
  864. } else {
  865. name = GetSsmlAttribute(pw, "name");
  866. variant = GetSsmlAttribute(pw, "variant");
  867. age = GetSsmlAttribute(pw, "age");
  868. gender = GetSsmlAttribute(pw, "gender");
  869. }
  870. if ((tag_type != SSML_VOICE) && (lang == NULL))
  871. return 0; // <s> or <p> without language spec, nothing to do
  872. ssml_sp = &ssml_stack[n_ssml_stack++];
  873. attrcopy_utf8(ssml_sp->language, lang, sizeof(ssml_sp->language));
  874. attrcopy_utf8(ssml_sp->voice_name, name, sizeof(ssml_sp->voice_name));
  875. if ((value = attrnumber(variant, 1, 0)) > 0)
  876. value--; // variant='0' and variant='1' the same
  877. ssml_sp->voice_variant_number = value;
  878. ssml_sp->voice_age = attrnumber(age, 0, 0);
  879. ssml_sp->voice_gender = attrlookup(gender, mnem_gender);
  880. ssml_sp->tag_type = tag_type;
  881. }
  882. new_voice_id = VoiceFromStack();
  883. if (strcmp(new_voice_id, current_voice_id) != 0) {
  884. // add an embedded command to change the voice
  885. strcpy(current_voice_id, new_voice_id);
  886. return CLAUSE_TYPE_VOICE_CHANGE;
  887. }
  888. return 0;
  889. }
  890. static void SetProsodyParameter(int param_type, wchar_t *attr1, PARAM_STACK *sp)
  891. {
  892. int value;
  893. int sign;
  894. static const MNEM_TAB mnem_volume[] = {
  895. { "default", 100 },
  896. { "silent", 0 },
  897. { "x-soft", 30 },
  898. { "soft", 65 },
  899. { "medium", 100 },
  900. { "loud", 150 },
  901. { "x-loud", 230 },
  902. { NULL, -1 }
  903. };
  904. static const MNEM_TAB mnem_rate[] = {
  905. { "default", 100 },
  906. { "x-slow", 60 },
  907. { "slow", 80 },
  908. { "medium", 100 },
  909. { "fast", 125 },
  910. { "x-fast", 160 },
  911. { NULL, -1 }
  912. };
  913. static const MNEM_TAB mnem_pitch[] = {
  914. { "default", 100 },
  915. { "x-low", 70 },
  916. { "low", 85 },
  917. { "medium", 100 },
  918. { "high", 110 },
  919. { "x-high", 120 },
  920. { NULL, -1 }
  921. };
  922. static const MNEM_TAB mnem_range[] = {
  923. { "default", 100 },
  924. { "x-low", 20 },
  925. { "low", 50 },
  926. { "medium", 100 },
  927. { "high", 140 },
  928. { "x-high", 180 },
  929. { NULL, -1 }
  930. };
  931. static const MNEM_TAB *mnem_tabs[5] = {
  932. NULL, mnem_rate, mnem_volume, mnem_pitch, mnem_range
  933. };
  934. if ((value = attrlookup(attr1, mnem_tabs[param_type])) >= 0) {
  935. // mnemonic specifies a value as a percentage of the base pitch/range/rate/volume
  936. sp->parameter[param_type] = (param_stack[0].parameter[param_type] * value)/100;
  937. } else {
  938. sign = attr_prosody_value(param_type, attr1, &value);
  939. if (sign == 0)
  940. sp->parameter[param_type] = value; // absolute value in Hz
  941. else if (sign == 2) {
  942. // change specified as percentage or in semitones
  943. sp->parameter[param_type] = (speech_parameters[param_type] * value)/100;
  944. } else {
  945. // change specified as plus or minus Hz
  946. sp->parameter[param_type] = speech_parameters[param_type] + (value*sign);
  947. }
  948. }
  949. }
  950. static int ReplaceKeyName(char *outbuf, int index, int *outix)
  951. {
  952. // Replace some key-names by single characters, so they can be pronounced in different languages
  953. static MNEM_TAB keynames[] = {
  954. { "space ", 0xe020 },
  955. { "tab ", 0xe009 },
  956. { "underscore ", 0xe05f },
  957. { "double-quote ", '"' },
  958. { NULL, 0 }
  959. };
  960. int ix;
  961. int letter;
  962. char *p;
  963. p = &outbuf[index];
  964. if ((letter = LookupMnem(keynames, p)) != 0) {
  965. ix = utf8_out(letter, p);
  966. *outix = index + ix;
  967. return letter;
  968. }
  969. return 0;
  970. }
  971. static int ProcessSsmlTag(wchar_t *xml_buf, char *outbuf, int *outix, int n_outbuf, int self_closing)
  972. {
  973. // xml_buf is the tag and attributes with a zero terminator in place of the original '>'
  974. // returns a clause terminator value.
  975. unsigned int ix;
  976. int index;
  977. int c;
  978. int tag_type;
  979. int value;
  980. int value2;
  981. int value3;
  982. int voice_change_flag;
  983. wchar_t *px;
  984. wchar_t *attr1;
  985. wchar_t *attr2;
  986. wchar_t *attr3;
  987. int terminator;
  988. char *uri;
  989. int param_type;
  990. char tag_name[40];
  991. char buf[80];
  992. PARAM_STACK *sp;
  993. SSML_STACK *ssml_sp;
  994. static const MNEM_TAB mnem_phoneme_alphabet[] = {
  995. { "espeak", 1 },
  996. { NULL, -1 }
  997. };
  998. static const MNEM_TAB mnem_punct[] = {
  999. { "none", 1 },
  1000. { "all", 2 },
  1001. { "some", 3 },
  1002. { NULL, -1 }
  1003. };
  1004. static const MNEM_TAB mnem_capitals[] = {
  1005. { "no", 0 },
  1006. { "icon", 1 },
  1007. { "spelling", 2 },
  1008. { "pitch", 20 }, // this is the amount by which to raise the pitch
  1009. { NULL, -1 }
  1010. };
  1011. static const MNEM_TAB mnem_interpret_as[] = {
  1012. { "characters", SAYAS_CHARS },
  1013. { "tts:char", SAYAS_SINGLE_CHARS },
  1014. { "tts:key", SAYAS_KEY },
  1015. { "tts:digits", SAYAS_DIGITS },
  1016. { "telephone", SAYAS_DIGITS1 },
  1017. { NULL, -1 }
  1018. };
  1019. static const MNEM_TAB mnem_sayas_format[] = {
  1020. { "glyphs", 1 },
  1021. { NULL, -1 }
  1022. };
  1023. static const MNEM_TAB mnem_break[] = {
  1024. { "none", 0 },
  1025. { "x-weak", 1 },
  1026. { "weak", 2 },
  1027. { "medium", 3 },
  1028. { "strong", 4 },
  1029. { "x-strong", 5 },
  1030. { NULL, -1 }
  1031. };
  1032. static const MNEM_TAB mnem_emphasis[] = {
  1033. { "none", 1 },
  1034. { "reduced", 2 },
  1035. { "moderate", 3 },
  1036. { "strong", 4 },
  1037. { "x-strong", 5 },
  1038. { NULL, -1 }
  1039. };
  1040. static const char *prosody_attr[5] = {
  1041. NULL, "rate", "volume", "pitch", "range"
  1042. };
  1043. for (ix = 0; ix < (sizeof(tag_name)-1); ix++) {
  1044. if (((c = xml_buf[ix]) == 0) || iswspace(c))
  1045. break;
  1046. tag_name[ix] = tolower((char)c);
  1047. }
  1048. tag_name[ix] = 0;
  1049. px = &xml_buf[ix]; // the tag's attributes
  1050. if (tag_name[0] == '/') {
  1051. // closing tag
  1052. if ((tag_type = LookupMnem(ssmltags, &tag_name[1])) != HTML_NOSPACE)
  1053. outbuf[(*outix)++] = ' ';
  1054. tag_type += SSML_CLOSE;
  1055. } else {
  1056. if ((tag_type = LookupMnem(ssmltags, tag_name)) != HTML_NOSPACE) {
  1057. // separate SSML tags from the previous word (but not HMTL tags such as <b> <font> which can occur inside a word)
  1058. outbuf[(*outix)++] = ' ';
  1059. }
  1060. if (self_closing && ignore_if_self_closing[tag_type])
  1061. return 0;
  1062. }
  1063. voice_change_flag = 0;
  1064. ssml_sp = &ssml_stack[n_ssml_stack-1];
  1065. switch (tag_type)
  1066. {
  1067. case SSML_STYLE:
  1068. sp = PushParamStack(tag_type);
  1069. attr1 = GetSsmlAttribute(px, "field");
  1070. attr2 = GetSsmlAttribute(px, "mode");
  1071. if (attrcmp(attr1, "punctuation") == 0) {
  1072. value = attrlookup(attr2, mnem_punct);
  1073. sp->parameter[espeakPUNCTUATION] = value;
  1074. } else if (attrcmp(attr1, "capital_letters") == 0) {
  1075. value = attrlookup(attr2, mnem_capitals);
  1076. sp->parameter[espeakCAPITALS] = value;
  1077. }
  1078. ProcessParamStack(outbuf, outix);
  1079. break;
  1080. case SSML_PROSODY:
  1081. sp = PushParamStack(tag_type);
  1082. // look for attributes: rate, volume, pitch, range
  1083. for (param_type = espeakRATE; param_type <= espeakRANGE; param_type++) {
  1084. if ((attr1 = GetSsmlAttribute(px, prosody_attr[param_type])) != NULL)
  1085. SetProsodyParameter(param_type, attr1, sp);
  1086. }
  1087. ProcessParamStack(outbuf, outix);
  1088. break;
  1089. case SSML_EMPHASIS:
  1090. sp = PushParamStack(tag_type);
  1091. value = 3; // default is "moderate"
  1092. if ((attr1 = GetSsmlAttribute(px, "level")) != NULL)
  1093. value = attrlookup(attr1, mnem_emphasis);
  1094. if (translator->langopts.tone_language == 1) {
  1095. static unsigned char emphasis_to_pitch_range[] = { 50, 50, 40, 70, 90, 100 };
  1096. static unsigned char emphasis_to_volume[] = { 100, 100, 70, 110, 135, 150 };
  1097. // tone language (eg.Chinese) do emphasis by increasing the pitch range.
  1098. sp->parameter[espeakRANGE] = emphasis_to_pitch_range[value];
  1099. sp->parameter[espeakVOLUME] = emphasis_to_volume[value];
  1100. } else {
  1101. static unsigned char emphasis_to_volume2[] = { 100, 100, 75, 100, 120, 150 };
  1102. sp->parameter[espeakVOLUME] = emphasis_to_volume2[value];
  1103. sp->parameter[espeakEMPHASIS] = value;
  1104. }
  1105. ProcessParamStack(outbuf, outix);
  1106. break;
  1107. case SSML_STYLE + SSML_CLOSE:
  1108. case SSML_PROSODY + SSML_CLOSE:
  1109. case SSML_EMPHASIS + SSML_CLOSE:
  1110. PopParamStack(tag_type, outbuf, outix);
  1111. break;
  1112. case SSML_PHONEME:
  1113. attr1 = GetSsmlAttribute(px, "alphabet");
  1114. attr2 = GetSsmlAttribute(px, "ph");
  1115. value = attrlookup(attr1, mnem_phoneme_alphabet);
  1116. if (value == 1) { // alphabet="espeak"
  1117. outbuf[(*outix)++] = '[';
  1118. outbuf[(*outix)++] = '[';
  1119. *outix += attrcopy_utf8(&outbuf[*outix], attr2, n_outbuf-*outix);
  1120. outbuf[(*outix)++] = ']';
  1121. outbuf[(*outix)++] = ']';
  1122. }
  1123. break;
  1124. case SSML_SAYAS:
  1125. attr1 = GetSsmlAttribute(px, "interpret-as");
  1126. attr2 = GetSsmlAttribute(px, "format");
  1127. attr3 = GetSsmlAttribute(px, "detail");
  1128. value = attrlookup(attr1, mnem_interpret_as);
  1129. value2 = attrlookup(attr2, mnem_sayas_format);
  1130. if (value2 == 1)
  1131. value = SAYAS_GLYPHS;
  1132. value3 = attrnumber(attr3, 0, 0);
  1133. if (value == SAYAS_DIGITS) {
  1134. if (value3 <= 1)
  1135. value = SAYAS_DIGITS1;
  1136. else
  1137. value = SAYAS_DIGITS + value3;
  1138. }
  1139. sprintf(buf, "%c%dY", CTRL_EMBEDDED, value);
  1140. strcpy(&outbuf[*outix], buf);
  1141. *outix += strlen(buf);
  1142. sayas_start = *outix;
  1143. sayas_mode = value; // punctuation doesn't end clause during SAY-AS
  1144. break;
  1145. case SSML_SAYAS + SSML_CLOSE:
  1146. if (sayas_mode == SAYAS_KEY) {
  1147. outbuf[*outix] = 0;
  1148. ReplaceKeyName(outbuf, sayas_start, outix);
  1149. }
  1150. outbuf[(*outix)++] = CTRL_EMBEDDED;
  1151. outbuf[(*outix)++] = 'Y';
  1152. sayas_mode = 0;
  1153. break;
  1154. case SSML_SUB:
  1155. if ((attr1 = GetSsmlAttribute(px, "alias")) != NULL) {
  1156. // use the alias rather than the text
  1157. ignore_text = 1;
  1158. *outix += attrcopy_utf8(&outbuf[*outix], attr1, n_outbuf-*outix);
  1159. }
  1160. break;
  1161. case SSML_IGNORE_TEXT:
  1162. ignore_text = 1;
  1163. break;
  1164. case SSML_SUB + SSML_CLOSE:
  1165. case SSML_IGNORE_TEXT + SSML_CLOSE:
  1166. ignore_text = 0;
  1167. break;
  1168. case SSML_MARK:
  1169. if ((attr1 = GetSsmlAttribute(px, "name")) != NULL) {
  1170. // add name to circular buffer of marker names
  1171. attrcopy_utf8(buf, attr1, sizeof(buf));
  1172. if (strcmp(skip_marker, buf) == 0) {
  1173. // This is the marker we are waiting for before starting to speak
  1174. clear_skipping_text = 1;
  1175. skip_marker[0] = 0;
  1176. return CLAUSE_NONE;
  1177. }
  1178. if ((index = AddNameData(buf, 0)) >= 0) {
  1179. sprintf(buf, "%c%dM", CTRL_EMBEDDED, index);
  1180. strcpy(&outbuf[*outix], buf);
  1181. *outix += strlen(buf);
  1182. }
  1183. }
  1184. break;
  1185. case SSML_AUDIO:
  1186. sp = PushParamStack(tag_type);
  1187. if ((attr1 = GetSsmlAttribute(px, "src")) != NULL) {
  1188. char fname[256];
  1189. attrcopy_utf8(buf, attr1, sizeof(buf));
  1190. if (uri_callback == NULL) {
  1191. if ((xmlbase != NULL) && (buf[0] != '/')) {
  1192. sprintf(fname, "%s/%s", xmlbase, buf);
  1193. index = LoadSoundFile2(fname);
  1194. } else
  1195. index = LoadSoundFile2(buf);
  1196. if (index >= 0) {
  1197. sprintf(buf, "%c%dI", CTRL_EMBEDDED, index);
  1198. strcpy(&outbuf[*outix], buf);
  1199. *outix += strlen(buf);
  1200. sp->parameter[espeakSILENCE] = 1;
  1201. }
  1202. } else {
  1203. if ((index = AddNameData(buf, 0)) >= 0) {
  1204. uri = &namedata[index];
  1205. if (uri_callback(1, uri, xmlbase) == 0) {
  1206. sprintf(buf, "%c%dU", CTRL_EMBEDDED, index);
  1207. strcpy(&outbuf[*outix], buf);
  1208. *outix += strlen(buf);
  1209. sp->parameter[espeakSILENCE] = 1;
  1210. }
  1211. }
  1212. }
  1213. }
  1214. ProcessParamStack(outbuf, outix);
  1215. if (self_closing)
  1216. PopParamStack(tag_type, outbuf, outix);
  1217. else
  1218. audio_text = 1;
  1219. return CLAUSE_NONE;
  1220. case SSML_AUDIO + SSML_CLOSE:
  1221. PopParamStack(tag_type, outbuf, outix);
  1222. audio_text = 0;
  1223. return CLAUSE_NONE;
  1224. case SSML_BREAK:
  1225. value = 21;
  1226. terminator = CLAUSE_NONE;
  1227. if ((attr1 = GetSsmlAttribute(px, "strength")) != NULL) {
  1228. static int break_value[6] = { 0, 7, 14, 21, 40, 80 }; // *10mS
  1229. value = attrlookup(attr1, mnem_break);
  1230. if (value < 3) {
  1231. // adjust prepause on the following word
  1232. sprintf(&outbuf[*outix], "%c%dB", CTRL_EMBEDDED, value);
  1233. *outix += 3;
  1234. terminator = 0;
  1235. }
  1236. value = break_value[value];
  1237. }
  1238. if ((attr2 = GetSsmlAttribute(px, "time")) != NULL) {
  1239. value2 = attrnumber(attr2, 0, 1); // pause in mS
  1240. // compensate for speaking speed to keep constant pause length, see function PauseLength()
  1241. // 'value' here is x 10mS
  1242. value = (value2 * 256) / (speed.clause_pause_factor * 10);
  1243. if (value < 200)
  1244. value = (value2 * 256) / (speed.pause_factor * 10);
  1245. if (terminator == 0)
  1246. terminator = CLAUSE_NONE;
  1247. }
  1248. if (terminator) {
  1249. if (value > 0xfff) {
  1250. // scale down the value and set a scaling indicator bit
  1251. value = value / 32;
  1252. if (value > 0xfff)
  1253. value = 0xfff;
  1254. terminator |= CLAUSE_PAUSE_LONG;
  1255. }
  1256. return terminator + value;
  1257. }
  1258. break;
  1259. case SSML_SPEAK:
  1260. if ((attr1 = GetSsmlAttribute(px, "xml:base")) != NULL) {
  1261. attrcopy_utf8(buf, attr1, sizeof(buf));
  1262. if ((index = AddNameData(buf, 0)) >= 0)
  1263. xmlbase = &namedata[index];
  1264. }
  1265. if (GetVoiceAttributes(px, tag_type) == 0)
  1266. return 0; // no voice change
  1267. return CLAUSE_VOICE;
  1268. case SSML_VOICE:
  1269. if (GetVoiceAttributes(px, tag_type) == 0)
  1270. return 0; // no voice change
  1271. return CLAUSE_VOICE;
  1272. case SSML_SPEAK + SSML_CLOSE:
  1273. // unwind stack until the previous <voice> or <speak> tag
  1274. while ((n_ssml_stack > 1) && (ssml_stack[n_ssml_stack-1].tag_type != SSML_SPEAK))
  1275. n_ssml_stack--;
  1276. return CLAUSE_PERIOD + GetVoiceAttributes(px, tag_type);
  1277. case SSML_VOICE + SSML_CLOSE:
  1278. // unwind stack until the previous <voice> or <speak> tag
  1279. while ((n_ssml_stack > 1) && (ssml_stack[n_ssml_stack-1].tag_type != SSML_VOICE))
  1280. n_ssml_stack--;
  1281. terminator = 0; // ?? Sentence intonation, but no pause ??
  1282. return terminator + GetVoiceAttributes(px, tag_type);
  1283. case HTML_BREAK:
  1284. case HTML_BREAK + SSML_CLOSE:
  1285. return CLAUSE_COLON;
  1286. case SSML_SENTENCE:
  1287. if (ssml_sp->tag_type == SSML_SENTENCE) {
  1288. // new sentence implies end-of-sentence
  1289. voice_change_flag = GetVoiceAttributes(px, SSML_SENTENCE+SSML_CLOSE);
  1290. }
  1291. voice_change_flag |= GetVoiceAttributes(px, tag_type);
  1292. return CLAUSE_PARAGRAPH + voice_change_flag;
  1293. case SSML_PARAGRAPH:
  1294. if (ssml_sp->tag_type == SSML_SENTENCE) {
  1295. // new paragraph implies end-of-sentence or end-of-paragraph
  1296. voice_change_flag = GetVoiceAttributes(px, SSML_SENTENCE+SSML_CLOSE);
  1297. }
  1298. if (ssml_sp->tag_type == SSML_PARAGRAPH) {
  1299. // new paragraph implies end-of-sentence or end-of-paragraph
  1300. voice_change_flag |= GetVoiceAttributes(px, SSML_PARAGRAPH+SSML_CLOSE);
  1301. }
  1302. voice_change_flag |= GetVoiceAttributes(px, tag_type);
  1303. return CLAUSE_PARAGRAPH + voice_change_flag;
  1304. case SSML_SENTENCE + SSML_CLOSE:
  1305. if (ssml_sp->tag_type == SSML_SENTENCE) {
  1306. // end of a sentence which specified a language
  1307. voice_change_flag = GetVoiceAttributes(px, tag_type);
  1308. }
  1309. return CLAUSE_PERIOD + voice_change_flag;
  1310. case SSML_PARAGRAPH + SSML_CLOSE:
  1311. if ((ssml_sp->tag_type == SSML_SENTENCE) || (ssml_sp->tag_type == SSML_PARAGRAPH)) {
  1312. // End of a paragraph which specified a language.
  1313. // (End-of-paragraph also implies end-of-sentence)
  1314. return GetVoiceAttributes(px, tag_type) + CLAUSE_PARAGRAPH;
  1315. }
  1316. return CLAUSE_PARAGRAPH;
  1317. }
  1318. return 0;
  1319. }
  1320. static void RemoveChar(char *p)
  1321. {
  1322. // Replace a UTF-8 character by spaces
  1323. int c;
  1324. memset(p, ' ', utf8_in(&c, p));
  1325. }
  1326. static MNEM_TAB xml_char_mnemonics[] = {
  1327. { "gt", '>' },
  1328. { "lt", 0xe000 + '<' }, // private usage area, to avoid confusion with XML tag
  1329. { "amp", '&' },
  1330. { "quot", '"' },
  1331. { "nbsp", ' ' },
  1332. { "apos", '\'' },
  1333. { NULL, -1 }
  1334. };
  1335. int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_buf, int *tone_type, char *voice_change)
  1336. {
  1337. /* Find the end of the current clause.
  1338. Write the clause into buf
  1339. returns: clause type (bits 0-7: pause x10mS, bits 8-11 intonation type)
  1340. Also checks for blank line (paragraph) as end-of-clause indicator.
  1341. Does not end clause for:
  1342. punctuation immediately followed by alphanumeric eg. 1.23 !Speak :path
  1343. repeated punctuation, eg. ... !!!
  1344. */
  1345. int c1 = ' '; // current character
  1346. int c2; // next character
  1347. int cprev = ' '; // previous character
  1348. int cprev2 = ' ';
  1349. int c_next;
  1350. int parag;
  1351. int ix = 0;
  1352. int j;
  1353. int nl_count;
  1354. int linelength = 0;
  1355. int phoneme_mode = 0;
  1356. int n_xml_buf;
  1357. int terminator;
  1358. int found;
  1359. int any_alnum = 0;
  1360. int self_closing;
  1361. int punct_data = 0;
  1362. int is_end_clause;
  1363. int announced_punctuation = 0;
  1364. int stressed_word = 0;
  1365. int end_clause_after_tag = 0;
  1366. int end_clause_index = 0;
  1367. wchar_t xml_buf[N_XML_BUF+1];
  1368. #define N_XML_BUF2 20
  1369. char xml_buf2[N_XML_BUF2+2]; // for &<name> and &<number> sequences
  1370. static char ungot_string[N_XML_BUF2+4];
  1371. static int ungot_string_ix = -1;
  1372. if (clear_skipping_text) {
  1373. skipping_text = 0;
  1374. clear_skipping_text = 0;
  1375. }
  1376. tr->phonemes_repeat_count = 0;
  1377. tr->clause_upper_count = 0;
  1378. tr->clause_lower_count = 0;
  1379. *tone_type = 0;
  1380. *voice_change = 0;
  1381. if (ungot_word != NULL) {
  1382. strcpy(buf, ungot_word);
  1383. ix += strlen(ungot_word);
  1384. ungot_word = NULL;
  1385. }
  1386. if (ungot_char2 != 0)
  1387. c2 = ungot_char2;
  1388. else
  1389. c2 = GetC();
  1390. while (!Eof() || (ungot_char != 0) || (ungot_char2 != 0) || (ungot_string_ix >= 0)) {
  1391. if (!iswalnum(c1)) {
  1392. if ((end_character_position > 0) && (count_characters > end_character_position)) {
  1393. return CLAUSE_EOF;
  1394. }
  1395. if ((skip_characters > 0) && (count_characters >= skip_characters)) {
  1396. // reached the specified start position
  1397. // don't break a word
  1398. clear_skipping_text = 1;
  1399. skip_characters = 0;
  1400. UngetC(c2);
  1401. return CLAUSE_NONE;
  1402. }
  1403. }
  1404. cprev2 = cprev;
  1405. cprev = c1;
  1406. c1 = c2;
  1407. if (ungot_string_ix >= 0) {
  1408. if (ungot_string[ungot_string_ix] == 0)
  1409. ungot_string_ix = -1;
  1410. }
  1411. if ((ungot_string_ix == 0) && (ungot_char2 == 0))
  1412. c1 = ungot_string[ungot_string_ix++];
  1413. if (ungot_string_ix >= 0)
  1414. c2 = ungot_string[ungot_string_ix++];
  1415. else {
  1416. c2 = GetC();
  1417. if (Eof())
  1418. c2 = ' ';
  1419. }
  1420. ungot_char2 = 0;
  1421. if ((option_ssml) && (phoneme_mode == 0)) {
  1422. if ((ssml_ignore_l_angle != '&') && (c1 == '&') && ((c2 == '#') || ((c2 >= 'a') && (c2 <= 'z')))) {
  1423. n_xml_buf = 0;
  1424. c1 = c2;
  1425. while (!Eof() && (iswalnum(c1) || (c1 == '#')) && (n_xml_buf < N_XML_BUF2)) {
  1426. xml_buf2[n_xml_buf++] = c1;
  1427. c1 = GetC();
  1428. }
  1429. xml_buf2[n_xml_buf] = 0;
  1430. c2 = GetC();
  1431. sprintf(ungot_string, "%s%c%c", &xml_buf2[0], c1, c2);
  1432. if (c1 == ';') {
  1433. if (xml_buf2[0] == '#') {
  1434. // character code number
  1435. if (xml_buf2[1] == 'x')
  1436. found = sscanf(&xml_buf2[2], "%x", (unsigned int *)(&c1));
  1437. else
  1438. found = sscanf(&xml_buf2[1], "%d", &c1);
  1439. } else {
  1440. if ((found = LookupMnem(xml_char_mnemonics, xml_buf2)) != -1) {
  1441. c1 = found;
  1442. if (c2 == 0)
  1443. c2 = ' ';
  1444. }
  1445. }
  1446. } else
  1447. found = -1;
  1448. if (found <= 0) {
  1449. ungot_string_ix = 0;
  1450. c1 = '&';
  1451. c2 = ' ';
  1452. }
  1453. if ((c1 <= 0x20) && ((sayas_mode == SAYAS_SINGLE_CHARS) || (sayas_mode == SAYAS_KEY)))
  1454. c1 += 0xe000; // move into unicode private usage area
  1455. } else if ((c1 == '<') && (ssml_ignore_l_angle != '<')) {
  1456. if ((c2 == '!') || (c2 == '?')) {
  1457. // a comment, ignore until closing '<' (or <?xml tag )
  1458. while (!Eof() && (c1 != '>'))
  1459. c1 = GetC();
  1460. c2 = ' ';
  1461. } else if ((c2 == '/') || iswalpha(c2)) {
  1462. // check for space in the output buffer for embedded commands produced by the SSML tag
  1463. if (ix > (n_buf - 20)) {
  1464. // Perhaps not enough room, end the clause before the SSML tag
  1465. UngetC(c2);
  1466. ungot_char2 = c1;
  1467. buf[ix] = ' ';
  1468. buf[ix+1] = 0;
  1469. return CLAUSE_NONE;
  1470. }
  1471. // SSML Tag
  1472. n_xml_buf = 0;
  1473. c1 = c2;
  1474. while (!Eof() && (c1 != '>') && (n_xml_buf < N_XML_BUF)) {
  1475. xml_buf[n_xml_buf++] = c1;
  1476. c1 = GetC();
  1477. }
  1478. xml_buf[n_xml_buf] = 0;
  1479. c2 = ' ';
  1480. self_closing = 0;
  1481. if (xml_buf[n_xml_buf-1] == '/') {
  1482. // a self-closing tag
  1483. xml_buf[n_xml_buf-1] = ' ';
  1484. self_closing = 1;
  1485. }
  1486. terminator = ProcessSsmlTag(xml_buf, buf, &ix, n_buf, self_closing);
  1487. if (terminator != 0) {
  1488. if (end_clause_after_tag)
  1489. ix = end_clause_index;
  1490. buf[ix] = ' ';
  1491. buf[ix++] = 0;
  1492. if (terminator & CLAUSE_TYPE_VOICE_CHANGE)
  1493. strcpy(voice_change, current_voice_id);
  1494. return terminator;
  1495. }
  1496. c1 = ' ';
  1497. c2 = GetC();
  1498. continue;
  1499. }
  1500. }
  1501. }
  1502. ssml_ignore_l_angle = 0;
  1503. if (ignore_text)
  1504. continue;
  1505. if ((c2 == '\n') && (option_linelength == -1)) {
  1506. // single-line mode, return immediately on NL
  1507. if ((terminator = clause_type_from_codepoint(c1)) == CLAUSE_NONE) {
  1508. charix[ix] = count_characters - clause_start_char;
  1509. *charix_top = ix;
  1510. ix += utf8_out(c1, &buf[ix]);
  1511. terminator = CLAUSE_PERIOD; // line doesn't end in punctuation, assume period
  1512. }
  1513. buf[ix] = ' ';
  1514. buf[ix+1] = 0;
  1515. return terminator;
  1516. }
  1517. if ((c1 == CTRL_EMBEDDED) || (c1 == ctrl_embedded)) {
  1518. // an embedded command. If it's a voice change, end the clause
  1519. if (c2 == 'V') {
  1520. buf[ix++] = 0; // end the clause at this point
  1521. while (!iswspace(c1 = GetC()) && !Eof() && (ix < (n_buf-1)))
  1522. buf[ix++] = c1; // add voice name to end of buffer, after the text
  1523. buf[ix++] = 0;
  1524. return CLAUSE_VOICE;
  1525. } else if (c2 == 'B') {
  1526. // set the punctuation option from an embedded command
  1527. // B0 B1 B<punct list><space>
  1528. strcpy(&buf[ix], " ");
  1529. ix += 3;
  1530. if ((c2 = GetC()) == '0')
  1531. option_punctuation = 0;
  1532. else {
  1533. option_punctuation = 1;
  1534. option_punctlist[0] = 0;
  1535. if (c2 != '1') {
  1536. // a list of punctuation characters to be spoken, terminated by space
  1537. j = 0;
  1538. while (!iswspace(c2) && !Eof()) {
  1539. option_punctlist[j++] = c2;
  1540. c2 = GetC();
  1541. buf[ix++] = ' ';
  1542. }
  1543. option_punctlist[j] = 0; // terminate punctuation list
  1544. option_punctuation = 2;
  1545. }
  1546. }
  1547. c2 = GetC();
  1548. continue;
  1549. }
  1550. }
  1551. linelength++;
  1552. if ((j = lookupwchar2(tr->chars_ignore, c1)) != 0) {
  1553. if (j == 1) {
  1554. // ignore this character (eg. zero-width-non-joiner U+200C)
  1555. continue;
  1556. }
  1557. c1 = j; // replace the character
  1558. }
  1559. if (iswalnum(c1))
  1560. any_alnum = 1;
  1561. else {
  1562. if (stressed_word) {
  1563. stressed_word = 0;
  1564. c1 = CHAR_EMPHASIS; // indicate this word is stressed
  1565. UngetC(c2);
  1566. c2 = ' ';
  1567. }
  1568. if (c1 == 0xf0b)
  1569. c1 = ' '; // Tibet inter-syllabic mark, ?? replace by space ??
  1570. if (iswspace(c1)) {
  1571. char *p_word;
  1572. if (tr->translator_name == 0x6a626f) {
  1573. // language jbo : lojban
  1574. // treat "i" or ".i" as end-of-sentence
  1575. p_word = &buf[ix-1];
  1576. if (p_word[0] == 'i') {
  1577. if (p_word[-1] == '.')
  1578. p_word--;
  1579. if (p_word[-1] == ' ') {
  1580. ungot_word = "i ";
  1581. UngetC(c2);
  1582. p_word[0] = 0;
  1583. return CLAUSE_PERIOD;
  1584. }
  1585. }
  1586. }
  1587. }
  1588. if (c1 == 0xd4d) {
  1589. // Malayalam virama, check if next character is Zero-width-joiner
  1590. if (c2 == 0x200d)
  1591. c1 = 0xd4e; // use this unofficial code for chillu-virama
  1592. }
  1593. }
  1594. if (iswupper(c1)) {
  1595. tr->clause_upper_count++;
  1596. if ((option_capitals == 2) && (sayas_mode == 0) && !iswupper(cprev)) {
  1597. char text_buf[40];
  1598. char text_buf2[30];
  1599. if (LookupSpecial(tr, "_cap", text_buf2) != NULL) {
  1600. sprintf(text_buf, "%s", text_buf2);
  1601. j = strlen(text_buf);
  1602. if ((ix + j) < n_buf) {
  1603. strcpy(&buf[ix], text_buf);
  1604. ix += j;
  1605. }
  1606. }
  1607. }
  1608. } else if (iswalpha(c1))
  1609. tr->clause_lower_count++;
  1610. if (option_phoneme_input) {
  1611. if (phoneme_mode > 0)
  1612. phoneme_mode--;
  1613. else if ((c1 == '[') && (c2 == '['))
  1614. phoneme_mode = -1; // input is phoneme mnemonics, so don't look for punctuation
  1615. else if ((c1 == ']') && (c2 == ']'))
  1616. phoneme_mode = 2; // set phoneme_mode to zero after the next two characters
  1617. }
  1618. if (c1 == '\n') {
  1619. parag = 0;
  1620. // count consecutive newlines, ignoring other spaces
  1621. while (!Eof() && iswspace(c2)) {
  1622. if (c2 == '\n')
  1623. parag++;
  1624. c2 = GetC();
  1625. }
  1626. if (parag > 0) {
  1627. // 2nd newline, assume paragraph
  1628. UngetC(c2);
  1629. if (end_clause_after_tag)
  1630. RemoveChar(&buf[end_clause_index]); // delete clause-end punctiation
  1631. buf[ix] = ' ';
  1632. buf[ix+1] = 0;
  1633. if (parag > 3)
  1634. parag = 3;
  1635. if (option_ssml) parag = 1;
  1636. return (CLAUSE_PARAGRAPH-30) + 30*parag; // several blank lines, longer pause
  1637. }
  1638. if (linelength <= option_linelength) {
  1639. // treat lines shorter than a specified length as end-of-clause
  1640. UngetC(c2);
  1641. buf[ix] = ' ';
  1642. buf[ix+1] = 0;
  1643. return CLAUSE_COLON;
  1644. }
  1645. linelength = 0;
  1646. }
  1647. announced_punctuation = 0;
  1648. if ((phoneme_mode == 0) && (sayas_mode == 0)) {
  1649. is_end_clause = 0;
  1650. if (end_clause_after_tag) {
  1651. // Because of an xml tag, we are waiting for the
  1652. // next non-blank character to decide whether to end the clause
  1653. // i.e. is dot followed by an upper-case letter?
  1654. if (!iswspace(c1)) {
  1655. if (!IsAlpha(c1) || !iswlower(c1)) {
  1656. UngetC(c2);
  1657. ungot_char2 = c1;
  1658. buf[end_clause_index] = ' '; // delete the end-clause punctuation
  1659. buf[end_clause_index+1] = 0;
  1660. return end_clause_after_tag;
  1661. }
  1662. end_clause_after_tag = 0;
  1663. }
  1664. }
  1665. if ((c1 == '.') && (c2 == '.')) {
  1666. while ((c_next = GetC()) == '.') {
  1667. // 3 or more dots, replace by elipsis
  1668. c1 = 0x2026;
  1669. c2 = ' ';
  1670. }
  1671. if (c1 == 0x2026)
  1672. c2 = c_next;
  1673. else
  1674. UngetC(c_next);
  1675. }
  1676. punct_data = 0;
  1677. if ((punct_data = clause_type_from_codepoint(c1)) != CLAUSE_NONE) {
  1678. if (punct_data & CLAUSE_PUNCTUATION_IN_WORD) {
  1679. // Armenian punctuation inside a word
  1680. stressed_word = 1;
  1681. *tone_type = punct_data >> 12 & 0xf; // override the end-of-sentence type
  1682. continue;
  1683. }
  1684. if ((iswspace(c2) || (punct_data & CLAUSE_OPTIONAL_SPACE_AFTER) || IsBracket(c2) || (c2 == '?') || Eof() || (c2 == ctrl_embedded))) { // don't check for '-' because it prevents recognizing ':-)'
  1685. // note: (c2='?') is for when a smart-quote has been replaced by '?'
  1686. is_end_clause = 1;
  1687. }
  1688. }
  1689. // don't announce punctuation for the alternative text inside inside <audio> ... </audio>
  1690. if (c1 == 0xe000+'<') c1 = '<';
  1691. if (option_punctuation && iswpunct(c1) && (audio_text == 0)) {
  1692. // option is set to explicitly speak punctuation characters
  1693. // if a list of allowed punctuation has been set up, check whether the character is in it
  1694. if ((option_punctuation == 1) || (wcschr(option_punctlist, c1) != NULL)) {
  1695. tr->phonemes_repeat_count = 0;
  1696. if ((terminator = AnnouncePunctuation(tr, c1, &c2, buf, &ix, is_end_clause)) >= 0)
  1697. return terminator;
  1698. announced_punctuation = c1;
  1699. }
  1700. }
  1701. if ((punct_data & CLAUSE_SPEAK_PUNCTUATION_NAME) && (announced_punctuation == 0)) {
  1702. // used for elipsis (and 3 dots) if a pronunciation for elipsis is given in *_list
  1703. char *p2;
  1704. p2 = &buf[ix];
  1705. sprintf(p2, "%s", LookupCharName(tr, c1, 1));
  1706. if (p2[0] != 0) {
  1707. ix += strlen(p2);
  1708. announced_punctuation = c1;
  1709. punct_data = punct_data & ~CLAUSE_INTONATION_TYPE; // change intonation type to 0 (full-stop)
  1710. }
  1711. }
  1712. if (is_end_clause) {
  1713. nl_count = 0;
  1714. c_next = c2;
  1715. if (iswspace(c_next)) {
  1716. while (!Eof() && iswspace(c_next)) {
  1717. if (c_next == '\n')
  1718. nl_count++;
  1719. c_next = GetC(); // skip past space(s)
  1720. }
  1721. }
  1722. if ((c1 == '.') && (nl_count < 2))
  1723. punct_data |= CLAUSE_DOT_AFTER_LAST_WORD;
  1724. if (nl_count == 0) {
  1725. if ((c1 == ',') && (cprev == '.') && (tr->translator_name == L('h', 'u')) && iswdigit(cprev2) && (iswdigit(c_next) || (iswlower(c_next)))) {
  1726. // lang=hu, fix for ordinal numbers, eg: "december 2., szerda", ignore ',' after ordinal number
  1727. c1 = CHAR_COMMA_BREAK;
  1728. is_end_clause = 0;
  1729. }
  1730. if (c1 == '.') {
  1731. if ((tr->langopts.numbers & NUM_ORDINAL_DOT) &&
  1732. (iswdigit(cprev) || (IsRomanU(cprev) && (IsRomanU(cprev2) || iswspace(cprev2))))) { // lang=hu
  1733. // dot after a number indicates an ordinal number
  1734. if (!iswdigit(cprev))
  1735. is_end_clause = 0; // Roman number followed by dot
  1736. else if (iswlower(c_next) || (c_next == '-')) // hyphen is needed for lang-hu (eg. 2.-kal)
  1737. is_end_clause = 0; // only if followed by lower-case, (or if there is a XML tag)
  1738. } else if (c_next == '\'')
  1739. is_end_clause = 0; // eg. u.s.a.'s
  1740. if (iswlower(c_next)) {
  1741. // next word has no capital letter, this dot is probably from an abbreviation
  1742. is_end_clause = 0;
  1743. }
  1744. if (any_alnum == 0) {
  1745. // no letters or digits yet, so probably not a sentence terminator
  1746. // Here, dot is followed by space or bracket
  1747. c1 = ' ';
  1748. is_end_clause = 0;
  1749. }
  1750. } else {
  1751. if (any_alnum == 0) {
  1752. // no letters or digits yet, so probably not a sentence terminator
  1753. is_end_clause = 0;
  1754. }
  1755. }
  1756. if (is_end_clause && (c1 == '.') && (c_next == '<') && option_ssml) {
  1757. // wait until after the end of the xml tag, then look for upper-case letter
  1758. is_end_clause = 0;
  1759. end_clause_index = ix;
  1760. end_clause_after_tag = punct_data;
  1761. }
  1762. }
  1763. if (is_end_clause) {
  1764. UngetC(c_next);
  1765. buf[ix] = ' ';
  1766. buf[ix+1] = 0;
  1767. if (iswdigit(cprev) && !IsAlpha(c_next)) // ????
  1768. punct_data &= ~CLAUSE_DOT_AFTER_LAST_WORD;
  1769. if (nl_count > 1) {
  1770. if ((punct_data == CLAUSE_QUESTION) || (punct_data == CLAUSE_EXCLAMATION))
  1771. return punct_data + 35; // with a longer pause
  1772. return CLAUSE_PARAGRAPH;
  1773. }
  1774. return punct_data; // only recognise punctuation if followed by a blank or bracket/quote
  1775. } else if (!Eof()) {
  1776. if (iswspace(c2))
  1777. UngetC(c_next);
  1778. }
  1779. }
  1780. }
  1781. if (speech_parameters[espeakSILENCE] == 1)
  1782. continue;
  1783. if (c1 == announced_punctuation) {
  1784. // This character has already been announced, so delete it so that it isn't spoken a second time.
  1785. // Unless it's a hyphen or apostrophe (which is used by TranslateClause() )
  1786. if (IsBracket(c1))
  1787. c1 = 0xe000 + '('; // Unicode private useage area. So TranslateRules() knows the bracket name has been spoken
  1788. else if (c1 != '-')
  1789. c1 = ' ';
  1790. }
  1791. j = ix+1;
  1792. if (c1 == 0xe000 + '<') c1 = '<';
  1793. ix += utf8_out(c1, &buf[ix]);
  1794. if (!iswspace(c1) && !IsBracket(c1)) {
  1795. charix[ix] = count_characters - clause_start_char;
  1796. while (j < ix)
  1797. charix[j++] = -1; // subsequent bytes of a multibyte character
  1798. }
  1799. *charix_top = ix;
  1800. if (((ix > (n_buf-75)) && !IsAlpha(c1) && !iswdigit(c1)) || (ix >= (n_buf-4))) {
  1801. // clause too long, getting near end of buffer, so break here
  1802. // try to break at a word boundary (unless we actually reach the end of buffer).
  1803. // (n_buf-4) is to allow for 3 bytes of multibyte character plus terminator.
  1804. buf[ix] = ' ';
  1805. buf[ix+1] = 0;
  1806. UngetC(c2);
  1807. return CLAUSE_NONE;
  1808. }
  1809. }
  1810. if (stressed_word)
  1811. ix += utf8_out(CHAR_EMPHASIS, &buf[ix]);
  1812. if (end_clause_after_tag)
  1813. RemoveChar(&buf[end_clause_index]); // delete clause-end punctiation
  1814. buf[ix] = ' ';
  1815. buf[ix+1] = 0;
  1816. return CLAUSE_EOF; // end of file
  1817. }
  1818. void InitNamedata(void)
  1819. {
  1820. namedata_ix = 0;
  1821. if (namedata != NULL) {
  1822. free(namedata);
  1823. namedata = NULL;
  1824. n_namedata = 0;
  1825. }
  1826. }
  1827. void InitText2(void)
  1828. {
  1829. int param;
  1830. ungot_char = 0;
  1831. ungot_char2 = 0;
  1832. n_ssml_stack = 1;
  1833. n_param_stack = 1;
  1834. ssml_stack[0].tag_type = 0;
  1835. for (param = 0; param < N_SPEECH_PARAM; param++)
  1836. speech_parameters[param] = param_stack[0].parameter[param]; // set all speech parameters to defaults
  1837. option_punctuation = speech_parameters[espeakPUNCTUATION];
  1838. option_capitals = speech_parameters[espeakCAPITALS];
  1839. current_voice_id[0] = 0;
  1840. ignore_text = 0;
  1841. audio_text = 0;
  1842. clear_skipping_text = 0;
  1843. count_characters = -1;
  1844. sayas_mode = 0;
  1845. xmlbase = NULL;
  1846. }