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

readclause.cpp 67KB

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