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

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