eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readclause.cpp 45KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2007 by Jonathan Duddington *
  3. * email: [email protected] *
  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. const 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. }
  389. const char *Translator::LookupSpecial(const char *string)
  390. {//======================================================
  391. unsigned int flags;
  392. char phonemes[55];
  393. char phonemes2[55];
  394. static char buf[60];
  395. char *string1 = (char *)string;
  396. if(LookupDictList(&string1,phonemes,&flags,0))
  397. {
  398. SetWordStress(phonemes,flags,-1,0);
  399. DecodePhonemes(phonemes,phonemes2);
  400. sprintf(buf,"[[%s]] ",phonemes2);
  401. option_phoneme_input = 1;
  402. return(buf);
  403. }
  404. return(NULL);
  405. }
  406. const char *Translator::LookupCharName(int c)
  407. {//==========================================
  408. // Find the phoneme string (in ascii) to speak the name of character c
  409. int ix;
  410. const char *p;
  411. static char buf[24];
  412. buf[0] = '_';
  413. ix = utf8_out(c,&buf[1]);
  414. buf[1+ix]=0;
  415. if((p = LookupSpecial(buf)) == NULL)
  416. {
  417. p = LookupSpecial(&buf[1]);
  418. }
  419. if(p != NULL)
  420. return(p);
  421. if((p = LookupSpecial("_??")) == NULL)
  422. {
  423. p = "symbol";
  424. }
  425. strcpy(buf,p);
  426. // sprintf(buf,"%s%d ",p,c);
  427. return(buf);
  428. }
  429. static int LoadSoundFile(const char *fname, int index)
  430. {//===================================================
  431. FILE *f;
  432. char *p;
  433. int *ip;
  434. int length;
  435. char fname_temp[100];
  436. char fname2[sizeof(path_home)+13+40];
  437. char command[sizeof(fname2)+sizeof(fname2)+30];
  438. if(fname == NULL)
  439. fname = soundicon_tab[index].filename;
  440. if((fname==NULL) || (GetFileLength(fname) <= 0))
  441. return(1);
  442. if(fname[0] != '/')
  443. {
  444. // a relative path, look in espeak-data/soundicons
  445. sprintf(fname2,"%s%csoundicons%c%s",path_home,PATHSEP,PATHSEP,fname);
  446. fname = fname2;
  447. }
  448. sprintf(fname_temp,"%s.wav",tmpnam(NULL));
  449. sprintf(command,"soxx \"%s\" -r %d -w %s polyphase\n",fname,samplerate,fname_temp);
  450. if(system(command) != 0)
  451. {
  452. // resample has failed, use the original file
  453. fprintf(stderr,"Failed to resample: %s\n",command);
  454. }
  455. else
  456. {
  457. fname = fname_temp;
  458. }
  459. length = GetFileLength(fname);
  460. f = fopen(fname,"rb");
  461. if(f == NULL)
  462. {
  463. fprintf(stderr,"Can't read temp file: %s",fname);
  464. return(3);
  465. }
  466. fseek(f,0,SEEK_SET);
  467. p = Alloc(length);
  468. fread(p,length,1,f);
  469. fclose(f);
  470. remove(fname_temp);
  471. ip = (int *)(&p[40]);
  472. soundicon_tab[index].length = (*ip) / 2; // length in samples
  473. soundicon_tab[index].data = p+44; // skip WAV header
  474. return(0);
  475. } // end of LoadSoundFile
  476. static int LookupSoundicon(int c)
  477. {//==============================
  478. // Find the sound icon number for a punctuation chatacter
  479. int ix;
  480. for(ix=0; ix<n_soundicon_tab; ix++)
  481. {
  482. if(soundicon_tab[ix].name == c)
  483. {
  484. if(soundicon_tab[ix].length == 0)
  485. {
  486. if(LoadSoundFile(NULL,ix)!=0)
  487. return(-1); // sound file is not available
  488. }
  489. return(ix);
  490. }
  491. }
  492. return(-1);
  493. }
  494. int Translator::AnnouncePunctuation(int c1, int c2, char *buf, int bufix)
  495. {//======================================================================
  496. // announce punctuation names
  497. // c1: the punctuation character
  498. // c2: the following character
  499. int punct_count;
  500. const char *punctname;
  501. int found = 0;
  502. int soundicon;
  503. char *p;
  504. if((soundicon = LookupSoundicon(c1)) >= 0)
  505. {
  506. // add an embedded command to play the soundicon
  507. sprintf(&buf[bufix],"\001%dI ",soundicon);
  508. UngetC(c2);
  509. found = 1;
  510. }
  511. else
  512. if((punctname = LookupCharName(c1)) != NULL)
  513. {
  514. found = 1;
  515. if(bufix==0)
  516. {
  517. punct_count=1;
  518. while(c2 == c1)
  519. {
  520. punct_count++;
  521. c2 = GetC();
  522. }
  523. UngetC(c2);
  524. p = &buf[bufix];
  525. if(punct_count==1)
  526. sprintf(p,"%s %s %s",tone_punct_on,punctname,tone_punct_off);
  527. else
  528. if(punct_count < 4)
  529. {
  530. sprintf(p,"\001+10S%s",tone_punct_on);
  531. while(punct_count-- > 0)
  532. sprintf(buf,"%s %s",buf,punctname);
  533. sprintf(p,"%s %s\001-10S",buf,tone_punct_off);
  534. }
  535. else
  536. {
  537. sprintf(p,"%s %s %d %s %s [[______]]",
  538. tone_punct_on,punctname,punct_count,punctname,tone_punct_off);
  539. option_phoneme_input = 1;
  540. }
  541. }
  542. else
  543. {
  544. // end the clause now and pick up the punctuation next time
  545. UngetC(c2);
  546. ungot_char2 = c1;
  547. buf[bufix] = ' ';
  548. buf[bufix+1] = 0;
  549. }
  550. }
  551. if(found == 0)
  552. return(-1);
  553. if(c1 == '-')
  554. return(CLAUSE_NONE); // no pause
  555. if((strchr_w(punct_close,c1) != NULL) && !iswalnum(c2))
  556. return(CLAUSE_COLON);
  557. if(iswspace(c2) && strchr_w(punct_stop,c1)!=NULL)
  558. return(punct_attributes[lookupwchar(punct_chars,c1)]);
  559. return(CLAUSE_COMMA);
  560. } // end of AnnouncePunctuation
  561. #define SSML_SPEAK 1
  562. #define SSML_VOICE 2
  563. #define SSML_PROSODY 3
  564. #define SSML_SAYAS 4
  565. #define SSML_MARK 5
  566. #define SSML_SENTENCE 6
  567. #define SSML_PARAGRAPH 7
  568. #define SSML_PHONEME 8
  569. #define SSML_SUB 9
  570. #define SSML_STYLE 10
  571. #define SSML_AUDIO 11
  572. #define SSML_EMPHASIS 12
  573. #define SSML_BREAK 13
  574. #define HTML_BREAK 15
  575. #define SSML_CLOSE 0x10 // for a closing tag, OR this with the tag type
  576. // these tags have no effect if they are self-closing, eg. <voice />
  577. static char ignore_if_self_closing[] = {0,1,1,1,1,0,0,0,0,1,1,0,1,0};
  578. MNEM_TAB ssmltags[] = {
  579. {"speak", SSML_SPEAK},
  580. {"voice", SSML_VOICE},
  581. {"prosody", SSML_PROSODY},
  582. {"say-as", SSML_SAYAS},
  583. {"mark", SSML_MARK},
  584. {"s", SSML_SENTENCE},
  585. {"p", SSML_PARAGRAPH},
  586. {"phoneme", SSML_PHONEME},
  587. {"sub", SSML_SUB},
  588. {"tts:style", SSML_STYLE},
  589. {"audio", SSML_AUDIO},
  590. {"emphasis", SSML_EMPHASIS},
  591. {"break", SSML_BREAK},
  592. {"br", HTML_BREAK},
  593. {"li", HTML_BREAK},
  594. {"img", HTML_BREAK},
  595. {"td", HTML_BREAK},
  596. {"h1", SSML_PARAGRAPH},
  597. {"h2", SSML_PARAGRAPH},
  598. {"h3", SSML_PARAGRAPH},
  599. {"h4", SSML_PARAGRAPH},
  600. {"hr", SSML_PARAGRAPH},
  601. {NULL,0}};
  602. static const char *VoiceFromStack()
  603. {//================================
  604. // Use the voice properties from the SSML stack to choose a voice, and switch
  605. // to that voice if it's not the current voice
  606. int ix;
  607. SSML_STACK *sp;
  608. const char *v_id;
  609. espeak_VOICE voice_select;
  610. char voice_name[40];
  611. char language[40];
  612. strcpy(voice_name,ssml_stack[0].voice_name);
  613. strcpy(language,ssml_stack[0].language);
  614. voice_select.age = ssml_stack[0].voice_age;
  615. voice_select.gender = ssml_stack[0].voice_gender;
  616. voice_select.variant = ssml_stack[0].voice_variant;
  617. voice_select.identifier = NULL;
  618. for(ix=0; ix<n_ssml_stack; ix++)
  619. {
  620. sp = &ssml_stack[ix];
  621. if(sp->voice_name[0] != 0)
  622. {
  623. strcpy(voice_name, sp->voice_name);
  624. language[0] = 0;
  625. voice_select.gender = 0;
  626. voice_select.age = 0;
  627. voice_select.variant = 0;
  628. }
  629. if(sp->language[0] != 0)
  630. strcpy(language, sp->language);
  631. if(sp->voice_gender != 0)
  632. voice_select.gender = sp->voice_gender;
  633. if(sp->voice_age != 0)
  634. voice_select.age = sp->voice_age;
  635. if(sp->voice_variant != 0)
  636. voice_select.variant = sp->voice_variant;
  637. }
  638. voice_select.name = voice_name;
  639. voice_select.languages = language;
  640. v_id = SelectVoice(&voice_select);
  641. if(v_id == NULL)
  642. return("default");
  643. return(v_id);
  644. } // end of VoiceFromStack
  645. static void ProcessParamStack(char *outbuf, int &outix)
  646. {//====================================================
  647. // Set the speech parameters from the parameter stack
  648. int param;
  649. int ix;
  650. int value;
  651. char buf[20];
  652. int new_parameters[N_SPEECH_PARAM];
  653. static char cmd_letter[N_SPEECH_PARAM] = {0, 'S','A','P','R', 0, 0, 'F'}; // embedded command letters
  654. for(param=0; param<N_SPEECH_PARAM; param++)
  655. new_parameters[param] = -1;
  656. for(ix=0; ix<n_param_stack; ix++)
  657. {
  658. for(param=0; param<N_SPEECH_PARAM; param++)
  659. {
  660. if(param_stack[ix].parameter[param] >= 0)
  661. new_parameters[param] = param_stack[ix].parameter[param];
  662. }
  663. }
  664. for(param=0; param<N_SPEECH_PARAM; param++)
  665. {
  666. if((value = new_parameters[param]) != speech_parameters[param])
  667. {
  668. buf[0] = 0;
  669. switch(param)
  670. {
  671. case espeakPUNCTUATION:
  672. option_punctuation = value-1;
  673. break;
  674. case espeakCAPITALS:
  675. option_capitals = value;
  676. break;
  677. case espeakRATE:
  678. case espeakVOLUME:
  679. case espeakPITCH:
  680. case espeakRANGE:
  681. case espeakEMPHASIS:
  682. sprintf(buf,"%c%d%c",CTRL_EMBEDDED,value,cmd_letter[param]);
  683. break;
  684. }
  685. speech_parameters[param] = new_parameters[param];
  686. strcpy(&outbuf[outix],buf);
  687. outix += strlen(buf);
  688. }
  689. }
  690. } // end of ProcessParamStack
  691. static PARAM_STACK *PushParamStack(int tag_type)
  692. {//=============================================
  693. int ix;
  694. PARAM_STACK *sp;
  695. sp = &param_stack[n_param_stack];
  696. if(n_param_stack < (N_PARAM_STACK-1))
  697. n_param_stack++;
  698. sp->type = tag_type;
  699. for(ix=0; ix<N_SPEECH_PARAM; ix++)
  700. {
  701. sp->parameter[ix] = -1;
  702. }
  703. return(sp);
  704. } // end of PushParamStack
  705. static void PopParamStack(int tag_type, char *outbuf, int &outix)
  706. {//==============================================================
  707. // unwind the stack up to and including the previous tag of this type
  708. int ix;
  709. int top = 0;
  710. if(tag_type >= SSML_CLOSE)
  711. tag_type -= SSML_CLOSE;
  712. for(ix=0; ix<n_param_stack; ix++)
  713. {
  714. if(param_stack[ix].type == tag_type)
  715. {
  716. top = ix;
  717. }
  718. }
  719. if(top > 0)
  720. {
  721. n_param_stack = top;
  722. }
  723. ProcessParamStack(outbuf, outix);
  724. } // end of PopParamStack
  725. static wchar_t *GetSsmlAttribute(wchar_t *pw, const char *name)
  726. {//============================================================
  727. // Gets the value string for an attribute.
  728. // Returns NULL if the attribute is not present
  729. int ix;
  730. static wchar_t empty[1] = {0};
  731. while(*pw != 0)
  732. {
  733. if(iswspace(pw[-1]))
  734. {
  735. ix = 0;
  736. while(*pw == name[ix])
  737. {
  738. pw++;
  739. ix++;
  740. }
  741. if(name[ix]==0)
  742. {
  743. // found the attribute, now get the value
  744. while(iswspace(*pw)) pw++;
  745. if(*pw == '=') pw++;
  746. while(iswspace(*pw)) pw++;
  747. if(*pw == '"')
  748. return(pw+1);
  749. else
  750. return(empty);
  751. }
  752. }
  753. pw++;
  754. }
  755. return(NULL);
  756. } // end of GetSsmlAttribute
  757. static int attrcmp(const wchar_t *string1, const char *string2)
  758. {//============================================================
  759. int ix;
  760. if(string1 == NULL)
  761. return(1);
  762. for(ix=0; (string1[ix] == string2[ix]) && (string1[ix] != 0); ix++)
  763. {
  764. }
  765. if((string1[ix]=='"') && (string2[ix]==0))
  766. return(0);
  767. return(1);
  768. }
  769. static int attrlookup(const wchar_t *string1, const MNEM_TAB *mtab)
  770. {//================================================================
  771. int ix;
  772. for(ix=0; mtab[ix].mnem != NULL; ix++)
  773. {
  774. if(attrcmp(string1,mtab[ix].mnem) == 0)
  775. return(mtab[ix].value);
  776. }
  777. return(mtab[ix].value);
  778. }
  779. static int attrnumber(const wchar_t *pw, int default_value, int type)
  780. {//==================================================================
  781. int value = 0;
  782. if((pw == NULL) || !isdigit(*pw))
  783. return(default_value);
  784. while(isdigit(*pw))
  785. {
  786. value = value*10 + *pw++ - '0';
  787. }
  788. if((type==1) && (towlower(*pw)=='s'))
  789. {
  790. // time: seconds rather than ms
  791. value *= 1000;
  792. }
  793. return(value);
  794. } // end of attrnumber
  795. static int attrcopy_utf8(char *buf, const wchar_t *pw, int len)
  796. {//============================================================
  797. // Convert attribute string into utf8, write to buf, and return its utf8 length
  798. unsigned int c;
  799. int ix = 0;
  800. int n;
  801. int prev_c = 0;
  802. if(pw != NULL)
  803. {
  804. while((ix < (len-4)) && ((c = *pw++) != 0))
  805. {
  806. if((c=='"') && (prev_c != '\\'))
  807. break; // " indicates end of attribute, unless preceded by backstroke
  808. n = utf8_out(c,&buf[ix]);
  809. ix += n;
  810. prev_c = c;
  811. }
  812. }
  813. buf[ix] = 0;
  814. return(ix);
  815. } // end of attrcopy_utf8
  816. static int attr_prosody_value(int param_type, const wchar_t *pw, int *value_out)
  817. {//=============================================================================
  818. int sign = 0;
  819. wchar_t *tail;
  820. float value;
  821. while(iswspace(*pw)) pw++;
  822. if(*pw == '+')
  823. {
  824. pw++;
  825. sign = 1;
  826. }
  827. if(*pw == '-')
  828. {
  829. pw++;
  830. sign = -1;
  831. }
  832. value = wcstod(pw,&tail);
  833. if(tail == pw)
  834. {
  835. // failed to find a number, return 100%
  836. *value_out = 100;
  837. return(2);
  838. }
  839. if(*tail == '%')
  840. {
  841. if(sign != 0)
  842. value = 100 + (sign * value);
  843. *value_out = (int)value;
  844. return(2); // percentage
  845. }
  846. if((tail[0]=='s') && (tail[1]=='t'))
  847. {
  848. double x;
  849. // convert from semitones to a frequency percentage
  850. x = pow(double(2.0),double((value*sign)/12)) * 100;
  851. *value_out = (int)x;
  852. return(2); // percentage
  853. }
  854. if(param_type == espeakRATE)
  855. {
  856. *value_out = (int)(value * 100);
  857. return(2); // percentage
  858. }
  859. *value_out = (int)value;
  860. return(sign); // -1, 0, or 1
  861. } // end of attr_prosody_value
  862. int AddNameData(const char *name, int wide)
  863. {//========================================
  864. // Add the name to the namedata and return its position
  865. int ix;
  866. int len;
  867. void *vp;
  868. if(wide)
  869. {
  870. len = (wcslen((const wchar_t *)name)+1)*sizeof(wchar_t);
  871. n_namedata = (n_namedata + sizeof(wchar_t) - 1) % sizeof(wchar_t); // round to wchar_t boundary
  872. }
  873. else
  874. {
  875. len = strlen(name)+1;
  876. }
  877. if(namedata_ix+len >= n_namedata)
  878. {
  879. // allocate more space for marker names
  880. if((vp = realloc(namedata, namedata_ix+len + 300)) == NULL)
  881. return(-1); // failed to allocate, original data is unchanged but ignore this new name
  882. namedata = (char *)vp;
  883. n_namedata = namedata_ix+len + 300;
  884. }
  885. memcpy(&namedata[ix = namedata_ix],name,len);
  886. namedata_ix += len;
  887. return(ix);
  888. } // end of AddNameData
  889. void SetVoiceStack(espeak_VOICE *v)
  890. {//================================
  891. SSML_STACK *sp;
  892. sp = &ssml_stack[0];
  893. if(v == NULL)
  894. {
  895. memset(sp,0,sizeof(ssml_stack[0]));
  896. return;
  897. }
  898. if(v->languages != NULL)
  899. strcpy(sp->language,v->languages);
  900. if(v->name != NULL)
  901. strcpy(sp->voice_name,v->name);
  902. sp->voice_variant = v->variant;
  903. sp->voice_age = v->age;
  904. sp->voice_gender = v->gender;
  905. }
  906. static int GetVoiceAttributes(wchar_t *pw, int tag_type)
  907. {//=====================================================
  908. // Determines whether voice attribute are specified in this tag, and if so, whether this means
  909. // a voice change.
  910. // If it's a closing tag, delete the top frame of the stack and determine whether this implies
  911. // a voice change.
  912. // Returns CLAUSE_BIT_VOICE if there is a voice change
  913. wchar_t *lang;
  914. wchar_t *gender;
  915. wchar_t *name;
  916. wchar_t *age;
  917. wchar_t *variant;
  918. const char *new_voice_id;
  919. static const MNEM_TAB mnem_gender[] = {
  920. {"male", 1},
  921. {"female", 2},
  922. {"neutral", 3},
  923. {NULL, 0}};
  924. if(tag_type & SSML_CLOSE)
  925. {
  926. // delete a stack frame
  927. if(n_ssml_stack > 1)
  928. {
  929. n_ssml_stack--;
  930. }
  931. ssml_sp = &ssml_stack[n_ssml_stack];
  932. }
  933. else
  934. {
  935. // add a stack frame if any voice details are specified
  936. lang = GetSsmlAttribute(pw,"xml:lang");
  937. if(tag_type != SSML_VOICE)
  938. {
  939. // only expect an xml:lang attribute
  940. name = NULL;
  941. variant = NULL;
  942. age = NULL;
  943. gender = NULL;
  944. }
  945. else
  946. {
  947. name = GetSsmlAttribute(pw,"name");
  948. variant = GetSsmlAttribute(pw,"variant");
  949. age = GetSsmlAttribute(pw,"age");
  950. gender = GetSsmlAttribute(pw,"gender");
  951. }
  952. if((tag_type != SSML_VOICE) && (lang==NULL))
  953. return(0); // <s> or <p> without language spec, nothing to do
  954. ssml_sp = &ssml_stack[n_ssml_stack++];
  955. attrcopy_utf8(ssml_sp->language,lang,sizeof(ssml_sp->language));
  956. attrcopy_utf8(ssml_sp->voice_name,name,sizeof(ssml_sp->voice_name));
  957. ssml_sp->voice_variant = attrnumber(variant,1,0)-1;
  958. ssml_sp->voice_age = attrnumber(age,0,0);
  959. ssml_sp->voice_gender = attrlookup(gender,mnem_gender);
  960. ssml_sp->tag_type = tag_type;
  961. }
  962. new_voice_id = VoiceFromStack();
  963. if(strcmp(new_voice_id,current_voice_id) != 0)
  964. {
  965. // add an embedded command to change the voice
  966. strcpy(current_voice_id,new_voice_id);
  967. return(CLAUSE_BIT_VOICE); // change of voice
  968. }
  969. return(0);
  970. } // end of GetVoiceAttributes
  971. static void SetProsodyParameter(int param_type, wchar_t *attr1, PARAM_STACK *sp)
  972. {//=============================================================================
  973. int value;
  974. int sign;
  975. static const MNEM_TAB mnem_volume[] = {
  976. {"default",100},
  977. {"silent",0},
  978. {"x-soft",30},
  979. {"soft",65},
  980. {"medium",100},
  981. {"loud",150},
  982. {"x-loud",230},
  983. {NULL, -1}};
  984. static const MNEM_TAB mnem_rate[] = {
  985. {"default",100},
  986. {"x-slow",60},
  987. {"slow",80},
  988. {"medium",100},
  989. {"fast",120},
  990. {"x-fast",150},
  991. {NULL, -1}};
  992. static const MNEM_TAB mnem_pitch[] = {
  993. {"default",100},
  994. {"x-low",70},
  995. {"low",85},
  996. {"medium",100},
  997. {"high",110},
  998. {"x-high",120},
  999. {NULL, -1}};
  1000. static const MNEM_TAB mnem_range[] = {
  1001. {"default",100},
  1002. {"x-low",20},
  1003. {"low",50},
  1004. {"medium",100},
  1005. {"high",125},
  1006. {"x-high",150},
  1007. {NULL, -1}};
  1008. static const MNEM_TAB *mnem_tabs[5] = {
  1009. NULL, mnem_rate, mnem_volume, mnem_pitch, mnem_range };
  1010. if((value = attrlookup(attr1,mnem_tabs[param_type])) >= 0)
  1011. {
  1012. // mnemonic specifies a value as a percentage of the base pitch/range/rate/volume
  1013. sp->parameter[param_type] = (param_stack[0].parameter[param_type] * value)/100;
  1014. }
  1015. else
  1016. {
  1017. sign = attr_prosody_value(param_type,attr1,&value);
  1018. if(sign == 0)
  1019. sp->parameter[param_type] = value; // absolute value in Hz
  1020. else
  1021. if(sign == 2)
  1022. {
  1023. // change specified as percentage or in semitones
  1024. sp->parameter[param_type] = (speech_parameters[param_type] * value)/100;
  1025. }
  1026. else
  1027. {
  1028. // change specified as plus or minus Hz
  1029. sp->parameter[param_type] = speech_parameters[param_type] + (value*sign);
  1030. }
  1031. }
  1032. } // end of SetProsodyParemeter
  1033. static int ProcessSsmlTag(wchar_t *xml_buf, char *outbuf, int &outix, int n_outbuf, int self_closing)
  1034. {//==================================================================================================
  1035. // xml_buf is the tag and attributes with a zero terminator in place of the original '>'
  1036. // returns a clause terminator value.
  1037. unsigned int ix;
  1038. int index;
  1039. int c;
  1040. int tag_type;
  1041. int value;
  1042. int value2;
  1043. int value3;
  1044. int voice_change_flag;
  1045. wchar_t *px;
  1046. wchar_t *attr1;
  1047. wchar_t *attr2;
  1048. wchar_t *attr3;
  1049. int terminator;
  1050. char *uri;
  1051. int param_type;
  1052. char tag_name[40];
  1053. char buf[80];
  1054. PARAM_STACK *sp;
  1055. static const MNEM_TAB mnem_punct[] = {
  1056. {"none", 1},
  1057. {"all", 2},
  1058. {"some", 3},
  1059. {NULL, -1}};
  1060. static const MNEM_TAB mnem_capitals[] = {
  1061. {"no", 0},
  1062. {"spelling", 2},
  1063. {"icon", 1},
  1064. {"pitch", 20}, // this is the amount by which to raise the pitch
  1065. {NULL, -1}};
  1066. static const MNEM_TAB mnem_interpret_as[] = {
  1067. {"characters",SAYAS_CHARS},
  1068. {"tts:char",SAYAS_SINGLE_CHARS},
  1069. {"tts:key",SAYAS_KEY},
  1070. {"tts:digits",SAYAS_DIGITS},
  1071. {"telephone",SAYAS_DIGITS1},
  1072. {NULL, -1}};
  1073. static const MNEM_TAB mnem_sayas_format[] = {
  1074. {"glyphs",1},
  1075. {NULL, -1}};
  1076. static const MNEM_TAB mnem_break[] = {
  1077. {"none",0},
  1078. {"x-weak",1},
  1079. {"weak",2},
  1080. {"medium",3},
  1081. {"strong",4},
  1082. {"x-strong",5},
  1083. {NULL,-1}};
  1084. static const MNEM_TAB mnem_emphasis[] = {
  1085. {"none",1},
  1086. {"reduced",2},
  1087. {"moderate",3},
  1088. {"strong",4},
  1089. {NULL,-1}};
  1090. static const char *prosody_attr[5] = {
  1091. NULL, "rate", "volume", "pitch", "range" };
  1092. for(ix=0; ix<(sizeof(tag_name)-1); ix++)
  1093. {
  1094. if(((c = xml_buf[ix]) == 0) || iswspace(c))
  1095. break;
  1096. tag_name[ix] = tolower((char)c);
  1097. }
  1098. tag_name[ix] = 0;
  1099. px = &xml_buf[ix]; // the tag's attributes
  1100. if(tag_name[0] == '/')
  1101. {
  1102. tag_type = LookupMnem(ssmltags,&tag_name[1]) + SSML_CLOSE; // closing tag
  1103. }
  1104. else
  1105. {
  1106. tag_type = LookupMnem(ssmltags,tag_name);
  1107. if(self_closing && ignore_if_self_closing[tag_type])
  1108. return(0);
  1109. }
  1110. voice_change_flag = 0;
  1111. terminator = CLAUSE_NONE;
  1112. switch(tag_type)
  1113. {
  1114. case SSML_STYLE:
  1115. sp = PushParamStack(tag_type);
  1116. attr1 = GetSsmlAttribute(px,"field");
  1117. attr2 = GetSsmlAttribute(px,"mode");
  1118. if(attrcmp(attr1,"punctuation")==0)
  1119. {
  1120. value = attrlookup(attr2,mnem_punct);
  1121. sp->parameter[espeakPUNCTUATION] = value;
  1122. }
  1123. else
  1124. if(attrcmp(attr1,"capital_letters")==0)
  1125. {
  1126. value = attrlookup(attr2,mnem_capitals);
  1127. sp->parameter[espeakCAPITALS] = value;
  1128. }
  1129. ProcessParamStack(outbuf, outix);
  1130. break;
  1131. case SSML_PROSODY:
  1132. sp = PushParamStack(tag_type);
  1133. // look for attributes: rate, volume, pitch, range
  1134. for(param_type=espeakRATE; param_type <= espeakRANGE; param_type++)
  1135. {
  1136. if((attr1 = GetSsmlAttribute(px,prosody_attr[param_type])) != NULL)
  1137. {
  1138. SetProsodyParameter(param_type, attr1, sp);
  1139. }
  1140. }
  1141. ProcessParamStack(outbuf, outix);
  1142. break;
  1143. case SSML_EMPHASIS:
  1144. sp = PushParamStack(tag_type);
  1145. value = 3; // default is "moderate"
  1146. if((attr1 = GetSsmlAttribute(px,"level")) != NULL)
  1147. {
  1148. value = attrlookup(attr1,mnem_emphasis);
  1149. }
  1150. sp->parameter[espeakEMPHASIS] = value;
  1151. ProcessParamStack(outbuf, outix);
  1152. break;
  1153. case SSML_STYLE + SSML_CLOSE:
  1154. case SSML_PROSODY + SSML_CLOSE:
  1155. case SSML_EMPHASIS + SSML_CLOSE:
  1156. PopParamStack(tag_type, outbuf, outix);
  1157. break;
  1158. case SSML_SAYAS:
  1159. attr1 = GetSsmlAttribute(px,"interpret-as");
  1160. attr2 = GetSsmlAttribute(px,"format");
  1161. attr3 = GetSsmlAttribute(px,"detail");
  1162. value = attrlookup(attr1,mnem_interpret_as);
  1163. value2 = attrlookup(attr2,mnem_sayas_format);
  1164. if(value2 == 1)
  1165. value = SAYAS_GLYPHS;
  1166. value3 = attrnumber(attr3,0,0);
  1167. if(value == SAYAS_DIGITS)
  1168. {
  1169. if(value3 <= 1)
  1170. value = SAYAS_DIGITS1;
  1171. else
  1172. value = SAYAS_DIGITS + value3;
  1173. }
  1174. sprintf(buf,"%c%dY",CTRL_EMBEDDED,value);
  1175. strcpy(&outbuf[outix],buf);
  1176. outix += strlen(buf);
  1177. sayas_mode = value; // punctuation doesn't end clause during SAY-AS
  1178. break;
  1179. case SSML_SAYAS + SSML_CLOSE:
  1180. outbuf[outix++] = CTRL_EMBEDDED;
  1181. outbuf[outix++] = 'Y';
  1182. sayas_mode = 0;
  1183. break;
  1184. case SSML_SUB:
  1185. if((attr1 = GetSsmlAttribute(px,"alias")) != NULL)
  1186. {
  1187. // use the alias rather than the text
  1188. ignore_text = 1;
  1189. outix += attrcopy_utf8(&outbuf[outix],attr1,n_outbuf-outix);
  1190. }
  1191. break;
  1192. case SSML_SUB + SSML_CLOSE:
  1193. ignore_text = 0;
  1194. break;
  1195. case SSML_MARK:
  1196. if((attr1 = GetSsmlAttribute(px,"name")) != NULL)
  1197. {
  1198. // add name to circular buffer of marker names
  1199. attrcopy_utf8(buf,attr1,sizeof(buf));
  1200. if(strcmp(skip_marker,buf)==0)
  1201. {
  1202. // This is the marker we are waiting for before starting to speak
  1203. clear_skipping_text = 1;
  1204. skip_marker[0] = 0;
  1205. return(CLAUSE_NONE);
  1206. }
  1207. if((index = AddNameData(buf,0)) >= 0)
  1208. {
  1209. sprintf(buf,"%c%dM",CTRL_EMBEDDED,index);
  1210. strcpy(&outbuf[outix],buf);
  1211. outix += strlen(buf);
  1212. }
  1213. }
  1214. break;
  1215. case SSML_AUDIO:
  1216. if(uri_callback == NULL)
  1217. break;
  1218. sp = PushParamStack(tag_type);
  1219. if((attr1 = GetSsmlAttribute(px,"src")) != NULL)
  1220. {
  1221. attrcopy_utf8(buf,attr1,sizeof(buf));
  1222. if((index = AddNameData(buf,0)) >= 0)
  1223. {
  1224. uri = &namedata[index];
  1225. if(uri_callback(1,uri,xmlbase) == 0)
  1226. {
  1227. sprintf(buf,"%c%dU",CTRL_EMBEDDED,index);
  1228. strcpy(&outbuf[outix],buf);
  1229. outix += strlen(buf);
  1230. sp->parameter[espeakSILENCE] = 1;
  1231. }
  1232. }
  1233. }
  1234. ProcessParamStack(outbuf, outix);
  1235. if(self_closing)
  1236. PopParamStack(tag_type, outbuf, outix);
  1237. return(CLAUSE_NONE);
  1238. case SSML_AUDIO + SSML_CLOSE:
  1239. if(uri_callback == NULL)
  1240. break;
  1241. PopParamStack(tag_type, outbuf, outix);
  1242. return(CLAUSE_NONE);
  1243. case SSML_BREAK:
  1244. value = 50;
  1245. terminator = CLAUSE_NONE;
  1246. if((attr1 = GetSsmlAttribute(px,"strength")) != NULL)
  1247. {
  1248. static int break_value[6] = {0,7,14,21,40,80}; // *10mS
  1249. value = attrlookup(attr1,mnem_break);
  1250. if(value < 3)
  1251. {
  1252. // adjust prepause on the following word
  1253. sprintf(&outbuf[outix],"%c%dB",CTRL_EMBEDDED,value);
  1254. outix += 3;
  1255. terminator = 0;
  1256. }
  1257. // if(value > 3)
  1258. // terminator = CLAUSE_COMMA & 0xff00;
  1259. value = break_value[value];
  1260. }
  1261. if((attr2 = GetSsmlAttribute(px,"time")) != NULL)
  1262. {
  1263. value2 = attrnumber(px,0,1) / 10;
  1264. if(terminator == 0)
  1265. terminator = CLAUSE_NONE;
  1266. }
  1267. if(terminator)
  1268. return(terminator + value);
  1269. break;
  1270. case SSML_SPEAK:
  1271. if((attr1 = GetSsmlAttribute(px,"xml:base")) != NULL)
  1272. {
  1273. attrcopy_utf8(buf,attr1,sizeof(buf));
  1274. if((index = AddNameData(buf,0)) >= 0)
  1275. {
  1276. xmlbase = &namedata[index];
  1277. }
  1278. }
  1279. if(GetVoiceAttributes(px, tag_type) == 0)
  1280. return(0); // no voice change
  1281. return(CLAUSE_VOICE);
  1282. case SSML_VOICE:
  1283. if(GetVoiceAttributes(px, tag_type) == 0)
  1284. return(0); // no voice change
  1285. return(CLAUSE_VOICE);
  1286. case SSML_SPEAK + SSML_CLOSE:
  1287. terminator = CLAUSE_PERIOD;
  1288. case SSML_VOICE + SSML_CLOSE:
  1289. // unwind stack until the previous <voice> or <speak> tag
  1290. while((n_ssml_stack > 1) && (ssml_stack[n_ssml_stack-1].tag_type != (tag_type - SSML_CLOSE)))
  1291. {
  1292. n_ssml_stack--;
  1293. }
  1294. return(terminator + GetVoiceAttributes(px, tag_type));
  1295. case HTML_BREAK:
  1296. case HTML_BREAK + SSML_CLOSE:
  1297. return(CLAUSE_COLON);
  1298. case SSML_SENTENCE:
  1299. if((ssml_sp != NULL) && (ssml_sp->tag_type == SSML_SENTENCE))
  1300. {
  1301. // new sentence implies end-of-sentence
  1302. voice_change_flag = GetVoiceAttributes(px, SSML_SENTENCE+SSML_CLOSE);
  1303. }
  1304. voice_change_flag |= GetVoiceAttributes(px, tag_type);
  1305. return(CLAUSE_PARAGRAPH + voice_change_flag);
  1306. case SSML_PARAGRAPH:
  1307. if(ssml_sp != NULL)
  1308. {
  1309. if(ssml_sp->tag_type == SSML_SENTENCE)
  1310. {
  1311. // new paragraph implies end-of-sentence or end-of-paragraph
  1312. voice_change_flag = GetVoiceAttributes(px, SSML_SENTENCE+SSML_CLOSE);
  1313. }
  1314. if(ssml_sp->tag_type == SSML_PARAGRAPH)
  1315. {
  1316. // new paragraph implies end-of-sentence or end-of-paragraph
  1317. voice_change_flag |= GetVoiceAttributes(px, SSML_PARAGRAPH+SSML_CLOSE);
  1318. }
  1319. }
  1320. voice_change_flag |= GetVoiceAttributes(px, tag_type);
  1321. return(CLAUSE_PARAGRAPH + voice_change_flag);
  1322. case SSML_SENTENCE + SSML_CLOSE:
  1323. if(ssml_sp->tag_type == SSML_SENTENCE)
  1324. {
  1325. // end of a sentence which specified a language
  1326. voice_change_flag = GetVoiceAttributes(px, tag_type);
  1327. }
  1328. return(CLAUSE_PERIOD + voice_change_flag);
  1329. case SSML_PARAGRAPH + SSML_CLOSE:
  1330. if((ssml_sp->tag_type == SSML_SENTENCE) || (ssml_sp->tag_type == SSML_PARAGRAPH))
  1331. {
  1332. // End of a paragraph which specified a language.
  1333. // (End-of-paragraph also implies end-of-sentence)
  1334. return(GetVoiceAttributes(px, tag_type) + CLAUSE_PARAGRAPH);
  1335. }
  1336. return(CLAUSE_PARAGRAPH);
  1337. }
  1338. return(0);
  1339. } // end of ProcessSsmlTag
  1340. MNEM_TAB xml_char_mnemonics[] = {
  1341. {"gt",'>'},
  1342. {"lt",'<'},
  1343. {"amp", '&'},
  1344. {"quot", '"'},
  1345. {"nbsp", ' '},
  1346. {NULL,-1}};
  1347. int Translator::ReadClause(FILE *f_in, char *buf, unsigned short *charix, int n_buf)
  1348. {//=================================================================================
  1349. /* Find the end of the current clause.
  1350. Write the clause into buf
  1351. returns: clause type (bits 0-7: pause x10mS, bits 8-11 intonation type)
  1352. Also checks for blank line (paragraph) as end-of-clause indicator.
  1353. Does not end clause for:
  1354. punctuation immediately followed by alphanumeric eg. 1.23 !Speak :path
  1355. repeated punctuation, eg. ... !!!
  1356. */
  1357. int c1=' '; // current character
  1358. int c2; // next character
  1359. int cprev=' '; // previous character
  1360. int parag;
  1361. int ix = 0;
  1362. int j;
  1363. int nl_count;
  1364. int linelength = 0;
  1365. int phoneme_mode = 0;
  1366. int n_xml_buf;
  1367. int terminator;
  1368. int punct;
  1369. int any_alnum = 0;
  1370. int self_closing;
  1371. const char *p;
  1372. char buf2[40];
  1373. wchar_t xml_buf[N_XML_BUF+1];
  1374. if(clear_skipping_text)
  1375. {
  1376. skipping_text = 0;
  1377. clear_skipping_text = 0;
  1378. }
  1379. clause_upper_count = 0;
  1380. clause_lower_count = 0;
  1381. end_of_input = 0;
  1382. f_input = f_in; // for GetC etc
  1383. if(ungot_char2 != 0)
  1384. {
  1385. c2 = ungot_char2;
  1386. ungot_char2 = 0;
  1387. }
  1388. else
  1389. {
  1390. c2 = GetC();
  1391. }
  1392. while(!Eof())
  1393. {
  1394. if(!iswalnum(c1))
  1395. {
  1396. if((end_character_position > 0) && (count_characters > end_character_position))
  1397. {
  1398. end_of_input = 1;
  1399. return(CLAUSE_EOF);
  1400. }
  1401. if((skip_characters > 0) && (count_characters > skip_characters))
  1402. {
  1403. // reached the specified start position
  1404. // don't break a word
  1405. clear_skipping_text = 1;
  1406. skip_characters = 0;
  1407. UngetC(c2);
  1408. return(CLAUSE_NONE);
  1409. }
  1410. }
  1411. cprev = c1;
  1412. c1 = c2;
  1413. c2 = GetC();
  1414. if(Eof())
  1415. {
  1416. c2 = ' ';
  1417. }
  1418. if((option_ssml) && (phoneme_mode==0))
  1419. {
  1420. if((c1 == '&') && ((c2=='#') || isalpha(c2)))
  1421. {
  1422. n_xml_buf = 0;
  1423. c1 = c2;
  1424. while(!Eof() && (iswalnum(c1) || (c1=='#')) && (n_xml_buf < 12))
  1425. {
  1426. buf2[n_xml_buf++] = c1;
  1427. c1 = GetC();
  1428. }
  1429. buf2[n_xml_buf] = 0;
  1430. c2 = GetC();
  1431. if(buf2[0] == '#')
  1432. {
  1433. // character code number
  1434. c1 = '#'; // in case there isn't a number
  1435. if(buf2[1] == 'x')
  1436. sscanf(&buf2[2],"%x",(unsigned int *)(&c1));
  1437. else
  1438. sscanf(&buf2[1],"%d",&c1);
  1439. }
  1440. else
  1441. {
  1442. if((j = LookupMnem(xml_char_mnemonics,buf2)) != -1)
  1443. {
  1444. c1 = j;
  1445. }
  1446. }
  1447. if((sayas_mode == 0x14) && (c1 <= 0x20))
  1448. {
  1449. c1 += 0xe000; // move into unicode private usage area
  1450. }
  1451. }
  1452. else
  1453. if(c1 == '<')
  1454. {
  1455. // SSML Tag
  1456. n_xml_buf = 0;
  1457. c1 = c2;
  1458. while(!Eof() && (c1 != '>') && (n_xml_buf < N_XML_BUF))
  1459. {
  1460. xml_buf[n_xml_buf++] = c1;
  1461. c1 = GetC();
  1462. }
  1463. xml_buf[n_xml_buf] = 0;
  1464. c2 = ' ';
  1465. buf[ix++] = ' ';
  1466. self_closing = 0;
  1467. if(xml_buf[n_xml_buf-1] == '/')
  1468. {
  1469. // a self-closing tag
  1470. self_closing = 1;
  1471. }
  1472. terminator = ProcessSsmlTag(xml_buf,buf,ix,n_buf,self_closing);
  1473. if(terminator != 0)
  1474. {
  1475. buf[ix] = ' ';
  1476. buf[ix++] = 0;
  1477. if(terminator & CLAUSE_BIT_VOICE)
  1478. {
  1479. // a change in voice, write the new voice name to the end of the buf
  1480. p = current_voice_id;
  1481. while((*p != 0) && (ix < (n_buf-1)))
  1482. {
  1483. buf[ix++] = *p++;
  1484. }
  1485. buf[ix++] = 0;
  1486. }
  1487. return(terminator);
  1488. }
  1489. continue;
  1490. }
  1491. }
  1492. if(ignore_text)
  1493. continue;
  1494. if((c2=='\n') && (option_linelength == -1))
  1495. {
  1496. // single-line mode, return immediately on NL
  1497. if((punct = lookupwchar(punct_chars,c1)) == 0)
  1498. {
  1499. charix[ix] = count_characters - clause_start_char;
  1500. ix += utf8_out(c1,&buf[ix]);
  1501. terminator = CLAUSE_PERIOD; // line doesn't end in punctuation, assume period
  1502. }
  1503. else
  1504. {
  1505. terminator = punct_attributes[punct];
  1506. }
  1507. buf[ix] = ' ';
  1508. buf[ix+1] = 0;
  1509. return(terminator);
  1510. }
  1511. if((c1 == CTRL_EMBEDDED) || (c1 == ctrl_embedded))
  1512. {
  1513. // an embedded command. If it's a voice change, end the clause
  1514. if(c2 == 'V')
  1515. {
  1516. buf[ix++] = 0; // end the clause at this point
  1517. while(!iswspace(c1 = GetC()) && !Eof() && (ix < (n_buf-1)))
  1518. buf[ix++] = c1; // add voice name to end of buffer, after the text
  1519. buf[ix++] = 0;
  1520. return(CLAUSE_VOICE);
  1521. }
  1522. else
  1523. if(c2 == 'B')
  1524. {
  1525. // set the punctuation option from an embedded command
  1526. // B0 B1 B<punct list><space>
  1527. strcpy(&buf[ix]," ");
  1528. ix += 3;
  1529. if((c2 = GetC()) == '0')
  1530. option_punctuation = 0;
  1531. else
  1532. {
  1533. option_punctuation = 1;
  1534. option_punctlist[0] = 0;
  1535. if(c2 != '1')
  1536. {
  1537. // a list of punctuation characters to be spoken, terminated by space
  1538. j = 0;
  1539. while(!iswspace(c2) && !Eof())
  1540. {
  1541. option_punctlist[j++] = c2;
  1542. c2 = GetC();
  1543. buf[ix++] = ' ';
  1544. }
  1545. option_punctlist[j] = 0; // terminate punctuation list
  1546. option_punctuation = 2;
  1547. }
  1548. }
  1549. c2 = GetC();
  1550. continue;
  1551. }
  1552. }
  1553. linelength++;
  1554. if(iswalnum(c1))
  1555. any_alnum = 1;
  1556. if(iswupper(c1))
  1557. {
  1558. clause_upper_count++;
  1559. if((option_capitals == 2) && !iswupper(cprev))
  1560. {
  1561. p = LookupSpecial("_cap");
  1562. if(p != NULL)
  1563. {
  1564. sprintf(buf2,"%s%s%s",tone_punct_on,p,tone_punct_off);
  1565. j = strlen(buf2);
  1566. if((ix + j) < n_buf)
  1567. {
  1568. strcpy(&buf[ix],buf2);
  1569. ix += j;
  1570. }
  1571. }
  1572. }
  1573. }
  1574. else
  1575. if(iswalpha(c1))
  1576. clause_lower_count++;
  1577. if(option_phoneme_input)
  1578. {
  1579. if(phoneme_mode > 0)
  1580. phoneme_mode--;
  1581. else
  1582. if((c1 == '[') && (c2 == '['))
  1583. phoneme_mode = -1; // input is phoneme mnemonics, so don't look for punctuation
  1584. else
  1585. if((c1 == ']') && (c2 == ']'))
  1586. phoneme_mode = 2; // set phoneme_mode to zero after the next two characters
  1587. }
  1588. if(c1 == '\n')
  1589. {
  1590. parag = 0;
  1591. // count consecutive newlines, ignoring other spaces
  1592. while(!Eof() && iswspace(c2))
  1593. {
  1594. if(c2 == '\n')
  1595. parag++;
  1596. c2 = GetC();
  1597. }
  1598. if(parag > 0)
  1599. {
  1600. // 2nd newline, assume paragraph
  1601. UngetC(c2);
  1602. buf[ix] = ' ';
  1603. buf[ix+1] = 0;
  1604. if(parag > 3)
  1605. parag = 3;
  1606. if(option_ssml) parag=1;
  1607. return((CLAUSE_PARAGRAPH-30) + 30*parag); // several blank lines, longer pause
  1608. }
  1609. if(linelength <= option_linelength)
  1610. {
  1611. // treat lines shorter than a specified length as end-of-clause
  1612. UngetC(c2);
  1613. buf[ix] = ' ';
  1614. buf[ix+1] = 0;
  1615. return(CLAUSE_COLON);
  1616. }
  1617. linelength = 0;
  1618. }
  1619. if(option_punctuation && (phoneme_mode==0) && (sayas_mode==0) && iswpunct(c1))
  1620. {
  1621. // option is set to explicitly speak punctuation characters
  1622. // if a list of allowed punctuation has been set up, check whether the character is in it
  1623. if((option_punctuation == 1) || (wcschr(option_punctlist,c1) != NULL))
  1624. {
  1625. if((terminator = AnnouncePunctuation(c1, c2, buf, ix)) >= 0)
  1626. return(terminator);
  1627. }
  1628. }
  1629. if((phoneme_mode==0) && (sayas_mode==0) && ((punct = lookupwchar(punct_chars,c1)) != 0))
  1630. {
  1631. if((iswspace(c2) || (punct_attributes[punct] & 0x800) || IsBracket(c2) || (c2=='?') || (c2=='-') || Eof()))
  1632. {
  1633. // note: (c2='?') is for when a smart-quote has been replaced by '?'
  1634. buf[ix] = ' ';
  1635. buf[ix+1] = 0;
  1636. if((c1 == '.') && (cprev == '.'))
  1637. {
  1638. c1 = 0x2026;
  1639. punct = 9; // elipsis
  1640. }
  1641. nl_count = 0;
  1642. while(!Eof() && iswspace(c2))
  1643. {
  1644. if(c2 == '\n')
  1645. nl_count++;
  1646. c2 = GetC(); // skip past space(s)
  1647. }
  1648. UngetC(c2);
  1649. if((nl_count==0) && (c1 == '.'))
  1650. {
  1651. if(iswdigit(cprev) && (langopts.numbers & 0x10000))
  1652. {
  1653. // dot after a number indicates an ordinal number
  1654. c2 = ' ';
  1655. continue;
  1656. }
  1657. if(iswlower(c2))
  1658. {
  1659. c2 = ' ';
  1660. continue; // next word has no capital letter, this dot is probably from an abbreviation
  1661. }
  1662. if(any_alnum==0)
  1663. {
  1664. c2 = ' '; // no letters or digits yet, so probably not a sentence terminator
  1665. continue;
  1666. }
  1667. }
  1668. if(nl_count > 1)
  1669. return(CLAUSE_PARAGRAPH);
  1670. return(punct_attributes[punct]); // only recognise punctuation if followed by a blank or bracket/quote
  1671. }
  1672. }
  1673. if(speech_parameters[espeakSILENCE]==1)
  1674. continue;
  1675. if(!iswspace(c1) && !IsBracket(c1))
  1676. {
  1677. charix[ix] = count_characters - clause_start_char;
  1678. }
  1679. ix += utf8_out(c1,&buf[ix]); // buf[ix++] = c1;
  1680. if(!iswalnum(c1) && (ix > (n_buf-20)))
  1681. {
  1682. // clause too long, getting near end of buffer, so break here
  1683. buf[ix] = ' ';
  1684. buf[ix+1] = 0;
  1685. UngetC(c2);
  1686. return(CLAUSE_NONE);
  1687. }
  1688. if(ix >= (n_buf-2))
  1689. {
  1690. // reached end of buffer, must break now
  1691. buf[n_buf-2] = ' ';
  1692. buf[n_buf-1] = 0;
  1693. UngetC(c2);
  1694. return(CLAUSE_NONE);
  1695. }
  1696. }
  1697. buf[ix] = ' ';
  1698. buf[ix+1] = 0;
  1699. return(CLAUSE_EOF); // end of file
  1700. } // end of ReadClause
  1701. void InitNamedata(void)
  1702. {//====================
  1703. namedata_ix = 0;
  1704. if(namedata != NULL)
  1705. {
  1706. free(namedata);
  1707. namedata = NULL;
  1708. n_namedata = 0;
  1709. }
  1710. }
  1711. void InitText2(void)
  1712. {//=================
  1713. int param;
  1714. n_ssml_stack =1;
  1715. n_param_stack = 1;
  1716. for(param=0; param<N_SPEECH_PARAM; param++)
  1717. speech_parameters[param] = param_stack[0].parameter[param]; // set all speech parameters to defaults
  1718. option_punctuation = speech_parameters[espeakPUNCTUATION];
  1719. option_capitals = speech_parameters[espeakCAPITALS];
  1720. current_voice_id[0] = 0;
  1721. ignore_text = 0;
  1722. clear_skipping_text = 0;
  1723. count_characters = -1;
  1724. sayas_mode = 0;
  1725. xmlbase = NULL;
  1726. }