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.

compiledata.c 78KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2014 by Jonathan Duddington *
  3. * email: [email protected] *
  4. * Copyright (C) 2013-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: *
  18. * <http://www.gnu.org/licenses/>. *
  19. ***************************************************************************/
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <strings.h>
  23. #include <ctype.h>
  24. #include <stdlib.h>
  25. #include <time.h>
  26. #include "speak_lib.h"
  27. #include "espeak_ng.h"
  28. #include "speech.h"
  29. #include "phoneme.h"
  30. #include "synthesize.h"
  31. #include "voice.h"
  32. #include "spect.h"
  33. #include <sys/stat.h>
  34. #ifdef PLATFORM_POSIX
  35. #include <unistd.h>
  36. #endif
  37. typedef struct {
  38. unsigned int value;
  39. char *name;
  40. } NAMETAB;
  41. NAMETAB *manifest = NULL;
  42. int n_manifest;
  43. extern int utf8_in(int *c, const char *buf);
  44. extern int utf8_out(unsigned int c, char *buf);
  45. extern void DrawEnvelopes();
  46. typedef struct {
  47. const char *mnem;
  48. int type;
  49. int data;
  50. } keywtab_t;
  51. #define k_AND 1
  52. #define k_OR 2
  53. #define k_THEN 3
  54. #define k_NOT 4
  55. #define kTHISSTRESS 0x800
  56. // keyword types
  57. enum {
  58. tPHONEME_TYPE = 1,
  59. tPHONEME_FLAG,
  60. tTRANSITION,
  61. tPLACE,
  62. tSTATEMENT,
  63. tINSTRN1,
  64. tWHICH_PHONEME,
  65. tTEST,
  66. };
  67. static keywtab_t k_conditions[] = {
  68. {"AND", 0, k_AND},
  69. {"OR", 0, k_OR},
  70. {"THEN", 0, k_THEN},
  71. {"NOT", 0, k_NOT},
  72. {"prevPh", tWHICH_PHONEME, 0},
  73. {"prevPhW", tWHICH_PHONEME, 5},
  74. {"thisPh", tWHICH_PHONEME, 1},
  75. {"nextPh", tWHICH_PHONEME, 2},
  76. {"next2Ph", tWHICH_PHONEME, 3},
  77. {"nextPhW", tWHICH_PHONEME, 4},
  78. {"next2PhW", tWHICH_PHONEME, 6},
  79. {"nextVowel",tWHICH_PHONEME, 7},
  80. {"prevVowel",tWHICH_PHONEME, 8},
  81. {"next3PhW", tWHICH_PHONEME, 9},
  82. {"prev2PhW", tWHICH_PHONEME, 10},
  83. {"PreVoicing", tTEST, 0xf01},
  84. {"KlattSynth", tTEST, 0xf02},
  85. {"MbrolaSynth", tTEST, 0xf03},
  86. {NULL, 0, 0}
  87. };
  88. static keywtab_t k_properties[] = {
  89. {"isPause", 0, phPAUSE},
  90. {"isVowel", 0, phVOWEL},
  91. {"isNasal", 0, phNASAL},
  92. {"isLiquid", 0, phLIQUID},
  93. {"isUStop", 0, phSTOP},
  94. {"isVStop", 0, phVSTOP},
  95. {"isVFricative", 0, phVFRICATIVE},
  96. {"isPalatal", 0, i_isPalatal},
  97. {"isLong", 0, i_isLong},
  98. {"isRhotic", 0, i_isRhotic},
  99. {"isSibilant", 0, i_isSibilant},
  100. {"isFlag1", 0, i_isFlag1},
  101. {"isFlag2", 0, i_isFlag2},
  102. {"isFlag3", 0, i_isFlag3},
  103. {"isVel", 0, i_isVel},
  104. {"isDiminished", 0, i_isDiminished},
  105. {"isUnstressed", 0, i_isUnstressed},
  106. {"isNotStressed",0, i_isNotStressed},
  107. {"isStressed", 0, i_isStressed},
  108. {"isMaxStress", 0, i_isMaxStress},
  109. {"isBreak", 0, i_isBreak},
  110. {"isPause2", 0, i_isBreak}, // synonym for isBreak
  111. {"isWordStart", 0, i_isWordStart},
  112. {"notWordStart", 0, i_notWordStart},
  113. {"isWordEnd", 0, i_isWordEnd},
  114. {"isAfterStress",0, i_isAfterStress},
  115. {"isNotVowel", 0, i_isNotVowel},
  116. {"isFinalVowel", 0, i_isFinalVowel},
  117. {"isVoiced", 0, i_isVoiced}, // voiced consonant, or vowel
  118. {"isFirstVowel", 0, i_isFirstVowel},
  119. {"isSecondVowel", 0, i_isSecondVowel},
  120. {"isSeqFlag1", 0, i_isSeqFlag1},
  121. {"isTranslationGiven", 0, i_IsTranslationGiven}, // phoneme translation given in **_list or as [[...]]
  122. {NULL, 0, 0}
  123. };
  124. enum {
  125. kPHONEMESTART = 1,
  126. kUTF8_BOM,
  127. kPROCEDURE,
  128. kENDPHONEME,
  129. kENDPROCEDURE,
  130. kEQUIVALENTS,
  131. kPHONEMENUMBER,
  132. kPHONEMETABLE,
  133. kINCLUDE,
  134. kIMPORT_PH,
  135. kSTARTTYPE,
  136. kENDTYPE,
  137. kSTRESSTYPE,
  138. kVOICINGSWITCH,
  139. kIF,
  140. kELSE,
  141. kELIF,
  142. kENDIF,
  143. kCALLPH,
  144. kSWITCH_PREVVOWEL,
  145. kSWITCH_NEXTVOWEL,
  146. kENDSWITCH,
  147. kFMT,
  148. kWAV,
  149. kVOWELSTART,
  150. kVOWELENDING,
  151. kANDWAV,
  152. kVOWELIN,
  153. kVOWELOUT,
  154. kTONESPEC,
  155. kRETURN,
  156. kCONTINUE,
  157. };
  158. enum {
  159. kTUNE = 1,
  160. kENDTUNE,
  161. kTUNE_PREHEAD,
  162. kTUNE_ONSET,
  163. kTUNE_HEAD,
  164. kTUNE_HEADENV,
  165. kTUNE_HEADEXTEND,
  166. kTUNE_HEADLAST,
  167. kTUNE_NUCLEUS0,
  168. kTUNE_NUCLEUS1,
  169. kTUNE_SPLIT,
  170. };
  171. static unsigned const char utf8_bom[] = {0xef,0xbb,0xbf,0};
  172. static keywtab_t k_intonation[] = {
  173. {"tune", 0, kTUNE},
  174. {"endtune", 0, kENDTUNE},
  175. {"prehead", 0, kTUNE_PREHEAD},
  176. {"onset", 0, kTUNE_ONSET},
  177. {"head", 0, kTUNE_HEAD},
  178. {"headenv", 0, kTUNE_HEADENV},
  179. {"headextend", 0, kTUNE_HEADEXTEND},
  180. {"headlast", 0, kTUNE_HEADLAST},
  181. {"nucleus0", 0, kTUNE_NUCLEUS0},
  182. {"nucleus", 0, kTUNE_NUCLEUS1},
  183. {"split", 0, kTUNE_SPLIT},
  184. {NULL, 0, -1}
  185. };
  186. static keywtab_t keywords[] = {
  187. {"vowel", tPHONEME_TYPE, phVOWEL},
  188. {"liquid", tPHONEME_TYPE, phLIQUID},
  189. {"pause", tPHONEME_TYPE, phPAUSE},
  190. {"stress", tPHONEME_TYPE, phSTRESS},
  191. {"virtual", tPHONEME_TYPE, phVIRTUAL},
  192. {"fricative", tPHONEME_TYPE, phFRICATIVE},
  193. {"vstop", tPHONEME_TYPE, phVSTOP},
  194. {"vfricative",tPHONEME_TYPE, phVFRICATIVE},
  195. {"delete_phoneme", tPHONEME_TYPE, phDELETED},
  196. // type of consonant
  197. {"stop", tPHONEME_TYPE, phSTOP},
  198. {"frc", tPHONEME_TYPE, phFRICATIVE},
  199. {"nasal", tPHONEME_TYPE, phNASAL},
  200. {"flp", tPHONEME_TYPE, phVSTOP},
  201. {"afr", tPHONEME_TYPE, phSTOP}, // treat as stop
  202. {"apr", tPHONEME_TYPE, phFRICATIVE}, // [h] voiceless approximant
  203. // keywords
  204. {"phonemenumber",tSTATEMENT, kPHONEMENUMBER},
  205. {"phonemetable",tSTATEMENT, kPHONEMETABLE},
  206. {"include", tSTATEMENT, kINCLUDE},
  207. {(const char *)utf8_bom, tSTATEMENT, kUTF8_BOM},
  208. {"phoneme", tSTATEMENT, kPHONEMESTART},
  209. {"procedure", tSTATEMENT, kPROCEDURE},
  210. {"endphoneme", tSTATEMENT, kENDPHONEME},
  211. {"endprocedure", tSTATEMENT, kENDPROCEDURE},
  212. {"equivalents", tSTATEMENT, kEQUIVALENTS},
  213. {"import_phoneme", tSTATEMENT, kIMPORT_PH},
  214. {"stress_type",tSTATEMENT, kSTRESSTYPE},
  215. {"starttype", tSTATEMENT, kSTARTTYPE},
  216. {"endtype", tSTATEMENT, kENDTYPE},
  217. {"voicingswitch", tSTATEMENT, kVOICINGSWITCH},
  218. {"IF", tSTATEMENT, kIF},
  219. {"ELSE", tSTATEMENT, kELSE},
  220. {"ELIF", tSTATEMENT, kELIF},
  221. {"ELSEIF", tSTATEMENT, kELIF}, // same as ELIF
  222. {"ENDIF", tSTATEMENT, kENDIF},
  223. {"CALL", tSTATEMENT, kCALLPH},
  224. {"RETURN", tSTATEMENT, kRETURN},
  225. {"PrevVowelEndings", tSTATEMENT, kSWITCH_PREVVOWEL},
  226. {"NextVowelStarts", tSTATEMENT, kSWITCH_NEXTVOWEL},
  227. {"EndSwitch", tSTATEMENT, kENDSWITCH},
  228. {"Tone", tSTATEMENT, kTONESPEC},
  229. {"FMT", tSTATEMENT, kFMT},
  230. {"WAV", tSTATEMENT, kWAV},
  231. {"VowelStart", tSTATEMENT, kVOWELSTART},
  232. {"VowelEnding",tSTATEMENT, kVOWELENDING},
  233. {"addWav", tSTATEMENT, kANDWAV},
  234. {"Vowelin", tSTATEMENT, kVOWELIN},
  235. {"Vowelout", tSTATEMENT, kVOWELOUT},
  236. {"Continue", tSTATEMENT, kCONTINUE},
  237. {"ChangePhoneme", tINSTRN1, i_CHANGE_PHONEME},
  238. {"ChangeNextPhoneme", tINSTRN1, i_REPLACE_NEXT_PHONEME},
  239. {"InsertPhoneme", tINSTRN1, i_INSERT_PHONEME},
  240. {"AppendPhoneme", tINSTRN1, i_APPEND_PHONEME},
  241. {"IfNextVowelAppend", tINSTRN1, i_APPEND_IFNEXTVOWEL},
  242. {"ChangeIfDiminished", tINSTRN1, i_CHANGE_IF + 0},
  243. {"ChangeIfUnstressed", tINSTRN1, i_CHANGE_IF + 1},
  244. {"ChangeIfNotStressed", tINSTRN1, i_CHANGE_IF + 2},
  245. {"ChangeIfStressed", tINSTRN1, i_CHANGE_IF + 3},
  246. {"PauseBefore",tINSTRN1, i_PAUSE_BEFORE},
  247. {"PauseAfter", tINSTRN1, i_PAUSE_AFTER},
  248. {"Length", tINSTRN1, i_SET_LENGTH},
  249. {"length", tINSTRN1, i_SET_LENGTH},
  250. {"LongLength", tINSTRN1, i_LONG_LENGTH},
  251. {"LengthAdd", tINSTRN1, i_ADD_LENGTH},
  252. {"Lengthmod", tINSTRN1, i_LENGTH_MOD},
  253. {"lengthmod", tINSTRN1, i_LENGTH_MOD},
  254. {"ipa", tINSTRN1, i_IPA_NAME},
  255. // flags
  256. {"wavef", tPHONEME_FLAG, phWAVE},
  257. {"unstressed", tPHONEME_FLAG, phUNSTRESSED},
  258. {"fortis", tPHONEME_FLAG, phFORTIS},
  259. {"sibilant", tPHONEME_FLAG, phSIBILANT},
  260. {"nolink", tPHONEME_FLAG, phNOLINK},
  261. {"trill", tPHONEME_FLAG, phTRILL},
  262. {"vowel2", tPHONEME_FLAG, phVOWEL2},
  263. {"palatal", tPHONEME_FLAG, phPALATAL},
  264. {"long", tPHONEME_FLAG, phLONG},
  265. {"dontlist", tPHONEME_FLAG, phDONTLIST},
  266. {"brkafter", tPHONEME_FLAG, phBRKAFTER},
  267. {"rhotic", tPHONEME_FLAG, phRHOTIC},
  268. {"nonsyllabic",tPHONEME_FLAG, phNONSYLLABIC},
  269. {"lengthenstop",tPHONEME_FLAG, phLENGTHENSTOP},
  270. {"nopause", tPHONEME_FLAG, phNOPAUSE},
  271. {"prevoice", tPHONEME_FLAG, phPREVOICE},
  272. {"flag1", tPHONEME_FLAG, phFLAG1},
  273. {"flag2", tPHONEME_FLAG, phFLAG2},
  274. {"flag3", tPHONEME_FLAG, phFLAG3},
  275. // voiced / unvoiced
  276. {"vcd", tPHONEME_FLAG, phVOICED},
  277. {"vls", tPHONEME_FLAG, phFORTIS},
  278. // place of articulation, set bits 16-19 of phflags
  279. {"blb", tPLACE, 1},
  280. {"lbd", tPLACE, 2},
  281. {"dnt", tPLACE, 3},
  282. {"alv", tPLACE, 4},
  283. {"rfx", tPLACE, 5},
  284. {"pla", tPLACE, 6},
  285. {"pal", tPLACE, 7},
  286. {"vel", tPLACE, 8},
  287. {"lbv", tPLACE, 9},
  288. {"uvl", tPLACE, 10},
  289. {"phr", tPLACE, 11},
  290. {"glt", tPLACE, 12},
  291. // vowel transition attributes
  292. {"len=", tTRANSITION, 1},
  293. {"rms=", tTRANSITION, 2},
  294. {"f1=", tTRANSITION, 3},
  295. {"f2=", tTRANSITION, 4},
  296. {"f3=", tTRANSITION, 5},
  297. {"brk", tTRANSITION, 6},
  298. {"rate", tTRANSITION, 7},
  299. {"glstop", tTRANSITION, 8},
  300. {"lenadd", tTRANSITION, 9},
  301. {"f4", tTRANSITION, 10},
  302. {"gpaus", tTRANSITION, 11},
  303. {"colr=", tTRANSITION, 12},
  304. {"amp=", tTRANSITION, 13}, // set rms of 1st frame as fraction of rms of 2nd frame (1/30ths)
  305. {NULL, 0, -1}
  306. };
  307. static keywtab_t *keyword_tabs[] = {
  308. keywords, k_conditions, k_properties, k_intonation
  309. };
  310. static PHONEME_TAB *phoneme_out;
  311. static int n_phcodes_list[N_PHONEME_TABS];
  312. static PHONEME_TAB_LIST phoneme_tab_list2[N_PHONEME_TABS];
  313. static PHONEME_TAB *phoneme_tab2;
  314. static int phoneme_flags;
  315. static int place_articulation;
  316. static char *p_equivalence;
  317. static char equivalence_buf[20000];
  318. #define N_PROCS 50
  319. int n_procs;
  320. int proc_addr[N_PROCS];
  321. char proc_names[40][N_PROCS];
  322. #define MAX_PROG_BUF 2000
  323. USHORT *prog_out;
  324. USHORT *prog_out_max;
  325. USHORT prog_buf[MAX_PROG_BUF+20];
  326. static void ReadPhondataManifest()
  327. {
  328. // Read the phondata-manifest file
  329. FILE *f;
  330. int n_lines=0;
  331. int ix;
  332. char *p;
  333. unsigned int value;
  334. char buf[sizeof(path_home)+40];
  335. char name[120];
  336. sprintf(buf,"%s%c%s",path_home,PATHSEP,"phondata-manifest");
  337. if((f = fopen(buf, "r")) == NULL)
  338. return;
  339. while(fgets(buf, sizeof(buf), f) != NULL)
  340. n_lines++;
  341. rewind(f);
  342. if(manifest != NULL)
  343. {
  344. for(ix=0; ix < n_manifest; ix++)
  345. free(manifest[ix].name);
  346. }
  347. if((manifest = (NAMETAB *)realloc(manifest, n_lines * sizeof(NAMETAB))) == NULL)
  348. {
  349. fclose(f);
  350. return;
  351. }
  352. n_manifest = 0;
  353. while(fgets(buf, sizeof(buf), f) != NULL)
  354. {
  355. if(!isalpha(buf[0]))
  356. continue;
  357. if(sscanf(&buf[2], "%x %s", &value, name) == 2)
  358. {
  359. if((p = (char *)malloc(strlen(name)+1)) != NULL)
  360. {
  361. strcpy(p, name);
  362. manifest[n_manifest].value = value;
  363. manifest[n_manifest].name = p;
  364. n_manifest++;
  365. }
  366. }
  367. }
  368. fclose(f);
  369. }
  370. static const char *KeyToMnem(keywtab_t *ktab, int type, int value)
  371. {
  372. while(ktab->mnem != NULL)
  373. {
  374. if(ktab->data == value)
  375. {
  376. if((type == -1) || (type == ktab->type))
  377. return(ktab->mnem);
  378. }
  379. ktab++;
  380. }
  381. return(NULL);
  382. }
  383. static void DecompilePhoneme(FILE *f_out, PHONEME_TAB *ph, int compile_phoneme)
  384. {
  385. USHORT *pc;
  386. int instn;
  387. int instn_category;
  388. int address, address2;
  389. int data1;
  390. int type2;
  391. int ix;
  392. int any;
  393. const char *name;
  394. char buf[120];
  395. static const char *instn_category_string[16] = {
  396. "", "", "IF", "IF OR",
  397. "", "", "", "",
  398. "", "", "", "FMT",
  399. "WAV", "NextVowelStart", "PrevVowelEnd", "+wav"
  400. };
  401. static const char *nextPh_string[6] = {
  402. "prevPh", "thisPh", "nextPh", "next2Ph", "nextPhW", "**",
  403. };
  404. static const char *instn0_string[] = {
  405. "invalid", "RETURN", "Continue", "DeleteNextPhoneme",
  406. };
  407. static const char *instn10_string[] = {
  408. "", "VowelIn", "VowelOut", "Tone", "",
  409. };
  410. static const char *instn_jumps[] = {
  411. "JMP", "Invalid", "Invalid", "Invalid",
  412. "JMP false", "SwitchNextVowelType", "SwitchPrevVowelType", "Invalid"
  413. };
  414. static char instn1_paramtype[] = {
  415. 0, 3, 3, 3, 3, 3, 3, 1,
  416. 1, 1, 1, 1, 1, 0, 0, 0,
  417. 3, 3, 3, 3, 3, 3, 3, 3,
  418. 0, 0, 0, 0, 0, 0, 0, 0
  419. };
  420. return;
  421. if(compile_phoneme)
  422. {
  423. fprintf(f_out,"\nPhoneme %s (%d)\n",WordToString(ph->mnemonic),ph->code);
  424. }
  425. else
  426. {
  427. fprintf(f_out,"\nProcedure %s\n",proc_names[n_procs]);
  428. }
  429. pc = prog_buf;
  430. while(pc < prog_out)
  431. {
  432. instn = *pc++;
  433. instn_category = (instn >> 12) & 0xf;
  434. data1 = instn & 0xff;
  435. type2 = (instn >> 8) & 0xf;
  436. fprintf(f_out, " %.3x: %.4x %s",(unsigned int)(pc-prog_buf),instn,instn_category_string[instn_category]);
  437. switch(instn_category)
  438. {
  439. case 0:
  440. case 1:
  441. type2 = instn >> 8;
  442. if(instn < 0x100)
  443. {
  444. if(data1 > 2)
  445. data1 = 0;
  446. fprintf(f_out,"%s",instn0_string[data1]);
  447. }
  448. else
  449. if(type2 == i_IPA_NAME)
  450. {
  451. for(ix = 0; ix < data1; ix += 2)
  452. {
  453. instn = *pc++;
  454. buf[ix] = instn >> 8;
  455. buf[ix+1] = instn & 0xff;
  456. }
  457. buf[ix] = 0;
  458. fprintf(f_out,"ipa %s",buf);
  459. }
  460. else
  461. {
  462. fprintf(f_out,"%s(",KeyToMnem(keywords, tINSTRN1, type2));
  463. switch(instn1_paramtype[type2])
  464. {
  465. case 0:
  466. fprintf(f_out,"%.4x",instn);
  467. break;
  468. case 1:
  469. fprintf(f_out,"%d",data1);
  470. break;
  471. case 3:
  472. fprintf(f_out,"%s",WordToString(phoneme_tab2[data1].mnemonic));
  473. break;
  474. }
  475. fprintf(f_out,")");
  476. }
  477. break;
  478. case 2:
  479. case 3:
  480. if(type2 < 12)
  481. {
  482. fprintf(f_out," %s(",nextPh_string[type2 % 6]);
  483. if(type2 >= 6)
  484. {
  485. switch(data1 >> 5)
  486. {
  487. case 0:
  488. name = KeyToMnem(keywords, tPHONEME_TYPE, (data1 & 0x1f));
  489. if(name != NULL)
  490. fprintf(f_out,"is%s",name);
  491. else
  492. fprintf(f_out,"%d %d",(data1 >> 5),(data1 & 0x1f));
  493. break;
  494. case 1:
  495. fprintf(f_out,"%d %d",(data1 >> 5),(data1 & 0x1f));
  496. break;
  497. case 2:
  498. fprintf(f_out,"%d %d",(data1 >> 5),(data1 & 0x1f));
  499. break;
  500. case 4:
  501. name = KeyToMnem(k_properties, -1, 0x80+(data1 & 0x1f));
  502. if(name != NULL)
  503. fprintf(f_out,"%s",name);
  504. else
  505. fprintf(f_out,"%d %d",(data1 >> 5),(data1 & 0x1f));
  506. break;
  507. default:
  508. fprintf(f_out,"%d %d",(data1 >> 5),(data1 & 0x1f));
  509. break;
  510. }
  511. }
  512. else
  513. {
  514. fprintf(f_out,"%s",WordToString(phoneme_tab2[data1].mnemonic));
  515. }
  516. }
  517. else
  518. if(type2 == 8)
  519. {
  520. // list of numbers
  521. fprintf(f_out," StressLevel(");
  522. any = 0;
  523. for(ix=0; ix<8; ix++)
  524. {
  525. if(data1 & (1 << ix))
  526. {
  527. if(any)
  528. fputc(',',f_out);
  529. any = 1;
  530. fprintf(f_out,"%d",ix);
  531. }
  532. }
  533. }
  534. else
  535. {
  536. name = KeyToMnem(k_conditions, tTEST, instn & 0xfff);
  537. fprintf(f_out,"%s(",name);
  538. }
  539. fprintf(f_out,")");
  540. break;
  541. case 6:
  542. fprintf(f_out,"%s",instn_jumps[(instn >> 9) & 7]);
  543. fprintf(f_out," %d",instn & 0x1ff);
  544. break;
  545. case 9:
  546. address = ((data1 & 0xf) << 4) + *pc++;
  547. fprintf(f_out,"CALL %.5x",address);
  548. break;
  549. case 10:
  550. fprintf(f_out,"%s",instn10_string[type2]);
  551. switch(type2)
  552. {
  553. case 1:
  554. case 2:
  555. address = (data1 << 16) + pc[0];
  556. address2 = (pc[1] << 16) + pc[2];
  557. pc += 3;
  558. fprintf(f_out, " %.6x %.8x",address,address2);
  559. break;
  560. case 3:
  561. address = ((instn & 0xf) << 16) + *pc++;
  562. address2 = ((instn & 0xf0) << 12) + *pc++;
  563. fprintf(f_out, " %.5x %.5x",address,address2);
  564. break;
  565. }
  566. break;
  567. case 11:
  568. case 12:
  569. case 13:
  570. case 14:
  571. case 15:
  572. address = ((instn & 0xf) << 16) + *pc++;
  573. fprintf(f_out, " %d %.5x",(instn >> 4) & 0xff,address*4);
  574. break;
  575. }
  576. fprintf(f_out,"\n");
  577. }
  578. }
  579. static int n_phoneme_tabs;
  580. static int n_phcodes;
  581. // outout files
  582. static FILE *f_phdata;
  583. static FILE *f_phindex;
  584. static FILE *f_phtab;
  585. static FILE *f_phcontents;
  586. static FILE *f_errors = NULL;
  587. static FILE *f_prog_log = NULL;
  588. static FILE *f_report;
  589. static FILE *f_in;
  590. static int f_in_linenum;
  591. static int f_in_displ;
  592. static int linenum;
  593. static int count_references = 0;
  594. static int duplicate_references = 0;
  595. static int count_frames = 0;
  596. static int error_count = 0;
  597. static int resample_count = 0;
  598. static int resample_fails = 0;
  599. static int then_count = 0;
  600. static int after_if = 0;
  601. static char current_fname[80];
  602. static int markers_used[8];
  603. typedef struct {
  604. void *link;
  605. int value;
  606. int ph_mnemonic;
  607. short ph_table;
  608. char string[1];
  609. } REF_HASH_TAB;
  610. static REF_HASH_TAB *ref_hash_tab[256];
  611. #define N_ENVELOPES 30
  612. int n_envelopes = 0;
  613. char envelope_paths[N_ENVELOPES][80];
  614. unsigned char envelope_dat[N_ENVELOPES][ENV_LEN];
  615. typedef struct {
  616. FILE *file;
  617. int linenum;
  618. char fname[80];
  619. } STACK;
  620. #define N_STACK 12
  621. int stack_ix;
  622. STACK stack[N_STACK];
  623. #define N_IF_STACK 12
  624. int if_level;
  625. typedef struct {
  626. USHORT *p_then;
  627. USHORT *p_else;
  628. int returned;
  629. } IF_STACK;
  630. IF_STACK if_stack[N_IF_STACK];
  631. enum {
  632. tENDFILE = 1,
  633. tSTRING,
  634. tNUMBER,
  635. tSIGNEDNUMBER,
  636. tPHONEMEMNEM,
  637. tOPENBRACKET,
  638. tKEYWORD,
  639. tCONDITION,
  640. tPROPERTIES,
  641. tINTONATION,
  642. };
  643. int item_type;
  644. int item_terminator;
  645. #define N_ITEM_STRING 256
  646. char item_string[N_ITEM_STRING];
  647. static int ref_sorter(char **a, char **b)
  648. {
  649. int ix;
  650. REF_HASH_TAB *p1 = (REF_HASH_TAB *)(*a);
  651. REF_HASH_TAB *p2 = (REF_HASH_TAB *)(*b);
  652. ix = strcoll(p1->string,p2->string);
  653. if(ix != 0)
  654. return ix;
  655. ix = p1->ph_table - p2->ph_table;
  656. if(ix != 0)
  657. return ix;
  658. return(p1->ph_mnemonic - p2->ph_mnemonic);
  659. }
  660. static void CompileReport(void)
  661. {
  662. int ix;
  663. int hash;
  664. int n;
  665. REF_HASH_TAB *p;
  666. REF_HASH_TAB **list;
  667. const char *data_path;
  668. int prev_table;
  669. int procedure_num;
  670. int prev_mnemonic;
  671. if(f_report == NULL)
  672. return;
  673. // make a list of all the references and sort it
  674. list = (REF_HASH_TAB **)malloc((count_references)* sizeof(REF_HASH_TAB *));
  675. if(list == NULL)
  676. return;
  677. fprintf(f_report,"\n%d phoneme tables\n",n_phoneme_tabs);
  678. fprintf(f_report," new total\n");
  679. for(ix=0; ix<n_phoneme_tabs; ix++)
  680. {
  681. fprintf(f_report,"%8s %3d %4d\n",phoneme_tab_list2[ix].name, phoneme_tab_list2[ix].n_phonemes, n_phcodes_list[ix]+1);
  682. }
  683. fputc('\n',f_report);
  684. fprintf(f_report,"Data file Used by\n");
  685. ix = 0;
  686. for(hash=0; (hash < 256) && (ix < count_references); hash++)
  687. {
  688. p = ref_hash_tab[hash];
  689. while(p != NULL)
  690. {
  691. list[ix++] = p;
  692. p = (REF_HASH_TAB *)(p->link);
  693. }
  694. }
  695. n = ix;
  696. qsort((void *)list,n,sizeof(REF_HASH_TAB *),(int (*)(const void *,const void *))ref_sorter);
  697. data_path = "";
  698. prev_mnemonic = 0;
  699. prev_table = 0;
  700. for(ix=0; ix<n; ix++)
  701. {
  702. int j = 0;
  703. if(strcmp(list[ix]->string, data_path) != 0)
  704. {
  705. data_path = list[ix]->string;
  706. j = strlen(data_path);
  707. fprintf(f_report,"%s",data_path);
  708. }
  709. else
  710. {
  711. if((list[ix]->ph_table == prev_table) && (list[ix]->ph_mnemonic == prev_mnemonic))
  712. continue; // same phoneme, don't list twice
  713. }
  714. while(j < 14)
  715. {
  716. fputc(' ',f_report); // pad filename with spaces
  717. j++;
  718. }
  719. prev_mnemonic = list[ix]->ph_mnemonic;
  720. if((prev_mnemonic >> 24) == 'P')
  721. {
  722. // a procedure, not a phoneme
  723. procedure_num = atoi(WordToString(prev_mnemonic));
  724. fprintf(f_report," %s %s", phoneme_tab_list2[prev_table = list[ix]->ph_table].name, proc_names[procedure_num]);
  725. }
  726. else
  727. {
  728. fprintf(f_report," [%s] %s",WordToString(prev_mnemonic), phoneme_tab_list2[prev_table = list[ix]->ph_table].name);
  729. }
  730. fputc('\n',f_report);
  731. }
  732. for(ix=0; ix<n; ix++)
  733. {
  734. free(list[ix]);
  735. list[ix] = NULL;
  736. }
  737. free(list);
  738. list = NULL;
  739. fclose(f_report);
  740. }
  741. #ifdef PLATFORM_WINDOWS
  742. int strcasecmp(const char *s1, const char *s2)
  743. {
  744. int ix=0;
  745. int diff;
  746. for(;; )
  747. {
  748. if((diff = (tolower(s1[ix]) - tolower(s2[ix]))) != 0)
  749. return(diff);
  750. if((s1[ix] == 0) || (s2[ix] == 0))
  751. return(diff);
  752. ix++;
  753. }
  754. }
  755. #endif
  756. static int ph_sorter(char **a, char **b)
  757. {
  758. int ix;
  759. int t1, t2;
  760. char mnem1[6];
  761. PHONEME_TAB *p1 = (PHONEME_TAB *)(*a);
  762. PHONEME_TAB *p2 = (PHONEME_TAB *)(*b);
  763. t1 = p1->type;
  764. if(t1 > phVOWEL) t1 = phVOWEL+1;
  765. t2 = p2->type;
  766. if(t2 > phVOWEL) t2 = phVOWEL+1;
  767. if((ix = t1 - t2) != 0)
  768. return(ix);
  769. strcpy(mnem1,WordToString(p1->mnemonic));
  770. return(strcasecmp(mnem1,WordToString(p2->mnemonic)));
  771. }
  772. static void PrintPhonemesUsed(FILE *f, const char *dsource, const char *dictname)
  773. {
  774. int ix;
  775. PHONEME_TAB *ph;
  776. PHONEME_TAB *ph_tab[N_PHONEME_TAB];
  777. int count = 0;
  778. int n_ph = 0;
  779. int section = 0;
  780. time_t mod_time;
  781. char fname[sizeof(path_home)+45];
  782. struct stat statbuf;
  783. char time_string[20];
  784. const char *files[] = {"rules","list","listx"};
  785. // find the date-stamp of the dictionary source files
  786. mod_time = 0;
  787. for(ix=0; ix<3; ix++)
  788. {
  789. sprintf(fname,"%s%s_%s",dsource, dictname, files[ix]);
  790. if(stat(fname,&statbuf) == 0)
  791. {
  792. if(statbuf.st_mtime > mod_time)
  793. mod_time = statbuf.st_mtime;
  794. }
  795. }
  796. if(mod_time > 0)
  797. {
  798. strftime(time_string, sizeof(time_string), "%Y-%m-%d", localtime(&mod_time));
  799. fprintf(f,"\n\nDictionary %s_dict %s\n",dictname, time_string);
  800. }
  801. else
  802. {
  803. fprintf(f,"\n\nDictionary %s_dict\n",dictname);
  804. }
  805. fflush(f);
  806. for(ix=0; (ix<N_PHONEME_TAB) && (phoneme_tab[ix] != NULL); ix++)
  807. {
  808. if(phoneme_tab_flags[ix] & 2)
  809. {
  810. ph_tab[n_ph++] = phoneme_tab[ix];
  811. }
  812. }
  813. qsort((void *)ph_tab,n_ph,sizeof(PHONEME_TAB *),(int (*)(const void *,const void *))ph_sorter);
  814. for(ix=0; ix<n_ph; ix++)
  815. {
  816. ph = ph_tab[ix];
  817. if(ph->phflags & phDONTLIST) // "dontlist" attribute
  818. continue;
  819. if(ph->type > 1)
  820. {
  821. if((ph->type > phVOWEL) && (section == 0))
  822. {
  823. section = 1;
  824. count = 0;
  825. fputc('\n',f);
  826. }
  827. if((count & 0x7) == 0)
  828. fputc('\n',f);
  829. fprintf(f,"%-4s ",WordToString(ph->mnemonic));
  830. count++;
  831. }
  832. }
  833. fputc('\n',f);
  834. }
  835. static void error(const char *format, const char *string)
  836. {
  837. if(string==NULL)
  838. string = "";
  839. fprintf(f_errors,"%4d: ",linenum-1);
  840. fprintf(f_errors,format,string);
  841. fprintf(f_errors,"\n");
  842. error_count++;
  843. }
  844. static void Error(const char *string)
  845. {
  846. error("%s",string);
  847. }
  848. static FILE *fopen_log(FILE *f_log, const char *fname,const char *access)
  849. {
  850. // performs fopen, but produces error message to f_log if it fails
  851. FILE *f;
  852. if((f = fopen(fname,access)) == NULL)
  853. {
  854. if(f_log != NULL)
  855. fprintf(f_log,"Can't access (%s) file '%s'\n",access,fname);
  856. }
  857. return(f);
  858. }
  859. static unsigned int StringToWord(const char *string)
  860. {
  861. // Pack 4 characters into a word
  862. int ix;
  863. unsigned char c;
  864. unsigned int word;
  865. if(string==NULL)
  866. return(0);
  867. word = 0;
  868. for(ix=0; ix<4; ix++)
  869. {
  870. if(string[ix]==0) break;
  871. c = string[ix];
  872. word |= (c << (ix*8));
  873. }
  874. return(word);
  875. }
  876. static MNEM_TAB reserved_phonemes[] = {
  877. {"_\001", phonCONTROL}, // NOT USED
  878. {"%", phonSTRESS_U},
  879. {"%%", phonSTRESS_D},
  880. {",", phonSTRESS_2},
  881. {",,", phonSTRESS_3},
  882. {"'", phonSTRESS_P},
  883. {"''", phonSTRESS_P2},
  884. {"=", phonSTRESS_PREV}, // stress previous syllable
  885. {"_:", phonPAUSE}, // pause
  886. {"_", phonPAUSE_SHORT}, // short pause
  887. {"_!", phonPAUSE_NOLINK}, // short pause, no link
  888. {":", phonLENGTHEN},
  889. {"@", phonSCHWA},
  890. {"@-", phonSCHWA_SHORT},
  891. {"||", phonEND_WORD},
  892. {"1", phonDEFAULTTONE}, // (numeral 1) default tone (for tone language)
  893. {"#X1",phonCAPITAL}, // capital letter indication
  894. {"?", phonGLOTTALSTOP}, // glottal stop
  895. {"-", phonSYLLABIC}, // syllabic consonant
  896. {"_^_",phonSWITCH}, // Change language
  897. {"_X1",phonX1}, // a language specific action
  898. {"_|", phonPAUSE_VSHORT}, // very short pause
  899. {"_::",phonPAUSE_LONG}, // long pause
  900. {"t#", phonT_REDUCED}, // reduced version of [t]
  901. {"'!", phonSTRESS_TONIC}, // stress - emphasized
  902. {"_;_",phonPAUSE_CLAUSE}, // clause pause
  903. {"#@", phonVOWELTYPES}, // vowel type groups, these must be consecutive
  904. {"#a", phonVOWELTYPES+1},
  905. {"#e", phonVOWELTYPES+2},
  906. {"#i", phonVOWELTYPES+3},
  907. {"#o", phonVOWELTYPES+4},
  908. {"#u", phonVOWELTYPES+5},
  909. {NULL, 0}
  910. };
  911. static void ReservePhCodes()
  912. {
  913. // Reserve phoneme codes which have fixed numbers so that they can be
  914. // referred to from the program code.
  915. unsigned int word;
  916. MNEM_TAB *p;
  917. p = reserved_phonemes;
  918. while(p->mnem != NULL)
  919. {
  920. word = StringToWord(p->mnem);
  921. phoneme_tab2[p->value].mnemonic = word;
  922. phoneme_tab2[p->value].code = p->value;
  923. if(n_phcodes <= p->value)
  924. n_phcodes = p->value+1;
  925. p++;
  926. }
  927. }
  928. static int LookupPhoneme(const char *string, int control)
  929. {
  930. // control = 0 explicit declaration
  931. // control = 1 declare phoneme if not found
  932. // control = 2 start looking after control & stress phonemes
  933. int ix;
  934. int start;
  935. int use;
  936. unsigned int word;
  937. if(strcmp(string,"NULL")==0)
  938. return(1);
  939. ix = strlen(string);
  940. if((ix==0) || (ix> 4))
  941. {
  942. error("Bad phoneme name '%s'",string);
  943. }
  944. word = StringToWord(string);
  945. // don't use phoneme number 0, reserved for string terminator
  946. start = 1;
  947. if(control==2)
  948. start = 8; // don't look for control and stress phonemes (allows these characters to be
  949. // used for other purposes)
  950. use = 0;
  951. for(ix=start; ix<n_phcodes; ix++)
  952. {
  953. if(phoneme_tab2[ix].mnemonic == word)
  954. return(ix);
  955. if((use==0) && (phoneme_tab2[ix].mnemonic == 0))
  956. {
  957. use = ix;
  958. }
  959. }
  960. if(use == 0)
  961. {
  962. if(control == 0)
  963. return(-1);
  964. if(n_phcodes >= N_PHONEME_TAB-1)
  965. return(-1); // phoneme table is full
  966. use = n_phcodes++;
  967. }
  968. // add this phoneme to the phoneme table
  969. phoneme_tab2[use].mnemonic = word;
  970. phoneme_tab2[use].type = phINVALID;
  971. phoneme_tab2[use].program = linenum; // for error report if the phoneme remains undeclared
  972. return(use);
  973. }
  974. static unsigned int get_char()
  975. {
  976. unsigned int c;
  977. c = fgetc(f_in);
  978. if(c == '\n')
  979. linenum++;
  980. return(c);
  981. }
  982. static void unget_char(unsigned int c)
  983. {
  984. ungetc(c,f_in);
  985. if(c == '\n')
  986. linenum--;
  987. }
  988. int CheckNextChar()
  989. {
  990. int c;
  991. while(((c = get_char()) == ' ') || (c == '\t'));
  992. unget_char(c);
  993. return(c);
  994. }
  995. static int NextItem(int type)
  996. {
  997. int acc;
  998. unsigned char c=0;
  999. unsigned char c2;
  1000. int ix;
  1001. int sign;
  1002. char *p;
  1003. keywtab_t *pk;
  1004. item_type = -1;
  1005. f_in_displ = ftell(f_in);
  1006. f_in_linenum = linenum;
  1007. while(!feof(f_in))
  1008. {
  1009. c = get_char();
  1010. if(c=='/')
  1011. {
  1012. if((c2 = get_char())=='/')
  1013. {
  1014. // comment, ignore to end of line
  1015. while(!feof(f_in) && ((c = get_char()) != '\n'));
  1016. }
  1017. else
  1018. {
  1019. unget_char(c2);
  1020. }
  1021. }
  1022. if(!isspace(c))
  1023. {
  1024. break;
  1025. }
  1026. }
  1027. if(feof(f_in))
  1028. return(-2);
  1029. if(c == '(')
  1030. {
  1031. if(type == tOPENBRACKET)
  1032. return(1);
  1033. return(-1);
  1034. }
  1035. ix = 0;
  1036. while(!feof(f_in) && !isspace(c) && (c != '(') && (c != ')') && (c != ','))
  1037. {
  1038. if(c == '\\')
  1039. c = get_char();
  1040. item_string[ix++] = c;
  1041. c = get_char();
  1042. if(feof(f_in))
  1043. break;
  1044. if(item_string[ix-1] == '=')
  1045. break;
  1046. }
  1047. item_string[ix] = 0;
  1048. while(isspace(c))
  1049. {
  1050. c = get_char();
  1051. }
  1052. item_terminator = ' ';
  1053. if((c == ')') || (c == '(') || (c == ','))
  1054. item_terminator = c;
  1055. if((c == ')') || (c == ','))
  1056. c = ' ';
  1057. if(!feof(f_in))
  1058. unget_char(c);
  1059. if(type == tSTRING)
  1060. {
  1061. return(0);
  1062. }
  1063. if((type == tNUMBER) || (type == tSIGNEDNUMBER))
  1064. {
  1065. acc = 0;
  1066. sign = 1;
  1067. p = item_string;
  1068. if((*p == '-') && (type == tSIGNEDNUMBER))
  1069. {
  1070. sign = -1;
  1071. p++;
  1072. }
  1073. if(!isdigit(*p))
  1074. {
  1075. if((type == tNUMBER) && (*p == '-'))
  1076. error("Expected an unsigned number",NULL);
  1077. else
  1078. error("Expected a number",NULL);
  1079. }
  1080. while(isdigit(*p))
  1081. {
  1082. acc *= 10;
  1083. acc += (*p - '0');
  1084. p++;
  1085. }
  1086. return(acc * sign);
  1087. }
  1088. if((type >= tKEYWORD) && (type <= tINTONATION))
  1089. {
  1090. pk = keyword_tabs[type-tKEYWORD];
  1091. while(pk->mnem != NULL)
  1092. {
  1093. if(strcmp(item_string,pk->mnem)==0)
  1094. {
  1095. item_type = pk->type;
  1096. return(pk->data);
  1097. }
  1098. pk++;
  1099. }
  1100. item_type = -1;
  1101. return(-1); // keyword not found
  1102. }
  1103. if(type == tPHONEMEMNEM)
  1104. {
  1105. return(LookupPhoneme(item_string,2));
  1106. }
  1107. return(-1);
  1108. }
  1109. static int NextItemMax(int max)
  1110. {
  1111. // Get a number, but restrict value to max
  1112. int value;
  1113. char msg[80];
  1114. value = NextItem(tNUMBER);
  1115. if(value > max)
  1116. {
  1117. sprintf(msg, "Value %d is greater than maximum %d", value, max);
  1118. error(msg, NULL);
  1119. value = max;
  1120. }
  1121. return(value);
  1122. }
  1123. static int NextItemBrackets(int type, int control)
  1124. {
  1125. // Expect a parameter inside parantheses
  1126. // control: bit 0 0= need (
  1127. // bit 1 1= allow comma
  1128. int value;
  1129. if((control & 1) == 0)
  1130. {
  1131. if(!NextItem(tOPENBRACKET))
  1132. {
  1133. error("Expected '('", NULL);
  1134. }
  1135. }
  1136. value = NextItem(type);
  1137. if((control & 2) && (item_terminator == ','))
  1138. return(value);
  1139. if(item_terminator != ')')
  1140. {
  1141. error("Expected ')'", NULL);
  1142. }
  1143. return(value);
  1144. }
  1145. static void UngetItem()
  1146. {
  1147. fseek(f_in,f_in_displ,SEEK_SET);
  1148. linenum = f_in_linenum;
  1149. }
  1150. static int Range(int value, int divide, int min, int max)
  1151. {
  1152. if(value < 0)
  1153. value -= divide/2;
  1154. else
  1155. value += divide/2;
  1156. value = value / divide;
  1157. if(value > max)
  1158. value = max;
  1159. if(value < min)
  1160. value = min;
  1161. return(value - min);
  1162. }
  1163. int CompileVowelTransition(int which)
  1164. {
  1165. // Compile a vowel transition
  1166. int key;
  1167. int len=0;
  1168. int rms=0;
  1169. int f1=0;
  1170. int f2=0;
  1171. int f2_min=0;
  1172. int f2_max=0;
  1173. int f3_adj=0;
  1174. int f3_amp=0;
  1175. int flags=0;
  1176. int vcolour=0;
  1177. int x;
  1178. int instn = i_VOWELIN;
  1179. int word1;
  1180. int word2;
  1181. if(which==1)
  1182. {
  1183. len = 50 / 2; // defaults for transition into vowel
  1184. rms = 25 / 2;
  1185. if(phoneme_out->type == phSTOP)
  1186. {
  1187. len = 42 / 2; // defaults for transition into vowel
  1188. rms = 30 / 2;
  1189. }
  1190. }
  1191. else
  1192. if(which==2)
  1193. {
  1194. instn = i_VOWELOUT;
  1195. len = 36 / 2; // defaults for transition out of vowel
  1196. rms = 16 / 2;
  1197. }
  1198. for(;; )
  1199. {
  1200. key = NextItem(tKEYWORD);
  1201. if(item_type != tTRANSITION)
  1202. {
  1203. UngetItem();
  1204. break;
  1205. }
  1206. switch(key & 0xf)
  1207. {
  1208. case 1:
  1209. len = Range(NextItem(tNUMBER), 2, 0, 63) & 0x3f;
  1210. flags |= 1;
  1211. break;
  1212. case 2:
  1213. rms = Range(NextItem(tNUMBER), 2, 0, 31) & 0x1f;
  1214. flags |= 1;
  1215. break;
  1216. case 3:
  1217. f1 = NextItem(tNUMBER);
  1218. break;
  1219. case 4:
  1220. f2 = Range(NextItem(tNUMBER), 50, 0, 63) & 0x3f;
  1221. f2_min = Range(NextItem(tSIGNEDNUMBER), 50, -15, 15) & 0x1f;
  1222. f2_max = Range(NextItem(tSIGNEDNUMBER), 50, -15, 15) & 0x1f;
  1223. if(f2_min > f2_max)
  1224. {
  1225. x = f2_min;
  1226. f2_min = f2_max;
  1227. f2_max = x;
  1228. }
  1229. break;
  1230. case 5:
  1231. f3_adj = Range(NextItem(tSIGNEDNUMBER), 50, -15, 15) & 0x1f;
  1232. f3_amp = Range(NextItem(tNUMBER), 8, 0, 15) & 0x1f;
  1233. break;
  1234. case 6:
  1235. flags |= 2; // break
  1236. break;
  1237. case 7:
  1238. flags |= 4; // rate
  1239. break;
  1240. case 8:
  1241. flags |= 8; // glstop
  1242. break;
  1243. case 9:
  1244. flags |= 16; // lenadd
  1245. break;
  1246. case 10:
  1247. flags |= 32; // f4
  1248. break;
  1249. case 11:
  1250. flags |= 64; // paus
  1251. break;
  1252. case 12:
  1253. vcolour = NextItem(tNUMBER);
  1254. break;
  1255. case 13:
  1256. // set rms of 1st frame as fraction of rms of 2nd frame (1/30ths)
  1257. rms = (Range(NextItem(tNUMBER), 1, 0, 31) & 0x1f) | 0x20;
  1258. flags |= 1;
  1259. break;
  1260. }
  1261. }
  1262. word1 = len + (rms << 6) + (flags << 12);
  1263. word2 = f2 + (f2_min << 6) + (f2_max << 11) + (f3_adj << 16) + (f3_amp << 21) + (f1 << 26) + (vcolour << 29);
  1264. prog_out[0] = instn + ((word1 >> 16) & 0xff);
  1265. prog_out[1] = word1;
  1266. prog_out[2] = word2 >> 16;
  1267. prog_out[3] = word2;
  1268. prog_out += 4;
  1269. return(0);
  1270. }
  1271. int LoadSpect(const char *path, int control)
  1272. {
  1273. SpectSeq *spectseq;
  1274. int peak;
  1275. int displ;
  1276. int frame;
  1277. int n_frames;
  1278. int ix;
  1279. int x, x2;
  1280. int rms;
  1281. float total;
  1282. float pkheight;
  1283. int marker1_set=0;
  1284. int frame_vowelbreak=0;
  1285. int klatt_flag=0;
  1286. SpectFrame *fr;
  1287. frame_t *fr_out;
  1288. char filename[sizeof(path_home)+20];
  1289. SPECT_SEQ seq_out;
  1290. SPECT_SEQK seqk_out;
  1291. // create SpectSeq and import data
  1292. spectseq = SpectSeqCreate();
  1293. if(spectseq == NULL)
  1294. {
  1295. Error("Failed to create SpectSeq");
  1296. return(0);
  1297. }
  1298. snprintf(filename, sizeof(filename), "%s/../phsource/%s", path_home, path);
  1299. LoadSpectSeq(spectseq, filename);
  1300. if(spectseq->frames == NULL)
  1301. {
  1302. error("Bad vowel file, no frames: '%s'",path);
  1303. SpectSeqDestroy(spectseq);
  1304. return(0);
  1305. }
  1306. // do we need additional klatt data ?
  1307. for(frame=0; frame < spectseq->numframes; frame++)
  1308. {
  1309. for(ix=5; ix<N_KLATTP2; ix++)
  1310. {
  1311. if(spectseq->frames[frame]->klatt_param[ix] != 0)
  1312. klatt_flag = FRFLAG_KLATT;
  1313. }
  1314. }
  1315. displ = ftell(f_phdata);
  1316. seq_out.n_frames=0;
  1317. seq_out.sqflags=0;
  1318. seq_out.length_total=0;
  1319. total = 0;
  1320. for(frame=0; frame < spectseq->numframes; frame++)
  1321. {
  1322. if(spectseq->frames[frame]->keyframe)
  1323. {
  1324. if(seq_out.n_frames == 1)
  1325. {
  1326. frame_vowelbreak = frame;
  1327. }
  1328. if(spectseq->frames[frame]->markers & 0x2)
  1329. {
  1330. // marker 1 is set
  1331. marker1_set = 1;
  1332. }
  1333. seq_out.n_frames++;
  1334. if(frame > 0)
  1335. total += spectseq->frames[frame-1]->length;
  1336. }
  1337. }
  1338. seq_out.length_total = (int)total;
  1339. if((control & 1) && (marker1_set == 0))
  1340. {
  1341. // This is a vowel, but no Vowel Break marker is set
  1342. // set a marker flag for the second frame of a vowel
  1343. spectseq->frames[frame_vowelbreak]->markers |= FRFLAG_VOWEL_CENTRE;
  1344. }
  1345. n_frames = 0;
  1346. for(frame=0; frame < spectseq->numframes; frame++)
  1347. {
  1348. fr = spectseq->frames[frame];
  1349. if(fr->keyframe)
  1350. {
  1351. if(klatt_flag)
  1352. fr_out = &seqk_out.frame[n_frames];
  1353. else
  1354. fr_out = (frame_t *)&seq_out.frame[n_frames];
  1355. x = (int)(fr->length + 0.5); // round to nearest mS
  1356. if(x > 255) x = 255;
  1357. fr_out->length = x;
  1358. fr_out->frflags = fr->markers | klatt_flag;
  1359. rms = (int)GetFrameRms(fr, spectseq->amplitude);
  1360. if(rms > 255) rms = 255;
  1361. fr_out->rms = rms;
  1362. if(n_frames == (seq_out.n_frames-1))
  1363. fr_out->length = 0; // give last frame zero length
  1364. // write: peak data
  1365. count_frames++;
  1366. for(peak=0; peak < 8; peak++)
  1367. {
  1368. if(peak < 7)
  1369. fr_out->ffreq[peak] = fr->peaks[peak].pkfreq;
  1370. pkheight = spectseq->amplitude * fr->amp_adjust * fr->peaks[peak].pkheight;
  1371. pkheight = pkheight/640000;
  1372. if(pkheight > 255) pkheight = 255;
  1373. fr_out->fheight[peak] = (int)pkheight;
  1374. if(peak < 6)
  1375. {
  1376. x = fr->peaks[peak].pkwidth/4;
  1377. if(x > 255) x = 255;
  1378. fr_out->fwidth[peak] = x;
  1379. if(peak < 3)
  1380. {
  1381. x2 = fr->peaks[peak].pkright/4;
  1382. if(x2 > 255) x2 = 255;
  1383. fr_out->fright[peak] = x2;
  1384. }
  1385. }
  1386. if(peak < 4)
  1387. {
  1388. x = fr->peaks[peak].klt_bw / 2;
  1389. if(x > 255) x = 255;
  1390. fr_out->bw[peak] = x;
  1391. }
  1392. }
  1393. for(ix=0; ix<5; ix++)
  1394. {
  1395. fr_out->klattp[ix] = fr->klatt_param[ix];
  1396. fr_out->klattp[KLATT_FNZ] = fr->klatt_param[KLATT_FNZ] / 2;
  1397. }
  1398. if(klatt_flag)
  1399. {
  1400. // additional klatt parameters
  1401. for(ix=0; ix<5; ix++)
  1402. {
  1403. fr_out->klattp2[ix] = fr->klatt_param[ix+5];
  1404. }
  1405. for(peak=0; peak<7; peak++)
  1406. {
  1407. fr_out->klatt_ap[ix] = fr->peaks[peak].klt_ap;
  1408. x = fr->peaks[peak].klt_bp / 2;
  1409. if(x > 255) x = 255;
  1410. fr_out->klatt_bp[ix] = x;
  1411. }
  1412. }
  1413. if(fr_out->bw[1] == 0)
  1414. {
  1415. fr_out->bw[0] = 89 / 2;
  1416. fr_out->bw[1] = 90 / 2;
  1417. fr_out->bw[2] = 140 / 2;
  1418. fr_out->bw[3] = 260 / 2;
  1419. }
  1420. n_frames++;
  1421. }
  1422. }
  1423. if(klatt_flag)
  1424. {
  1425. seqk_out.n_frames = seq_out.n_frames;
  1426. seqk_out.sqflags = seq_out.sqflags;
  1427. seqk_out.length_total = seq_out.length_total;
  1428. ix = (char *)(&seqk_out.frame[seqk_out.n_frames]) - (char *)(&seqk_out);
  1429. ix = (ix+3) & 0xfffc; // round up to multiple of 4 bytes
  1430. fwrite(&seqk_out,ix,1,f_phdata);
  1431. }
  1432. else
  1433. {
  1434. ix = (char *)(&seq_out.frame[seq_out.n_frames]) - (char *)(&seq_out);
  1435. ix = (ix+3) & 0xfffc; // round up to multiple of 4 bytes
  1436. fwrite(&seq_out,ix,1,f_phdata);
  1437. }
  1438. SpectSeqDestroy(spectseq);
  1439. return(displ);
  1440. }
  1441. static int LoadWavefile(FILE *f, const char *fname)
  1442. {
  1443. int displ;
  1444. unsigned char c1;
  1445. unsigned char c3;
  1446. int c2;
  1447. int sample;
  1448. int sample2;
  1449. float x;
  1450. int max = 0;
  1451. int length;
  1452. int sr1, sr2;
  1453. int failed;
  1454. int len;
  1455. int resample_wav = 0;
  1456. const char *fname2;
  1457. char fname_temp[100];
  1458. char msg[120];
  1459. int scale_factor=0;
  1460. fseek(f,24,SEEK_SET);
  1461. sr1 = Read4Bytes(f);
  1462. sr2 = Read4Bytes(f);
  1463. fseek(f,40,SEEK_SET);
  1464. if((sr1 != samplerate_native) || (sr2 != sr1*2))
  1465. {
  1466. int fd_temp;
  1467. char command[sizeof(path_home)+250];
  1468. failed = 0;
  1469. #ifdef PLATFORM_POSIX
  1470. strcpy(fname_temp,"/tmp/espeakXXXXXX");
  1471. if((fd_temp = mkstemp(fname_temp)) >= 0)
  1472. {
  1473. close(fd_temp);
  1474. }
  1475. #else
  1476. strcpy(fname_temp,tmpnam(NULL));
  1477. #endif
  1478. fname2 = fname;
  1479. len = strlen(fname);
  1480. if(strcmp(&fname[len-4], ".wav") == 0)
  1481. {
  1482. strcpy(msg, fname);
  1483. msg[len-4] = 0;
  1484. fname2 = msg;
  1485. }
  1486. sprintf(command,"sox \"%s/../phsource/%s.wav\" -r %d -c1 -t wav %s\n",path_home,fname2,samplerate_native, fname_temp);
  1487. if(system(command) != 0)
  1488. {
  1489. failed = 1;
  1490. }
  1491. if(failed || (GetFileLength(fname_temp) <= 0))
  1492. {
  1493. if(resample_fails < 2)
  1494. error("Resample command failed: %s", command);
  1495. resample_fails++;
  1496. if(sr1 != samplerate_native)
  1497. {
  1498. sprintf(msg, "Can't resample (%d to %d): %s", sr1, samplerate_native, fname);
  1499. error("%s", msg);
  1500. }
  1501. else
  1502. {
  1503. error("WAV file is not mono: %s", fname);
  1504. }
  1505. remove(fname_temp);
  1506. return(0);
  1507. }
  1508. f = fopen(fname_temp,"rb");
  1509. if(f == NULL)
  1510. {
  1511. error("Can't read temp file: %s",fname_temp);
  1512. return(0);
  1513. }
  1514. if(f_report != NULL)
  1515. fprintf(f_report, "resampled %s\n", fname);
  1516. resample_count++;
  1517. resample_wav = 1;
  1518. fseek(f,40,SEEK_SET); // skip past the WAV header, up to before "data length"
  1519. }
  1520. displ = ftell(f_phdata);
  1521. // data contains: 4 bytes of length (n_samples * 2), followed by 2-byte samples (lsb byte first)
  1522. length = Read4Bytes(f);
  1523. while(!feof(f))
  1524. {
  1525. c1 = fgetc(f);
  1526. c3 = fgetc(f);
  1527. if(feof(f)) break;
  1528. c2 = c3 << 24;
  1529. c2 = c2 >> 16; // sign extend
  1530. sample = (c1 & 0xff) + c2;
  1531. if(sample > max)
  1532. max = sample;
  1533. else
  1534. if(sample < -max)
  1535. max = -sample;
  1536. }
  1537. scale_factor = (max / 127) + 1;
  1538. #define MIN_FACTOR -1 // was 6, disable use of 16 bit samples
  1539. if(scale_factor > MIN_FACTOR)
  1540. {
  1541. length = length/2 + (scale_factor << 16);
  1542. }
  1543. Write4Bytes(f_phdata,length);
  1544. fseek(f,44,SEEK_SET);
  1545. while(!feof(f))
  1546. {
  1547. c1 = fgetc(f);
  1548. c3 = fgetc(f);
  1549. c2 = c3 << 24;
  1550. c2 = c2 >> 16; // sign extend
  1551. sample = (c1 & 0xff) + c2;
  1552. if(feof(f)) break;
  1553. if(scale_factor <= MIN_FACTOR)
  1554. {
  1555. fputc(sample & 0xff,f_phdata);
  1556. fputc(sample >> 8,f_phdata);
  1557. }
  1558. else
  1559. {
  1560. x = ((float)sample / scale_factor) + 0.5;
  1561. sample2= (int)x;
  1562. if(sample2 > 127)
  1563. sample2 = 127;
  1564. if(sample2 < -128)
  1565. sample2 = -128;
  1566. fputc(sample2,f_phdata);
  1567. }
  1568. }
  1569. length = ftell(f_phdata);
  1570. while((length & 3) != 0)
  1571. {
  1572. // pad to a multiple of 4 bytes
  1573. fputc(0,f_phdata);
  1574. length++;
  1575. }
  1576. if(resample_wav != 0)
  1577. {
  1578. fclose(f);
  1579. remove(fname_temp);
  1580. }
  1581. return(displ | 0x800000); // set bit 23 to indicate a wave file rather than a spectrum
  1582. }
  1583. static int LoadEnvelope(FILE *f, const char *fname)
  1584. {
  1585. int displ;
  1586. char buf[128];
  1587. displ = ftell(f_phdata);
  1588. fseek(f,12,SEEK_SET);
  1589. if(fread(buf,128,1,f) == 0)
  1590. {
  1591. error("Failed to read envelope: %s",fname);
  1592. }
  1593. fwrite(buf,128,1,f_phdata);
  1594. if(n_envelopes < N_ENVELOPES)
  1595. {
  1596. strncpy0(envelope_paths[n_envelopes],fname,sizeof(envelope_paths[0]));
  1597. memcpy(envelope_dat[n_envelopes],buf,sizeof(envelope_dat[0]));
  1598. n_envelopes++;
  1599. }
  1600. return(displ);
  1601. }
  1602. /* Generate a hash code from the specified string */
  1603. static int Hash8(const char *string)
  1604. {
  1605. int c;
  1606. int chars=0;
  1607. int hash=0;
  1608. while((c = *string++) != 0)
  1609. {
  1610. c = tolower(c) - 'a';
  1611. hash = hash * 8 + c;
  1612. hash = (hash & 0x1ff) ^ (hash >> 8); /* exclusive or */
  1613. chars++;
  1614. }
  1615. return((hash+chars) & 0xff);
  1616. }
  1617. static int LoadEnvelope2(FILE *f, const char *fname)
  1618. {
  1619. int ix, ix2;
  1620. int n;
  1621. int x, y;
  1622. int displ;
  1623. int n_points;
  1624. double yy;
  1625. char line_buf[128];
  1626. float env_x[20];
  1627. float env_y[20];
  1628. int env_lin[20];
  1629. unsigned char env[ENV_LEN];
  1630. n_points = 0;
  1631. fgets(line_buf,sizeof(line_buf),f); // skip first line
  1632. while(!feof(f))
  1633. {
  1634. if(fgets(line_buf,sizeof(line_buf),f) == NULL)
  1635. break;
  1636. env_lin[n_points] = 0;
  1637. n = sscanf(line_buf,"%f %f %d",&env_x[n_points],&env_y[n_points],&env_lin[n_points]);
  1638. if(n >= 2)
  1639. {
  1640. env_x[n_points] *= (float)1.28; // convert range 0-100 to 0-128
  1641. n_points++;
  1642. }
  1643. }
  1644. env_x[n_points] = env_x[n_points-1];
  1645. env_y[n_points] = env_y[n_points-1];
  1646. ix = -1;
  1647. ix2 = 0;
  1648. for(x=0; x<ENV_LEN; x++)
  1649. {
  1650. if(x > env_x[ix+4])
  1651. ix++;
  1652. if(x >= env_x[ix2+1])
  1653. ix2++;
  1654. if(env_lin[ix2] > 0)
  1655. {
  1656. yy = env_y[ix2] + (env_y[ix2+1] - env_y[ix2]) * ((float)x - env_x[ix2]) / (env_x[ix2+1] - env_x[ix2]);
  1657. y = (int)(yy * 2.55);
  1658. }
  1659. else
  1660. if(n_points > 3)
  1661. y = (int)(polint(&env_x[ix],&env_y[ix],4,x) * 2.55); // convert to range 0-255
  1662. else
  1663. y = (int)(polint(&env_x[ix],&env_y[ix],3,x) * 2.55);
  1664. if(y < 0) y = 0;
  1665. if(y > 255) y = 255;
  1666. env[x] = y;
  1667. }
  1668. if(n_envelopes < N_ENVELOPES)
  1669. {
  1670. strncpy0(envelope_paths[n_envelopes],fname,sizeof(envelope_paths[0]));
  1671. memcpy(envelope_dat[n_envelopes],env,ENV_LEN);
  1672. n_envelopes++;
  1673. }
  1674. displ = ftell(f_phdata);
  1675. fwrite(env,1,128,f_phdata);
  1676. return(displ);
  1677. }
  1678. static int LoadDataFile(const char *path, int control)
  1679. {
  1680. // load spectrum sequence or sample data from a file.
  1681. // return index into spect or sample data area. bit 23=1 if a sample
  1682. FILE *f;
  1683. int id;
  1684. int hash;
  1685. int addr = 0;
  1686. int type_code=' ';
  1687. REF_HASH_TAB *p, *p2;
  1688. char buf[sizeof(path_home)+150];
  1689. if(strcmp(path,"NULL")==0)
  1690. return(0);
  1691. if(strcmp(path,"DFT")==0)
  1692. return(1);
  1693. count_references++;
  1694. hash = Hash8(path);
  1695. p = ref_hash_tab[hash];
  1696. while(p != NULL)
  1697. {
  1698. if(strcmp(path,p->string)==0)
  1699. {
  1700. duplicate_references++;
  1701. addr = p->value; // already loaded this data
  1702. break;
  1703. }
  1704. p = (REF_HASH_TAB *)p->link;
  1705. }
  1706. if(addr == 0)
  1707. {
  1708. sprintf(buf,"%s/../phsource/%s",path_home,path);
  1709. if((f = fopen(buf,"rb")) == NULL)
  1710. {
  1711. sprintf(buf,"%s/../phsource/%s.wav",path_home,path);
  1712. if((f = fopen(buf,"rb")) == NULL)
  1713. {
  1714. error("Can't read file: %s",path);
  1715. return(0);
  1716. }
  1717. }
  1718. id = Read4Bytes(f);
  1719. rewind(f);
  1720. if(id == 0x43455053)
  1721. {
  1722. addr = LoadSpect(path, control);
  1723. type_code = 'S';
  1724. }
  1725. else
  1726. if(id == 0x46464952)
  1727. {
  1728. addr = LoadWavefile(f,path);
  1729. type_code = 'W';
  1730. }
  1731. else
  1732. if(id == 0x43544950)
  1733. {
  1734. addr = LoadEnvelope(f,path);
  1735. type_code = 'E';
  1736. }
  1737. else
  1738. if(id == 0x45564E45)
  1739. {
  1740. addr = LoadEnvelope2(f,path);
  1741. type_code = 'E';
  1742. }
  1743. else
  1744. {
  1745. error("File not SPEC or RIFF: %s",path);
  1746. addr = -1;
  1747. }
  1748. fclose(f);
  1749. if(addr > 0)
  1750. {
  1751. fprintf(f_phcontents,"%c 0x%.5x %s\n",type_code,addr & 0x7fffff,path);
  1752. }
  1753. }
  1754. // add this item to the hash table
  1755. if(addr > 0)
  1756. {
  1757. p = ref_hash_tab[hash];
  1758. p2 = (REF_HASH_TAB *)malloc(sizeof(REF_HASH_TAB)+strlen(path)+1);
  1759. p2->value = addr;
  1760. p2->ph_mnemonic = phoneme_out->mnemonic; // phoneme which uses this file
  1761. p2->ph_table = n_phoneme_tabs-1;
  1762. strcpy(p2->string,path);
  1763. p2->link = (char *)p;
  1764. ref_hash_tab[hash] = p2;
  1765. }
  1766. return(addr);
  1767. }
  1768. static int CompileToneSpec(void)
  1769. {
  1770. int pitch1=0;
  1771. int pitch2=0;
  1772. int pitch_env = 0;
  1773. int amp_env = 0;
  1774. pitch1 = NextItemBrackets(tNUMBER,2);
  1775. pitch2 = NextItemBrackets(tNUMBER,3);
  1776. if(item_terminator == ',')
  1777. {
  1778. NextItemBrackets(tSTRING,3);
  1779. pitch_env = LoadDataFile(item_string, 0);
  1780. }
  1781. if(item_terminator == ',')
  1782. {
  1783. NextItemBrackets(tSTRING,1);
  1784. amp_env = LoadDataFile(item_string, 0);
  1785. }
  1786. if(pitch1 < pitch2)
  1787. {
  1788. phoneme_out->start_type = pitch1;
  1789. phoneme_out->end_type = pitch2;
  1790. }
  1791. else
  1792. {
  1793. phoneme_out->start_type = pitch2;
  1794. phoneme_out->end_type = pitch1;
  1795. }
  1796. if(pitch_env != 0)
  1797. {
  1798. *prog_out++ = i_PITCHENV + ((pitch_env >> 16) & 0xf);
  1799. *prog_out++ = pitch_env;
  1800. }
  1801. if(amp_env != 0)
  1802. {
  1803. *prog_out++ = i_AMPENV + ((amp_env >> 16) & 0xf);
  1804. *prog_out++ = amp_env;
  1805. }
  1806. return(0);
  1807. }
  1808. int CompileSound(int keyword, int isvowel)
  1809. {
  1810. int addr;
  1811. int value = 0;
  1812. char path[N_ITEM_STRING];
  1813. static int sound_instns[] = {i_FMT, i_WAV, i_VWLSTART, i_VWLENDING, i_WAVADD};
  1814. NextItemBrackets(tSTRING,2);
  1815. strcpy(path, item_string);
  1816. if(item_terminator == ',')
  1817. {
  1818. if((keyword == kVOWELSTART) || (keyword == kVOWELENDING))
  1819. {
  1820. value = NextItemBrackets(tSIGNEDNUMBER,1);
  1821. if(value > 127)
  1822. {
  1823. value = 127;
  1824. error("Parameter > 127",NULL);
  1825. }
  1826. if(value < -128)
  1827. {
  1828. value = -128;
  1829. error("Parameter < -128",NULL);
  1830. }
  1831. }
  1832. else
  1833. {
  1834. value = NextItemBrackets(tNUMBER,1);
  1835. if(value > 255)
  1836. {
  1837. value = 255;
  1838. error("Parameter > 255",NULL);
  1839. }
  1840. }
  1841. }
  1842. addr = LoadDataFile(path, isvowel);
  1843. addr = addr / 4; // addr is words not bytes
  1844. *prog_out++ = sound_instns[keyword-kFMT] + ((value & 0xff) << 4) + ((addr >> 16) & 0xf);
  1845. *prog_out++ = addr & 0xffff;
  1846. return(0);
  1847. }
  1848. /*
  1849. Condition
  1850. bits 14,15 1
  1851. bit 13 1 = AND, 0 = OR
  1852. bit 12 spare
  1853. bit 8-11
  1854. =0-3 p,t,n,n2 data=phoneme code
  1855. =4-7 p,t,n,n2 data=(bits5-7: phtype, place, property, special) (bits0-4: data)
  1856. =8 data = stress bitmap
  1857. =9 special tests
  1858. */
  1859. int CompileIf(int elif)
  1860. {
  1861. int key;
  1862. int finish = 0;
  1863. int word = 0;
  1864. int word2;
  1865. int data;
  1866. int bitmap;
  1867. int brackets;
  1868. int not_flag;
  1869. USHORT *prog_last_if = NULL;
  1870. then_count = 2;
  1871. after_if = 1;
  1872. while(!finish)
  1873. {
  1874. not_flag = 0;
  1875. word2 = 0;
  1876. if(prog_out >= prog_out_max)
  1877. {
  1878. error("Phoneme program too large", NULL);
  1879. return(0);
  1880. }
  1881. if((key = NextItem(tCONDITION)) < 0)
  1882. error("Expected a condition, not '%s'",item_string);
  1883. if((item_type == 0) && (key == k_NOT))
  1884. {
  1885. not_flag = 1;
  1886. if((key = NextItem(tCONDITION)) < 0)
  1887. error("Expected a condition, not '%s'",item_string);
  1888. }
  1889. if(item_type == tWHICH_PHONEME)
  1890. {
  1891. // prevPh(), thisPh(), nextPh(), next2Ph() etc
  1892. if(key >= 6)
  1893. {
  1894. // put the 'which' code in the next instruction
  1895. word2 = key;
  1896. key = 6;
  1897. }
  1898. key = key << 8;
  1899. data = NextItemBrackets(tPROPERTIES,0);
  1900. if(data >= 0)
  1901. {
  1902. word = key + data + 0x700;
  1903. }
  1904. else
  1905. {
  1906. data = LookupPhoneme(item_string,2);
  1907. word = key + data;
  1908. }
  1909. }
  1910. else
  1911. if(item_type == tTEST)
  1912. {
  1913. if(key == kTHISSTRESS)
  1914. {
  1915. bitmap = 0;
  1916. brackets = 2;
  1917. do {
  1918. data = NextItemBrackets(tNUMBER,brackets);
  1919. if(data > 7)
  1920. error("Expected list of stress levels",NULL);
  1921. bitmap |= (1 << data);
  1922. brackets = 3;
  1923. } while(item_terminator == ',');
  1924. word = i_StressLevel | bitmap;
  1925. }
  1926. else
  1927. {
  1928. word = key;
  1929. }
  1930. }
  1931. else
  1932. {
  1933. error("Unexpected keyword '%s'",item_string);
  1934. if((strcmp(item_string, "phoneme") == 0) || (strcmp(item_string, "endphoneme") == 0))
  1935. return(-1);
  1936. }
  1937. // output the word
  1938. prog_last_if = prog_out;
  1939. *prog_out++ = word | i_CONDITION;
  1940. if(word2 != 0)
  1941. *prog_out++ = word2;
  1942. if(not_flag)
  1943. *prog_out++ = i_NOT;
  1944. // expect AND, OR, THEN
  1945. switch(NextItem(tCONDITION))
  1946. {
  1947. case k_AND:
  1948. break;
  1949. case k_OR:
  1950. if(prog_last_if != NULL)
  1951. *prog_last_if |= i_OR;
  1952. break;
  1953. case k_THEN:
  1954. finish = 1;
  1955. break;
  1956. default:
  1957. error("Expected AND, OR, THEN",NULL);
  1958. break;
  1959. }
  1960. }
  1961. if(finish != 1)
  1962. {
  1963. }
  1964. if(elif == 0)
  1965. {
  1966. if_level++;
  1967. if_stack[if_level].p_else = NULL;
  1968. }
  1969. if_stack[if_level].returned = 0;
  1970. if_stack[if_level].p_then = prog_out;
  1971. *prog_out++ = i_JUMP_FALSE;
  1972. return(0);
  1973. }
  1974. void FillThen(int add)
  1975. {
  1976. USHORT *p;
  1977. int offset;
  1978. p = if_stack[if_level].p_then;
  1979. if(p != NULL)
  1980. {
  1981. offset = prog_out - p + add;
  1982. if((then_count == 1) && (if_level == 1))
  1983. {
  1984. // The THEN part only contains one statement, we can remove the THEN jump
  1985. // and the interpreter will implicitly skip the statement.
  1986. while(p < prog_out)
  1987. {
  1988. p[0] = p[1];
  1989. p++;
  1990. }
  1991. prog_out--;
  1992. }
  1993. else
  1994. {
  1995. if(offset > MAX_JUMP)
  1996. {
  1997. error("IF block is too long",NULL);
  1998. }
  1999. *p = i_JUMP_FALSE + offset;
  2000. }
  2001. if_stack[if_level].p_then = NULL;
  2002. }
  2003. then_count = 0;
  2004. }
  2005. int CompileElse(void)
  2006. {
  2007. USHORT *ref;
  2008. USHORT *p;
  2009. if(if_level < 1)
  2010. {
  2011. error("ELSE not expected",NULL);
  2012. return(0);
  2013. }
  2014. if(if_stack[if_level].returned == 0)
  2015. {
  2016. FillThen(1);
  2017. }
  2018. else
  2019. {
  2020. FillThen(0);
  2021. }
  2022. if(if_stack[if_level].returned == 0)
  2023. {
  2024. ref = prog_out;
  2025. *prog_out++ = 0;
  2026. if((p = if_stack[if_level].p_else) != NULL)
  2027. {
  2028. *ref = ref - p; // backwards offset to the previous else
  2029. }
  2030. if_stack[if_level].p_else = ref;
  2031. }
  2032. return(0);
  2033. }
  2034. int CompileElif(void)
  2035. {
  2036. if(if_level < 1)
  2037. {
  2038. error("ELIF not expected",NULL);
  2039. return(0);
  2040. }
  2041. CompileElse();
  2042. CompileIf(1);
  2043. return(0);
  2044. }
  2045. int CompileEndif(void)
  2046. {
  2047. USHORT *p;
  2048. int chain;
  2049. int offset;
  2050. if(if_level < 1)
  2051. {
  2052. error("ENDIF not expected",NULL);
  2053. return(0);
  2054. }
  2055. FillThen(0);
  2056. if((p = if_stack[if_level].p_else) != NULL)
  2057. {
  2058. do
  2059. {
  2060. chain = *p; // a chain of previous else links
  2061. offset = prog_out - p;
  2062. if(offset > MAX_JUMP)
  2063. {
  2064. error("IF block is too long",NULL);
  2065. }
  2066. *p = i_JUMP + offset;
  2067. p -= chain;
  2068. } while (chain > 0);
  2069. }
  2070. if_level--;
  2071. return(0);
  2072. }
  2073. static int CompileSwitch(int type)
  2074. {
  2075. // Type 0: EndSwitch
  2076. // 1: SwitchPrevVowelType
  2077. // 2: SwitchNextVowelType
  2078. if(type == 0)
  2079. {
  2080. // check the instructions in the Switch
  2081. return(0);
  2082. }
  2083. if(type == 1)
  2084. *prog_out++ = i_SWITCH_PREVVOWEL+6;
  2085. if(type == 2)
  2086. *prog_out++ = i_SWITCH_NEXTVOWEL+6;
  2087. return(0);
  2088. }
  2089. static PHONEME_TAB_LIST *FindPhonemeTable(const char *string)
  2090. {
  2091. int ix;
  2092. for(ix=0; ix<n_phoneme_tabs; ix++)
  2093. {
  2094. if(strcmp(phoneme_tab_list2[ix].name, string) == 0)
  2095. {
  2096. return(&phoneme_tab_list2[ix]);
  2097. }
  2098. }
  2099. error("Unknown phoneme table: '%s'",string);
  2100. return(NULL);
  2101. }
  2102. static PHONEME_TAB *FindPhoneme(const char *string)
  2103. {
  2104. PHONEME_TAB_LIST *phtab = NULL;
  2105. int ix;
  2106. unsigned int mnem;
  2107. char *phname;
  2108. char buf[200];
  2109. // is this the name of a phoneme which is in scope
  2110. if((strlen(string) <= 4) && ((ix = LookupPhoneme(string,0)) != -1))
  2111. {
  2112. return(&phoneme_tab2[ix]);
  2113. }
  2114. // no, treat the name as phonemetable/phoneme
  2115. strcpy(buf,string);
  2116. if((phname = strchr(buf,'/')) != 0)
  2117. {
  2118. *phname++ = 0;
  2119. }
  2120. phtab = FindPhonemeTable(buf);
  2121. if(phtab == NULL)
  2122. {
  2123. return(NULL); // phoneme table not found
  2124. }
  2125. mnem = StringToWord(phname);
  2126. for(ix=1; ix<256; ix++)
  2127. {
  2128. if(mnem == phtab->phoneme_tab_ptr[ix].mnemonic)
  2129. {
  2130. return(&phtab->phoneme_tab_ptr[ix]);
  2131. }
  2132. }
  2133. error("Phoneme reference not found: '%s'",string);
  2134. return(NULL);
  2135. }
  2136. static void ImportPhoneme(void)
  2137. {
  2138. unsigned int ph_mnem;
  2139. unsigned int ph_code;
  2140. PHONEME_TAB *ph;
  2141. NextItem(tSTRING);
  2142. if((ph = FindPhoneme(item_string)) == NULL)
  2143. return;
  2144. ph_mnem = phoneme_out->mnemonic;
  2145. ph_code = phoneme_out->code;
  2146. memcpy(phoneme_out,ph,sizeof(PHONEME_TAB));
  2147. phoneme_out->mnemonic = ph_mnem;
  2148. phoneme_out->code = ph_code;
  2149. if(phoneme_out->type != phVOWEL)
  2150. {
  2151. phoneme_out->end_type = 0; // voicingswitch, this must be set later to refer to a local phoneme
  2152. }
  2153. }
  2154. static void CallPhoneme(void)
  2155. {
  2156. PHONEME_TAB *ph;
  2157. int ix;
  2158. int addr = 0;
  2159. NextItem(tSTRING);
  2160. // first look for a procedure name
  2161. for(ix=0; ix<n_procs; ix++)
  2162. {
  2163. if(strcmp(proc_names[ix],item_string) == 0)
  2164. {
  2165. addr = proc_addr[ix];
  2166. break;
  2167. }
  2168. }
  2169. if(ix == n_procs)
  2170. {
  2171. // procedure not found, try a phoneme name
  2172. if((ph = FindPhoneme(item_string)) == NULL)
  2173. return;
  2174. addr = ph->program;
  2175. if(phoneme_out->type == phINVALID)
  2176. {
  2177. // Phoneme type has not been set. Copy it from the called phoneme
  2178. phoneme_out->type = ph->type;
  2179. phoneme_flags = ph->phflags & ~phARTICULATION;
  2180. place_articulation = (ph->phflags & phARTICULATION) >> 16;
  2181. phoneme_out->start_type = ph->start_type;
  2182. phoneme_out->end_type = ph->end_type;
  2183. phoneme_out->std_length = ph->std_length;
  2184. phoneme_out->length_mod = ph->length_mod;
  2185. }
  2186. }
  2187. *prog_out++ = i_CALLPH + (addr >> 16);
  2188. *prog_out++ = addr;
  2189. }
  2190. static void DecThenCount()
  2191. {
  2192. if(then_count >0)
  2193. then_count--;
  2194. }
  2195. static void InstnPlusPhoneme(int instn)
  2196. {
  2197. int phcode;
  2198. phcode = NextItemBrackets(tPHONEMEMNEM,0);
  2199. *prog_out++ = instn + phcode;
  2200. }
  2201. int CompilePhoneme(int compile_phoneme)
  2202. {
  2203. int endphoneme = 0;
  2204. int keyword;
  2205. int value;
  2206. int phcode = 0;
  2207. int flags;
  2208. int ix;
  2209. int start;
  2210. int count;
  2211. int c;
  2212. char *p;
  2213. int vowel_length_factor = 100; // for testing
  2214. char number_buf[12];
  2215. char ipa_buf[N_ITEM_STRING+1];
  2216. PHONEME_TAB phoneme_out2;
  2217. PHONEME_PROG_LOG phoneme_prog_log;
  2218. prog_out = prog_buf;
  2219. prog_out_max = &prog_buf[MAX_PROG_BUF-1];
  2220. if_level = 0;
  2221. if_stack[0].returned = 0;
  2222. after_if = 0;
  2223. phoneme_flags = 0;
  2224. place_articulation = 0;
  2225. NextItem(tSTRING);
  2226. if(compile_phoneme)
  2227. {
  2228. phcode = LookupPhoneme(item_string,1); // declare phoneme if not already there
  2229. if(phcode == -1) return(0);
  2230. phoneme_out = &phoneme_tab2[phcode];
  2231. }
  2232. else
  2233. {
  2234. // declare a procedure
  2235. if(n_procs >= N_PROCS)
  2236. {
  2237. error("Too many procedures",NULL);
  2238. return(0);
  2239. }
  2240. strcpy(proc_names[n_procs], item_string);
  2241. phoneme_out = &phoneme_out2;
  2242. sprintf(number_buf,"%.3dP", n_procs);
  2243. phoneme_out->mnemonic = StringToWord(number_buf);
  2244. }
  2245. phoneme_out->code = phcode;
  2246. phoneme_out->program = 0;
  2247. phoneme_out->type = phINVALID;
  2248. phoneme_out->std_length = 0;
  2249. phoneme_out->start_type = 0;
  2250. phoneme_out->end_type = 0;
  2251. phoneme_out->length_mod = 0;
  2252. while(!endphoneme && !feof(f_in))
  2253. {
  2254. if((keyword = NextItem(tKEYWORD)) < 0)
  2255. {
  2256. if(keyword == -2)
  2257. {
  2258. error("Missing 'endphoneme' before end-of-file",NULL); // end of file
  2259. break;
  2260. }
  2261. error("Bad keyword in phoneme definition '%s'",item_string);
  2262. continue;
  2263. }
  2264. switch(item_type)
  2265. {
  2266. case tPHONEME_TYPE:
  2267. if(phoneme_out->type != phINVALID)
  2268. error("More than one phoneme type: %s",item_string);
  2269. phoneme_out->type = keyword;
  2270. break;
  2271. case tPLACE:
  2272. if(place_articulation > 0)
  2273. error("Place of articulation has already been given: %s",item_string);
  2274. place_articulation = keyword;
  2275. break;
  2276. case tPHONEME_FLAG:
  2277. phoneme_flags |= keyword;
  2278. break;
  2279. case tINSTRN1:
  2280. // instruction group 0, with 8 bit operands which set data in PHONEME_DATA
  2281. switch(keyword)
  2282. {
  2283. case i_CHANGE_PHONEME:
  2284. case i_APPEND_PHONEME:
  2285. case i_APPEND_IFNEXTVOWEL:
  2286. case i_INSERT_PHONEME:
  2287. case i_REPLACE_NEXT_PHONEME:
  2288. case i_VOICING_SWITCH:
  2289. case i_CHANGE_IF+0:
  2290. case i_CHANGE_IF+1:
  2291. case i_CHANGE_IF+2:
  2292. case i_CHANGE_IF+3:
  2293. InstnPlusPhoneme(keyword << 8);
  2294. DecThenCount();
  2295. break;
  2296. case i_PAUSE_BEFORE:
  2297. value = NextItemMax(255);
  2298. *prog_out++ = (i_PAUSE_BEFORE << 8) + value;
  2299. DecThenCount();
  2300. break;
  2301. case i_PAUSE_AFTER:
  2302. value = NextItemMax(255);
  2303. *prog_out++ = (i_PAUSE_AFTER << 8) + value;
  2304. DecThenCount();
  2305. break;
  2306. case i_SET_LENGTH:
  2307. value = NextItemMax(511);
  2308. if(phoneme_out->type == phVOWEL)
  2309. {
  2310. value = (value * vowel_length_factor)/100;
  2311. }
  2312. if(after_if == 0)
  2313. {
  2314. phoneme_out->std_length = value/2;
  2315. }
  2316. else
  2317. {
  2318. *prog_out++ = (i_SET_LENGTH << 8) + value/2;
  2319. DecThenCount();
  2320. }
  2321. break;
  2322. case i_ADD_LENGTH:
  2323. value = NextItem(tSIGNEDNUMBER) / 2;
  2324. *prog_out++ = (i_ADD_LENGTH << 8) + (value & 0xff);
  2325. DecThenCount();
  2326. break;
  2327. case i_LENGTH_MOD:
  2328. value = NextItem(tNUMBER);
  2329. phoneme_out->length_mod = value;
  2330. break;
  2331. case i_IPA_NAME:
  2332. NextItem(tSTRING);
  2333. if(strcmp(item_string,"NULL")==0)
  2334. strcpy(item_string," ");
  2335. // copy the string, recognize characters in the form U+9999
  2336. flags = 0;
  2337. count = 0;
  2338. ix = 1;
  2339. for(p=item_string; *p != 0; )
  2340. {
  2341. p += utf8_in(&c, p);
  2342. if((c == '|') && (count > 0))
  2343. {
  2344. // '|' means don't allow a tie or joiner before this letter
  2345. flags |= (1 << (count -1));
  2346. }
  2347. else
  2348. if((c=='U') && (p[0]=='+'))
  2349. {
  2350. int j;
  2351. // U+9999
  2352. p++;
  2353. memcpy(number_buf,p,4); // U+ should be followed by 4 hex digits
  2354. number_buf[4] = 0;
  2355. c = '#';
  2356. sscanf(number_buf,"%x",&c);
  2357. // move past the 4 hexdecimal digits
  2358. for(j=0; j<4; j++)
  2359. {
  2360. if(!isalnum(*p))
  2361. break;
  2362. p++;
  2363. }
  2364. ix += utf8_out(c, &ipa_buf[ix]);
  2365. count++;
  2366. }
  2367. else
  2368. {
  2369. ix += utf8_out(c, &ipa_buf[ix]);
  2370. count++;
  2371. }
  2372. }
  2373. ipa_buf[0] = flags;
  2374. ipa_buf[ix] = 0;
  2375. start = 1;
  2376. if(flags != 0)
  2377. start = 0; // only include the flags byte if bits are set
  2378. value = strlen(&ipa_buf[start]); // number of UTF-8 bytes
  2379. *prog_out++ = (i_IPA_NAME << 8) + value;
  2380. for(ix=0; ix < value; ix += 2)
  2381. {
  2382. *prog_out++ = (ipa_buf[ix+start] << 8) + (ipa_buf[ix+start+1] & 0xff);
  2383. }
  2384. DecThenCount();
  2385. break;
  2386. }
  2387. break;
  2388. case tSTATEMENT:
  2389. switch(keyword)
  2390. {
  2391. case kIMPORT_PH:
  2392. ImportPhoneme();
  2393. phoneme_flags = phoneme_out->phflags;
  2394. break;
  2395. case kSTARTTYPE:
  2396. phcode = NextItem(tPHONEMEMNEM);
  2397. if(phcode == -1)
  2398. phcode = LookupPhoneme(item_string,1);
  2399. phoneme_out->start_type = phcode;
  2400. break;
  2401. case kENDTYPE:
  2402. phcode = NextItem(tPHONEMEMNEM);
  2403. if(phcode == -1)
  2404. phcode = LookupPhoneme(item_string,1);
  2405. if(phoneme_out->type == phVOWEL)
  2406. {
  2407. phoneme_out->end_type = phcode;
  2408. }
  2409. else
  2410. {
  2411. if(phcode != phoneme_out->start_type)
  2412. {
  2413. error("endtype must equal starttype for consonants",NULL);
  2414. }
  2415. }
  2416. break;
  2417. case kVOICINGSWITCH:
  2418. phcode = NextItem(tPHONEMEMNEM);
  2419. if(phcode == -1)
  2420. phcode = LookupPhoneme(item_string,1);
  2421. phoneme_out->end_type = phcode; // use end_type field for consonants as voicing_switch
  2422. break;
  2423. case kSTRESSTYPE:
  2424. value = NextItem(tNUMBER);
  2425. phoneme_out->std_length = value;
  2426. if(prog_out > prog_buf)
  2427. {
  2428. error("stress phonemes can't contain program instructions",NULL);
  2429. prog_out = prog_buf;
  2430. }
  2431. break;
  2432. case kIF:
  2433. endphoneme = CompileIf(0);
  2434. break;
  2435. case kELSE:
  2436. endphoneme = CompileElse();
  2437. break;
  2438. case kELIF:
  2439. endphoneme = CompileElif();
  2440. break;
  2441. case kENDIF:
  2442. endphoneme = CompileEndif();
  2443. break;
  2444. case kENDSWITCH:
  2445. break;
  2446. case kSWITCH_PREVVOWEL:
  2447. endphoneme = CompileSwitch(1);
  2448. break;
  2449. case kSWITCH_NEXTVOWEL:
  2450. endphoneme = CompileSwitch(2);
  2451. break;
  2452. case kCALLPH:
  2453. CallPhoneme();
  2454. DecThenCount();
  2455. break;
  2456. case kFMT:
  2457. if_stack[if_level].returned = 1;
  2458. DecThenCount();
  2459. if(phoneme_out->type == phVOWEL)
  2460. endphoneme = CompileSound(keyword,1);
  2461. else
  2462. endphoneme = CompileSound(keyword,0);
  2463. break;
  2464. case kWAV:
  2465. if_stack[if_level].returned = 1;
  2466. case kVOWELSTART:
  2467. case kVOWELENDING:
  2468. case kANDWAV:
  2469. DecThenCount();
  2470. endphoneme = CompileSound(keyword,0);
  2471. break;
  2472. case kVOWELIN:
  2473. DecThenCount();
  2474. endphoneme = CompileVowelTransition(1);
  2475. break;
  2476. case kVOWELOUT:
  2477. DecThenCount();
  2478. endphoneme = CompileVowelTransition(2);
  2479. break;
  2480. case kTONESPEC:
  2481. DecThenCount();
  2482. endphoneme = CompileToneSpec();
  2483. break;
  2484. case kCONTINUE:
  2485. *prog_out++ = i_CONTINUE;
  2486. DecThenCount();
  2487. break;
  2488. case kRETURN:
  2489. *prog_out++ = i_RETURN;
  2490. DecThenCount();
  2491. break;
  2492. case kINCLUDE:
  2493. case kPHONEMENUMBER:
  2494. case kPHONEMETABLE:
  2495. error("Missing 'endphoneme' before '%s'",item_string); // drop through to endphoneme
  2496. case kENDPHONEME:
  2497. case kENDPROCEDURE:
  2498. endphoneme = 1;
  2499. if(if_level > 0)
  2500. {
  2501. error("Missing ENDIF",NULL);
  2502. }
  2503. if((prog_out > prog_buf) && (if_stack[0].returned == 0))
  2504. {
  2505. *prog_out++ = i_RETURN;
  2506. }
  2507. break;
  2508. }
  2509. break;
  2510. }
  2511. }
  2512. if(endphoneme != 1)
  2513. {
  2514. error("'endphoneme' not expected here",NULL);
  2515. }
  2516. if(compile_phoneme)
  2517. {
  2518. if(phoneme_out->type == phINVALID)
  2519. {
  2520. error("Phoneme type is missing",NULL);
  2521. phoneme_out->type = 0;
  2522. }
  2523. phoneme_out->phflags = place_articulation << 16;
  2524. phoneme_out->phflags |= phoneme_flags;
  2525. if(phoneme_out->phflags & phVOICED)
  2526. {
  2527. if(phoneme_out->type == phSTOP)
  2528. phoneme_out->type = phVSTOP;
  2529. else
  2530. if(phoneme_out->type == phFRICATIVE)
  2531. phoneme_out->type = phVFRICATIVE;
  2532. }
  2533. if(phoneme_out->std_length == 0)
  2534. {
  2535. if(phoneme_out->type == phVOWEL)
  2536. phoneme_out->std_length = 180/2; // default length for vowel
  2537. }
  2538. phoneme_out->phflags |= phLOCAL; //declared in this phoneme table
  2539. if(phoneme_out->type == phDELETED)
  2540. {
  2541. phoneme_out->mnemonic = 0x01; // will not be recognised
  2542. }
  2543. }
  2544. DecompilePhoneme(f_errors, phoneme_out, compile_phoneme);
  2545. if(prog_out > prog_buf)
  2546. {
  2547. // write out the program for this phoneme
  2548. fflush(f_phindex);
  2549. phoneme_out->program = ftell(f_phindex) / sizeof(USHORT);
  2550. if(f_prog_log != NULL)
  2551. {
  2552. phoneme_prog_log.addr = phoneme_out->program;
  2553. phoneme_prog_log.length = prog_out - prog_buf;
  2554. fwrite(&phoneme_prog_log, 1, sizeof(phoneme_prog_log), f_prog_log);
  2555. }
  2556. if(compile_phoneme == 0)
  2557. {
  2558. proc_addr[n_procs++] = ftell(f_phindex) / sizeof(USHORT);
  2559. }
  2560. fwrite(prog_buf, sizeof(USHORT), prog_out - prog_buf, f_phindex);
  2561. }
  2562. return(0);
  2563. }
  2564. static void WritePhonemeTables()
  2565. {
  2566. int ix;
  2567. int j;
  2568. int n;
  2569. int value;
  2570. int count;
  2571. PHONEME_TAB *p;
  2572. value = n_phoneme_tabs;
  2573. fputc(value,f_phtab);
  2574. fputc(0,f_phtab);
  2575. fputc(0,f_phtab);
  2576. fputc(0,f_phtab);
  2577. for(ix=0; ix<n_phoneme_tabs; ix++)
  2578. {
  2579. p = phoneme_tab_list2[ix].phoneme_tab_ptr;
  2580. n = n_phcodes_list[ix];
  2581. p[n].mnemonic = 0; // terminate the phoneme table
  2582. // count number of locally declared phonemes
  2583. count=0;
  2584. for(j=0; j<n; j++)
  2585. {
  2586. if(ix==0)
  2587. p[j].phflags |= phLOCAL; // write all phonemes in the base phoneme table
  2588. if(p[j].phflags & phLOCAL)
  2589. count++;
  2590. }
  2591. phoneme_tab_list2[ix].n_phonemes = count+1;
  2592. fputc(count+1,f_phtab);
  2593. fputc(phoneme_tab_list2[ix].includes,f_phtab);
  2594. fputc(0,f_phtab);
  2595. fputc(0,f_phtab);
  2596. Write4Bytes(f_phtab, phoneme_tab_list2[ix].equivalence_tables); // byte index into phondata for equivalence tables
  2597. fwrite(phoneme_tab_list2[ix].name,1,N_PHONEME_TAB_NAME,f_phtab);
  2598. for(j=0; j<n; j++)
  2599. {
  2600. if(p[j].phflags & phLOCAL)
  2601. {
  2602. // this bit is set temporarily to incidate a local phoneme, declared in
  2603. // in the current phoneme file
  2604. p[j].phflags &= ~phLOCAL;
  2605. fwrite(&p[j],sizeof(PHONEME_TAB),1,f_phtab);
  2606. }
  2607. }
  2608. fwrite(&p[n],sizeof(PHONEME_TAB),1,f_phtab); // include the extra list-terminator phoneme entry
  2609. free(p);
  2610. }
  2611. }
  2612. static void EndPhonemeTable()
  2613. {
  2614. int ix;
  2615. int *pw;
  2616. int length;
  2617. if(n_phoneme_tabs == 0)
  2618. return;
  2619. fprintf(f_errors,"\n");
  2620. // check that all referenced phonemes have been declared
  2621. for(ix=0; ix<n_phcodes; ix++)
  2622. {
  2623. if(phoneme_tab2[ix].type == phINVALID)
  2624. {
  2625. fprintf(f_errors,"%3d: Phoneme [%s] not declared, referenced at line %d\n",linenum,
  2626. WordToString(phoneme_tab2[ix].mnemonic),(int)(phoneme_tab2[ix].program));
  2627. error_count++;
  2628. phoneme_tab2[ix].type = 0; // prevent the error message repeating
  2629. }
  2630. }
  2631. n_phcodes_list[n_phoneme_tabs-1] = n_phcodes;
  2632. if((length = p_equivalence - equivalence_buf) > 0)
  2633. {
  2634. // terminate the list of phoneme equivalence tables
  2635. pw = (int *)p_equivalence;
  2636. pw[0] = 0;
  2637. // write the equivalence data into phondata, and remember it's address
  2638. ix = ftell(f_phdata);
  2639. fprintf(f_phcontents,"Q 0x%.5x %s\n", ix, phoneme_tab_list2[n_phoneme_tabs-1].name);
  2640. phoneme_tab_list2[n_phoneme_tabs-1].equivalence_tables = ix;
  2641. fwrite(equivalence_buf, length+4, 1, f_phdata);
  2642. }
  2643. }
  2644. static void StartPhonemeTable(const char *name)
  2645. {
  2646. int ix;
  2647. int j;
  2648. PHONEME_TAB *p;
  2649. fprintf(f_errors,"______________________________\nPhoneme Table: '%s'\n",name);
  2650. if(n_phoneme_tabs >= N_PHONEME_TABS-1)
  2651. {
  2652. Error("Too many phonemetables");
  2653. return;
  2654. }
  2655. p = (PHONEME_TAB *)calloc(sizeof(PHONEME_TAB),N_PHONEME_TAB);
  2656. if(p == NULL)
  2657. {
  2658. Error("Out of memory");
  2659. return;
  2660. }
  2661. memset(&phoneme_tab_list2[n_phoneme_tabs], 0, sizeof(PHONEME_TAB_LIST));
  2662. phoneme_tab_list2[n_phoneme_tabs].phoneme_tab_ptr = phoneme_tab2 = p;
  2663. strncpy0(phoneme_tab_list2[n_phoneme_tabs].name, name, N_PHONEME_TAB_NAME);
  2664. n_phcodes = 1;
  2665. phoneme_tab_list2[n_phoneme_tabs].includes = 0;
  2666. p_equivalence = equivalence_buf;
  2667. if(n_phoneme_tabs > 0)
  2668. {
  2669. NextItem(tSTRING); // name of base phoneme table
  2670. for(ix=0; ix<n_phoneme_tabs; ix++)
  2671. {
  2672. if(strcmp(item_string,phoneme_tab_list2[ix].name)==0)
  2673. {
  2674. phoneme_tab_list2[n_phoneme_tabs].includes = ix+1;
  2675. // initialise the new phoneme table with the contents of this one
  2676. memcpy(phoneme_tab2,phoneme_tab_list2[ix].phoneme_tab_ptr,sizeof(PHONEME_TAB)*N_PHONEME_TAB);
  2677. n_phcodes = n_phcodes_list[ix];
  2678. // clear "local phoneme" bit"
  2679. for(j=0; j<n_phcodes; j++)
  2680. phoneme_tab2[j].phflags &= ~phLOCAL;
  2681. break;
  2682. }
  2683. }
  2684. if(ix == n_phoneme_tabs)
  2685. {
  2686. error("Can't find base phonemetable '%s'",item_string);
  2687. }
  2688. }
  2689. else
  2690. {
  2691. ReservePhCodes();
  2692. }
  2693. n_phoneme_tabs++;
  2694. }
  2695. static void CompileEquivalents()
  2696. {
  2697. // a list of phonemes in another language and the equivalent phoneme strings in this language
  2698. int ix;
  2699. int n_names;
  2700. int n_bytes;
  2701. int foreign_phoneme;
  2702. int foreign_error = 0;
  2703. int remove_stress = 0;
  2704. char *p_start;
  2705. char *p;
  2706. int foreign_table;
  2707. char foreign_table_name[40];
  2708. char line_buf[80];
  2709. char names[6][80];
  2710. char phcode[7];
  2711. NextItem(tSTRING);
  2712. strcpy(foreign_table_name, item_string);
  2713. if((foreign_table = SelectPhonemeTableName(foreign_table_name)) < 0)
  2714. {
  2715. if(strcmp(foreign_table_name, "NULL") != 0)
  2716. {
  2717. error("Unknown phoneme table '%s'", foreign_table_name);
  2718. }
  2719. foreign_error = 1;
  2720. foreign_phoneme = 0;
  2721. }
  2722. p_start = p_equivalence;
  2723. p_equivalence += 8;
  2724. p_start[0] = foreign_table;
  2725. linenum--;
  2726. while(!feof(f_in))
  2727. {
  2728. linenum++;
  2729. if(fgets(line_buf, sizeof(line_buf), f_in) == NULL)
  2730. break;
  2731. if((p = strstr(line_buf,"//")) != NULL)
  2732. *p = 0;
  2733. for(ix=0; ix<6; ix++)
  2734. names[ix][0] = 0;
  2735. n_names = sscanf(line_buf,"%s %s %s %s %s %s",names[0],names[1],names[2],names[3],names[4],names[5]);
  2736. if(n_names < 1)
  2737. continue;
  2738. if(strcmp(names[0],"endphoneme") == 0)
  2739. break;
  2740. if(foreign_error)
  2741. continue;
  2742. if(strcmp(names[0],"remove_stress") == 0)
  2743. {
  2744. remove_stress = 1;
  2745. continue;
  2746. }
  2747. if(p_equivalence > &equivalence_buf[sizeof(equivalence_buf) - 16])
  2748. {
  2749. error("'equivalents' tables are too large",NULL);
  2750. break;
  2751. }
  2752. if(foreign_error == 0)
  2753. {
  2754. phcode[0] = foreign_phoneme = LookupPhonemeString(names[0]);
  2755. if(foreign_phoneme == 0)
  2756. {
  2757. sprintf(line_buf,"%s/%s", foreign_table_name, names[0]);
  2758. error("Unknown phoneme '%s'", line_buf);
  2759. }
  2760. }
  2761. for(ix=1; ix<n_names; ix++)
  2762. {
  2763. phcode[ix] = LookupPhoneme(names[ix], 1);
  2764. }
  2765. // only write a translation if it has an effect
  2766. if((n_names > 2) || (phcode[0] != phcode[1]))
  2767. {
  2768. // write: foreign phoneme number, then a string of local phoneme numbers
  2769. memcpy(p_equivalence, phcode, n_names);
  2770. p_equivalence += n_names;
  2771. *p_equivalence++ = 0;
  2772. }
  2773. }
  2774. *p_equivalence++ = 0;
  2775. p_equivalence = (char *)((long int)(p_equivalence + 3) & ~0x3); // align to word boundary
  2776. n_bytes = p_equivalence - p_start;
  2777. p_start[1] = remove_stress;
  2778. n_bytes = n_bytes / 4;
  2779. p_start[2] = n_bytes >> 8; // index of next table
  2780. p_start[3] = n_bytes;
  2781. }
  2782. static void CompilePhonemeFiles()
  2783. {
  2784. int item;
  2785. FILE *f;
  2786. char buf[sizeof(path_home)+120];
  2787. linenum = 1;
  2788. count_references = 0;
  2789. duplicate_references = 0;
  2790. count_frames = 0;
  2791. n_procs = 0;
  2792. for(;; )
  2793. {
  2794. if(feof(f_in))
  2795. {
  2796. // end of file, go back to previous from, from which this was included
  2797. if(stack_ix == 0)
  2798. break; // end of top level, finished
  2799. fclose(f_in);
  2800. f_in = stack[--stack_ix].file;
  2801. strcpy(current_fname,stack[stack_ix].fname);
  2802. linenum = stack[stack_ix].linenum;
  2803. fprintf(f_errors,"\n\n");
  2804. }
  2805. item = NextItem(tKEYWORD);
  2806. switch(item)
  2807. {
  2808. case kUTF8_BOM:
  2809. break; // ignore bytes 0xef 0xbb 0xbf
  2810. case kINCLUDE:
  2811. NextItem(tSTRING);
  2812. sprintf(buf,"%s/../phsource/%s",path_home,item_string);
  2813. if((stack_ix < N_STACK) && (f = fopen_log(f_errors,buf,"rb")) != NULL)
  2814. {
  2815. fprintf(f_errors,"include %s\n",item_string);
  2816. stack[stack_ix].linenum = linenum;
  2817. strcpy(stack[stack_ix].fname,current_fname);
  2818. stack[stack_ix++].file = f_in;
  2819. f_in = f;
  2820. strncpy0(current_fname,item_string,sizeof(current_fname));
  2821. linenum = 1;
  2822. }
  2823. else
  2824. {
  2825. error("Missing file: %s", item_string);
  2826. }
  2827. break;
  2828. case kPHONEMETABLE:
  2829. EndPhonemeTable();
  2830. NextItem(tSTRING); // name of the new phoneme table
  2831. StartPhonemeTable(item_string);
  2832. break;
  2833. case kPHONEMESTART:
  2834. if(n_phoneme_tabs == 0)
  2835. {
  2836. Error("phonemetable is missing");
  2837. return;
  2838. }
  2839. CompilePhoneme(1);
  2840. break;
  2841. case kPROCEDURE:
  2842. CompilePhoneme(0);
  2843. break;
  2844. case kEQUIVALENTS:
  2845. CompileEquivalents();
  2846. break;
  2847. default:
  2848. if(!feof(f_in))
  2849. Error("Keyword 'phoneme' expected");
  2850. break;
  2851. }
  2852. }
  2853. phoneme_tab2[n_phcodes+1].mnemonic = 0; // terminator
  2854. }
  2855. static espeak_ng_STATUS CompilePhonemeData2(const char *source, FILE *log)
  2856. {
  2857. char fname[sizeof(path_home)+40];
  2858. sprintf(fname,"%s/../phsource",path_home);
  2859. #ifdef MAKE_ENVELOPES
  2860. make_envs();
  2861. #endif
  2862. fprintf(log,"Compiling phoneme data: %s\n",fname);
  2863. n_envelopes = 0;
  2864. error_count = 0;
  2865. resample_count = 0;
  2866. memset(markers_used,0,sizeof(markers_used));
  2867. f_errors = log;
  2868. if(!access(fname, 755))
  2869. {
  2870. fprintf(log,"Can't find phoneme source directory: %s\n",fname);
  2871. return ENE_READ_ERROR;
  2872. }
  2873. strncpy0(current_fname,source,sizeof(current_fname));
  2874. sprintf(fname,"%s/../phsource/phonemes",path_home);
  2875. f_in = fopen_log(f_errors,fname,"rb");
  2876. if(f_in == NULL)
  2877. {
  2878. fprintf(log,"Can't read master phonemes file: %s\n",fname);
  2879. return ENE_READ_ERROR;
  2880. }
  2881. sprintf(fname,"%s/../phsource/%s",path_home,"error_log");
  2882. if((f_errors = fopen_log(f_errors,fname,"w")) == NULL)
  2883. f_errors = stderr;
  2884. sprintf(fname,"%s/../phsource/%s",path_home,"compile_report");
  2885. f_report = fopen_log(f_errors, fname,"w");
  2886. sprintf(fname,"%s/%s",path_home,"phondata-manifest");
  2887. if((f_phcontents = fopen_log(f_phcontents,fname,"w")) == NULL)
  2888. f_phcontents = stderr;
  2889. fprintf (f_phcontents,
  2890. "# This file lists the type of data that has been compiled into the\n"
  2891. "# phondata file\n"
  2892. "#\n"
  2893. "# The first character of a line indicates the type of data:\n"
  2894. "# S - A SPECT_SEQ structure\n"
  2895. "# W - A wavefile segment\n"
  2896. "# E - An envelope\n"
  2897. "# Q - Phoneme equivalence tables\n"
  2898. "#\n"
  2899. "# Address is the displacement within phondata of this item\n"
  2900. "#\n"
  2901. "# Address Data file\n"
  2902. "# ------- ---------\n");
  2903. fprintf(f_errors, "Source data path = '%s/../phsource'\n", path_home);
  2904. fprintf(f_errors, "Master phonemes file = '%s/../phsource/phonemes'\n", path_home);
  2905. fprintf(f_errors, "Output to '%s/'\n\n", path_home);
  2906. sprintf(fname,"%s/%s",path_home,"phondata");
  2907. f_phdata = fopen_log(f_errors,fname,"wb");
  2908. sprintf(fname,"%s/%s",path_home,"phonindex");
  2909. f_phindex = fopen_log(f_errors,fname,"wb");
  2910. sprintf(fname,"%s/%s",path_home,"phontab");
  2911. f_phtab = fopen_log(f_errors,fname,"wb");
  2912. if(f_phdata==NULL || f_phindex==NULL || f_phtab==NULL)
  2913. {
  2914. return ENE_WRITE_ERROR;
  2915. }
  2916. sprintf(fname,"%s/../phsource/compile_prog_log",path_home);
  2917. f_prog_log = fopen_log(f_errors,fname,"wb");
  2918. fprintf(log,"Compiling phoneme data: %s/../phsource\n",path_home);
  2919. // write a word so that further data doesn't start at displ=0
  2920. Write4Bytes(f_phdata,version_phdata);
  2921. Write4Bytes(f_phdata,samplerate_native);
  2922. Write4Bytes(f_phindex,version_phdata);
  2923. memset(ref_hash_tab,0,sizeof(ref_hash_tab));
  2924. n_phoneme_tabs = 0;
  2925. stack_ix = 0;
  2926. StartPhonemeTable("base");
  2927. CompilePhonemeFiles();
  2928. EndPhonemeTable();
  2929. WritePhonemeTables();
  2930. fprintf(f_errors,"\nRefs %d, Reused %d\n",count_references,duplicate_references);
  2931. fclose(f_in);
  2932. fclose(f_phdata);
  2933. if(f_prog_log != NULL)
  2934. fclose(f_prog_log);
  2935. fclose(f_phindex);
  2936. fclose(f_phtab);
  2937. fclose(f_phcontents);
  2938. LoadPhData(NULL);
  2939. CompileReport();
  2940. #ifdef MAKE_ENVELOPES
  2941. DrawEnvelopes();
  2942. #endif
  2943. if(resample_count > 0)
  2944. {
  2945. fprintf(f_errors, "\n%d WAV files resampled to %d Hz\n", resample_count, samplerate_native);
  2946. fprintf(log,"Compiled phonemes: %d errors, %d files resampled to %d Hz.\n",error_count, resample_count, samplerate_native);
  2947. }
  2948. else
  2949. {
  2950. fprintf(log,"Compiled phonemes: %d errors.\n",error_count);
  2951. }
  2952. if(f_errors != stderr)
  2953. fclose(f_errors);
  2954. ReadPhondataManifest();
  2955. return ENS_OK;
  2956. }
  2957. static const char *preset_tune_names[] = {
  2958. "s1", "c1", "q1", "e1", NULL
  2959. };
  2960. static const TUNE default_tune = {
  2961. {0,0,0,0,0,0,0,0,0,0,0,0},
  2962. {0,0,0,0},
  2963. {0, 40, 24, 8, 0, 0, 0, 0},
  2964. 46, 57, PITCHfall, 16, 0, 0,
  2965. 255, 78, 50, 255,
  2966. 3, 5,
  2967. {-7,-7,-7}, {-7,-7,-7},
  2968. PITCHfall, 64, 8,
  2969. PITCHfall, 70, 18, 24, 12,
  2970. PITCHfall, 70, 18, 24, 12, 0,
  2971. {0,0,0,0,0,0,0,0}, 0
  2972. };
  2973. #define N_TUNE_NAMES 100
  2974. MNEM_TAB envelope_names[] = {
  2975. {"fall", 0},
  2976. {"rise", 2},
  2977. {"fall-rise", 4},
  2978. {"fall-rise2", 6},
  2979. {"rise-fall", 8},
  2980. {"fall-rise3",10},
  2981. {"fall-rise4",12},
  2982. {"fall2", 14},
  2983. {"rise2", 16},
  2984. {"rise-fall-rise", 18},
  2985. {NULL, -1}
  2986. };
  2987. int LookupEnvelopeName(const char *name)
  2988. {
  2989. return(LookupMnem(envelope_names, name));
  2990. }
  2991. #pragma GCC visibility push(default)
  2992. espeak_ng_STATUS espeak_ng_CompileIntonation(FILE *log)
  2993. {
  2994. if(!log) log=stderr;
  2995. int ix;
  2996. char *p;
  2997. char c;
  2998. int keyword;
  2999. int compiling_tune = 0;
  3000. int n_tune_names = 0;
  3001. int done_split;
  3002. int done_onset = 0;
  3003. int done_last = 0;
  3004. int n_preset_tunes = 0;
  3005. int found;
  3006. int tune_number = 0;
  3007. FILE *f_out;
  3008. TUNE *tune_data;
  3009. TUNE new_tune;
  3010. char name[12];
  3011. char tune_names[N_TUNE_NAMES][12];
  3012. char buf[sizeof(path_home)+150];
  3013. error_count = 0;
  3014. f_errors = log;
  3015. sprintf(buf,"%s/../phsource/intonation.txt",path_home);
  3016. if((f_in = fopen(buf, "r")) == NULL)
  3017. {
  3018. sprintf(buf,"%s/../phsource/intonation",path_home);
  3019. if((f_in = fopen_log(f_errors, buf, "r")) == NULL)
  3020. {
  3021. fprintf(log,"Can't read file: %s\n",buf);
  3022. fclose(f_errors);
  3023. return ENE_READ_ERROR;
  3024. }
  3025. }
  3026. for(ix=0; preset_tune_names[ix] != NULL; ix++)
  3027. {
  3028. strcpy(tune_names[ix], preset_tune_names[ix]);
  3029. }
  3030. n_tune_names = ix;
  3031. n_preset_tunes = ix;
  3032. // make a list of the tune names
  3033. while(!feof(f_in))
  3034. {
  3035. if(fgets(buf,sizeof(buf),f_in) == NULL)
  3036. break;
  3037. if((memcmp(buf,"tune",4)==0) && isspace(buf[4]))
  3038. {
  3039. p = &buf[5];
  3040. while(isspace(*p)) p++;
  3041. ix = 0;
  3042. while((ix < (int)(sizeof(name) - 1)) && !isspace(*p))
  3043. {
  3044. name[ix++] = *p++;
  3045. }
  3046. name[ix] = 0;
  3047. found = 0;
  3048. for(ix = 0; ix < n_tune_names; ix++)
  3049. {
  3050. if(strcmp(name, tune_names[ix]) == 0)
  3051. {
  3052. found = 1;
  3053. break;
  3054. }
  3055. }
  3056. if(found == 0)
  3057. {
  3058. strncpy0(tune_names[n_tune_names++], name, sizeof(name));
  3059. if(n_tune_names >= N_TUNE_NAMES)
  3060. break;
  3061. }
  3062. }
  3063. }
  3064. rewind(f_in);
  3065. linenum = 1;
  3066. tune_data = (TUNE *)calloc(sizeof(TUNE), n_tune_names);
  3067. if(tune_data == NULL)
  3068. {
  3069. fprintf(f_errors, "Failed to allocate data for tunes\n");
  3070. fclose(f_in);
  3071. fclose(f_errors);
  3072. return ENE_OUT_OF_MEMORY;
  3073. }
  3074. sprintf(buf,"%s/intonations",path_home);
  3075. f_out = fopen_log(f_errors, buf, "wb");
  3076. if(f_out == NULL)
  3077. {
  3078. fclose(f_in);
  3079. fclose(f_errors);
  3080. free(tune_data);
  3081. return ENE_WRITE_ERROR;
  3082. }
  3083. while(!feof(f_in))
  3084. {
  3085. keyword = NextItem(tINTONATION);
  3086. switch(keyword)
  3087. {
  3088. case kTUNE:
  3089. if(compiling_tune)
  3090. {
  3091. }
  3092. compiling_tune = 1;
  3093. done_split = 0;
  3094. memcpy(&new_tune, &default_tune, sizeof(TUNE));
  3095. NextItem(tSTRING);
  3096. strncpy0(new_tune.name, item_string, sizeof(new_tune.name));
  3097. found = 0;
  3098. tune_number = 0;
  3099. for(ix=0; ix < n_tune_names; ix++)
  3100. {
  3101. if(strcmp(new_tune.name, tune_names[ix]) == 0)
  3102. {
  3103. found = 1;
  3104. tune_number = ix;
  3105. if(tune_data[ix].name[0] != 0)
  3106. found = 2;
  3107. break;
  3108. }
  3109. }
  3110. if(found == 2)
  3111. {
  3112. error("Duplicate tune name: '%s'",new_tune.name);
  3113. }
  3114. if(found == 0)
  3115. {
  3116. error("Bad tune name: '%s;", new_tune.name);
  3117. }
  3118. break;
  3119. case kENDTUNE:
  3120. compiling_tune = 0;
  3121. if(done_onset == 0)
  3122. {
  3123. new_tune.unstr_start[0] = new_tune.unstr_start[1];
  3124. new_tune.unstr_end[0] = new_tune.unstr_end[1];
  3125. }
  3126. if(done_last == 0)
  3127. {
  3128. new_tune.unstr_start[2] = new_tune.unstr_start[1];
  3129. new_tune.unstr_end[2] = new_tune.unstr_end[1];
  3130. }
  3131. memcpy(&tune_data[tune_number], &new_tune, sizeof(TUNE));
  3132. break;
  3133. case kTUNE_PREHEAD:
  3134. new_tune.prehead_start = NextItem(tNUMBER);
  3135. new_tune.prehead_end = NextItem(tNUMBER);
  3136. break;
  3137. case kTUNE_ONSET:
  3138. new_tune.onset = NextItem(tNUMBER);
  3139. new_tune.unstr_start[0] = NextItem(tSIGNEDNUMBER);
  3140. new_tune.unstr_end[0] = NextItem(tSIGNEDNUMBER);
  3141. done_onset = 1;
  3142. break;
  3143. case kTUNE_HEADLAST:
  3144. new_tune.head_last = NextItem(tNUMBER);
  3145. new_tune.unstr_start[2] = NextItem(tSIGNEDNUMBER);
  3146. new_tune.unstr_end[2] = NextItem(tSIGNEDNUMBER);
  3147. done_last = 1;
  3148. break;
  3149. case kTUNE_HEADENV:
  3150. NextItem(tSTRING);
  3151. if((ix = LookupEnvelopeName(item_string)) < 0)
  3152. error("Bad envelope name: '%s'",item_string);
  3153. else
  3154. new_tune.stressed_env = ix;
  3155. new_tune.stressed_drop = NextItem(tNUMBER);
  3156. break;
  3157. case kTUNE_HEAD:
  3158. new_tune.head_max_steps = NextItem(tNUMBER);
  3159. new_tune.head_start = NextItem(tNUMBER);
  3160. new_tune.head_end = NextItem(tNUMBER);
  3161. new_tune.unstr_start[1] = NextItem(tSIGNEDNUMBER);
  3162. new_tune.unstr_end[1] = NextItem(tSIGNEDNUMBER);
  3163. break;
  3164. case kTUNE_HEADEXTEND:
  3165. // up to 8 numbers
  3166. for(ix=0; ix < (int)(sizeof(new_tune.head_extend)); ix++)
  3167. {
  3168. if(!isdigit(c = CheckNextChar()) && (c != '-'))
  3169. break;
  3170. new_tune.head_extend[ix] = (NextItem(tSIGNEDNUMBER) * 64) / 100; // convert from percentage to 64ths
  3171. }
  3172. new_tune.n_head_extend = ix; // number of values
  3173. break;
  3174. case kTUNE_NUCLEUS0:
  3175. NextItem(tSTRING);
  3176. if((ix = LookupEnvelopeName(item_string)) < 0)
  3177. {
  3178. error("Bad envelope name: '%s'",item_string);
  3179. break;
  3180. }
  3181. new_tune.nucleus0_env = ix;
  3182. new_tune.nucleus0_max = NextItem(tNUMBER);
  3183. new_tune.nucleus0_min = NextItem(tNUMBER);
  3184. break;
  3185. case kTUNE_NUCLEUS1:
  3186. NextItem(tSTRING);
  3187. if((ix = LookupEnvelopeName(item_string)) < 0)
  3188. {
  3189. error("Bad envelope name: '%s'",item_string);
  3190. break;
  3191. }
  3192. new_tune.nucleus1_env = ix;
  3193. new_tune.nucleus1_max = NextItem(tNUMBER);
  3194. new_tune.nucleus1_min = NextItem(tNUMBER);
  3195. new_tune.tail_start = NextItem(tNUMBER);
  3196. new_tune.tail_end = NextItem(tNUMBER);
  3197. if(!done_split)
  3198. {
  3199. // also this as the default setting for 'split'
  3200. new_tune.split_nucleus_env = ix;
  3201. new_tune.split_nucleus_max = new_tune.nucleus1_max;
  3202. new_tune.split_nucleus_min = new_tune.nucleus1_min;
  3203. new_tune.split_tail_start = new_tune.tail_start;
  3204. new_tune.split_tail_end = new_tune.tail_end;
  3205. }
  3206. break;
  3207. case kTUNE_SPLIT:
  3208. NextItem(tSTRING);
  3209. if((ix = LookupEnvelopeName(item_string)) < 0)
  3210. {
  3211. error("Bad envelope name: '%s'",item_string);
  3212. break;
  3213. }
  3214. done_split = 1;
  3215. new_tune.split_nucleus_env = ix;
  3216. new_tune.split_nucleus_max = NextItem(tNUMBER);
  3217. new_tune.split_nucleus_min = NextItem(tNUMBER);
  3218. new_tune.split_tail_start = NextItem(tNUMBER);
  3219. new_tune.split_tail_end = NextItem(tNUMBER);
  3220. NextItem(tSTRING);
  3221. item_string[12] = 0;
  3222. for(ix=0; ix < n_tune_names; ix++)
  3223. {
  3224. if(strcmp(item_string, tune_names[ix]) == 0)
  3225. break;
  3226. }
  3227. if(ix == n_tune_names)
  3228. error("Tune '%s' not found",item_string);
  3229. else
  3230. new_tune.split_tune = ix;
  3231. break;
  3232. default:
  3233. error("Unexpected: '%s'",item_string);
  3234. break;
  3235. }
  3236. }
  3237. for(ix = 0; ix < n_preset_tunes; ix++)
  3238. {
  3239. if(tune_data[ix].name[0] == 0)
  3240. {
  3241. error("Tune '%s' not defined", preset_tune_names[ix]);
  3242. }
  3243. }
  3244. fwrite(tune_data, n_tune_names, sizeof(TUNE), f_out);
  3245. free(tune_data);
  3246. fclose(f_in);
  3247. fclose(f_out);
  3248. fprintf(log,"Compiled %d intonation tunes: %d errors.\n",n_tune_names, error_count);
  3249. LoadPhData(NULL);
  3250. return error_count > 0 ? ENE_COMPILE_ERRORS : ENS_OK;
  3251. }
  3252. espeak_ng_STATUS espeak_ng_CompilePhonemeData(long rate, FILE *log)
  3253. {
  3254. if(!log) log=stderr;
  3255. WavegenInit(rate, 0);
  3256. WavegenSetVoice(voice);
  3257. return CompilePhonemeData2("phonemes", log);
  3258. }
  3259. #pragma GCC visibility pop