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

readclause.cpp 65KB

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