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

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