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

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