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

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