eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.

compiledata.c 79KB

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