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

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