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

readclause.cpp 55KB

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