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

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