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

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