eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.

readclause.cpp 45KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2007 by Jonathan Duddington *
  3. * email: jonsd@users.sourceforge.net *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 3 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write see: *
  17. * <http://www.gnu.org/licenses/>. *
  18. ***************************************************************************/
  19. #include "StdAfx.h"
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <wctype.h>
  25. #include <wchar.h>
  26. #include <math.h>
  27. #include "speak_lib.h"
  28. #include "speech.h"
  29. #include "phoneme.h"
  30. #include "synthesize.h"
  31. #include "voice.h"
  32. #include "translate.h"
  33. #include <locale.h>
  34. #define N_XML_BUF 256
  35. char *xmlbase = ""; // base URL from <speak>
  36. int namedata_ix=0;
  37. int n_namedata = 0;
  38. char *namedata = NULL;
  39. FILE *f_input = NULL;
  40. int ungot_char2 = 0;
  41. char *p_textinput;
  42. wchar_t *p_wchar_input;
  43. int ungot_char;
  44. int end_of_input;
  45. int ignore_text=0; // set during <sub> ... </sub> to ignore text which has been replaced by an alias
  46. int clear_skipping_text = 0; // next clause should clear the skipping_text flag
  47. int count_characters = 0;
  48. int sayas_mode;
  49. static const char *punct_stop = ".:!?"; // pitch fall if followed by space
  50. static const char *punct_close = ")]}>;'\""; // always pitch fall unless followed by alnum
  51. // alter tone for announce punctuation or capitals
  52. static const char *tone_punct_on = "\001+50R\001+15T"; // add reverberation, reduce low frequencies
  53. static const char *tone_punct_off = "\001R\001T";
  54. // punctuations symbols that can end a clause
  55. static const unsigned short punct_chars[] = {',','.','?','!',':',';',
  56. 0x2013, // en-dash
  57. 0x2014, // em-dash
  58. 0x2026, // elipsis
  59. 0x037e, // Greek question mark (looks like semicolon)
  60. 0x0387, // Greek semicolon, ano teleia
  61. 0x0964, // Devanagari Danda (fullstop)
  62. 0x3001, // ideograph comma
  63. 0x3002, // ideograph period
  64. 0xff01, // fullwidth exclamation
  65. 0xff0c, // fullwidth comma
  66. 0xff0e, // fullwidth period
  67. 0xff1a, // fullwidth colon
  68. 0xff1b, // fullwidth semicolon
  69. 0xff1f, // fullwidth question mark
  70. 0};
  71. // indexed by (entry num. in punct_chars) + 1
  72. // bits 0-7 pause x 10mS, bits 8-10 intonation type, bit 11 don't need following space or bracket
  73. static const unsigned short punct_attributes [] = { 0,
  74. CLAUSE_COMMA, CLAUSE_PERIOD, CLAUSE_QUESTION, CLAUSE_EXCLAMATION, CLAUSE_COLON, CLAUSE_SEMICOLON,
  75. CLAUSE_SEMICOLON, // en-dash
  76. CLAUSE_SEMICOLON, // em-dash
  77. CLAUSE_SEMICOLON, // elipsis
  78. CLAUSE_QUESTION, // Greek question mark
  79. CLAUSE_SEMICOLON, // Greek semicolon
  80. CLAUSE_PERIOD+0x800, // Devanagari Danda (fullstop)
  81. CLAUSE_COMMA+0x800, // ideograph comma
  82. CLAUSE_PERIOD+0x800, // ideograph period
  83. CLAUSE_EXCLAMATION+0x800, // fullwidth
  84. CLAUSE_COMMA+0x800,
  85. CLAUSE_PERIOD+0x800,
  86. CLAUSE_COLON+0x800,
  87. CLAUSE_SEMICOLON+0x800,
  88. CLAUSE_QUESTION+0x800,
  89. CLAUSE_SEMICOLON, // spare
  90. 0 };
  91. // stack for language and voice properties
  92. // frame 0 is for the defaults, before any ssml tags.
  93. typedef struct {
  94. int tag_type;
  95. int voice_variant;
  96. int voice_gender;
  97. int voice_age;
  98. char voice_name[40];
  99. char language[20];
  100. } SSML_STACK;
  101. #define N_SSML_STACK 20
  102. int n_ssml_stack;
  103. SSML_STACK *ssml_sp;
  104. SSML_STACK ssml_stack[N_SSML_STACK];
  105. char current_voice_id[40] = {0};
  106. #define N_PARAM_STACK 20
  107. int n_param_stack;
  108. PARAM_STACK param_stack[N_PARAM_STACK];
  109. int speech_parameters[N_SPEECH_PARAM]; // current values, from param_stack
  110. const int param_defaults[N_SPEECH_PARAM] = {
  111. 0, // silence (internal use)
  112. 165, // rate wpm
  113. 100, // volume
  114. 50, // pitch
  115. 50, // range
  116. 0, // punctuation
  117. 0, // capital letters
  118. 0, // emphasis
  119. 0, // line length
  120. 0, // voice type
  121. };
  122. #ifdef NEED_WCHAR_FUNCTIONS
  123. // additional Latin characters beyond the Latin1 character set
  124. #define MAX_WALPHA 0x233
  125. // indexed by character - 0x100
  126. // 0=not alphabetic, 0xff=lower case, other=value to add to upper case to convert to lower case
  127. static unsigned char walpha_tab[MAX_WALPHA-0xff] = {
  128. 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 100
  129. 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 110
  130. 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 120
  131. 0xff,0xff, 1,0xff, 1,0xff, 1,0xff,0xff, 1,0xff, 1,0xff, 1,0xff, 1, // 130
  132. 0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff,0xff, 1,0xff, 1,0xff, 1,0xff, // 140
  133. 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 150
  134. 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 160
  135. 1,0xff, 1,0xff, 1,0xff, 1,0xff,0xff, 1,0xff, 1,0xff, 1,0xff,0xff, // 170
  136. 0xff, 210, 1,0xff, 1,0xff, 206, 1,0xff, 205, 205, 1,0xff,0xff, 79, 202, // 180
  137. 203, 1,0xff, 205, 207,0xff, 211, 209, 1,0xff,0xff,0xff, 211, 213,0xff, 214, // 190
  138. 1,0xff, 1,0xff, 1,0xff, 218, 1,0xff, 218,0xff,0xff, 1,0xff, 218, 1, // 1a0
  139. 0xff, 217, 217, 1,0xff, 1,0xff, 219, 1,0xff,0xff,0xff, 1,0xff,0xff,0xff, // 1b0
  140. 0xff,0xff,0xff,0xff, 2, 1,0xff, 2, 1,0xff, 2, 1,0xff, 1,0xff, 1, // 1c0
  141. 0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff,0xff, 1,0xff, // 1d0
  142. 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 1e0
  143. 0xff, 2, 1,0xff, 1,0xff,0xff,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 1f0
  144. 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 200
  145. 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 210
  146. 0xff, 0, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 220
  147. 1,0xff, 1,0xff }; // 230
  148. // use ctype.h functions for Latin1 (character < 0x100)
  149. int iswalpha(int c)
  150. {
  151. if(c < 0x100)
  152. return(isalpha(c));
  153. if(c > MAX_WALPHA)
  154. return(0);
  155. return(walpha_tab[c-0x100]);
  156. }
  157. int iswdigit(int c)
  158. {
  159. if(c < 0x100)
  160. return(isdigit(c));
  161. return(0);
  162. }
  163. int iswalnum(int c)
  164. {
  165. if(iswdigit(c))
  166. return(1);
  167. return(iswalpha(c));
  168. }
  169. int towlower(int c)
  170. {
  171. int x;
  172. if(c < 0x100)
  173. return(tolower(c));
  174. if((c > MAX_WALPHA) || ((x = walpha_tab[c-0x100])==0xff))
  175. return(c); // already lower case
  176. return(c + x); // convert to lower case
  177. }
  178. int towupper(int c)
  179. {
  180. // check whether the previous character code is the upper-case equivalent of this character
  181. if(tolower(c-1) == c)
  182. return(c-1); // yes, use it
  183. return(c); // no
  184. }
  185. int iswupper(int c)
  186. {
  187. int x;
  188. if(c < 0x100)
  189. return(isupper(c));
  190. if(((c > MAX_WALPHA) || (x = walpha_tab[c-0x100])==0) || (x == 0xff))
  191. return(0);
  192. return(1);
  193. }
  194. int iswlower(int c)
  195. {
  196. if(c < 0x100)
  197. return(islower(c));
  198. if((c > MAX_WALPHA) || (walpha_tab[c-0x100] != 0xff))
  199. return(0);
  200. return(1);
  201. }
  202. int iswspace(int c)
  203. {
  204. if(c < 0x100)
  205. return(isspace(c));
  206. return(0);
  207. }
  208. int iswpunct(int c)
  209. {
  210. if(c < 0x100)
  211. return(ispunct(c));
  212. return(0);
  213. }
  214. const wchar_t *wcschr(const wchar_t *str, int c)
  215. {
  216. while(*str != 0)
  217. {
  218. if(*str == c)
  219. return(str);
  220. str++;
  221. }
  222. return(NULL);
  223. }
  224. const int wcslen(const wchar_t *str)
  225. {
  226. int ix=0;
  227. while(*str != 0)
  228. {
  229. ix++;
  230. }
  231. return(ix);
  232. }
  233. float wcstod(const wchar_t *str, wchar_t **tailptr)
  234. {
  235. int ix;
  236. char buf[80];
  237. while(isspace(*str)) str++;
  238. for(ix=0; ix<80; ix++)
  239. {
  240. buf[ix] = str[ix];
  241. if(isspace(buf[ix]))
  242. break;
  243. }
  244. *tailptr = (wchar_t *)&str[ix];
  245. return(atof(buf));
  246. }
  247. #endif
  248. static void GetC_unget(int c)
  249. {//==========================
  250. // This is only called with UTF8 input, not wchar input
  251. if(f_input != NULL)
  252. ungetc(c,f_input);
  253. else
  254. {
  255. p_textinput--;
  256. *p_textinput = c;
  257. end_of_input = 0;
  258. }
  259. }
  260. int Eof(void)
  261. {//==========
  262. if(ungot_char != 0)
  263. return(0);
  264. if(f_input != 0)
  265. return(feof(f_input));
  266. return(end_of_input);
  267. }
  268. static int GetC_get(void)
  269. {//======================
  270. int c;
  271. if(f_input != NULL)
  272. {
  273. c = fgetc(f_input);
  274. if(feof(f_input)) c = ' ';
  275. return(c & 0xff);
  276. }
  277. if(option_multibyte == espeakCHARS_WCHAR)
  278. {
  279. if(*p_wchar_input == 0)
  280. {
  281. end_of_input = 1;
  282. return(0);
  283. }
  284. if(!end_of_input)
  285. return(*p_wchar_input++);
  286. }
  287. else
  288. {
  289. if(*p_textinput == 0)
  290. {
  291. end_of_input = 1;
  292. return(0);
  293. }
  294. if(!end_of_input)
  295. return(*p_textinput++ & 0xff);
  296. }
  297. return(0);
  298. }
  299. static int GetC(void)
  300. {//==================
  301. // Returns a unicode wide character
  302. // Performs UTF8 checking and conversion
  303. int c;
  304. int c1;
  305. int c2;
  306. int cbuf[4];
  307. int ix;
  308. int n_bytes;
  309. unsigned char m;
  310. static int ungot2 = 0;
  311. static const unsigned char mask[4] = {0xff,0x1f,0x0f,0x07};
  312. static const unsigned char mask2[4] = {0,0x80,0x20,0x30};
  313. if((c1 = ungot_char) != 0)
  314. {
  315. ungot_char = 0;
  316. return(c1);
  317. }
  318. if(ungot2 != 0)
  319. {
  320. c1 = ungot2;
  321. ungot2 = 0;
  322. }
  323. else
  324. {
  325. c1 = GetC_get();
  326. }
  327. if(option_multibyte == espeakCHARS_WCHAR)
  328. {
  329. count_characters++;
  330. return(c1); // wchar_t text
  331. }
  332. if((option_multibyte < 2) && (c1 & 0x80))
  333. {
  334. // multi-byte utf8 encoding, convert to unicode
  335. n_bytes = 0;
  336. if(((c1 & 0xe0) == 0xc0) && ((c1 & 0x1e) != 0))
  337. n_bytes = 1;
  338. else
  339. if((c1 & 0xf0) == 0xe0)
  340. n_bytes = 2;
  341. else
  342. if(((c1 & 0xf8) == 0xf0) && ((c1 & 0x0f) <= 4))
  343. n_bytes = 3;
  344. if((ix = n_bytes) > 0)
  345. {
  346. c = c1 & mask[ix];
  347. m = mask2[ix];
  348. while(ix > 0)
  349. {
  350. if((c2 = cbuf[ix] = GetC_get()) == 0)
  351. {
  352. if(option_multibyte==espeakCHARS_AUTO)
  353. option_multibyte=espeakCHARS_8BIT; // change "auto" option to "no"
  354. GetC_unget(' ');
  355. break;
  356. }
  357. if((c2 & 0xc0) != 0x80)
  358. {
  359. // This is not UTF8. Change to 8-bit characterset.
  360. if((n_bytes == 2) && (ix == 1))
  361. ungot2 = cbuf[2];
  362. GetC_unget(c2);
  363. break;
  364. }
  365. m = 0x80;
  366. c = (c << 6) + (c2 & 0x3f);
  367. ix--;
  368. }
  369. if(ix == 0)
  370. {
  371. count_characters++;
  372. return(c);
  373. }
  374. }
  375. // top-bit-set character is not utf8, drop through to 8bit charset case
  376. if((option_multibyte==espeakCHARS_AUTO) && !Eof())
  377. option_multibyte=espeakCHARS_8BIT; // change "auto" option to "no"
  378. }
  379. // 8 bit character set, convert to unicode if
  380. count_characters++;
  381. if(c1 >= 0xa0)
  382. return(translator->charset_a0[c1-0xa0]);
  383. return(c1);
  384. } // end of GetC
  385. static void UngetC(int c)
  386. {//======================
  387. ungot_char = c;
  388. if((f_input != NULL) && feof(f_input))
  389. {
  390. // ungetc(' ',f_input);
  391. }
  392. }
  393. const char *Translator::LookupSpecial(char *string)
  394. {//================================================
  395. unsigned int flags;
  396. char phonemes[55];
  397. char phonemes2[55];
  398. static char buf[60];
  399. if(LookupDictList(string,phonemes,&flags,0))
  400. {
  401. SetWordStress(phonemes,flags,-1,0);
  402. DecodePhonemes(phonemes,phonemes2);
  403. sprintf(buf,"[[%s]] ",phonemes2);
  404. option_phoneme_input = 1;
  405. return(buf);
  406. }
  407. return(NULL);
  408. }
  409. const char *Translator::LookupCharName(int c)
  410. {//==========================================
  411. // Find the phoneme string (in ascii) to speak the name of character c
  412. int ix;
  413. const char *p;
  414. static char buf[24];
  415. buf[0] = '_';
  416. ix = utf8_out(c,&buf[1]);
  417. buf[1+ix]=0;
  418. if((p = LookupSpecial(buf)) == NULL)
  419. {
  420. p = LookupSpecial(&buf[1]);
  421. }
  422. if(p != NULL)
  423. return(p);
  424. if((p = LookupSpecial("_??")) == NULL)
  425. {
  426. p = "symbol";
  427. }
  428. strcpy(buf,p);
  429. // sprintf(buf,"%s%d ",p,c);
  430. return(buf);
  431. }
  432. static int LoadSoundFile(const char *fname, int index)
  433. {//===================================================
  434. FILE *f;
  435. char *p;
  436. int *ip;
  437. int length;
  438. char fname_temp[100];
  439. char fname2[sizeof(path_home)+13+40];
  440. char command[sizeof(fname2)+sizeof(fname2)+30];
  441. if(fname == NULL)
  442. fname = soundicon_tab[index].filename;
  443. if((fname==NULL) || (GetFileLength(fname) <= 0))
  444. return(1);
  445. if(fname[0] != '/')
  446. {
  447. // a relative path, look in espeak-data/soundicons
  448. sprintf(fname2,"%s%csoundicons%c%s",path_home,PATHSEP,PATHSEP,fname);
  449. fname = fname2;
  450. }
  451. sprintf(fname_temp,"%s.wav",tmpnam(NULL));
  452. sprintf(command,"sox \"%s\" -r %d -w %s polyphase\n",fname,samplerate,fname_temp);
  453. if(system(command) < 0)
  454. {
  455. fprintf(stderr,"Failed to resample: %s\n",command);
  456. return(2);
  457. }
  458. length = GetFileLength(fname_temp);
  459. f = fopen(fname_temp,"rb");
  460. if(f == NULL)
  461. {
  462. fprintf(stderr,"Can't read temp file: %s",fname_temp);
  463. return(3);
  464. }
  465. fseek(f,0,SEEK_SET);
  466. p = Alloc(length);
  467. fread(p,length,1,f);
  468. fclose(f);
  469. remove(fname_temp);
  470. ip = (int *)(&p[40]);
  471. soundicon_tab[index].length = (*ip) / 2; // length in samples
  472. soundicon_tab[index].data = p+44; // skip WAV header
  473. return(0);
  474. } // end of LoadSoundFile
  475. static int LookupSoundicon(int c)
  476. {//==============================
  477. // Find the sound icon number for a punctuation chatacter
  478. int ix;
  479. for(ix=0; ix<n_soundicon_tab; ix++)
  480. {
  481. if(soundicon_tab[ix].name == c)
  482. {
  483. if(soundicon_tab[ix].length == 0)
  484. {
  485. if(LoadSoundFile(NULL,ix)!=0)
  486. return(-1); // sound file is not available
  487. }
  488. return(ix);
  489. }
  490. }
  491. return(-1);
  492. }
  493. int Translator::AnnouncePunctuation(int c1, int c2, char *buf, int bufix)
  494. {//======================================================================
  495. // announce punctuation names
  496. // c1: the punctuation character
  497. // c2: the following character
  498. int punct_count;
  499. const char *punctname;
  500. int found = 0;
  501. int soundicon;
  502. char *p;
  503. if((soundicon = LookupSoundicon(c1)) >= 0)
  504. {
  505. // add an embedded command to play the soundicon
  506. sprintf(&buf[bufix],"\001%dI ",soundicon);
  507. UngetC(c2);
  508. found = 1;
  509. }
  510. else
  511. if((punctname = LookupCharName(c1)) != NULL)
  512. {
  513. found = 1;
  514. if(bufix==0)
  515. {
  516. punct_count=1;
  517. while(c2 == c1)
  518. {
  519. punct_count++;
  520. c2 = GetC();
  521. }
  522. UngetC(c2);
  523. p = &buf[bufix];
  524. if(punct_count==1)
  525. sprintf(p,"%s %s %s",tone_punct_on,punctname,tone_punct_off);
  526. else
  527. if(punct_count < 4)
  528. {
  529. sprintf(p,"\001+10S%s",tone_punct_on);
  530. while(punct_count-- > 0)
  531. sprintf(buf,"%s %s",buf,punctname);
  532. sprintf(p,"%s %s\001-10S",buf,tone_punct_off);
  533. }
  534. else
  535. {
  536. sprintf(p,"%s %s %d %s %s [[______]]",
  537. tone_punct_on,punctname,punct_count,punctname,tone_punct_off);
  538. option_phoneme_input = 1;
  539. }
  540. }
  541. else
  542. {
  543. // end the clause now and pick up the punctuation next time
  544. UngetC(c2);
  545. ungot_char2 = c1;
  546. buf[bufix] = ' ';
  547. buf[bufix+1] = 0;
  548. }
  549. }
  550. if(found == 0)
  551. return(-1);
  552. if(c1 == '-')
  553. return(CLAUSE_NONE); // no pause
  554. if((strchr_w(punct_close,c1) != NULL) && !iswalnum(c2))
  555. return(CLAUSE_COLON);
  556. if(iswspace(c2) && strchr_w(punct_stop,c1)!=NULL)
  557. return(punct_attributes[lookupwchar(punct_chars,c1)]);
  558. return(CLAUSE_COMMA);
  559. } // end of AnnouncePunctuation
  560. #define SSML_SPEAK 1
  561. #define SSML_VOICE 2
  562. #define SSML_PROSODY 3
  563. #define SSML_SAYAS 4
  564. #define SSML_MARK 5
  565. #define SSML_SENTENCE 6
  566. #define SSML_PARAGRAPH 7
  567. #define SSML_PHONEME 8
  568. #define SSML_SUB 9
  569. #define SSML_STYLE 10
  570. #define SSML_AUDIO 11
  571. #define SSML_EMPHASIS 12
  572. #define SSML_BREAK 13
  573. #define HTML_BREAK 15
  574. #define SSML_CLOSE 0x10 // for a closing tag, OR this with the tag type
  575. // these tags have no effect if they are self-closing, eg. <voice />
  576. static char ignore_if_self_closing[] = {0,1,1,1,1,0,0,0,0,1,1,0,1,0};
  577. MNEM_TAB ssmltags[] = {
  578. {"speak", SSML_SPEAK},
  579. {"voice", SSML_VOICE},
  580. {"prosody", SSML_PROSODY},
  581. {"say-as", SSML_SAYAS},
  582. {"mark", SSML_MARK},
  583. {"s", SSML_SENTENCE},
  584. {"p", SSML_PARAGRAPH},
  585. {"phoneme", SSML_PHONEME},
  586. {"sub", SSML_SUB},
  587. {"tts:style", SSML_STYLE},
  588. {"audio", SSML_AUDIO},
  589. {"emphasis", SSML_EMPHASIS},
  590. {"break", SSML_BREAK},
  591. {"br", HTML_BREAK},
  592. {"li", HTML_BREAK},
  593. {"img", HTML_BREAK},
  594. {"td", HTML_BREAK},
  595. {"h1", SSML_PARAGRAPH},
  596. {"h2", SSML_PARAGRAPH},
  597. {"h3", SSML_PARAGRAPH},
  598. {"h4", SSML_PARAGRAPH},
  599. {"hr", SSML_PARAGRAPH},
  600. {NULL,0}};
  601. static char *VoiceFromStack()
  602. {//==========================
  603. // Use the voice properties from the SSML stack to choose a voice, and switch
  604. // to that voice if it's not the current voice
  605. int ix;
  606. SSML_STACK *sp;
  607. char *v_id;
  608. espeak_VOICE voice_select;
  609. char voice_name[40];
  610. char language[40];
  611. strcpy(voice_name,ssml_stack[0].voice_name);
  612. strcpy(language,ssml_stack[0].language);
  613. voice_select.age = ssml_stack[0].voice_age;
  614. voice_select.gender = ssml_stack[0].voice_gender;
  615. voice_select.variant = ssml_stack[0].voice_variant;
  616. voice_select.identifier = NULL;
  617. for(ix=0; ix<n_ssml_stack; ix++)
  618. {
  619. sp = &ssml_stack[ix];
  620. if(sp->voice_name[0] != 0)
  621. {
  622. strcpy(voice_name, sp->voice_name);
  623. language[0] = 0;
  624. voice_select.gender = 0;
  625. voice_select.age = 0;
  626. voice_select.variant = 0;
  627. }
  628. if(sp->language[0] != 0)
  629. strcpy(language, sp->language);
  630. if(sp->voice_gender != 0)
  631. voice_select.gender = sp->voice_gender;
  632. if(sp->voice_age != 0)
  633. voice_select.age = sp->voice_age;
  634. if(sp->voice_variant != 0)
  635. voice_select.variant = sp->voice_variant;
  636. }
  637. voice_select.name = voice_name;
  638. voice_select.languages = language;
  639. v_id = SelectVoice(&voice_select);
  640. if(v_id == NULL)
  641. return("default");
  642. return(v_id);
  643. } // end of VoiceFromStack
  644. static void ProcessParamStack(char *outbuf, int &outix)
  645. {//====================================================
  646. // Set the speech parameters from the parameter stack
  647. int param;
  648. int ix;
  649. int value;
  650. char buf[20];
  651. int new_parameters[N_SPEECH_PARAM];
  652. static char cmd_letter[N_SPEECH_PARAM] = {0, 'S','A','P','R', 0, 0, 'F'}; // embedded command letters
  653. for(param=0; param<N_SPEECH_PARAM; param++)
  654. new_parameters[param] = -1;
  655. for(ix=0; ix<n_param_stack; ix++)
  656. {
  657. for(param=0; param<N_SPEECH_PARAM; param++)
  658. {
  659. if(param_stack[ix].parameter[param] >= 0)
  660. new_parameters[param] = param_stack[ix].parameter[param];
  661. }
  662. }
  663. for(param=0; param<N_SPEECH_PARAM; param++)
  664. {
  665. if((value = new_parameters[param]) != speech_parameters[param])
  666. {
  667. buf[0] = 0;
  668. switch(param)
  669. {
  670. case espeakPUNCTUATION:
  671. option_punctuation = value-1;
  672. break;
  673. case espeakCAPITALS:
  674. option_capitals = value;
  675. break;
  676. case espeakRATE:
  677. case espeakVOLUME:
  678. case espeakPITCH:
  679. case espeakRANGE:
  680. case espeakEMPHASIS:
  681. sprintf(buf,"%c%d%c",CTRL_EMBEDDED,value,cmd_letter[param]);
  682. break;
  683. }
  684. speech_parameters[param] = new_parameters[param];
  685. strcpy(&outbuf[outix],buf);
  686. outix += strlen(buf);
  687. }
  688. }
  689. } // end of ProcessParamStack
  690. static PARAM_STACK *PushParamStack(int tag_type)
  691. {//=============================================
  692. int ix;
  693. PARAM_STACK *sp;
  694. sp = &param_stack[n_param_stack];
  695. if(n_param_stack < (N_PARAM_STACK-1))
  696. n_param_stack++;
  697. sp->type = tag_type;
  698. for(ix=0; ix<N_SPEECH_PARAM; ix++)
  699. {
  700. sp->parameter[ix] = -1;
  701. }
  702. return(sp);
  703. } // end of PushParamStack
  704. static void PopParamStack(int tag_type, char *outbuf, int &outix)
  705. {//==============================================================
  706. // unwind the stack up to and including the previous tag of this type
  707. int ix;
  708. int top = 0;
  709. if(tag_type >= SSML_CLOSE)
  710. tag_type -= SSML_CLOSE;
  711. for(ix=0; ix<n_param_stack; ix++)
  712. {
  713. if(param_stack[ix].type == tag_type)
  714. {
  715. top = ix;
  716. }
  717. }
  718. if(top > 0)
  719. {
  720. n_param_stack = top;
  721. }
  722. ProcessParamStack(outbuf, outix);
  723. } // end of PopParamStack
  724. static wchar_t *GetSsmlAttribute(wchar_t *pw, const char *name)
  725. {//============================================================
  726. // Gets the value string for an attribute.
  727. // Returns NULL if the attribute is not present
  728. int ix;
  729. static wchar_t empty[1] = {0};
  730. while(*pw != 0)
  731. {
  732. if(iswspace(pw[-1]))
  733. {
  734. ix = 0;
  735. while(*pw == name[ix])
  736. {
  737. pw++;
  738. ix++;
  739. }
  740. if(name[ix]==0)
  741. {
  742. // found the attribute, now get the value
  743. while(iswspace(*pw)) pw++;
  744. if(*pw == '=') pw++;
  745. while(iswspace(*pw)) pw++;
  746. if(*pw == '"')
  747. return(pw+1);
  748. else
  749. return(empty);
  750. }
  751. }
  752. pw++;
  753. }
  754. return(NULL);
  755. } // end of GetSsmlAttribute
  756. static int attrcmp(const wchar_t *string1, const char *string2)
  757. {//============================================================
  758. int ix;
  759. if(string1 == NULL)
  760. return(1);
  761. for(ix=0; (string1[ix] == string2[ix]) && (string1[ix] != 0); ix++)
  762. {
  763. }
  764. if((string1[ix]=='"') && (string2[ix]==0))
  765. return(0);
  766. return(1);
  767. }
  768. static int attrlookup(const wchar_t *string1, const MNEM_TAB *mtab)
  769. {//================================================================
  770. int ix;
  771. for(ix=0; mtab[ix].mnem != NULL; ix++)
  772. {
  773. if(attrcmp(string1,mtab[ix].mnem) == 0)
  774. return(mtab[ix].value);
  775. }
  776. return(mtab[ix].value);
  777. }
  778. static int attrnumber(const wchar_t *pw, int default_value, int type)
  779. {//==================================================================
  780. int value = 0;
  781. if((pw == NULL) || !isdigit(*pw))
  782. return(default_value);
  783. while(isdigit(*pw))
  784. {
  785. value = value*10 + *pw++ - '0';
  786. }
  787. if((type==1) && (towlower(*pw)=='s'))
  788. {
  789. // time: seconds rather than ms
  790. value *= 1000;
  791. }
  792. return(value);
  793. } // end of attrnumber
  794. static int attrcopy_utf8(char *buf, const wchar_t *pw, int len)
  795. {//============================================================
  796. // Convert attribute string into utf8, write to buf, and return its utf8 length
  797. unsigned int c;
  798. int ix = 0;
  799. int n;
  800. int prev_c = 0;
  801. if(pw != NULL)
  802. {
  803. while((ix < (len-4)) && ((c = *pw++) != 0))
  804. {
  805. if((c=='"') && (prev_c != '\\'))
  806. break; // " indicates end of attribute, unless preceded by backstroke
  807. n = utf8_out(c,&buf[ix]);
  808. ix += n;
  809. prev_c = c;
  810. }
  811. }
  812. buf[ix] = 0;
  813. return(ix);
  814. } // end of attrcopy_utf8
  815. static int attr_prosody_value(int param_type, const wchar_t *pw, int *value_out)
  816. {//=============================================================================
  817. int sign = 0;
  818. wchar_t *tail;
  819. float value;
  820. while(iswspace(*pw)) pw++;
  821. if(*pw == '+')
  822. {
  823. pw++;
  824. sign = 1;
  825. }
  826. if(*pw == '-')
  827. {
  828. pw++;
  829. sign = -1;
  830. }
  831. value = wcstod(pw,&tail);
  832. if(tail == pw)
  833. {
  834. // failed to find a number, return 100%
  835. *value_out = 100;
  836. return(2);
  837. }
  838. if(*tail == '%')
  839. {
  840. if(sign != 0)
  841. value = 100 + (sign * value);
  842. *value_out = (int)value;
  843. return(2); // percentage
  844. }
  845. if((tail[0]=='s') && (tail[1]=='t'))
  846. {
  847. double x;
  848. // convert from semitones to a frequency percentage
  849. x = pow(double(2.0),double((value*sign)/12)) * 100;
  850. *value_out = (int)x;
  851. return(2); // percentage
  852. }
  853. if(param_type == espeakRATE)
  854. {
  855. *value_out = (int)(value * 100);
  856. return(2); // percentage
  857. }
  858. *value_out = (int)value;
  859. return(sign); // -1, 0, or 1
  860. } // end of attr_prosody_value
  861. int AddNameData(const char *name, int wide)
  862. {//========================================
  863. // Add the name to the namedata and return its position
  864. int ix;
  865. int len;
  866. void *vp;
  867. if(wide)
  868. {
  869. len = (wcslen((const wchar_t *)name)+1)*sizeof(wchar_t);
  870. n_namedata = (n_namedata + sizeof(wchar_t) - 1) % sizeof(wchar_t); // round to wchar_t boundary
  871. }
  872. else
  873. {
  874. len = strlen(name)+1;
  875. }
  876. if(namedata_ix+len >= n_namedata)
  877. {
  878. // allocate more space for marker names
  879. if((vp = realloc(namedata, namedata_ix+len + 300)) == NULL)
  880. return(-1); // failed to allocate, original data is unchanged but ignore this new name
  881. namedata = (char *)vp;
  882. n_namedata = namedata_ix+len + 300;
  883. }
  884. memcpy(&namedata[ix = namedata_ix],name,len);
  885. namedata_ix += len;
  886. return(ix);
  887. } // end of AddNameData
  888. void SetVoiceStack(espeak_VOICE *v)
  889. {//================================
  890. SSML_STACK *sp;
  891. sp = &ssml_stack[0];
  892. if(v == NULL)
  893. {
  894. memset(sp,0,sizeof(ssml_stack[0]));
  895. return;
  896. }
  897. if(v->languages != NULL)
  898. strcpy(sp->language,v->languages);
  899. if(v->name != NULL)
  900. strcpy(sp->voice_name,v->name);
  901. sp->voice_variant = v->variant;
  902. sp->voice_age = v->age;
  903. sp->voice_gender = v->gender;
  904. }
  905. static int GetVoiceAttributes(wchar_t *pw, int tag_type)
  906. {//=====================================================
  907. // Determines whether voice attribute are specified in this tag, and if so, whether this means
  908. // a voice change.
  909. // If it's a closing tag, delete the top frame of the stack and determine whether this implies
  910. // a voice change.
  911. // Returns CLAUSE_BIT_VOICE if there is a voice change
  912. wchar_t *lang;
  913. wchar_t *gender;
  914. wchar_t *name;
  915. wchar_t *age;
  916. wchar_t *variant;
  917. char *new_voice_id;
  918. static const MNEM_TAB mnem_gender[] = {
  919. {"male", 1},
  920. {"female", 2},
  921. {"neutral", 3},
  922. {NULL, 0}};
  923. if(tag_type & SSML_CLOSE)
  924. {
  925. // delete a stack frame
  926. if(n_ssml_stack > 1)
  927. {
  928. n_ssml_stack--;
  929. }
  930. ssml_sp = &ssml_stack[n_ssml_stack];
  931. }
  932. else
  933. {
  934. // add a stack frame if any voice details are specified
  935. lang = GetSsmlAttribute(pw,"xml:lang");
  936. if(tag_type != SSML_VOICE)
  937. {
  938. // only expect an xml:lang attribute
  939. name = NULL;
  940. variant = NULL;
  941. age = NULL;
  942. gender = NULL;
  943. }
  944. else
  945. {
  946. name = GetSsmlAttribute(pw,"name");
  947. variant = GetSsmlAttribute(pw,"variant");
  948. age = GetSsmlAttribute(pw,"age");
  949. gender = GetSsmlAttribute(pw,"gender");
  950. }
  951. if((tag_type != SSML_VOICE) && (lang==NULL))
  952. return(0); // <s> or <p> without language spec, nothing to do
  953. ssml_sp = &ssml_stack[n_ssml_stack++];
  954. attrcopy_utf8(ssml_sp->language,lang,sizeof(ssml_sp->language));
  955. attrcopy_utf8(ssml_sp->voice_name,name,sizeof(ssml_sp->voice_name));
  956. ssml_sp->voice_variant = attrnumber(variant,1,0)-1;
  957. ssml_sp->voice_age = attrnumber(age,0,0);
  958. ssml_sp->voice_gender = attrlookup(gender,mnem_gender);
  959. ssml_sp->tag_type = tag_type;
  960. }
  961. new_voice_id = VoiceFromStack();
  962. if(strcmp(new_voice_id,current_voice_id) != 0)
  963. {
  964. // add an embedded command to change the voice
  965. strcpy(current_voice_id,new_voice_id);
  966. return(CLAUSE_BIT_VOICE); // change of voice
  967. }
  968. return(0);
  969. } // end of GetVoiceAttributes
  970. static void SetProsodyParameter(int param_type, wchar_t *attr1, PARAM_STACK *sp)
  971. {//=============================================================================
  972. int value;
  973. int sign;
  974. static const MNEM_TAB mnem_volume[] = {
  975. {"default",100},
  976. {"silent",0},
  977. {"x-soft",30},
  978. {"soft",65},
  979. {"medium",100},
  980. {"loud",150},
  981. {"x-loud",230},
  982. {NULL, -1}};
  983. static const MNEM_TAB mnem_rate[] = {
  984. {"default",100},
  985. {"x-slow",60},
  986. {"slow",80},
  987. {"medium",100},
  988. {"fast",120},
  989. {"x-fast",150},
  990. {NULL, -1}};
  991. static const MNEM_TAB mnem_pitch[] = {
  992. {"default",100},
  993. {"x-low",70},
  994. {"low",85},
  995. {"medium",100},
  996. {"high",110},
  997. {"x-high",120},
  998. {NULL, -1}};
  999. static const MNEM_TAB mnem_range[] = {
  1000. {"default",100},
  1001. {"x-low",20},
  1002. {"low",50},
  1003. {"medium",100},
  1004. {"high",125},
  1005. {"x-high",150},
  1006. {NULL, -1}};
  1007. static const MNEM_TAB *mnem_tabs[5] = {
  1008. NULL, mnem_rate, mnem_volume, mnem_pitch, mnem_range };
  1009. if((value = attrlookup(attr1,mnem_tabs[param_type])) >= 0)
  1010. {
  1011. // mnemonic specifies a value as a percentage of the base pitch/range/rate/volume
  1012. sp->parameter[param_type] = (param_stack[0].parameter[param_type] * value)/100;
  1013. }
  1014. else
  1015. {
  1016. sign = attr_prosody_value(param_type,attr1,&value);
  1017. if(sign == 0)
  1018. sp->parameter[param_type] = value; // absolute value in Hz
  1019. else
  1020. if(sign == 2)
  1021. {
  1022. // change specified as percentage or in semitones
  1023. sp->parameter[param_type] = (speech_parameters[param_type] * value)/100;
  1024. }
  1025. else
  1026. {
  1027. // change specified as plus or minus Hz
  1028. sp->parameter[param_type] = speech_parameters[param_type] + (value*sign);
  1029. }
  1030. }
  1031. } // end of SetProsodyParemeter
  1032. static int ProcessSsmlTag(wchar_t *xml_buf, char *outbuf, int &outix, int n_outbuf, int self_closing)
  1033. {//==================================================================================================
  1034. // xml_buf is the tag and attributes with a zero terminator in place of the original '>'
  1035. // returns a clause terminator value.
  1036. unsigned int ix;
  1037. int index;
  1038. int c;
  1039. int tag_type;
  1040. int value;
  1041. int value2;
  1042. int value3;
  1043. int voice_change_flag;
  1044. wchar_t *px;
  1045. wchar_t *attr1;
  1046. wchar_t *attr2;
  1047. wchar_t *attr3;
  1048. int terminator;
  1049. char *uri;
  1050. int param_type;
  1051. char tag_name[40];
  1052. char buf[80];
  1053. PARAM_STACK *sp;
  1054. static const MNEM_TAB mnem_punct[] = {
  1055. {"none", 1},
  1056. {"all", 2},
  1057. {"some", 3},
  1058. {NULL, -1}};
  1059. static const MNEM_TAB mnem_capitals[] = {
  1060. {"no", 0},
  1061. {"spelling", 2},
  1062. {"icon", 1},
  1063. {"pitch", 20}, // this is the amount by which to raise the pitch
  1064. {NULL, -1}};
  1065. static const MNEM_TAB mnem_interpret_as[] = {
  1066. {"characters",SAYAS_CHARS},
  1067. {"tts:char",SAYAS_SINGLE_CHARS},
  1068. {"tts:key",SAYAS_KEY},
  1069. {"tts:digits",SAYAS_DIGITS},
  1070. {"telephone",SAYAS_DIGITS1},
  1071. {NULL, -1}};
  1072. static const MNEM_TAB mnem_sayas_format[] = {
  1073. {"glyphs",1},
  1074. {NULL, -1}};
  1075. static const MNEM_TAB mnem_break[] = {
  1076. {"none",0},
  1077. {"x-weak",1},
  1078. {"weak",2},
  1079. {"medium",3},
  1080. {"strong",4},
  1081. {"x-strong",5},
  1082. {NULL,-1}};
  1083. static const MNEM_TAB mnem_emphasis[] = {
  1084. {"none",1},
  1085. {"reduced",2},
  1086. {"moderate",3},
  1087. {"strong",4},
  1088. {NULL,-1}};
  1089. static const char *prosody_attr[5] = {
  1090. NULL, "rate", "volume", "pitch", "range" };
  1091. for(ix=0; ix<(sizeof(tag_name)-1); ix++)
  1092. {
  1093. if(((c = xml_buf[ix]) == 0) || iswspace(c))
  1094. break;
  1095. tag_name[ix] = tolower((char)c);
  1096. }
  1097. tag_name[ix] = 0;
  1098. px = &xml_buf[ix]; // the tag's attributes
  1099. if(tag_name[0] == '/')
  1100. {
  1101. tag_type = LookupMnem(ssmltags,&tag_name[1]) + SSML_CLOSE; // closing tag
  1102. }
  1103. else
  1104. {
  1105. tag_type = LookupMnem(ssmltags,tag_name);
  1106. if(self_closing && ignore_if_self_closing[tag_type])
  1107. return(0);
  1108. }
  1109. voice_change_flag = 0;
  1110. terminator = CLAUSE_NONE;
  1111. switch(tag_type)
  1112. {
  1113. case SSML_STYLE:
  1114. sp = PushParamStack(tag_type);
  1115. attr1 = GetSsmlAttribute(px,"field");
  1116. attr2 = GetSsmlAttribute(px,"mode");
  1117. if(attrcmp(attr1,"punctuation")==0)
  1118. {
  1119. value = attrlookup(attr2,mnem_punct);
  1120. sp->parameter[espeakPUNCTUATION] = value;
  1121. }
  1122. else
  1123. if(attrcmp(attr1,"capital_letters")==0)
  1124. {
  1125. value = attrlookup(attr2,mnem_capitals);
  1126. sp->parameter[espeakCAPITALS] = value;
  1127. }
  1128. ProcessParamStack(outbuf, outix);
  1129. break;
  1130. case SSML_PROSODY:
  1131. sp = PushParamStack(tag_type);
  1132. // look for attributes: rate, volume, pitch, range
  1133. for(param_type=espeakRATE; param_type <= espeakRANGE; param_type++)
  1134. {
  1135. if((attr1 = GetSsmlAttribute(px,prosody_attr[param_type])) != NULL)
  1136. {
  1137. SetProsodyParameter(param_type, attr1, sp);
  1138. }
  1139. }
  1140. ProcessParamStack(outbuf, outix);
  1141. break;
  1142. case SSML_EMPHASIS:
  1143. sp = PushParamStack(tag_type);
  1144. value = 3; // default is "moderate"
  1145. if((attr1 = GetSsmlAttribute(px,"level")) != NULL)
  1146. {
  1147. value = attrlookup(attr1,mnem_emphasis);
  1148. }
  1149. sp->parameter[espeakEMPHASIS] = value;
  1150. ProcessParamStack(outbuf, outix);
  1151. break;
  1152. case SSML_STYLE + SSML_CLOSE:
  1153. case SSML_PROSODY + SSML_CLOSE:
  1154. case SSML_EMPHASIS + SSML_CLOSE:
  1155. PopParamStack(tag_type, outbuf, outix);
  1156. break;
  1157. case SSML_SAYAS:
  1158. attr1 = GetSsmlAttribute(px,"interpret-as");
  1159. attr2 = GetSsmlAttribute(px,"format");
  1160. attr3 = GetSsmlAttribute(px,"detail");
  1161. value = attrlookup(attr1,mnem_interpret_as);
  1162. value2 = attrlookup(attr2,mnem_sayas_format);
  1163. if(value2 == 1)
  1164. value = SAYAS_GLYPHS;
  1165. value3 = attrnumber(attr3,0,0);
  1166. if(value == SAYAS_DIGITS)
  1167. {
  1168. if(value3 <= 1)
  1169. value = SAYAS_DIGITS1;
  1170. else
  1171. value = SAYAS_DIGITS + value3;
  1172. }
  1173. sprintf(buf,"%c%dY",CTRL_EMBEDDED,value);
  1174. strcpy(&outbuf[outix],buf);
  1175. outix += strlen(buf);
  1176. sayas_mode = value; // punctuation doesn't end clause during SAY-AS
  1177. break;
  1178. case SSML_SAYAS + SSML_CLOSE:
  1179. outbuf[outix++] = CTRL_EMBEDDED;
  1180. outbuf[outix++] = 'Y';
  1181. sayas_mode = 0;
  1182. break;
  1183. case SSML_SUB:
  1184. if((attr1 = GetSsmlAttribute(px,"alias")) != NULL)
  1185. {
  1186. // use the alias rather than the text
  1187. ignore_text = 1;
  1188. outix += attrcopy_utf8(&outbuf[outix],attr1,n_outbuf-outix);
  1189. }
  1190. break;
  1191. case SSML_SUB + SSML_CLOSE:
  1192. ignore_text = 0;
  1193. break;
  1194. case SSML_MARK:
  1195. if((attr1 = GetSsmlAttribute(px,"name")) != NULL)
  1196. {
  1197. // add name to circular buffer of marker names
  1198. attrcopy_utf8(buf,attr1,sizeof(buf));
  1199. if(strcmp(skip_marker,buf)==0)
  1200. {
  1201. // This is the marker we are waiting for before starting to speak
  1202. clear_skipping_text = 1;
  1203. skip_marker[0] = 0;
  1204. return(CLAUSE_NONE);
  1205. }
  1206. if((index = AddNameData(buf,0)) >= 0)
  1207. {
  1208. sprintf(buf,"%c%dM",CTRL_EMBEDDED,index);
  1209. strcpy(&outbuf[outix],buf);
  1210. outix += strlen(buf);
  1211. }
  1212. }
  1213. break;
  1214. case SSML_AUDIO:
  1215. if(uri_callback == NULL)
  1216. break;
  1217. sp = PushParamStack(tag_type);
  1218. if((attr1 = GetSsmlAttribute(px,"src")) != NULL)
  1219. {
  1220. attrcopy_utf8(buf,attr1,sizeof(buf));
  1221. if((index = AddNameData(buf,0)) >= 0)
  1222. {
  1223. uri = &namedata[index];
  1224. if(uri_callback(1,uri,xmlbase) == 0)
  1225. {
  1226. sprintf(buf,"%c%dU",CTRL_EMBEDDED,index);
  1227. strcpy(&outbuf[outix],buf);
  1228. outix += strlen(buf);
  1229. sp->parameter[espeakSILENCE] = 1;
  1230. }
  1231. }
  1232. }
  1233. ProcessParamStack(outbuf, outix);
  1234. if(self_closing)
  1235. PopParamStack(tag_type, outbuf, outix);
  1236. return(CLAUSE_NONE);
  1237. case SSML_AUDIO + SSML_CLOSE:
  1238. if(uri_callback == NULL)
  1239. break;
  1240. PopParamStack(tag_type, outbuf, outix);
  1241. return(CLAUSE_NONE);
  1242. case SSML_BREAK:
  1243. value = 50;
  1244. terminator = CLAUSE_NONE;
  1245. if((attr1 = GetSsmlAttribute(px,"strength")) != NULL)
  1246. {
  1247. static int break_value[6] = {0,7,14,21,40,80}; // *10mS
  1248. value = attrlookup(attr1,mnem_break);
  1249. if(value < 3)
  1250. {
  1251. // adjust prepause on the following word
  1252. sprintf(&outbuf[outix],"%c%dB",CTRL_EMBEDDED,value);
  1253. outix += 3;
  1254. terminator = 0;
  1255. }
  1256. // if(value > 3)
  1257. // terminator = CLAUSE_COMMA & 0xff00;
  1258. value = break_value[value];
  1259. }
  1260. if((attr2 = GetSsmlAttribute(px,"time")) != NULL)
  1261. {
  1262. value2 = attrnumber(px,0,1) / 10;
  1263. if(terminator == 0)
  1264. terminator = CLAUSE_NONE;
  1265. }
  1266. if(terminator)
  1267. return(terminator + value);
  1268. break;
  1269. case SSML_SPEAK:
  1270. if((attr1 = GetSsmlAttribute(px,"xml:base")) != NULL)
  1271. {
  1272. attrcopy_utf8(buf,attr1,sizeof(buf));
  1273. if((index = AddNameData(buf,0)) >= 0)
  1274. {
  1275. xmlbase = &namedata[index];
  1276. }
  1277. }
  1278. if(GetVoiceAttributes(px, tag_type) == 0)
  1279. return(0); // no voice change
  1280. return(CLAUSE_VOICE);
  1281. case SSML_VOICE:
  1282. if(GetVoiceAttributes(px, tag_type) == 0)
  1283. return(0); // no voice change
  1284. return(CLAUSE_VOICE);
  1285. case SSML_SPEAK + SSML_CLOSE:
  1286. terminator = CLAUSE_PERIOD;
  1287. case SSML_VOICE + SSML_CLOSE:
  1288. // unwind stack until the previous <voice> or <speak> tag
  1289. while((n_ssml_stack > 1) && (ssml_stack[n_ssml_stack-1].tag_type != (tag_type - SSML_CLOSE)))
  1290. {
  1291. n_ssml_stack--;
  1292. }
  1293. return(terminator + GetVoiceAttributes(px, tag_type));
  1294. case HTML_BREAK:
  1295. case HTML_BREAK + SSML_CLOSE:
  1296. return(CLAUSE_COLON);
  1297. case SSML_SENTENCE:
  1298. if(ssml_sp->tag_type == SSML_SENTENCE)
  1299. {
  1300. // new sentence implies end-of-sentence
  1301. voice_change_flag = GetVoiceAttributes(px, SSML_SENTENCE+SSML_CLOSE);
  1302. }
  1303. voice_change_flag |= GetVoiceAttributes(px, tag_type);
  1304. return(CLAUSE_PARAGRAPH + voice_change_flag);
  1305. case SSML_PARAGRAPH:
  1306. if((ssml_sp->tag_type == SSML_SENTENCE) || (ssml_sp->tag_type == SSML_PARAGRAPH))
  1307. {
  1308. // new paragraph implies end-of-sentence or end-of-paragraph
  1309. voice_change_flag = GetVoiceAttributes(px, SSML_SENTENCE+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. {
  1316. // end of a sentence which specified a language
  1317. voice_change_flag = GetVoiceAttributes(px, tag_type);
  1318. }
  1319. return(CLAUSE_PERIOD + voice_change_flag);
  1320. case SSML_PARAGRAPH + SSML_CLOSE:
  1321. if((ssml_sp->tag_type == SSML_SENTENCE) || (ssml_sp->tag_type == SSML_PARAGRAPH))
  1322. {
  1323. // End of a paragraph which specified a language.
  1324. // (End-of-paragraph also implies end-of-sentence)
  1325. return(GetVoiceAttributes(px, tag_type) + CLAUSE_PARAGRAPH);
  1326. }
  1327. return(CLAUSE_PARAGRAPH);
  1328. }
  1329. return(0);
  1330. } // end of ProcessSsmlTag
  1331. MNEM_TAB xml_char_mnemonics[] = {
  1332. {"gt",'>'},
  1333. {"lt",'<'},
  1334. {"amp", '&'},
  1335. {"quot", '"'},
  1336. {"nbsp", ' '},
  1337. {NULL,-1}};
  1338. int Translator::ReadClause(FILE *f_in, char *buf, unsigned short *charix, int n_buf)
  1339. {//=================================================================================
  1340. /* Find the end of the current clause.
  1341. Write the clause into buf
  1342. returns: clause type (bits 0-7: pause x10mS, bits 8-11 intonation type)
  1343. Also checks for blank line (paragraph) as end-of-clause indicator.
  1344. Does not end clause for:
  1345. punctuation immediately followed by alphanumeric eg. 1.23 !Speak :path
  1346. repeated punctuation, eg. ... !!!
  1347. */
  1348. int c1=' '; // current character
  1349. int c2; // next character
  1350. int cprev=' '; // previous character
  1351. int parag;
  1352. int ix = 0;
  1353. int j;
  1354. int nl_count;
  1355. int linelength = 0;
  1356. int phoneme_mode = 0;
  1357. int n_xml_buf;
  1358. int terminator;
  1359. int punct;
  1360. int any_alnum = 0;
  1361. int self_closing;
  1362. const char *p;
  1363. char buf2[40];
  1364. wchar_t xml_buf[N_XML_BUF+1];
  1365. if(clear_skipping_text)
  1366. {
  1367. skipping_text = 0;
  1368. clear_skipping_text = 0;
  1369. }
  1370. clause_upper_count = 0;
  1371. clause_lower_count = 0;
  1372. end_of_input = 0;
  1373. f_input = f_in; // for GetC etc
  1374. if(ungot_char2 != 0)
  1375. {
  1376. c2 = ungot_char2;
  1377. ungot_char2 = 0;
  1378. }
  1379. else
  1380. {
  1381. c2 = GetC();
  1382. }
  1383. while(!Eof())
  1384. {
  1385. if(!iswalnum(c1))
  1386. {
  1387. if((end_character_position > 0) && (count_characters > end_character_position))
  1388. {
  1389. end_of_input = 1;
  1390. return(CLAUSE_EOF);
  1391. }
  1392. if((skip_characters > 0) && (count_characters > skip_characters))
  1393. {
  1394. // reached the specified start position
  1395. // don't break a word
  1396. clear_skipping_text = 1;
  1397. skip_characters = 0;
  1398. UngetC(c2);
  1399. return(CLAUSE_NONE);
  1400. }
  1401. }
  1402. cprev = c1;
  1403. c1 = c2;
  1404. c2 = GetC();
  1405. if(Eof())
  1406. {
  1407. c2 = ' ';
  1408. }
  1409. if((option_ssml) && (phoneme_mode==0))
  1410. {
  1411. if((c1 == '&') && ((c2=='#') || isalpha(c2)))
  1412. {
  1413. n_xml_buf = 0;
  1414. c1 = c2;
  1415. while(!Eof() && (iswalnum(c1) || (c1=='#')) && (n_xml_buf < 12))
  1416. {
  1417. buf2[n_xml_buf++] = c1;
  1418. c1 = GetC();
  1419. }
  1420. buf2[n_xml_buf] = 0;
  1421. c2 = GetC();
  1422. if(buf2[0] == '#')
  1423. {
  1424. // character code number
  1425. c1 = '#'; // in case there isn't a number
  1426. if(buf2[1] == 'x')
  1427. sscanf(&buf2[2],"%x",(unsigned int *)(&c1));
  1428. else
  1429. sscanf(&buf2[1],"%d",&c1);
  1430. }
  1431. else
  1432. {
  1433. if((j = LookupMnem(xml_char_mnemonics,buf2)) != -1)
  1434. {
  1435. c1 = j;
  1436. }
  1437. }
  1438. if((sayas_mode == 0x14) && (c1 <= 0x20))
  1439. {
  1440. c1 += 0xe000; // move into unicode private usage area
  1441. }
  1442. }
  1443. else
  1444. if(c1 == '<')
  1445. {
  1446. // SSML Tag
  1447. n_xml_buf = 0;
  1448. c1 = c2;
  1449. while(!Eof() && (c1 != '>') && (n_xml_buf < N_XML_BUF))
  1450. {
  1451. xml_buf[n_xml_buf++] = c1;
  1452. c1 = GetC();
  1453. }
  1454. xml_buf[n_xml_buf] = 0;
  1455. c2 = ' ';
  1456. buf[ix++] = ' ';
  1457. self_closing = 0;
  1458. if(xml_buf[n_xml_buf-1] == '/')
  1459. {
  1460. // a self-closing tag
  1461. self_closing = 1;
  1462. }
  1463. terminator = ProcessSsmlTag(xml_buf,buf,ix,n_buf,self_closing);
  1464. if(terminator != 0)
  1465. {
  1466. buf[ix] = ' ';
  1467. buf[ix++] = 0;
  1468. if(terminator & CLAUSE_BIT_VOICE)
  1469. {
  1470. // a change in voice, write the new voice name to the end of the buf
  1471. p = current_voice_id;
  1472. while((*p != 0) && (ix < (n_buf-1)))
  1473. {
  1474. buf[ix++] = *p++;
  1475. }
  1476. buf[ix++] = 0;
  1477. }
  1478. return(terminator);
  1479. }
  1480. continue;
  1481. }
  1482. }
  1483. if(ignore_text)
  1484. continue;
  1485. if((c2=='\n') && (option_linelength == -1))
  1486. {
  1487. // single-line mode, return immediately on NL
  1488. if((punct = lookupwchar(punct_chars,c1)) == 0)
  1489. {
  1490. charix[ix] = count_characters - clause_start_char;
  1491. ix += utf8_out(c1,&buf[ix]);
  1492. terminator = CLAUSE_PERIOD; // line doesn't end in punctuation, assume period
  1493. }
  1494. else
  1495. {
  1496. terminator = punct_attributes[punct];
  1497. }
  1498. buf[ix] = ' ';
  1499. buf[ix+1] = 0;
  1500. return(terminator);
  1501. }
  1502. if((c1 == CTRL_EMBEDDED) || (c1 == ctrl_embedded))
  1503. {
  1504. // an embedded command. If it's a voice change, end the clause
  1505. if(c2 == 'V')
  1506. {
  1507. buf[ix++] = 0; // end the clause at this point
  1508. while(!iswspace(c1 = GetC()) && !Eof() && (ix < (n_buf-1)))
  1509. buf[ix++] = c1; // add voice name to end of buffer, after the text
  1510. buf[ix++] = 0;
  1511. return(CLAUSE_VOICE);
  1512. }
  1513. else
  1514. if(c2 == 'B')
  1515. {
  1516. // set the punctuation option from an embedded command
  1517. // B0 B1 B<punct list><space>
  1518. strcpy(&buf[ix]," ");
  1519. ix += 3;
  1520. if((c2 = GetC()) == '0')
  1521. option_punctuation = 0;
  1522. else
  1523. {
  1524. option_punctuation = 1;
  1525. option_punctlist[0] = 0;
  1526. if(c2 != '1')
  1527. {
  1528. // a list of punctuation characters to be spoken, terminated by space
  1529. j = 0;
  1530. while(!iswspace(c2) && !Eof())
  1531. {
  1532. option_punctlist[j++] = c2;
  1533. c2 = GetC();
  1534. buf[ix++] = ' ';
  1535. }
  1536. option_punctlist[j] = 0; // terminate punctuation list
  1537. option_punctuation = 2;
  1538. }
  1539. }
  1540. c2 = GetC();
  1541. continue;
  1542. }
  1543. }
  1544. linelength++;
  1545. if(iswalnum(c1))
  1546. any_alnum = 1;
  1547. if(iswupper(c1))
  1548. {
  1549. clause_upper_count++;
  1550. if((option_capitals == 2) && !iswupper(cprev))
  1551. {
  1552. p = LookupSpecial("_cap");
  1553. if(p != NULL)
  1554. {
  1555. sprintf(buf2,"%s%s%s",tone_punct_on,p,tone_punct_off);
  1556. j = strlen(buf2);
  1557. if((ix + j) < n_buf)
  1558. {
  1559. strcpy(&buf[ix],buf2);
  1560. ix += j;
  1561. }
  1562. }
  1563. }
  1564. }
  1565. else
  1566. if(iswalpha(c1))
  1567. clause_lower_count++;
  1568. if(option_phoneme_input)
  1569. {
  1570. if(phoneme_mode > 0)
  1571. phoneme_mode--;
  1572. else
  1573. if((c1 == '[') && (c2 == '['))
  1574. phoneme_mode = -1; // input is phoneme mnemonics, so don't look for punctuation
  1575. else
  1576. if((c1 == ']') && (c2 == ']'))
  1577. phoneme_mode = 2; // set phoneme_mode to zero after the next two characters
  1578. }
  1579. if(c1 == '\n')
  1580. {
  1581. parag = 0;
  1582. // count consecutive newlines, ignoring other spaces
  1583. while(!Eof() && iswspace(c2))
  1584. {
  1585. if(c2 == '\n')
  1586. parag++;
  1587. c2 = GetC();
  1588. }
  1589. if(parag > 0)
  1590. {
  1591. // 2nd newline, assume paragraph
  1592. UngetC(c2);
  1593. buf[ix] = ' ';
  1594. buf[ix+1] = 0;
  1595. if(parag > 3)
  1596. parag = 3;
  1597. if(option_ssml) parag=1;
  1598. return((CLAUSE_PARAGRAPH-30) + 30*parag); // several blank lines, longer pause
  1599. }
  1600. if(linelength <= option_linelength)
  1601. {
  1602. // treat lines shorter than a specified length as end-of-clause
  1603. UngetC(c2);
  1604. buf[ix] = ' ';
  1605. buf[ix+1] = 0;
  1606. return(CLAUSE_COLON);
  1607. }
  1608. linelength = 0;
  1609. }
  1610. if(option_punctuation && (phoneme_mode==0) && (sayas_mode==0) && iswpunct(c1))
  1611. {
  1612. // option is set to explicitly speak punctuation characters
  1613. // if a list of allowed punctuation has been set up, check whether the character is in it
  1614. if((option_punctuation == 1) || (wcschr(option_punctlist,c1) != NULL))
  1615. {
  1616. if((terminator = AnnouncePunctuation(c1, c2, buf, ix)) >= 0)
  1617. return(terminator);
  1618. }
  1619. }
  1620. if((phoneme_mode==0) && (sayas_mode==0) && ((punct = lookupwchar(punct_chars,c1)) != 0))
  1621. {
  1622. if((iswspace(c2) || (punct_attributes[punct] & 0x800) || IsBracket(c2) || (c2=='?') || (c2=='-') || Eof()))
  1623. {
  1624. // note: (c2='?') is for when a smart-quote has been replaced by '?'
  1625. buf[ix] = ' ';
  1626. buf[ix+1] = 0;
  1627. if((c1 == '.') && (cprev == '.'))
  1628. {
  1629. c1 = 0x2026;
  1630. punct = 9; // elipsis
  1631. }
  1632. nl_count = 0;
  1633. while(!Eof() && iswspace(c2))
  1634. {
  1635. if(c2 == '\n')
  1636. nl_count++;
  1637. c2 = GetC(); // skip past space(s)
  1638. }
  1639. UngetC(c2);
  1640. if((nl_count==0) && (c1 == '.'))
  1641. {
  1642. if(iswdigit(cprev) && (langopts.numbers & 0x10000))
  1643. {
  1644. // dot after a number indicates an ordinal number
  1645. c2 = ' ';
  1646. continue;
  1647. }
  1648. if(iswlower(c2))
  1649. {
  1650. c2 = ' ';
  1651. continue; // next word has no capital letter, this dot is probably from an abbreviation
  1652. }
  1653. if(any_alnum==0)
  1654. {
  1655. c2 = ' '; // no letters or digits yet, so probably not a sentence terminator
  1656. continue;
  1657. }
  1658. }
  1659. if(nl_count > 1)
  1660. return(CLAUSE_PARAGRAPH);
  1661. return(punct_attributes[punct]); // only recognise punctuation if followed by a blank or bracket/quote
  1662. }
  1663. }
  1664. if(speech_parameters[espeakSILENCE]==1)
  1665. continue;
  1666. if(!iswspace(c1) && !IsBracket(c1))
  1667. {
  1668. charix[ix] = count_characters - clause_start_char;
  1669. }
  1670. ix += utf8_out(c1,&buf[ix]); // buf[ix++] = c1;
  1671. if(!iswalnum(c1) && (ix > (n_buf-20)))
  1672. {
  1673. // clause too long, getting near end of buffer, so break here
  1674. buf[ix] = ' ';
  1675. buf[ix+1] = 0;
  1676. UngetC(c2);
  1677. return(CLAUSE_NONE);
  1678. }
  1679. if(ix >= (n_buf-2))
  1680. {
  1681. // reached end of buffer, must break now
  1682. buf[n_buf-2] = ' ';
  1683. buf[n_buf-1] = 0;
  1684. UngetC(c2);
  1685. return(CLAUSE_NONE);
  1686. }
  1687. }
  1688. buf[ix] = ' ';
  1689. buf[ix+1] = 0;
  1690. return(CLAUSE_EOF); // end of file
  1691. } // end of ReadClause
  1692. void InitNamedata(void)
  1693. {//====================
  1694. namedata_ix = 0;
  1695. if(namedata != NULL)
  1696. {
  1697. free(namedata);
  1698. namedata = NULL;
  1699. n_namedata = 0;
  1700. }
  1701. }
  1702. void InitText2(void)
  1703. {//=================
  1704. int param;
  1705. n_ssml_stack =1;
  1706. n_param_stack = 1;
  1707. for(param=0; param<N_SPEECH_PARAM; param++)
  1708. speech_parameters[param] = param_stack[0].parameter[param]; // set all speech parameters to defaults
  1709. option_punctuation = speech_parameters[espeakPUNCTUATION];
  1710. option_capitals = speech_parameters[espeakCAPITALS];
  1711. current_voice_id[0] = 0;
  1712. ignore_text = 0;
  1713. clear_skipping_text = 0;
  1714. count_characters = -1;
  1715. sayas_mode = 0;
  1716. xmlbase = NULL;
  1717. }