eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.

readclause.cpp 68KB

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