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

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