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.cpp 85KB

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