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

readclause.c 69KB

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