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

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