eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

compiledata.c 77KB

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