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 68KB

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