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

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