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

readclause.cpp 42KB

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