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.c 63KB

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