eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readclause.cpp 69KB

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