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

readclause.cpp 65KB

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