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

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