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

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