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

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