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

readclause.cpp 65KB

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