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

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