compiledata.cpp 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964
  1. /*
  2. * Copyright (C) 2005 to 2014 by Jonathan Duddington
  3. * email: jonsd@users.sourceforge.net
  4. * Copyright (C) 2013-2017 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 "config.h"
  20. #include <ctype.h>
  21. #include <errno.h>
  22. #include <stdarg.h>
  23. #include <stdbool.h>
  24. #include <stdint.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/stat.h>
  29. #include <time.h>
  30. #include <unistd.h>
  31. #include "espeak_ng.h"
  32. #include "speak_lib.h"
  33. #include "encoding.h"
  34. #include "error.h"
  35. #include "speech.h"
  36. #include "synthesize.h"
  37. #include "translate.h"
  38. typedef struct {
  39. unsigned int value;
  40. char *name;
  41. } NAMETAB;
  42. NAMETAB *manifest = NULL;
  43. int n_manifest;
  44. char phsrc[sizeof(path_home)+40]; // Source: path to the 'phonemes' source file.
  45. extern ESPEAK_NG_API int utf8_in(int *c, const char *buf);
  46. typedef struct {
  47. const char *mnem;
  48. int type;
  49. int data;
  50. } keywtab_t;
  51. #define k_AND 1
  52. #define k_OR 2
  53. #define k_THEN 3
  54. #define k_NOT 4
  55. #define kTHISSTRESS 0x800
  56. // keyword types
  57. enum {
  58. tPHONEME_TYPE = 1,
  59. tPHONEME_FLAG,
  60. tTRANSITION,
  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. { "thisPh", tWHICH_PHONEME, 1 },
  73. { "nextPh", tWHICH_PHONEME, 2 },
  74. { "next2Ph", tWHICH_PHONEME, 3 },
  75. { "nextPhW", tWHICH_PHONEME, 4 },
  76. { "prevPhW", tWHICH_PHONEME, 5 },
  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, CONDITION_IS_PHONEME_TYPE | phPAUSE },
  89. { "isVowel", 0, CONDITION_IS_PHONEME_TYPE | phVOWEL },
  90. { "isNasal", 0, CONDITION_IS_PHONEME_TYPE | phNASAL },
  91. { "isLiquid", 0, CONDITION_IS_PHONEME_TYPE | phLIQUID },
  92. { "isUStop", 0, CONDITION_IS_PHONEME_TYPE | phSTOP },
  93. { "isVStop", 0, CONDITION_IS_PHONEME_TYPE | phVSTOP },
  94. { "isVFricative", 0, CONDITION_IS_PHONEME_TYPE | phVFRICATIVE },
  95. { "isPalatal", 0, CONDITION_IS_PHFLAG_SET | phFLAGBIT_PALATAL },
  96. { "isLong", 0, CONDITION_IS_PHFLAG_SET | phFLAGBIT_LONG },
  97. { "isRhotic", 0, CONDITION_IS_PHFLAG_SET | phFLAGBIT_RHOTIC },
  98. { "isSibilant", 0, CONDITION_IS_PHFLAG_SET | phFLAGBIT_SIBILANT },
  99. { "isFlag1", 0, CONDITION_IS_PHFLAG_SET | phFLAGBIT_FLAG1 },
  100. { "isFlag2", 0, CONDITION_IS_PHFLAG_SET | phFLAGBIT_FLAG2 },
  101. { "isVelar", 0, CONDITION_IS_PLACE_OF_ARTICULATION | phPLACE_VELAR },
  102. { "isDiminished", 0, CONDITION_IS_OTHER | STRESS_IS_DIMINISHED },
  103. { "isUnstressed", 0, CONDITION_IS_OTHER | STRESS_IS_UNSTRESSED },
  104. { "isNotStressed", 0, CONDITION_IS_OTHER | STRESS_IS_NOT_STRESSED },
  105. { "isStressed", 0, CONDITION_IS_OTHER | STRESS_IS_SECONDARY },
  106. { "isMaxStress", 0, CONDITION_IS_OTHER | STRESS_IS_PRIMARY },
  107. { "isPause2", 0, CONDITION_IS_OTHER | isBreak },
  108. { "isWordStart", 0, CONDITION_IS_OTHER | isWordStart },
  109. { "isWordEnd", 0, CONDITION_IS_OTHER | isWordEnd },
  110. { "isAfterStress", 0, CONDITION_IS_OTHER | isAfterStress },
  111. { "isNotVowel", 0, CONDITION_IS_OTHER | isNotVowel },
  112. { "isFinalVowel", 0, CONDITION_IS_OTHER | isFinalVowel },
  113. { "isVoiced", 0, CONDITION_IS_OTHER | isVoiced },
  114. { "isFirstVowel", 0, CONDITION_IS_OTHER | isFirstVowel },
  115. { "isSecondVowel", 0, CONDITION_IS_OTHER | isSecondVowel },
  116. { "isTranslationGiven", 0, CONDITION_IS_OTHER | isTranslationGiven },
  117. { NULL, 0, 0 }
  118. };
  119. enum {
  120. kPHONEMESTART = 1,
  121. kUTF8_BOM,
  122. kPROCEDURE,
  123. kENDPHONEME,
  124. kENDPROCEDURE,
  125. kPHONEMETABLE,
  126. kINCLUDE,
  127. kIMPORT_PH,
  128. kSTARTTYPE,
  129. kENDTYPE,
  130. kSTRESSTYPE,
  131. kVOICINGSWITCH,
  132. kIF,
  133. kELSE,
  134. kELIF,
  135. kENDIF,
  136. kCALLPH,
  137. kSWITCH_PREVVOWEL,
  138. kSWITCH_NEXTVOWEL,
  139. kENDSWITCH,
  140. kFMT,
  141. kWAV,
  142. kVOWELSTART,
  143. kVOWELENDING,
  144. kANDWAV,
  145. kVOWELIN,
  146. kVOWELOUT,
  147. kTONESPEC,
  148. kRETURN,
  149. kCONTINUE,
  150. };
  151. enum {
  152. kTUNE = 1,
  153. kENDTUNE,
  154. kTUNE_PREHEAD,
  155. kTUNE_ONSET,
  156. kTUNE_HEAD,
  157. kTUNE_HEADENV,
  158. kTUNE_HEADEXTEND,
  159. kTUNE_HEADLAST,
  160. kTUNE_NUCLEUS0,
  161. kTUNE_NUCLEUS1,
  162. kTUNE_SPLIT,
  163. };
  164. static unsigned const char utf8_bom[] = { 0xef, 0xbb, 0xbf, 0 };
  165. static keywtab_t k_intonation[] = {
  166. { "tune", 0, kTUNE },
  167. { "endtune", 0, kENDTUNE },
  168. { "prehead", 0, kTUNE_PREHEAD },
  169. { "onset", 0, kTUNE_ONSET },
  170. { "head", 0, kTUNE_HEAD },
  171. { "headenv", 0, kTUNE_HEADENV },
  172. { "headextend", 0, kTUNE_HEADEXTEND },
  173. { "headlast", 0, kTUNE_HEADLAST },
  174. { "nucleus0", 0, kTUNE_NUCLEUS0 },
  175. { "nucleus", 0, kTUNE_NUCLEUS1 },
  176. { "split", 0, kTUNE_SPLIT },
  177. { NULL, 0, -1 }
  178. };
  179. static keywtab_t keywords[] = {
  180. { "liquid", tPHONEME_TYPE, phLIQUID },
  181. { "pause", tPHONEME_TYPE, phPAUSE },
  182. { "stress", tPHONEME_TYPE, phSTRESS },
  183. { "virtual", tPHONEME_TYPE, phVIRTUAL },
  184. { "delete_phoneme", tPHONEME_TYPE, phDELETED },
  185. // keywords
  186. { "phonemetable", tSTATEMENT, kPHONEMETABLE },
  187. { "include", tSTATEMENT, kINCLUDE },
  188. { (const char *)utf8_bom, tSTATEMENT, kUTF8_BOM },
  189. { "phoneme", tSTATEMENT, kPHONEMESTART },
  190. { "procedure", tSTATEMENT, kPROCEDURE },
  191. { "endphoneme", tSTATEMENT, kENDPHONEME },
  192. { "endprocedure", tSTATEMENT, kENDPROCEDURE },
  193. { "import_phoneme", tSTATEMENT, kIMPORT_PH },
  194. { "stress_type", tSTATEMENT, kSTRESSTYPE },
  195. { "starttype", tSTATEMENT, kSTARTTYPE },
  196. { "endtype", tSTATEMENT, kENDTYPE },
  197. { "voicingswitch", tSTATEMENT, kVOICINGSWITCH },
  198. { "IF", tSTATEMENT, kIF },
  199. { "ELSE", tSTATEMENT, kELSE },
  200. { "ELIF", tSTATEMENT, kELIF },
  201. { "ELSEIF", tSTATEMENT, kELIF }, // same as ELIF
  202. { "ENDIF", tSTATEMENT, kENDIF },
  203. { "CALL", tSTATEMENT, kCALLPH },
  204. { "RETURN", tSTATEMENT, kRETURN },
  205. { "PrevVowelEndings", tSTATEMENT, kSWITCH_PREVVOWEL },
  206. { "NextVowelStarts", tSTATEMENT, kSWITCH_NEXTVOWEL },
  207. { "EndSwitch", tSTATEMENT, kENDSWITCH },
  208. { "Tone", tSTATEMENT, kTONESPEC },
  209. { "FMT", tSTATEMENT, kFMT },
  210. { "WAV", tSTATEMENT, kWAV },
  211. { "VowelStart", tSTATEMENT, kVOWELSTART },
  212. { "VowelEnding", tSTATEMENT, kVOWELENDING },
  213. { "addWav", tSTATEMENT, kANDWAV },
  214. { "Vowelin", tSTATEMENT, kVOWELIN },
  215. { "Vowelout", tSTATEMENT, kVOWELOUT },
  216. { "Continue", tSTATEMENT, kCONTINUE },
  217. { "ChangePhoneme", tINSTRN1, i_CHANGE_PHONEME },
  218. { "ChangeNextPhoneme", tINSTRN1, i_REPLACE_NEXT_PHONEME },
  219. { "InsertPhoneme", tINSTRN1, i_INSERT_PHONEME },
  220. { "AppendPhoneme", tINSTRN1, i_APPEND_PHONEME },
  221. { "IfNextVowelAppend", tINSTRN1, i_APPEND_IFNEXTVOWEL },
  222. { "ChangeIfDiminished", tINSTRN1, i_CHANGE_IF | STRESS_IS_DIMINISHED },
  223. { "ChangeIfUnstressed", tINSTRN1, i_CHANGE_IF | STRESS_IS_UNSTRESSED },
  224. { "ChangeIfNotStressed", tINSTRN1, i_CHANGE_IF | STRESS_IS_NOT_STRESSED },
  225. { "ChangeIfStressed", tINSTRN1, i_CHANGE_IF | STRESS_IS_SECONDARY },
  226. { "ChangeIfStressed", tINSTRN1, i_CHANGE_IF | STRESS_IS_PRIMARY },
  227. { "PauseBefore", tINSTRN1, i_PAUSE_BEFORE },
  228. { "PauseAfter", tINSTRN1, i_PAUSE_AFTER },
  229. { "length", tINSTRN1, i_SET_LENGTH },
  230. { "LongLength", tINSTRN1, i_LONG_LENGTH },
  231. { "LengthAdd", tINSTRN1, i_ADD_LENGTH },
  232. { "lengthmod", tINSTRN1, i_LENGTH_MOD },
  233. { "ipa", tINSTRN1, i_IPA_NAME },
  234. // flags
  235. { "unstressed", tPHONEME_FLAG, phUNSTRESSED },
  236. { "nolink", tPHONEME_FLAG, phNOLINK },
  237. { "brkafter", tPHONEME_FLAG, phBRKAFTER },
  238. { "rhotic", tPHONEME_FLAG, phRHOTIC },
  239. { "lengthenstop", tPHONEME_FLAG, phLENGTHENSTOP },
  240. { "nopause", tPHONEME_FLAG, phNOPAUSE },
  241. { "prevoice", tPHONEME_FLAG, phPREVOICE },
  242. { "flag1", tPHONEME_FLAG, phFLAG1 },
  243. { "flag2", tPHONEME_FLAG, phFLAG2 },
  244. // vowel transition attributes
  245. { "len=", tTRANSITION, 1 },
  246. { "rms=", tTRANSITION, 2 },
  247. { "f1=", tTRANSITION, 3 },
  248. { "f2=", tTRANSITION, 4 },
  249. { "f3=", tTRANSITION, 5 },
  250. { "brk", tTRANSITION, 6 },
  251. { "rate", tTRANSITION, 7 },
  252. { "glstop", tTRANSITION, 8 },
  253. { "lenadd", tTRANSITION, 9 },
  254. { "f4", tTRANSITION, 10 },
  255. { "gpaus", tTRANSITION, 11 },
  256. { "colr=", tTRANSITION, 12 },
  257. { "amp=", tTRANSITION, 13 }, // set rms of 1st frame as fraction of rms of 2nd frame (1/30ths)
  258. { NULL, 0, -1 }
  259. };
  260. static keywtab_t *keyword_tabs[] = {
  261. keywords, k_conditions, k_properties, k_intonation
  262. };
  263. static PHONEME_TAB *phoneme_out;
  264. static int n_phcodes_list[N_PHONEME_TABS];
  265. static PHONEME_TAB_LIST phoneme_tab_list2[N_PHONEME_TABS];
  266. static PHONEME_TAB *phoneme_tab2;
  267. static int phoneme_flags;
  268. #define N_PROCS 50
  269. int n_procs;
  270. int proc_addr[N_PROCS];
  271. char proc_names[40][N_PROCS];
  272. #define MAX_PROG_BUF 2000
  273. USHORT *prog_out;
  274. USHORT *prog_out_max;
  275. USHORT prog_buf[MAX_PROG_BUF+20];
  276. static espeak_ng_STATUS ReadPhondataManifest(espeak_ng_ERROR_CONTEXT *context)
  277. {
  278. // Read the phondata-manifest file
  279. FILE *f;
  280. int n_lines = 0;
  281. int ix;
  282. char *p;
  283. unsigned int value;
  284. char buf[sizeof(path_home)+40];
  285. char name[120];
  286. sprintf(buf, "%s%c%s", path_home, PATHSEP, "phondata-manifest");
  287. if ((f = fopen(buf, "r")) == NULL)
  288. return create_file_error_context(context, static_cast<espeak_ng_STATUS> (errno), buf);
  289. while (fgets(buf, sizeof(buf), f) != NULL)
  290. n_lines++;
  291. rewind(f);
  292. if (manifest != NULL) {
  293. for (ix = 0; ix < n_manifest; ix++)
  294. free(manifest[ix].name);
  295. }
  296. if (n_lines == 0) {
  297. fclose(f);
  298. return ENS_EMPTY_PHONEME_MANIFEST;
  299. }
  300. NAMETAB *new_manifest = (NAMETAB *)realloc(manifest, n_lines * sizeof(NAMETAB));
  301. if (new_manifest == NULL) {
  302. fclose(f);
  303. free(manifest);
  304. return static_cast<espeak_ng_STATUS> (ENOMEM);
  305. } else
  306. manifest = new_manifest;
  307. n_manifest = 0;
  308. while (fgets(buf, sizeof(buf), f) != NULL) {
  309. if (!isalpha(buf[0]))
  310. continue;
  311. if (sscanf(&buf[2], "%x %s", &value, name) == 2) {
  312. if ((p = (char *)malloc(strlen(name)+1)) != NULL) {
  313. strcpy(p, name);
  314. manifest[n_manifest].value = value;
  315. manifest[n_manifest].name = p;
  316. n_manifest++;
  317. }
  318. }
  319. }
  320. fclose(f);
  321. return ENS_OK;
  322. }
  323. static int n_phoneme_tabs;
  324. static int n_phcodes;
  325. // outout files
  326. static FILE *f_phdata;
  327. static FILE *f_phindex;
  328. static FILE *f_phtab;
  329. static FILE *f_phcontents;
  330. static FILE *f_errors = NULL;
  331. static FILE *f_prog_log = NULL;
  332. static FILE *f_report;
  333. static FILE *f_in;
  334. static int f_in_linenum;
  335. static int f_in_displ;
  336. static int linenum;
  337. static int count_references = 0;
  338. static int duplicate_references = 0;
  339. static int count_frames = 0;
  340. static int error_count = 0;
  341. static int resample_count = 0;
  342. static int resample_fails = 0;
  343. static int then_count = 0;
  344. static int after_if = 0;
  345. static char current_fname[80];
  346. static int markers_used[8];
  347. typedef struct {
  348. void *link;
  349. int value;
  350. int ph_mnemonic;
  351. short ph_table;
  352. char string[1];
  353. } REF_HASH_TAB;
  354. static REF_HASH_TAB *ref_hash_tab[256];
  355. #define N_ENVELOPES 30
  356. int n_envelopes = 0;
  357. char envelope_paths[N_ENVELOPES][80];
  358. unsigned char envelope_dat[N_ENVELOPES][ENV_LEN];
  359. typedef struct {
  360. FILE *file;
  361. int linenum;
  362. char fname[80];
  363. } STACK;
  364. #define N_STACK 12
  365. int stack_ix;
  366. STACK stack[N_STACK];
  367. #define N_IF_STACK 12
  368. int if_level;
  369. typedef struct {
  370. USHORT *p_then;
  371. USHORT *p_else;
  372. int returned;
  373. } IF_STACK;
  374. IF_STACK if_stack[N_IF_STACK];
  375. enum {
  376. tENDFILE = 1,
  377. tSTRING,
  378. tNUMBER,
  379. tSIGNEDNUMBER,
  380. tPHONEMEMNEM,
  381. tOPENBRACKET,
  382. tKEYWORD,
  383. tCONDITION,
  384. tPROPERTIES,
  385. tINTONATION,
  386. };
  387. int item_type;
  388. int item_terminator;
  389. #define N_ITEM_STRING 256
  390. char item_string[N_ITEM_STRING];
  391. static int ref_sorter(char **a, char **b)
  392. {
  393. int ix;
  394. REF_HASH_TAB *p1 = (REF_HASH_TAB *)(*a);
  395. REF_HASH_TAB *p2 = (REF_HASH_TAB *)(*b);
  396. ix = strcoll(p1->string, p2->string);
  397. if (ix != 0)
  398. return ix;
  399. ix = p1->ph_table - p2->ph_table;
  400. if (ix != 0)
  401. return ix;
  402. return p1->ph_mnemonic - p2->ph_mnemonic;
  403. }
  404. static void CompileReport(void)
  405. {
  406. int ix;
  407. int hash;
  408. int n;
  409. REF_HASH_TAB *p;
  410. REF_HASH_TAB **list;
  411. const char *data_path;
  412. int prev_table;
  413. int procedure_num;
  414. int prev_mnemonic;
  415. if (f_report == NULL)
  416. return;
  417. // make a list of all the references and sort it
  418. list = (REF_HASH_TAB **)malloc((count_references)* sizeof(REF_HASH_TAB *));
  419. if (list == NULL)
  420. return;
  421. fprintf(f_report, "\n%d phoneme tables\n", n_phoneme_tabs);
  422. fprintf(f_report, " new total\n");
  423. for (ix = 0; ix < n_phoneme_tabs; ix++)
  424. fprintf(f_report, "%8s %3d %4d\n", phoneme_tab_list2[ix].name, phoneme_tab_list2[ix].n_phonemes, n_phcodes_list[ix]+1);
  425. fputc('\n', f_report);
  426. fprintf(f_report, "Data file Used by\n");
  427. ix = 0;
  428. for (hash = 0; (hash < 256) && (ix < count_references); hash++) {
  429. p = ref_hash_tab[hash];
  430. while (p != NULL) {
  431. list[ix++] = p;
  432. p = (REF_HASH_TAB *)(p->link);
  433. }
  434. }
  435. n = ix;
  436. qsort((void *)list, n, sizeof(REF_HASH_TAB *), (int (*)(const void *, const void *))ref_sorter);
  437. data_path = "";
  438. prev_mnemonic = 0;
  439. prev_table = 0;
  440. for (ix = 0; ix < n; ix++) {
  441. int j = 0;
  442. if (strcmp(list[ix]->string, data_path) != 0) {
  443. data_path = list[ix]->string;
  444. j = strlen(data_path);
  445. fprintf(f_report, "%s", data_path);
  446. } else if ((list[ix]->ph_table == prev_table) && (list[ix]->ph_mnemonic == prev_mnemonic))
  447. continue; // same phoneme, don't list twice
  448. while (j < 14) {
  449. fputc(' ', f_report); // pad filename with spaces
  450. j++;
  451. }
  452. prev_mnemonic = list[ix]->ph_mnemonic;
  453. if ((prev_mnemonic >> 24) == 'P') {
  454. // a procedure, not a phoneme
  455. procedure_num = atoi(WordToString(prev_mnemonic));
  456. fprintf(f_report, " %s %s", phoneme_tab_list2[prev_table = list[ix]->ph_table].name, proc_names[procedure_num]);
  457. } else
  458. fprintf(f_report, " [%s] %s", WordToString(prev_mnemonic), phoneme_tab_list2[prev_table = list[ix]->ph_table].name);
  459. fputc('\n', f_report);
  460. }
  461. for (ix = 0; ix < n; ix++) {
  462. free(list[ix]);
  463. list[ix] = NULL;
  464. }
  465. free(list);
  466. list = NULL;
  467. }
  468. static void error(const char *format, ...)
  469. {
  470. va_list args;
  471. va_start(args, format);
  472. fprintf(f_errors, "%s(%d): ", current_fname, linenum-1);
  473. vfprintf(f_errors, format, args);
  474. fprintf(f_errors, "\n");
  475. error_count++;
  476. va_end(args);
  477. }
  478. static void error_from_status(espeak_ng_STATUS status, const char *context)
  479. {
  480. char message[512];
  481. espeak_ng_GetStatusCodeMessage(status, message, sizeof(message));
  482. if (context)
  483. error("%s: '%s'.", message, context);
  484. else
  485. error("%s.", message);
  486. }
  487. static unsigned int StringToWord(const char *string)
  488. {
  489. // Pack 4 characters into a word
  490. int ix;
  491. unsigned char c;
  492. unsigned int word;
  493. if (string == NULL)
  494. return 0;
  495. word = 0;
  496. for (ix = 0; ix < 4; ix++) {
  497. if (string[ix] == 0) break;
  498. c = string[ix];
  499. word |= (c << (ix*8));
  500. }
  501. return word;
  502. }
  503. static MNEM_TAB reserved_phonemes[] = {
  504. { "_\001", phonCONTROL }, // NOT USED
  505. { "%", phonSTRESS_U },
  506. { "%%", phonSTRESS_D },
  507. { ",", phonSTRESS_2 },
  508. { ",,", phonSTRESS_3 },
  509. { "'", phonSTRESS_P },
  510. { "''", phonSTRESS_P2 },
  511. { "=", phonSTRESS_PREV }, // stress previous syllable
  512. { "_:", phonPAUSE }, // pause
  513. { "_", phonPAUSE_SHORT }, // short pause
  514. { "_!", phonPAUSE_NOLINK }, // short pause, no link
  515. { ":", phonLENGTHEN },
  516. { "@", phonSCHWA },
  517. { "@-", phonSCHWA_SHORT },
  518. { "||", phonEND_WORD },
  519. { "1", phonDEFAULTTONE }, // (numeral 1) default tone (for tone language)
  520. { "#X1", phonCAPITAL }, // capital letter indication
  521. { "?", phonGLOTTALSTOP }, // glottal stop
  522. { "-", phonSYLLABIC }, // syllabic consonant
  523. { "_^_", phonSWITCH }, // Change language
  524. { "_X1", phonX1 }, // a language specific action
  525. { "_|", phonPAUSE_VSHORT }, // very short pause
  526. { "_::", phonPAUSE_LONG }, // long pause
  527. { "t#", phonT_REDUCED }, // reduced version of [t]
  528. { "'!", phonSTRESS_TONIC }, // stress - emphasized
  529. { "_;_", phonPAUSE_CLAUSE }, // clause pause
  530. { "#@", phonVOWELTYPES }, // vowel type groups, these should be consecutive
  531. { "#a", phonVOWELTYPES+1 },
  532. { "#e", phonVOWELTYPES+2 },
  533. { "#i", phonVOWELTYPES+3 },
  534. { "#o", phonVOWELTYPES+4 },
  535. { "#u", phonVOWELTYPES+5 },
  536. { NULL, 0 }
  537. };
  538. static void ReservePhCodes()
  539. {
  540. // Reserve phoneme codes which have fixed numbers so that they can be
  541. // referred to from the program code.
  542. unsigned int word;
  543. MNEM_TAB *p;
  544. p = reserved_phonemes;
  545. while (p->mnem != NULL) {
  546. word = StringToWord(p->mnem);
  547. phoneme_tab2[p->value].mnemonic = word;
  548. phoneme_tab2[p->value].code = p->value;
  549. if (n_phcodes <= p->value)
  550. n_phcodes = p->value+1;
  551. p++;
  552. }
  553. }
  554. static int LookupPhoneme(const char *string, int control)
  555. {
  556. // control = 0 explicit declaration
  557. // control = 1 declare phoneme if not found
  558. // control = 2 start looking after control & stress phonemes
  559. int ix;
  560. int start;
  561. int use;
  562. unsigned int word;
  563. if (strcmp(string, "NULL") == 0)
  564. return 1;
  565. ix = strlen(string);
  566. if ((ix == 0) || (ix > 4))
  567. error("Bad phoneme name '%s'", string);
  568. word = StringToWord(string);
  569. // don't use phoneme number 0, reserved for string terminator
  570. start = 1;
  571. if (control == 2) {
  572. // don't look for control and stress phonemes (allows these characters to be
  573. // used for other purposes)
  574. start = 8;
  575. }
  576. use = 0;
  577. for (ix = start; ix < n_phcodes; ix++) {
  578. if (phoneme_tab2[ix].mnemonic == word)
  579. return ix;
  580. if ((use == 0) && (phoneme_tab2[ix].mnemonic == 0))
  581. use = ix;
  582. }
  583. if (use == 0) {
  584. if (control == 0)
  585. return -1;
  586. if (n_phcodes >= N_PHONEME_TAB-1)
  587. return -1; // phoneme table is full
  588. use = n_phcodes++;
  589. }
  590. // add this phoneme to the phoneme table
  591. phoneme_tab2[use].mnemonic = word;
  592. phoneme_tab2[use].type = phINVALID;
  593. phoneme_tab2[use].program = linenum; // for error report if the phoneme remains undeclared
  594. return use;
  595. }
  596. static unsigned int get_char()
  597. {
  598. unsigned int c;
  599. c = fgetc(f_in);
  600. if (c == '\n')
  601. linenum++;
  602. return c;
  603. }
  604. static void unget_char(unsigned int c)
  605. {
  606. ungetc(c, f_in);
  607. if (c == '\n')
  608. linenum--;
  609. }
  610. static int CheckNextChar()
  611. {
  612. int c;
  613. while (((c = get_char()) == ' ') || (c == '\t'))
  614. ;
  615. unget_char(c);
  616. return c;
  617. }
  618. static int NextItem(int type)
  619. {
  620. int acc;
  621. unsigned char c = 0;
  622. unsigned char c2;
  623. int ix;
  624. int sign;
  625. char *p;
  626. keywtab_t *pk;
  627. item_type = -1;
  628. f_in_displ = ftell(f_in);
  629. f_in_linenum = linenum;
  630. while (!feof(f_in)) {
  631. c = get_char();
  632. if (c == '/') {
  633. if ((c2 = get_char()) == '/') {
  634. // comment, ignore to end of line
  635. while (!feof(f_in) && ((c = get_char()) != '\n'))
  636. ;
  637. } else
  638. unget_char(c2);
  639. }
  640. if (!isspace(c))
  641. break;
  642. }
  643. if (feof(f_in))
  644. return -2;
  645. if (c == '(') {
  646. if (type == tOPENBRACKET)
  647. return 1;
  648. return -1;
  649. }
  650. ix = 0;
  651. while (!feof(f_in) && !isspace(c) && (c != '(') && (c != ')') && (c != ',')) {
  652. if (c == '\\')
  653. c = get_char();
  654. item_string[ix++] = c;
  655. c = get_char();
  656. if (feof(f_in))
  657. break;
  658. if (item_string[ix-1] == '=')
  659. break;
  660. }
  661. item_string[ix] = 0;
  662. while (isspace(c))
  663. c = get_char();
  664. item_terminator = ' ';
  665. if ((c == ')') || (c == '(') || (c == ','))
  666. item_terminator = c;
  667. if ((c == ')') || (c == ','))
  668. c = ' ';
  669. if (!feof(f_in))
  670. unget_char(c);
  671. if (type == tSTRING)
  672. return 0;
  673. if ((type == tNUMBER) || (type == tSIGNEDNUMBER)) {
  674. acc = 0;
  675. sign = 1;
  676. p = item_string;
  677. if ((*p == '-') && (type == tSIGNEDNUMBER)) {
  678. sign = -1;
  679. p++;
  680. }
  681. if (!isdigit(*p)) {
  682. if ((type == tNUMBER) && (*p == '-'))
  683. error("Expected an unsigned number");
  684. else
  685. error("Expected a number");
  686. }
  687. while (isdigit(*p)) {
  688. acc *= 10;
  689. acc += (*p - '0');
  690. p++;
  691. }
  692. return acc * sign;
  693. }
  694. if ((type >= tKEYWORD) && (type <= tINTONATION)) {
  695. pk = keyword_tabs[type-tKEYWORD];
  696. while (pk->mnem != NULL) {
  697. if (strcmp(item_string, pk->mnem) == 0) {
  698. item_type = pk->type;
  699. return pk->data;
  700. }
  701. pk++;
  702. }
  703. item_type = -1;
  704. return -1; // keyword not found
  705. }
  706. if (type == tPHONEMEMNEM)
  707. return LookupPhoneme(item_string, 2);
  708. return -1;
  709. }
  710. static int NextItemMax(int max)
  711. {
  712. // Get a number, but restrict value to max
  713. int value;
  714. value = NextItem(tNUMBER);
  715. if (value > max) {
  716. error("Value %d is greater than maximum %d", value, max);
  717. value = max;
  718. }
  719. return value;
  720. }
  721. static int NextItemBrackets(int type, int control)
  722. {
  723. // Expect a parameter inside parantheses
  724. // control: bit 0 0= need (
  725. // bit 1 1= allow comma
  726. int value;
  727. if ((control & 1) == 0) {
  728. if (!NextItem(tOPENBRACKET))
  729. error("Expected '('");
  730. }
  731. value = NextItem(type);
  732. if ((control & 2) && (item_terminator == ','))
  733. return value;
  734. if (item_terminator != ')')
  735. error("Expected ')'");
  736. return value;
  737. }
  738. static void UngetItem()
  739. {
  740. fseek(f_in, f_in_displ, SEEK_SET);
  741. linenum = f_in_linenum;
  742. }
  743. static int Range(int value, int divide, int min, int max)
  744. {
  745. if (value < 0)
  746. value -= divide/2;
  747. else
  748. value += divide/2;
  749. value = value / divide;
  750. if (value > max)
  751. value = max;
  752. if (value < min)
  753. value = min;
  754. return value - min;
  755. }
  756. static int CompileVowelTransition(int which)
  757. {
  758. // Compile a vowel transition
  759. int key;
  760. int len = 0;
  761. int rms = 0;
  762. int f1 = 0;
  763. int f2 = 0;
  764. int f2_min = 0;
  765. int f2_max = 0;
  766. int f3_adj = 0;
  767. int f3_amp = 0;
  768. int flags = 0;
  769. int vcolour = 0;
  770. int x;
  771. int instn = i_VOWELIN;
  772. int word1;
  773. int word2;
  774. if (which == 1) {
  775. len = 50 / 2; // defaults for transition into vowel
  776. rms = 25 / 2;
  777. if (phoneme_out->type == phSTOP) {
  778. len = 42 / 2; // defaults for transition into vowel
  779. rms = 30 / 2;
  780. }
  781. } else if (which == 2) {
  782. instn = i_VOWELOUT;
  783. len = 36 / 2; // defaults for transition out of vowel
  784. rms = 16 / 2;
  785. }
  786. for (;;) {
  787. key = NextItem(tKEYWORD);
  788. if (item_type != tTRANSITION) {
  789. UngetItem();
  790. break;
  791. }
  792. switch (key & 0xf)
  793. {
  794. case 1:
  795. len = Range(NextItem(tNUMBER), 2, 0, 63) & 0x3f;
  796. flags |= 1;
  797. break;
  798. case 2:
  799. rms = Range(NextItem(tNUMBER), 2, 0, 31) & 0x1f;
  800. flags |= 1;
  801. break;
  802. case 3:
  803. f1 = NextItem(tNUMBER);
  804. break;
  805. case 4:
  806. f2 = Range(NextItem(tNUMBER), 50, 0, 63) & 0x3f;
  807. f2_min = Range(NextItem(tSIGNEDNUMBER), 50, -15, 15) & 0x1f;
  808. f2_max = Range(NextItem(tSIGNEDNUMBER), 50, -15, 15) & 0x1f;
  809. if (f2_min > f2_max) {
  810. x = f2_min;
  811. f2_min = f2_max;
  812. f2_max = x;
  813. }
  814. break;
  815. case 5:
  816. f3_adj = Range(NextItem(tSIGNEDNUMBER), 50, -15, 15) & 0x1f;
  817. f3_amp = Range(NextItem(tNUMBER), 8, 0, 15) & 0x1f;
  818. break;
  819. case 6:
  820. flags |= 2; // break
  821. break;
  822. case 7:
  823. flags |= 4; // rate
  824. break;
  825. case 8:
  826. flags |= 8; // glstop
  827. break;
  828. case 9:
  829. flags |= 16; // lenadd
  830. break;
  831. case 10:
  832. flags |= 32; // f4
  833. break;
  834. case 11:
  835. flags |= 64; // pause
  836. break;
  837. case 12:
  838. vcolour = NextItem(tNUMBER);
  839. break;
  840. case 13:
  841. // set rms of 1st frame as fraction of rms of 2nd frame (1/30ths)
  842. rms = (Range(NextItem(tNUMBER), 1, 0, 31) & 0x1f) | 0x20;
  843. flags |= 1;
  844. break;
  845. }
  846. }
  847. word1 = len + (rms << 6) + (flags << 12);
  848. word2 = f2 + (f2_min << 6) + (f2_max << 11) + (f3_adj << 16) + (f3_amp << 21) + (f1 << 26) + (vcolour << 29);
  849. prog_out[0] = instn + ((word1 >> 16) & 0xff);
  850. prog_out[1] = word1;
  851. prog_out[2] = word2 >> 16;
  852. prog_out[3] = word2;
  853. prog_out += 4;
  854. return 0;
  855. }
  856. #if ! DATA_FROM_SOURCECODE_FILES
  857. static espeak_ng_STATUS LoadSpect(const char *path, int control, int *addr)
  858. {
  859. SpectSeq *spectseq;
  860. int peak;
  861. int frame;
  862. int n_frames;
  863. int ix;
  864. int x, x2;
  865. int rms;
  866. float total;
  867. float pkheight;
  868. int marker1_set = 0;
  869. int frame_vowelbreak = 0;
  870. int klatt_flag = 0;
  871. SpectFrame *fr;
  872. frame_t *fr_out;
  873. char filename[sizeof(path_home)+20];
  874. SPECT_SEQ seq_out;
  875. SPECT_SEQK seqk_out;
  876. // create SpectSeq and import data
  877. spectseq = SpectSeqCreate();
  878. if (spectseq == NULL)
  879. return static_cast<espeak_ng_STATUS> (ENOMEM);
  880. snprintf(filename, sizeof(filename), "%s/%s", phsrc, path);
  881. espeak_ng_STATUS status = LoadSpectSeq(spectseq, filename);
  882. if (status != ENS_OK) {
  883. error("Bad vowel file: '%s'", path);
  884. SpectSeqDestroy(spectseq);
  885. return status;
  886. }
  887. // do we need additional klatt data ?
  888. for (frame = 0; frame < spectseq->numframes; frame++) {
  889. for (ix = 5; ix < N_KLATTP2; ix++) {
  890. if (spectseq->frames[frame]->klatt_param[ix] != 0)
  891. klatt_flag = FRFLAG_KLATT;
  892. }
  893. }
  894. *addr = ftell(f_phdata);
  895. seq_out.n_frames = 0;
  896. seq_out.sqflags = 0;
  897. seq_out.length_total = 0;
  898. total = 0;
  899. for (frame = 0; frame < spectseq->numframes; frame++) {
  900. if (spectseq->frames[frame]->keyframe) {
  901. if (seq_out.n_frames == 1)
  902. frame_vowelbreak = frame;
  903. if (spectseq->frames[frame]->markers & 0x2) {
  904. // marker 1 is set
  905. marker1_set = 1;
  906. }
  907. seq_out.n_frames++;
  908. if (frame > 0)
  909. total += spectseq->frames[frame-1]->length;
  910. }
  911. }
  912. seq_out.length_total = (int)total;
  913. if ((control & 1) && (marker1_set == 0)) {
  914. // This is a vowel, but no Vowel Break marker is set
  915. // set a marker flag for the second frame of a vowel
  916. spectseq->frames[frame_vowelbreak]->markers |= FRFLAG_VOWEL_CENTRE;
  917. }
  918. n_frames = 0;
  919. for (frame = 0; frame < spectseq->numframes; frame++) {
  920. fr = spectseq->frames[frame];
  921. if (fr->keyframe) {
  922. if (klatt_flag)
  923. fr_out = &seqk_out.frame[n_frames];
  924. else
  925. fr_out = (frame_t *)&seq_out.frame[n_frames];
  926. x = (int)(fr->length + 0.5); // round to nearest mS
  927. if (x > 255) x = 255;
  928. fr_out->length = x;
  929. fr_out->frflags = fr->markers | klatt_flag;
  930. rms = (int)GetFrameRms(fr, spectseq->amplitude);
  931. if (rms > 255) rms = 255;
  932. fr_out->rms = rms;
  933. if (n_frames == (seq_out.n_frames-1))
  934. fr_out->length = 0; // give last frame zero length
  935. // write: peak data
  936. count_frames++;
  937. for (peak = 0; peak < 8; peak++) {
  938. if (peak < 7)
  939. fr_out->ffreq[peak] = fr->peaks[peak].pkfreq;
  940. pkheight = spectseq->amplitude * fr->amp_adjust * fr->peaks[peak].pkheight;
  941. pkheight = pkheight/640000;
  942. if (pkheight > 255) pkheight = 255;
  943. fr_out->fheight[peak] = (int)pkheight;
  944. if (peak < 6) {
  945. x = fr->peaks[peak].pkwidth/4;
  946. if (x > 255) x = 255;
  947. fr_out->fwidth[peak] = x;
  948. if (peak < 3) {
  949. x2 = fr->peaks[peak].pkright/4;
  950. if (x2 > 255) x2 = 255;
  951. fr_out->fright[peak] = x2;
  952. }
  953. }
  954. if (peak < 4) {
  955. x = fr->peaks[peak].klt_bw / 2;
  956. if (x > 255) x = 255;
  957. fr_out->bw[peak] = x;
  958. }
  959. }
  960. for (ix = 0; ix < 5; ix++) {
  961. fr_out->klattp[ix] = fr->klatt_param[ix];
  962. fr_out->klattp[KLATT_FNZ] = fr->klatt_param[KLATT_FNZ] / 2;
  963. }
  964. if (klatt_flag) {
  965. // additional klatt parameters
  966. for (ix = 0; ix < 5; ix++)
  967. fr_out->klattp2[ix] = fr->klatt_param[ix+5];
  968. for (peak = 0; peak < 7; peak++) {
  969. fr_out->klatt_ap[peak] = fr->peaks[peak].klt_ap;
  970. x = fr->peaks[peak].klt_bp / 2;
  971. if (x > 255) x = 255;
  972. fr_out->klatt_bp[peak] = x;
  973. }
  974. fr_out->spare = 0;
  975. }
  976. if (fr_out->bw[1] == 0) {
  977. fr_out->bw[0] = 89 / 2;
  978. fr_out->bw[1] = 90 / 2;
  979. fr_out->bw[2] = 140 / 2;
  980. fr_out->bw[3] = 260 / 2;
  981. }
  982. n_frames++;
  983. }
  984. }
  985. if (klatt_flag) {
  986. seqk_out.n_frames = seq_out.n_frames;
  987. seqk_out.sqflags = seq_out.sqflags;
  988. seqk_out.length_total = seq_out.length_total;
  989. ix = (char *)(&seqk_out.frame[seqk_out.n_frames]) - (char *)(&seqk_out);
  990. fwrite(&seqk_out, ix, 1, f_phdata);
  991. while (ix & 3)
  992. {
  993. // round up to multiple of 4 bytes
  994. fputc(0, f_phdata);
  995. ix++;
  996. }
  997. } else {
  998. ix = (char *)(&seq_out.frame[seq_out.n_frames]) - (char *)(&seq_out);
  999. fwrite(&seq_out, ix, 1, f_phdata);
  1000. while (ix & 3)
  1001. {
  1002. // round up to multiple of 4 bytes
  1003. fputc(0, f_phdata);
  1004. ix++;
  1005. }
  1006. }
  1007. SpectSeqDestroy(spectseq);
  1008. return ENS_OK;
  1009. }
  1010. #endif
  1011. static int LoadWavefile(FILE *f, const char *fname)
  1012. {
  1013. int displ;
  1014. unsigned char c1;
  1015. unsigned char c3;
  1016. int c2;
  1017. int sample;
  1018. int sample2;
  1019. float x;
  1020. int max = 0;
  1021. int length;
  1022. int sr1, sr2;
  1023. int failed;
  1024. int len;
  1025. int resample_wav = 0;
  1026. const char *fname2;
  1027. char fname_temp[100];
  1028. char msg[120];
  1029. int scale_factor = 0;
  1030. fseek(f, 24, SEEK_SET);
  1031. sr1 = Read4Bytes(f);
  1032. sr2 = Read4Bytes(f);
  1033. fseek(f, 40, SEEK_SET);
  1034. if ((sr1 != samplerate_native) || (sr2 != sr1*2)) {
  1035. char command[sizeof(path_home)+250];
  1036. failed = 0;
  1037. #ifdef HAVE_MKSTEMP
  1038. int fd_temp;
  1039. strcpy(fname_temp, "/tmp/espeakXXXXXX");
  1040. if ((fd_temp = mkstemp(fname_temp)) >= 0)
  1041. close(fd_temp);
  1042. #else
  1043. strcpy(fname_temp, tmpnam(NULL));
  1044. #endif
  1045. fname2 = fname;
  1046. len = strlen(fname);
  1047. if (strcmp(&fname[len-4], ".wav") == 0) {
  1048. strcpy(msg, fname);
  1049. msg[len-4] = 0;
  1050. fname2 = msg;
  1051. }
  1052. sprintf(command, "sox \"%s/%s.wav\" -r %d -c1 -t wav %s\n", phsrc, fname2, samplerate_native, fname_temp);
  1053. if (system(command) != 0)
  1054. failed = 1;
  1055. if (failed || (GetFileLength(fname_temp) <= 0)) {
  1056. if (resample_fails < 2)
  1057. error("Resample command failed: %s", command);
  1058. resample_fails++;
  1059. if (sr1 != samplerate_native)
  1060. error("Can't resample (%d to %d): %s", sr1, samplerate_native, fname);
  1061. else
  1062. error("WAV file is not mono: %s", fname);
  1063. remove(fname_temp);
  1064. return 0;
  1065. }
  1066. f = fopen(fname_temp, "rb");
  1067. if (f == NULL) {
  1068. error("Can't read temp file: %s", fname_temp);
  1069. return 0;
  1070. }
  1071. if (f_report != NULL)
  1072. fprintf(f_report, "resampled %s\n", fname);
  1073. resample_count++;
  1074. resample_wav = 1;
  1075. fseek(f, 40, SEEK_SET); // skip past the WAV header, up to before "data length"
  1076. }
  1077. displ = ftell(f_phdata);
  1078. // data contains: 4 bytes of length (n_samples * 2), followed by 2-byte samples (lsb byte first)
  1079. length = Read4Bytes(f);
  1080. while (true) {
  1081. int c;
  1082. if ((c = fgetc(f)) == EOF)
  1083. break;
  1084. c1 = (unsigned char)c;
  1085. if ((c = fgetc(f)) == EOF)
  1086. break;
  1087. c3 = (unsigned char)c;
  1088. c2 = c3 << 24;
  1089. c2 = c2 >> 16; // sign extend
  1090. sample = (c1 & 0xff) + c2;
  1091. if (sample > max)
  1092. max = sample;
  1093. else if (sample < -max)
  1094. max = -sample;
  1095. }
  1096. scale_factor = (max / 127) + 1;
  1097. #define MIN_FACTOR -1 // was 6, disable use of 16 bit samples
  1098. if (scale_factor > MIN_FACTOR) {
  1099. length = length/2 + (scale_factor << 16);
  1100. }
  1101. Write4Bytes(f_phdata, length);
  1102. fseek(f, 44, SEEK_SET);
  1103. while (!feof(f)) {
  1104. c1 = fgetc(f);
  1105. c3 = fgetc(f);
  1106. c2 = c3 << 24;
  1107. c2 = c2 >> 16; // sign extend
  1108. sample = (c1 & 0xff) + c2;
  1109. if (feof(f)) break;
  1110. if (scale_factor <= MIN_FACTOR) {
  1111. fputc(sample & 0xff, f_phdata);
  1112. fputc(sample >> 8, f_phdata);
  1113. } else {
  1114. x = ((float)sample / scale_factor) + 0.5;
  1115. sample2 = (int)x;
  1116. if (sample2 > 127)
  1117. sample2 = 127;
  1118. if (sample2 < -128)
  1119. sample2 = -128;
  1120. fputc(sample2, f_phdata);
  1121. }
  1122. }
  1123. length = ftell(f_phdata);
  1124. while ((length & 3) != 0) {
  1125. // pad to a multiple of 4 bytes
  1126. fputc(0, f_phdata);
  1127. length++;
  1128. }
  1129. if (resample_wav != 0) {
  1130. fclose(f);
  1131. remove(fname_temp);
  1132. }
  1133. return displ | 0x800000; // set bit 23 to indicate a wave file rather than a spectrum
  1134. }
  1135. static espeak_ng_STATUS LoadEnvelope(FILE *f, const char *fname, int *displ)
  1136. {
  1137. char buf[128];
  1138. if (displ)
  1139. *displ = ftell(f_phdata);
  1140. if (fseek(f, 12, SEEK_SET) == -1)
  1141. return static_cast<espeak_ng_STATUS> (errno);
  1142. if (fread(buf, 128, 1, f) != 128)
  1143. return static_cast<espeak_ng_STATUS> (errno);
  1144. fwrite(buf, 128, 1, f_phdata);
  1145. if (n_envelopes < N_ENVELOPES) {
  1146. strncpy0(envelope_paths[n_envelopes], fname, sizeof(envelope_paths[0]));
  1147. memcpy(envelope_dat[n_envelopes], buf, sizeof(envelope_dat[0]));
  1148. n_envelopes++;
  1149. }
  1150. return ENS_OK;
  1151. }
  1152. // Generate a hash code from the specified string
  1153. static int Hash8(const char *string)
  1154. {
  1155. int c;
  1156. int chars = 0;
  1157. int hash = 0;
  1158. while ((c = *string++) != 0) {
  1159. c = tolower(c) - 'a';
  1160. hash = hash * 8 + c;
  1161. hash = (hash & 0x1ff) ^ (hash >> 8); // exclusive or
  1162. chars++;
  1163. }
  1164. return (hash+chars) & 0xff;
  1165. }
  1166. static int LoadEnvelope2(FILE *f, const char *fname)
  1167. {
  1168. int ix, ix2;
  1169. int n;
  1170. int x, y;
  1171. int displ;
  1172. int n_points;
  1173. double yy;
  1174. char line_buf[128];
  1175. float env_x[20];
  1176. float env_y[20];
  1177. int env_lin[20];
  1178. unsigned char env[ENV_LEN];
  1179. n_points = 0;
  1180. (void) fgets(line_buf, sizeof(line_buf), f); // skip first line
  1181. while (!feof(f)) {
  1182. if (fgets(line_buf, sizeof(line_buf), f) == NULL)
  1183. break;
  1184. env_lin[n_points] = 0;
  1185. n = sscanf(line_buf, "%f %f %d", &env_x[n_points], &env_y[n_points], &env_lin[n_points]);
  1186. if (n >= 2) {
  1187. env_x[n_points] *= (float)1.28; // convert range 0-100 to 0-128
  1188. n_points++;
  1189. }
  1190. }
  1191. if (n_points > 0) {
  1192. env_x[n_points] = env_x[n_points-1];
  1193. env_y[n_points] = env_y[n_points-1];
  1194. }
  1195. ix = -1;
  1196. ix2 = 0;
  1197. if (n_points > 0) for (x = 0; x < ENV_LEN; x++) {
  1198. if (n_points > 3 && x > env_x[ix+4])
  1199. ix++;
  1200. if (n_points > 2 && x >= env_x[ix2+1])
  1201. ix2++;
  1202. if (env_lin[ix2] > 0) {
  1203. yy = env_y[ix2] + (env_y[ix2+1] - env_y[ix2]) * ((float)x - env_x[ix2]) / (env_x[ix2+1] - env_x[ix2]);
  1204. y = (int)(yy * 2.55);
  1205. } else if (n_points > 3)
  1206. y = (int)(polint(&env_x[ix], &env_y[ix], 4, x) * 2.55); // convert to range 0-255
  1207. else
  1208. y = (int)(polint(&env_x[ix], &env_y[ix], 3, x) * 2.55);
  1209. if (y < 0) y = 0;
  1210. if (y > 255) y = 255;
  1211. env[x] = y;
  1212. }
  1213. if (n_envelopes < N_ENVELOPES) {
  1214. strncpy0(envelope_paths[n_envelopes], fname, sizeof(envelope_paths[0]));
  1215. memcpy(envelope_dat[n_envelopes], env, ENV_LEN);
  1216. n_envelopes++;
  1217. }
  1218. displ = ftell(f_phdata);
  1219. fwrite(env, 1, 128, f_phdata);
  1220. return displ;
  1221. }
  1222. #if ! DATA_FROM_SOURCECODE_FILES
  1223. static espeak_ng_STATUS LoadDataFile(const char *path, int control, int *addr)
  1224. {
  1225. // load spectrum sequence or sample data from a file.
  1226. // return index into spect or sample data area. bit 23=1 if a sample
  1227. FILE *f;
  1228. int id;
  1229. int hash;
  1230. int type_code = ' ';
  1231. REF_HASH_TAB *p, *p2;
  1232. char buf[sizeof(path_home)+150];
  1233. if (strcmp(path, "NULL") == 0)
  1234. return ENS_OK;
  1235. if (strcmp(path, "DFT") == 0) {
  1236. *addr = 1;
  1237. return ENS_OK;
  1238. }
  1239. count_references++;
  1240. hash = Hash8(path);
  1241. p = ref_hash_tab[hash];
  1242. while (p != NULL) {
  1243. if (strcmp(path, p->string) == 0) {
  1244. duplicate_references++;
  1245. *addr = p->value; // already loaded this data
  1246. break;
  1247. }
  1248. p = (REF_HASH_TAB *)p->link;
  1249. }
  1250. if (*addr == 0) {
  1251. sprintf(buf, "%s/%s", phsrc, path);
  1252. if ((f = fopen(buf, "rb")) == NULL) {
  1253. sprintf(buf, "%s/%s.wav", phsrc, path);
  1254. if ((f = fopen(buf, "rb")) == NULL) {
  1255. error("Can't read file: %s", path);
  1256. return static_cast<espeak_ng_STATUS> (errno);
  1257. }
  1258. }
  1259. id = Read4Bytes(f);
  1260. rewind(f);
  1261. espeak_ng_STATUS status = ENS_OK;
  1262. if (id == 0x43455053) {
  1263. status = LoadSpect(path, control, addr);
  1264. type_code = 'S';
  1265. } else if (id == 0x46464952) {
  1266. *addr = LoadWavefile(f, path);
  1267. type_code = 'W';
  1268. } else if (id == 0x43544950) {
  1269. status = LoadEnvelope(f, path, addr);
  1270. type_code = 'E';
  1271. } else if (id == 0x45564E45) {
  1272. *addr = LoadEnvelope2(f, path);
  1273. type_code = 'E';
  1274. } else {
  1275. error("File not SPEC or RIFF: %s", path);
  1276. *addr = -1;
  1277. status = ENS_UNSUPPORTED_PHON_FORMAT;
  1278. }
  1279. fclose(f);
  1280. if (status != ENS_OK)
  1281. return status;
  1282. if (*addr > 0)
  1283. fprintf(f_phcontents, "%c 0x%.5x %s\n", type_code, *addr & 0x7fffff, path);
  1284. }
  1285. // add this item to the hash table
  1286. if (*addr > 0) {
  1287. p = ref_hash_tab[hash];
  1288. p2 = (REF_HASH_TAB *)malloc(sizeof(REF_HASH_TAB)+strlen(path)+1);
  1289. if (p2 == NULL)
  1290. return static_cast<espeak_ng_STATUS> (ENOMEM);
  1291. p2->value = *addr;
  1292. p2->ph_mnemonic = phoneme_out->mnemonic; // phoneme which uses this file
  1293. p2->ph_table = n_phoneme_tabs-1;
  1294. strcpy(p2->string, path);
  1295. p2->link = (char *)p;
  1296. ref_hash_tab[hash] = p2;
  1297. }
  1298. return ENS_OK;
  1299. }
  1300. static void CompileToneSpec(void)
  1301. {
  1302. int pitch1 = 0;
  1303. int pitch2 = 0;
  1304. int pitch_env = 0;
  1305. int amp_env = 0;
  1306. pitch1 = NextItemBrackets(tNUMBER, 2);
  1307. pitch2 = NextItemBrackets(tNUMBER, 3);
  1308. if (item_terminator == ',') {
  1309. NextItemBrackets(tSTRING, 3);
  1310. LoadDataFile(item_string, 0, &pitch_env);
  1311. }
  1312. if (item_terminator == ',') {
  1313. NextItemBrackets(tSTRING, 1);
  1314. LoadDataFile(item_string, 0, &amp_env);
  1315. }
  1316. if (pitch1 < pitch2) {
  1317. phoneme_out->start_type = pitch1;
  1318. phoneme_out->end_type = pitch2;
  1319. } else {
  1320. phoneme_out->start_type = pitch2;
  1321. phoneme_out->end_type = pitch1;
  1322. }
  1323. if (pitch_env != 0) {
  1324. *prog_out++ = i_PITCHENV + ((pitch_env >> 16) & 0xf);
  1325. *prog_out++ = pitch_env;
  1326. }
  1327. if (amp_env != 0) {
  1328. *prog_out++ = i_AMPENV + ((amp_env >> 16) & 0xf);
  1329. *prog_out++ = amp_env;
  1330. }
  1331. }
  1332. static void CompileSound(int keyword, int isvowel)
  1333. {
  1334. int addr = 0;
  1335. int value = 0;
  1336. char path[N_ITEM_STRING];
  1337. static int sound_instns[] = { i_FMT, i_WAV, i_VWLSTART, i_VWLENDING, i_WAVADD };
  1338. NextItemBrackets(tSTRING, 2);
  1339. strcpy(path, item_string);
  1340. if (item_terminator == ',') {
  1341. if ((keyword == kVOWELSTART) || (keyword == kVOWELENDING)) {
  1342. value = NextItemBrackets(tSIGNEDNUMBER, 1);
  1343. if (value > 127) {
  1344. value = 127;
  1345. error("Parameter > 127");
  1346. }
  1347. if (value < -128) {
  1348. value = -128;
  1349. error("Parameter < -128");
  1350. }
  1351. } else {
  1352. value = NextItemBrackets(tNUMBER, 1);
  1353. if (value > 255) {
  1354. value = 255;
  1355. error("Parameter > 255");
  1356. }
  1357. }
  1358. }
  1359. LoadDataFile(path, isvowel, &addr);
  1360. addr = addr / 4; // addr is words not bytes
  1361. *prog_out++ = sound_instns[keyword-kFMT] + ((value & 0xff) << 4) + ((addr >> 16) & 0xf);
  1362. *prog_out++ = addr & 0xffff;
  1363. }
  1364. #endif
  1365. /*
  1366. Condition
  1367. bits 14,15 1
  1368. bit 13 1 = AND, 0 = OR
  1369. bit 12 spare
  1370. bit 8-11
  1371. =0-3 p,t,n,n2 data=phoneme code
  1372. =4-7 p,t,n,n2 data=(bits5-7: phtype, place, property, special) (bits0-4: data)
  1373. =8 data = stress bitmap
  1374. =9 special tests
  1375. */
  1376. static int CompileIf(int elif)
  1377. {
  1378. int key;
  1379. int finish = 0;
  1380. int word = 0;
  1381. int word2;
  1382. int data;
  1383. int bitmap;
  1384. int brackets;
  1385. int not_flag;
  1386. USHORT *prog_last_if = NULL;
  1387. then_count = 2;
  1388. after_if = 1;
  1389. while (!finish) {
  1390. not_flag = 0;
  1391. word2 = 0;
  1392. if (prog_out >= prog_out_max) {
  1393. error("Phoneme program too large");
  1394. return 0;
  1395. }
  1396. if ((key = NextItem(tCONDITION)) < 0)
  1397. error("Expected a condition, not '%s'", item_string);
  1398. if ((item_type == 0) && (key == k_NOT)) {
  1399. not_flag = 1;
  1400. if ((key = NextItem(tCONDITION)) < 0)
  1401. error("Expected a condition, not '%s'", item_string);
  1402. }
  1403. if (item_type == tWHICH_PHONEME) {
  1404. // prevPh(), thisPh(), nextPh(), next2Ph() etc
  1405. if (key >= 6) {
  1406. // put the 'which' code in the next instruction
  1407. word2 = key;
  1408. key = 6;
  1409. }
  1410. key = key << 8;
  1411. data = NextItemBrackets(tPROPERTIES, 0);
  1412. if (data >= 0)
  1413. word = key + data + 0x700;
  1414. else {
  1415. data = LookupPhoneme(item_string, 2);
  1416. word = key + data;
  1417. }
  1418. } else if (item_type == tTEST) {
  1419. if (key == kTHISSTRESS) {
  1420. bitmap = 0;
  1421. brackets = 2;
  1422. do {
  1423. data = NextItemBrackets(tNUMBER, brackets);
  1424. if (data > 7)
  1425. error("Expected list of stress levels");
  1426. bitmap |= (1 << data);
  1427. brackets = 3;
  1428. } while (item_terminator == ',');
  1429. word = i_StressLevel | bitmap;
  1430. } else
  1431. word = key;
  1432. } else {
  1433. error("Unexpected keyword '%s'", item_string);
  1434. if ((strcmp(item_string, "phoneme") == 0) || (strcmp(item_string, "endphoneme") == 0))
  1435. return -1;
  1436. }
  1437. // output the word
  1438. prog_last_if = prog_out;
  1439. *prog_out++ = word | i_CONDITION;
  1440. if (word2 != 0)
  1441. *prog_out++ = word2;
  1442. if (not_flag)
  1443. *prog_out++ = i_NOT;
  1444. // expect AND, OR, THEN
  1445. switch (NextItem(tCONDITION))
  1446. {
  1447. case k_AND:
  1448. break;
  1449. case k_OR:
  1450. if (prog_last_if != NULL)
  1451. *prog_last_if |= i_OR;
  1452. break;
  1453. case k_THEN:
  1454. finish = 1;
  1455. break;
  1456. default:
  1457. error("Expected AND, OR, THEN");
  1458. break;
  1459. }
  1460. }
  1461. if (elif == 0) {
  1462. if_level++;
  1463. if_stack[if_level].p_else = NULL;
  1464. }
  1465. if_stack[if_level].returned = 0;
  1466. if_stack[if_level].p_then = prog_out;
  1467. *prog_out++ = i_JUMP_FALSE;
  1468. return 0;
  1469. }
  1470. static void FillThen(int add)
  1471. {
  1472. USHORT *p;
  1473. int offset;
  1474. p = if_stack[if_level].p_then;
  1475. if (p != NULL) {
  1476. offset = prog_out - p + add;
  1477. if ((then_count == 1) && (if_level == 1)) {
  1478. // The THEN part only contains one statement, we can remove the THEN jump
  1479. // and the interpreter will implicitly skip the statement.
  1480. while (p < prog_out) {
  1481. p[0] = p[1];
  1482. p++;
  1483. }
  1484. prog_out--;
  1485. } else {
  1486. if (offset > MAX_JUMP)
  1487. error("IF block is too long");
  1488. *p = i_JUMP_FALSE + offset;
  1489. }
  1490. if_stack[if_level].p_then = NULL;
  1491. }
  1492. then_count = 0;
  1493. }
  1494. static int CompileElse(void)
  1495. {
  1496. USHORT *ref;
  1497. USHORT *p;
  1498. if (if_level < 1) {
  1499. error("ELSE not expected");
  1500. return 0;
  1501. }
  1502. if (if_stack[if_level].returned == 0)
  1503. FillThen(1);
  1504. else
  1505. FillThen(0);
  1506. if (if_stack[if_level].returned == 0) {
  1507. ref = prog_out;
  1508. *prog_out++ = 0;
  1509. if ((p = if_stack[if_level].p_else) != NULL)
  1510. *ref = ref - p; // backwards offset to the previous else
  1511. if_stack[if_level].p_else = ref;
  1512. }
  1513. return 0;
  1514. }
  1515. static int CompileElif(void)
  1516. {
  1517. if (if_level < 1) {
  1518. error("ELIF not expected");
  1519. return 0;
  1520. }
  1521. CompileElse();
  1522. CompileIf(1);
  1523. return 0;
  1524. }
  1525. static int CompileEndif(void)
  1526. {
  1527. USHORT *p;
  1528. int chain;
  1529. int offset;
  1530. if (if_level < 1) {
  1531. error("ENDIF not expected");
  1532. return 0;
  1533. }
  1534. FillThen(0);
  1535. if ((p = if_stack[if_level].p_else) != NULL) {
  1536. do {
  1537. chain = *p; // a chain of previous else links
  1538. offset = prog_out - p;
  1539. if (offset > MAX_JUMP)
  1540. error("IF block is too long");
  1541. *p = i_JUMP + offset;
  1542. p -= chain;
  1543. } while (chain > 0);
  1544. }
  1545. if_level--;
  1546. return 0;
  1547. }
  1548. static int CompileSwitch(int type)
  1549. {
  1550. // Type 0: EndSwitch
  1551. // 1: SwitchPrevVowelType
  1552. // 2: SwitchNextVowelType
  1553. if (type == 0) {
  1554. // check the instructions in the Switch
  1555. return 0;
  1556. }
  1557. if (type == 1)
  1558. *prog_out++ = i_SWITCH_PREVVOWEL+6;
  1559. if (type == 2)
  1560. *prog_out++ = i_SWITCH_NEXTVOWEL+6;
  1561. return 0;
  1562. }
  1563. static PHONEME_TAB_LIST *FindPhonemeTable(const char *string)
  1564. {
  1565. int ix;
  1566. for (ix = 0; ix < n_phoneme_tabs; ix++) {
  1567. if (strcmp(phoneme_tab_list2[ix].name, string) == 0)
  1568. return &phoneme_tab_list2[ix];
  1569. }
  1570. error("Unknown phoneme table: '%s'", string);
  1571. return NULL;
  1572. }
  1573. static PHONEME_TAB *FindPhoneme(const char *string)
  1574. {
  1575. PHONEME_TAB_LIST *phtab = NULL;
  1576. int ix;
  1577. unsigned int mnem;
  1578. char *phname;
  1579. char buf[200];
  1580. // is this the name of a phoneme which is in scope
  1581. if ((strlen(string) <= 4) && ((ix = LookupPhoneme(string, 0)) != -1))
  1582. return &phoneme_tab2[ix];
  1583. // no, treat the name as phonemetable/phoneme
  1584. strcpy(buf, string);
  1585. if ((phname = strchr(buf, '/')) != 0)
  1586. *phname++ = 0;
  1587. phtab = FindPhonemeTable(buf);
  1588. if (phtab == NULL)
  1589. return NULL; // phoneme table not found
  1590. mnem = StringToWord(phname);
  1591. for (ix = 1; ix < 256; ix++) {
  1592. if (mnem == phtab->phoneme_tab_ptr[ix].mnemonic)
  1593. return &phtab->phoneme_tab_ptr[ix];
  1594. }
  1595. error("Phoneme reference not found: '%s'", string);
  1596. return NULL;
  1597. }
  1598. static void ImportPhoneme(void)
  1599. {
  1600. unsigned int ph_mnem;
  1601. unsigned int ph_code;
  1602. PHONEME_TAB *ph;
  1603. NextItem(tSTRING);
  1604. if ((ph = FindPhoneme(item_string)) == NULL) {
  1605. error("Cannot find phoneme '%s' to import.", item_string);
  1606. return;
  1607. }
  1608. if (phoneme_out->phflags != 0 ||
  1609. phoneme_out->type != phINVALID ||
  1610. phoneme_out->start_type != 0 ||
  1611. phoneme_out->end_type != 0 ||
  1612. phoneme_out->std_length != 0 ||
  1613. phoneme_out->length_mod != 0) {
  1614. error("Phoneme import will override set properties.");
  1615. }
  1616. ph_mnem = phoneme_out->mnemonic;
  1617. ph_code = phoneme_out->code;
  1618. memcpy(phoneme_out, ph, sizeof(PHONEME_TAB));
  1619. phoneme_out->mnemonic = ph_mnem;
  1620. phoneme_out->code = ph_code;
  1621. if (phoneme_out->type != phVOWEL)
  1622. phoneme_out->end_type = 0; // voicingswitch, this must be set later to refer to a local phoneme
  1623. }
  1624. static void CallPhoneme(void)
  1625. {
  1626. PHONEME_TAB *ph;
  1627. int ix;
  1628. int addr = 0;
  1629. NextItem(tSTRING);
  1630. // first look for a procedure name
  1631. for (ix = 0; ix < n_procs; ix++) {
  1632. if (strcmp(proc_names[ix], item_string) == 0) {
  1633. addr = proc_addr[ix];
  1634. break;
  1635. }
  1636. }
  1637. if (ix == n_procs) {
  1638. // procedure not found, try a phoneme name
  1639. if ((ph = FindPhoneme(item_string)) == NULL)
  1640. return;
  1641. addr = ph->program;
  1642. if (phoneme_out->type == phINVALID) {
  1643. // Phoneme type has not been set. Copy it from the called phoneme
  1644. phoneme_out->type = ph->type;
  1645. phoneme_out->start_type = ph->start_type;
  1646. phoneme_out->end_type = ph->end_type;
  1647. phoneme_out->std_length = ph->std_length;
  1648. phoneme_out->length_mod = ph->length_mod;
  1649. phoneme_flags = ph->phflags & ~phARTICULATION;
  1650. }
  1651. }
  1652. *prog_out++ = i_CALLPH + (addr >> 16);
  1653. *prog_out++ = addr;
  1654. }
  1655. static void DecThenCount()
  1656. {
  1657. if (then_count > 0)
  1658. then_count--;
  1659. }
  1660. #if ! DATA_FROM_SOURCECODE_FILES
  1661. static int CompilePhoneme(int compile_phoneme)
  1662. {
  1663. int endphoneme = 0;
  1664. int keyword;
  1665. int value;
  1666. int phcode = 0;
  1667. int flags;
  1668. int ix;
  1669. int start;
  1670. int count;
  1671. int c;
  1672. char *p;
  1673. int vowel_length_factor = 100; // for testing
  1674. char number_buf[12];
  1675. char ipa_buf[N_ITEM_STRING+1];
  1676. PHONEME_TAB phoneme_out2;
  1677. PHONEME_PROG_LOG phoneme_prog_log;
  1678. prog_out = prog_buf;
  1679. prog_out_max = &prog_buf[MAX_PROG_BUF-1];
  1680. if_level = 0;
  1681. if_stack[0].returned = 0;
  1682. after_if = 0;
  1683. phoneme_flags = 0;
  1684. NextItem(tSTRING);
  1685. if (compile_phoneme) {
  1686. phcode = LookupPhoneme(item_string, 1); // declare phoneme if not already there
  1687. if (phcode == -1) return 0;
  1688. phoneme_out = &phoneme_tab2[phcode];
  1689. } else {
  1690. // declare a procedure
  1691. if (n_procs >= N_PROCS) {
  1692. error("Too many procedures");
  1693. return 0;
  1694. }
  1695. strcpy(proc_names[n_procs], item_string);
  1696. phoneme_out = &phoneme_out2;
  1697. sprintf(number_buf, "%.3dP", n_procs);
  1698. phoneme_out->mnemonic = StringToWord(number_buf);
  1699. }
  1700. phoneme_out->code = phcode;
  1701. phoneme_out->program = 0;
  1702. phoneme_out->type = phINVALID;
  1703. phoneme_out->std_length = 0;
  1704. phoneme_out->start_type = 0;
  1705. phoneme_out->end_type = 0;
  1706. phoneme_out->length_mod = 0;
  1707. phoneme_out->phflags = 0;
  1708. while (!endphoneme && !feof(f_in)) {
  1709. if ((keyword = NextItem(tKEYWORD)) < 0) {
  1710. if (keyword == -2) {
  1711. error("Missing 'endphoneme' before end-of-file"); // end of file
  1712. break;
  1713. }
  1714. phoneme_feature_t feature = phoneme_feature_from_string(item_string);
  1715. espeak_ng_STATUS status = phoneme_add_feature(phoneme_out, feature);
  1716. if (status == ENS_OK)
  1717. continue;
  1718. error_from_status(status, item_string);
  1719. continue;
  1720. }
  1721. switch (item_type)
  1722. {
  1723. case tPHONEME_TYPE:
  1724. if (phoneme_out->type != phINVALID) {
  1725. if (phoneme_out->type == phFRICATIVE && keyword == phLIQUID)
  1726. ; // apr liquid => ok
  1727. else
  1728. error("More than one phoneme type: %s", item_string);
  1729. }
  1730. phoneme_out->type = keyword;
  1731. break;
  1732. case tPHONEME_FLAG:
  1733. phoneme_flags |= keyword;
  1734. break;
  1735. case tINSTRN1:
  1736. // instruction group 0, with 8 bit operands which set data in PHONEME_DATA
  1737. switch (keyword)
  1738. {
  1739. case i_CHANGE_PHONEME:
  1740. case i_APPEND_PHONEME:
  1741. case i_APPEND_IFNEXTVOWEL:
  1742. case i_INSERT_PHONEME:
  1743. case i_REPLACE_NEXT_PHONEME:
  1744. case i_VOICING_SWITCH:
  1745. case i_CHANGE_IF | STRESS_IS_DIMINISHED:
  1746. case i_CHANGE_IF | STRESS_IS_UNSTRESSED:
  1747. case i_CHANGE_IF | STRESS_IS_NOT_STRESSED:
  1748. case i_CHANGE_IF | STRESS_IS_SECONDARY:
  1749. case i_CHANGE_IF | STRESS_IS_PRIMARY:
  1750. value = NextItemBrackets(tPHONEMEMNEM, 0);
  1751. *prog_out++ = (keyword << 8) + value;
  1752. DecThenCount();
  1753. break;
  1754. case i_PAUSE_BEFORE:
  1755. value = NextItemMax(255);
  1756. *prog_out++ = (i_PAUSE_BEFORE << 8) + value;
  1757. DecThenCount();
  1758. break;
  1759. case i_PAUSE_AFTER:
  1760. value = NextItemMax(255);
  1761. *prog_out++ = (i_PAUSE_AFTER << 8) + value;
  1762. DecThenCount();
  1763. break;
  1764. case i_SET_LENGTH:
  1765. value = NextItemMax(511);
  1766. if (phoneme_out->type == phVOWEL)
  1767. value = (value * vowel_length_factor)/100;
  1768. if (after_if == 0)
  1769. phoneme_out->std_length = value/2;
  1770. else {
  1771. *prog_out++ = (i_SET_LENGTH << 8) + value/2;
  1772. DecThenCount();
  1773. }
  1774. break;
  1775. case i_ADD_LENGTH:
  1776. value = NextItem(tSIGNEDNUMBER) / 2;
  1777. *prog_out++ = (i_ADD_LENGTH << 8) + (value & 0xff);
  1778. DecThenCount();
  1779. break;
  1780. case i_LENGTH_MOD:
  1781. value = NextItem(tNUMBER);
  1782. phoneme_out->length_mod = value;
  1783. break;
  1784. case i_IPA_NAME:
  1785. NextItem(tSTRING);
  1786. if (strcmp(item_string, "NULL") == 0)
  1787. strcpy(item_string, " ");
  1788. // copy the string, recognize characters in the form U+9999
  1789. flags = 0;
  1790. count = 0;
  1791. ix = 1;
  1792. for (p = item_string; *p != 0;) {
  1793. p += utf8_in(&c, p);
  1794. if ((c == '|') && (count > 0)) {
  1795. // '|' means don't allow a tie or joiner before this letter
  1796. flags |= (1 << (count -1));
  1797. } else if ((c == 'U') && (p[0] == '+')) {
  1798. int j;
  1799. // U+9999
  1800. p++;
  1801. memcpy(number_buf, p, 4); // U+ should be followed by 4 hex digits
  1802. number_buf[4] = 0;
  1803. c = '#';
  1804. sscanf(number_buf, "%x", (unsigned int *)&c);
  1805. // move past the 4 hexdecimal digits
  1806. for (j = 0; j < 4; j++) {
  1807. if (!isalnum(*p))
  1808. break;
  1809. p++;
  1810. }
  1811. ix += utf8_out(c, &ipa_buf[ix]);
  1812. count++;
  1813. } else {
  1814. ix += utf8_out(c, &ipa_buf[ix]);
  1815. count++;
  1816. }
  1817. }
  1818. ipa_buf[0] = flags;
  1819. ipa_buf[ix] = 0;
  1820. start = 1;
  1821. if (flags != 0)
  1822. start = 0; // only include the flags byte if bits are set
  1823. value = strlen(&ipa_buf[start]); // number of UTF-8 bytes
  1824. *prog_out++ = (i_IPA_NAME << 8) + value;
  1825. for (ix = 0; ix < value; ix += 2)
  1826. *prog_out++ = (ipa_buf[ix+start] << 8) + (ipa_buf[ix+start+1] & 0xff);
  1827. DecThenCount();
  1828. break;
  1829. }
  1830. break;
  1831. case tSTATEMENT:
  1832. switch (keyword)
  1833. {
  1834. case kIMPORT_PH:
  1835. ImportPhoneme();
  1836. phoneme_flags = phoneme_out->phflags;
  1837. break;
  1838. case kSTARTTYPE:
  1839. phcode = NextItem(tPHONEMEMNEM);
  1840. if (phcode == -1)
  1841. phcode = LookupPhoneme(item_string, 1);
  1842. phoneme_out->start_type = phcode;
  1843. if (phoneme_out->type == phINVALID)
  1844. error("a phoneme type or manner of articulation should be specified before starttype");
  1845. break;
  1846. case kENDTYPE:
  1847. phcode = NextItem(tPHONEMEMNEM);
  1848. if (phcode == -1)
  1849. phcode = LookupPhoneme(item_string, 1);
  1850. if (phoneme_out->type == phINVALID)
  1851. error("a phoneme type or manner of articulation should be specified before endtype");
  1852. else if (phoneme_out->type == phVOWEL)
  1853. phoneme_out->end_type = phcode;
  1854. else if (phcode != phoneme_out->start_type)
  1855. error("endtype must equal starttype for consonants");
  1856. break;
  1857. case kVOICINGSWITCH:
  1858. phcode = NextItem(tPHONEMEMNEM);
  1859. if (phcode == -1)
  1860. phcode = LookupPhoneme(item_string, 1);
  1861. if (phoneme_out->type == phVOWEL)
  1862. error("voicingswitch cannot be used on vowels");
  1863. else
  1864. phoneme_out->end_type = phcode; // use end_type field for consonants as voicing_switch
  1865. break;
  1866. case kSTRESSTYPE:
  1867. value = NextItem(tNUMBER);
  1868. phoneme_out->std_length = value;
  1869. if (prog_out > prog_buf) {
  1870. error("stress phonemes can't contain program instructions");
  1871. prog_out = prog_buf;
  1872. }
  1873. break;
  1874. case kIF:
  1875. endphoneme = CompileIf(0);
  1876. break;
  1877. case kELSE:
  1878. endphoneme = CompileElse();
  1879. break;
  1880. case kELIF:
  1881. endphoneme = CompileElif();
  1882. break;
  1883. case kENDIF:
  1884. endphoneme = CompileEndif();
  1885. break;
  1886. case kENDSWITCH:
  1887. break;
  1888. case kSWITCH_PREVVOWEL:
  1889. endphoneme = CompileSwitch(1);
  1890. break;
  1891. case kSWITCH_NEXTVOWEL:
  1892. endphoneme = CompileSwitch(2);
  1893. break;
  1894. case kCALLPH:
  1895. CallPhoneme();
  1896. DecThenCount();
  1897. break;
  1898. case kFMT:
  1899. if_stack[if_level].returned = 1;
  1900. DecThenCount();
  1901. if (phoneme_out->type == phVOWEL)
  1902. CompileSound(keyword, 1);
  1903. else
  1904. CompileSound(keyword, 0);
  1905. break;
  1906. case kWAV:
  1907. if_stack[if_level].returned = 1;
  1908. // fallthrough:
  1909. case kVOWELSTART:
  1910. case kVOWELENDING:
  1911. case kANDWAV:
  1912. DecThenCount();
  1913. CompileSound(keyword, 0);
  1914. break;
  1915. case kVOWELIN:
  1916. DecThenCount();
  1917. endphoneme = CompileVowelTransition(1);
  1918. break;
  1919. case kVOWELOUT:
  1920. DecThenCount();
  1921. endphoneme = CompileVowelTransition(2);
  1922. break;
  1923. case kTONESPEC:
  1924. DecThenCount();
  1925. CompileToneSpec();
  1926. break;
  1927. case kCONTINUE:
  1928. *prog_out++ = INSTN_CONTINUE;
  1929. DecThenCount();
  1930. break;
  1931. case kRETURN:
  1932. *prog_out++ = INSTN_RETURN;
  1933. DecThenCount();
  1934. break;
  1935. case kINCLUDE:
  1936. case kPHONEMETABLE:
  1937. error("Missing 'endphoneme' before '%s'", item_string); // drop through to endphoneme
  1938. // fallthrough:
  1939. case kENDPHONEME:
  1940. case kENDPROCEDURE:
  1941. endphoneme = 1;
  1942. if (if_level > 0)
  1943. error("Missing ENDIF");
  1944. if ((prog_out > prog_buf) && (if_stack[0].returned == 0))
  1945. *prog_out++ = INSTN_RETURN;
  1946. break;
  1947. }
  1948. break;
  1949. }
  1950. }
  1951. if (endphoneme != 1)
  1952. error("'endphoneme' not expected here");
  1953. if (compile_phoneme) {
  1954. if (phoneme_out->type == phINVALID) {
  1955. error("Phoneme type is missing");
  1956. phoneme_out->type = 0;
  1957. }
  1958. phoneme_out->phflags |= phoneme_flags;
  1959. if (phoneme_out->phflags & phVOICED) {
  1960. if (phoneme_out->type == phSTOP)
  1961. phoneme_out->type = phVSTOP;
  1962. else if (phoneme_out->type == phFRICATIVE)
  1963. phoneme_out->type = phVFRICATIVE;
  1964. }
  1965. if (phoneme_out->std_length == 0) {
  1966. if (phoneme_out->type == phVOWEL)
  1967. phoneme_out->std_length = 180/2; // default length for vowel
  1968. }
  1969. phoneme_out->phflags |= phLOCAL; // declared in this phoneme table
  1970. if (phoneme_out->type == phDELETED)
  1971. phoneme_out->mnemonic = 0x01; // will not be recognised
  1972. }
  1973. if (prog_out > prog_buf) {
  1974. // write out the program for this phoneme
  1975. fflush(f_phindex);
  1976. phoneme_out->program = ftell(f_phindex) / sizeof(USHORT);
  1977. if (f_prog_log != NULL) {
  1978. phoneme_prog_log.addr = phoneme_out->program;
  1979. phoneme_prog_log.length = prog_out - prog_buf;
  1980. fwrite(&phoneme_prog_log, 1, sizeof(phoneme_prog_log), f_prog_log);
  1981. }
  1982. if (compile_phoneme == 0)
  1983. proc_addr[n_procs++] = ftell(f_phindex) / sizeof(USHORT);
  1984. fwrite(prog_buf, sizeof(USHORT), prog_out - prog_buf, f_phindex);
  1985. }
  1986. return 0;
  1987. }
  1988. static void WritePhonemeTables()
  1989. {
  1990. int ix;
  1991. int j;
  1992. int n;
  1993. int value;
  1994. int count;
  1995. PHONEME_TAB *p;
  1996. value = n_phoneme_tabs;
  1997. fputc(value, f_phtab);
  1998. fputc(0, f_phtab);
  1999. fputc(0, f_phtab);
  2000. fputc(0, f_phtab);
  2001. for (ix = 0; ix < n_phoneme_tabs; ix++) {
  2002. p = phoneme_tab_list2[ix].phoneme_tab_ptr;
  2003. n = n_phcodes_list[ix];
  2004. memset(&p[n], 0, sizeof(p[n]));
  2005. p[n].mnemonic = 0; // terminate the phoneme table
  2006. // count number of locally declared phonemes
  2007. count = 0;
  2008. for (j = 0; j < n; j++) {
  2009. if (ix == 0)
  2010. p[j].phflags |= phLOCAL; // write all phonemes in the base phoneme table
  2011. if (p[j].phflags & phLOCAL)
  2012. count++;
  2013. }
  2014. phoneme_tab_list2[ix].n_phonemes = count+1;
  2015. fputc(count+1, f_phtab);
  2016. fputc(phoneme_tab_list2[ix].includes, f_phtab);
  2017. fputc(0, f_phtab);
  2018. fputc(0, f_phtab);
  2019. fwrite(phoneme_tab_list2[ix].name, 1, N_PHONEME_TAB_NAME, f_phtab);
  2020. for (j = 0; j < n; j++) {
  2021. if (p[j].phflags & phLOCAL) {
  2022. // this bit is set temporarily to incidate a local phoneme, declared in
  2023. // in the current phoneme file
  2024. p[j].phflags &= ~phLOCAL;
  2025. fwrite(&p[j], sizeof(PHONEME_TAB), 1, f_phtab);
  2026. }
  2027. }
  2028. fwrite(&p[n], sizeof(PHONEME_TAB), 1, f_phtab); // include the extra list-terminator phoneme entry
  2029. free(p);
  2030. }
  2031. }
  2032. static void EndPhonemeTable()
  2033. {
  2034. int ix;
  2035. if (n_phoneme_tabs == 0)
  2036. return;
  2037. // check that all referenced phonemes have been declared
  2038. for (ix = 0; ix < n_phcodes; ix++) {
  2039. if (phoneme_tab2[ix].type == phINVALID) {
  2040. error("Phoneme [%s] not declared, referenced at line %d",
  2041. WordToString(phoneme_tab2[ix].mnemonic), (int)(phoneme_tab2[ix].program));
  2042. error_count++;
  2043. phoneme_tab2[ix].type = 0; // prevent the error message repeating
  2044. }
  2045. }
  2046. n_phcodes_list[n_phoneme_tabs-1] = n_phcodes;
  2047. }
  2048. static void StartPhonemeTable(const char *name)
  2049. {
  2050. int ix;
  2051. int j;
  2052. PHONEME_TAB *p;
  2053. if (n_phoneme_tabs >= N_PHONEME_TABS-1) {
  2054. error("Too many phonemetables");
  2055. return;
  2056. }
  2057. p = (PHONEME_TAB *)calloc(sizeof(PHONEME_TAB), N_PHONEME_TAB);
  2058. if (p == NULL) {
  2059. error("Out of memory");
  2060. return;
  2061. }
  2062. memset(&phoneme_tab_list2[n_phoneme_tabs], 0, sizeof(PHONEME_TAB_LIST));
  2063. phoneme_tab_list2[n_phoneme_tabs].phoneme_tab_ptr = phoneme_tab2 = p;
  2064. memset(phoneme_tab_list2[n_phoneme_tabs].name, 0, sizeof(phoneme_tab_list2[n_phoneme_tabs].name));
  2065. strncpy0(phoneme_tab_list2[n_phoneme_tabs].name, name, N_PHONEME_TAB_NAME);
  2066. n_phcodes = 1;
  2067. phoneme_tab_list2[n_phoneme_tabs].includes = 0;
  2068. if (n_phoneme_tabs > 0) {
  2069. NextItem(tSTRING); // name of base phoneme table
  2070. for (ix = 0; ix < n_phoneme_tabs; ix++) {
  2071. if (strcmp(item_string, phoneme_tab_list2[ix].name) == 0) {
  2072. phoneme_tab_list2[n_phoneme_tabs].includes = ix+1;
  2073. // initialise the new phoneme table with the contents of this one
  2074. memcpy(phoneme_tab2, phoneme_tab_list2[ix].phoneme_tab_ptr, sizeof(PHONEME_TAB)*N_PHONEME_TAB);
  2075. n_phcodes = n_phcodes_list[ix];
  2076. // clear "local phoneme" bit"
  2077. for (j = 0; j < n_phcodes; j++)
  2078. phoneme_tab2[j].phflags &= ~phLOCAL;
  2079. break;
  2080. }
  2081. }
  2082. if (ix == n_phoneme_tabs && strcmp(item_string, "_") != 0)
  2083. error("Can't find base phonemetable '%s'", item_string);
  2084. } else
  2085. ReservePhCodes();
  2086. n_phoneme_tabs++;
  2087. }
  2088. static void CompilePhonemeFiles()
  2089. {
  2090. int item;
  2091. FILE *f;
  2092. char buf[sizeof(path_home)+120];
  2093. linenum = 1;
  2094. count_references = 0;
  2095. duplicate_references = 0;
  2096. count_frames = 0;
  2097. n_procs = 0;
  2098. for (;;) {
  2099. if (feof(f_in)) {
  2100. // end of file, go back to previous from, from which this was included
  2101. if (stack_ix == 0)
  2102. break; // end of top level, finished
  2103. fclose(f_in);
  2104. f_in = stack[--stack_ix].file;
  2105. strcpy(current_fname, stack[stack_ix].fname);
  2106. linenum = stack[stack_ix].linenum;
  2107. }
  2108. item = NextItem(tKEYWORD);
  2109. switch (item)
  2110. {
  2111. case kUTF8_BOM:
  2112. break; // ignore bytes 0xef 0xbb 0xbf
  2113. case kINCLUDE:
  2114. NextItem(tSTRING);
  2115. sprintf(buf, "%s/%s", phsrc, item_string);
  2116. if ((stack_ix < N_STACK) && (f = fopen(buf, "rb")) != NULL) {
  2117. stack[stack_ix].linenum = linenum;
  2118. strcpy(stack[stack_ix].fname, current_fname);
  2119. stack[stack_ix++].file = f_in;
  2120. f_in = f;
  2121. strncpy0(current_fname, item_string, sizeof(current_fname));
  2122. linenum = 1;
  2123. } else
  2124. error("Missing file: %s", item_string);
  2125. break;
  2126. case kPHONEMETABLE:
  2127. EndPhonemeTable();
  2128. NextItem(tSTRING); // name of the new phoneme table
  2129. StartPhonemeTable(item_string);
  2130. break;
  2131. case kPHONEMESTART:
  2132. if (n_phoneme_tabs == 0) {
  2133. error("phonemetable is missing");
  2134. return;
  2135. }
  2136. CompilePhoneme(1);
  2137. break;
  2138. case kPROCEDURE:
  2139. CompilePhoneme(0);
  2140. break;
  2141. default:
  2142. if (!feof(f_in))
  2143. error("Keyword 'phoneme' expected");
  2144. break;
  2145. }
  2146. }
  2147. memset(&phoneme_tab2[n_phcodes+1], 0, sizeof(phoneme_tab2[n_phcodes+1]));
  2148. phoneme_tab2[n_phcodes+1].mnemonic = 0; // terminator
  2149. }
  2150. #pragma GCC visibility push(default)
  2151. espeak_ng_STATUS
  2152. espeak_ng_CompilePhonemeData(long rate,
  2153. FILE *log,
  2154. espeak_ng_ERROR_CONTEXT *context)
  2155. {
  2156. return espeak_ng_CompilePhonemeDataPath(rate, NULL, NULL, log, context);
  2157. }
  2158. espeak_ng_STATUS
  2159. espeak_ng_CompilePhonemeDataPath(long rate,
  2160. const char *source_path,
  2161. const char *destination_path,
  2162. FILE *log,
  2163. espeak_ng_ERROR_CONTEXT *context)
  2164. {
  2165. if (!log) log = stderr;
  2166. char fname[sizeof(path_home)+40];
  2167. char phdst[sizeof(path_home)+40]; // Destination: path to the phondata/phontab/phonindex output files.
  2168. if (source_path) {
  2169. sprintf(phsrc, "%s", source_path);
  2170. } else {
  2171. sprintf(phsrc, "%s/../phsource", path_home);
  2172. }
  2173. if (destination_path) {
  2174. sprintf(phdst, "%s", destination_path);
  2175. } else {
  2176. sprintf(phdst, "%s", path_home);
  2177. }
  2178. samplerate_native = samplerate = rate;
  2179. LoadPhData(NULL, NULL);
  2180. if (LoadVoice("", 0) == NULL)
  2181. return ENS_VOICE_NOT_FOUND;
  2182. WavegenInit(rate, 0);
  2183. WavegenSetVoice(voice);
  2184. n_envelopes = 0;
  2185. error_count = 0;
  2186. resample_count = 0;
  2187. memset(markers_used, 0, sizeof(markers_used));
  2188. f_errors = log;
  2189. strncpy0(current_fname, "phonemes", sizeof(current_fname));
  2190. sprintf(fname, "%s/phonemes", phsrc);
  2191. fprintf(log, "Compiling phoneme data: %s\n", fname);
  2192. f_in = fopen(fname, "rb");
  2193. if (f_in == NULL)
  2194. return create_file_error_context(context, static_cast<espeak_ng_STATUS> (errno), fname);
  2195. sprintf(fname, "%s/%s", phsrc, "compile_report");
  2196. f_report = fopen(fname, "w");
  2197. if (f_report == NULL) {
  2198. int error = errno;
  2199. fclose(f_in);
  2200. return create_file_error_context(context, static_cast<espeak_ng_STATUS> (error), fname);
  2201. }
  2202. sprintf(fname, "%s/%s", phdst, "phondata-manifest");
  2203. if ((f_phcontents = fopen(fname, "w")) == NULL)
  2204. f_phcontents = stderr;
  2205. fprintf(f_phcontents,
  2206. "# This file lists the type of data that has been compiled into the\n"
  2207. "# phondata file\n"
  2208. "#\n"
  2209. "# The first character of a line indicates the type of data:\n"
  2210. "# S - A SPECT_SEQ structure\n"
  2211. "# W - A wavefile segment\n"
  2212. "# E - An envelope\n"
  2213. "#\n"
  2214. "# Address is the displacement within phondata of this item\n"
  2215. "#\n"
  2216. "# Address Data file\n"
  2217. "# ------- ---------\n");
  2218. sprintf(fname, "%s/%s", phdst, "phondata");
  2219. f_phdata = fopen(fname, "wb");
  2220. if (f_phdata == NULL) {
  2221. int error = errno;
  2222. fclose(f_in);
  2223. fclose(f_report);
  2224. fclose(f_phcontents);
  2225. return create_file_error_context(context, static_cast<espeak_ng_STATUS> (error), fname);
  2226. }
  2227. sprintf(fname, "%s/%s", phdst, "phonindex");
  2228. f_phindex = fopen(fname, "wb");
  2229. if (f_phindex == NULL) {
  2230. int error = errno;
  2231. fclose(f_in);
  2232. fclose(f_report);
  2233. fclose(f_phcontents);
  2234. fclose(f_phdata);
  2235. return create_file_error_context(context, static_cast<espeak_ng_STATUS> (error), fname);
  2236. }
  2237. sprintf(fname, "%s/%s", phdst, "phontab");
  2238. f_phtab = fopen(fname, "wb");
  2239. if (f_phtab == NULL) {
  2240. int error = errno;
  2241. fclose(f_in);
  2242. fclose(f_report);
  2243. fclose(f_phcontents);
  2244. fclose(f_phdata);
  2245. fclose(f_phindex);
  2246. return create_file_error_context(context, static_cast<espeak_ng_STATUS> (error), fname);
  2247. }
  2248. sprintf(fname, "%s/compile_prog_log", phsrc);
  2249. f_prog_log = fopen(fname, "wb");
  2250. // write a word so that further data doesn't start at displ=0
  2251. Write4Bytes(f_phdata, version_phdata);
  2252. Write4Bytes(f_phdata, samplerate_native);
  2253. Write4Bytes(f_phindex, version_phdata);
  2254. memset(ref_hash_tab, 0, sizeof(ref_hash_tab));
  2255. n_phoneme_tabs = 0;
  2256. stack_ix = 0;
  2257. StartPhonemeTable("base");
  2258. CompilePhonemeFiles();
  2259. EndPhonemeTable();
  2260. WritePhonemeTables();
  2261. fprintf(f_errors, "\nRefs %d, Reused %d\n", count_references, duplicate_references);
  2262. fclose(f_in);
  2263. fclose(f_phcontents);
  2264. fclose(f_phdata);
  2265. fclose(f_phindex);
  2266. fclose(f_phtab);
  2267. if (f_prog_log != NULL)
  2268. fclose(f_prog_log);
  2269. LoadPhData(NULL, NULL);
  2270. CompileReport();
  2271. fclose(f_report);
  2272. if (resample_count > 0) {
  2273. fprintf(f_errors, "\n%d WAV files resampled to %d Hz\n", resample_count, samplerate_native);
  2274. fprintf(log, "Compiled phonemes: %d errors, %d files resampled to %d Hz.\n", error_count, resample_count, samplerate_native);
  2275. } else
  2276. fprintf(log, "Compiled phonemes: %d errors.\n", error_count);
  2277. if (f_errors != stderr && f_errors != stdout)
  2278. fclose(f_errors);
  2279. espeak_ng_STATUS status = ReadPhondataManifest(context);
  2280. if (status != ENS_OK)
  2281. return status;
  2282. return error_count > 0 ? ENS_COMPILE_ERROR : ENS_OK;
  2283. }
  2284. #pragma GCC visibility pop
  2285. static const char *preset_tune_names[] = {
  2286. "s1", "c1", "q1", "e1", NULL
  2287. };
  2288. static const TUNE default_tune = {
  2289. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2290. { 0, 0, 0, 0 },
  2291. { 0, 40, 24, 8, 0, 0, 0, 0 },
  2292. 46, 57, PITCHfall, 16, 0, 0,
  2293. 255, 78, 50, 255,
  2294. 3, 5,
  2295. { -7, -7, -7 }, { -7, -7, -7 },
  2296. PITCHfall, 64, 8,
  2297. PITCHfall, 70, 18, 24, 12,
  2298. PITCHfall, 70, 18, 24, 12, 0,
  2299. { 0, 0, 0, 0, 0, 0, 0, 0 }, 0
  2300. };
  2301. #define N_TUNE_NAMES 100
  2302. MNEM_TAB envelope_names[] = {
  2303. { "fall", 0 },
  2304. { "rise", 2 },
  2305. { "fall-rise", 4 },
  2306. { "fall-rise2", 6 },
  2307. { "rise-fall", 8 },
  2308. { "fall-rise3", 10 },
  2309. { "fall-rise4", 12 },
  2310. { "fall2", 14 },
  2311. { "rise2", 16 },
  2312. { "rise-fall-rise", 18 },
  2313. { NULL, -1 }
  2314. };
  2315. static int LookupEnvelopeName(const char *name)
  2316. {
  2317. return LookupMnem(envelope_names, name);
  2318. }
  2319. #pragma GCC visibility push(default)
  2320. espeak_ng_STATUS espeak_ng_CompileIntonation(FILE *log, espeak_ng_ERROR_CONTEXT *context)
  2321. {
  2322. if (!log) log = stderr;
  2323. int ix;
  2324. char *p;
  2325. char c;
  2326. int keyword;
  2327. int n_tune_names = 0;
  2328. int done_split = 0;
  2329. int done_onset = 0;
  2330. int done_last = 0;
  2331. int n_preset_tunes = 0;
  2332. int found = 0;
  2333. int tune_number = 0;
  2334. FILE *f_out;
  2335. TUNE *tune_data;
  2336. TUNE new_tune;
  2337. char name[12];
  2338. char tune_names[N_TUNE_NAMES][12];
  2339. char buf[sizeof(path_home)+150];
  2340. error_count = 0;
  2341. f_errors = log;
  2342. sprintf(buf, "%s/../phsource/intonation.txt", path_home);
  2343. if ((f_in = fopen(buf, "r")) == NULL) {
  2344. sprintf(buf, "%s/../phsource/intonation", path_home);
  2345. if ((f_in = fopen(buf, "r")) == NULL) {
  2346. int error = errno;
  2347. fclose(f_errors);
  2348. return create_file_error_context(context, static_cast<espeak_ng_STATUS> (error), buf);
  2349. }
  2350. }
  2351. for (ix = 0; preset_tune_names[ix] != NULL; ix++)
  2352. strcpy(tune_names[ix], preset_tune_names[ix]);
  2353. n_tune_names = ix;
  2354. n_preset_tunes = ix;
  2355. // make a list of the tune names
  2356. while (!feof(f_in)) {
  2357. if (fgets(buf, sizeof(buf), f_in) == NULL)
  2358. break;
  2359. if ((memcmp(buf, "tune", 4) == 0) && isspace(buf[4])) {
  2360. p = &buf[5];
  2361. while (isspace(*p)) p++;
  2362. ix = 0;
  2363. while ((ix < (int)(sizeof(name) - 1)) && !isspace(*p))
  2364. name[ix++] = *p++;
  2365. name[ix] = 0;
  2366. found = 0;
  2367. for (ix = 0; ix < n_tune_names; ix++) {
  2368. if (strcmp(name, tune_names[ix]) == 0) {
  2369. found = 1;
  2370. break;
  2371. }
  2372. }
  2373. if (found == 0) {
  2374. strncpy0(tune_names[n_tune_names++], name, sizeof(name));
  2375. if (n_tune_names >= N_TUNE_NAMES)
  2376. break;
  2377. }
  2378. }
  2379. }
  2380. rewind(f_in);
  2381. linenum = 1;
  2382. tune_data = (n_tune_names == 0) ? NULL : (TUNE *)calloc(n_tune_names, sizeof(TUNE));
  2383. if (tune_data == NULL) {
  2384. fclose(f_in);
  2385. fclose(f_errors);
  2386. return static_cast<espeak_ng_STATUS> (ENOMEM);
  2387. }
  2388. sprintf(buf, "%s/intonations", path_home);
  2389. f_out = fopen(buf, "wb");
  2390. if (f_out == NULL) {
  2391. int error = errno;
  2392. fclose(f_in);
  2393. fclose(f_errors);
  2394. free(tune_data);
  2395. return create_file_error_context(context, static_cast<espeak_ng_STATUS> (error), buf);
  2396. }
  2397. while (!feof(f_in)) {
  2398. keyword = NextItem(tINTONATION);
  2399. switch (keyword)
  2400. {
  2401. case kTUNE:
  2402. done_split = 0;
  2403. memcpy(&new_tune, &default_tune, sizeof(TUNE));
  2404. NextItem(tSTRING);
  2405. strncpy0(new_tune.name, item_string, sizeof(new_tune.name));
  2406. found = 0;
  2407. tune_number = 0;
  2408. for (ix = 0; ix < n_tune_names; ix++) {
  2409. if (strcmp(new_tune.name, tune_names[ix]) == 0) {
  2410. found = 1;
  2411. tune_number = ix;
  2412. if (tune_data[ix].name[0] != 0)
  2413. found = 2;
  2414. break;
  2415. }
  2416. }
  2417. if (found == 2)
  2418. error("Duplicate tune name: '%s'", new_tune.name);
  2419. if (found == 0)
  2420. error("Bad tune name: '%s;", new_tune.name);
  2421. break;
  2422. case kENDTUNE:
  2423. if (!found) continue;
  2424. if (done_onset == 0) {
  2425. new_tune.unstr_start[0] = new_tune.unstr_start[1];
  2426. new_tune.unstr_end[0] = new_tune.unstr_end[1];
  2427. }
  2428. if (done_last == 0) {
  2429. new_tune.unstr_start[2] = new_tune.unstr_start[1];
  2430. new_tune.unstr_end[2] = new_tune.unstr_end[1];
  2431. }
  2432. memcpy(&tune_data[tune_number], &new_tune, sizeof(TUNE));
  2433. break;
  2434. case kTUNE_PREHEAD:
  2435. new_tune.prehead_start = NextItem(tNUMBER);
  2436. new_tune.prehead_end = NextItem(tNUMBER);
  2437. break;
  2438. case kTUNE_ONSET:
  2439. new_tune.onset = NextItem(tNUMBER);
  2440. new_tune.unstr_start[0] = NextItem(tSIGNEDNUMBER);
  2441. new_tune.unstr_end[0] = NextItem(tSIGNEDNUMBER);
  2442. done_onset = 1;
  2443. break;
  2444. case kTUNE_HEADLAST:
  2445. new_tune.head_last = NextItem(tNUMBER);
  2446. new_tune.unstr_start[2] = NextItem(tSIGNEDNUMBER);
  2447. new_tune.unstr_end[2] = NextItem(tSIGNEDNUMBER);
  2448. done_last = 1;
  2449. break;
  2450. case kTUNE_HEADENV:
  2451. NextItem(tSTRING);
  2452. if ((ix = LookupEnvelopeName(item_string)) < 0)
  2453. error("Bad envelope name: '%s'", item_string);
  2454. else
  2455. new_tune.stressed_env = ix;
  2456. new_tune.stressed_drop = NextItem(tNUMBER);
  2457. break;
  2458. case kTUNE_HEAD:
  2459. new_tune.head_max_steps = NextItem(tNUMBER);
  2460. new_tune.head_start = NextItem(tNUMBER);
  2461. new_tune.head_end = NextItem(tNUMBER);
  2462. new_tune.unstr_start[1] = NextItem(tSIGNEDNUMBER);
  2463. new_tune.unstr_end[1] = NextItem(tSIGNEDNUMBER);
  2464. break;
  2465. case kTUNE_HEADEXTEND:
  2466. // up to 8 numbers
  2467. for (ix = 0; ix < (int)(sizeof(new_tune.head_extend)); ix++) {
  2468. if (!isdigit(c = CheckNextChar()) && (c != '-'))
  2469. break;
  2470. new_tune.head_extend[ix] = (NextItem(tSIGNEDNUMBER) * 64) / 100; // convert from percentage to 64ths
  2471. }
  2472. new_tune.n_head_extend = ix; // number of values
  2473. break;
  2474. case kTUNE_NUCLEUS0:
  2475. NextItem(tSTRING);
  2476. if ((ix = LookupEnvelopeName(item_string)) < 0) {
  2477. error("Bad envelope name: '%s'", item_string);
  2478. break;
  2479. }
  2480. new_tune.nucleus0_env = ix;
  2481. new_tune.nucleus0_max = NextItem(tNUMBER);
  2482. new_tune.nucleus0_min = NextItem(tNUMBER);
  2483. break;
  2484. case kTUNE_NUCLEUS1:
  2485. NextItem(tSTRING);
  2486. if ((ix = LookupEnvelopeName(item_string)) < 0) {
  2487. error("Bad envelope name: '%s'", item_string);
  2488. break;
  2489. }
  2490. new_tune.nucleus1_env = ix;
  2491. new_tune.nucleus1_max = NextItem(tNUMBER);
  2492. new_tune.nucleus1_min = NextItem(tNUMBER);
  2493. new_tune.tail_start = NextItem(tNUMBER);
  2494. new_tune.tail_end = NextItem(tNUMBER);
  2495. if (!done_split) {
  2496. // also this as the default setting for 'split'
  2497. new_tune.split_nucleus_env = ix;
  2498. new_tune.split_nucleus_max = new_tune.nucleus1_max;
  2499. new_tune.split_nucleus_min = new_tune.nucleus1_min;
  2500. new_tune.split_tail_start = new_tune.tail_start;
  2501. new_tune.split_tail_end = new_tune.tail_end;
  2502. }
  2503. break;
  2504. case kTUNE_SPLIT:
  2505. NextItem(tSTRING);
  2506. if ((ix = LookupEnvelopeName(item_string)) < 0) {
  2507. error("Bad envelope name: '%s'", item_string);
  2508. break;
  2509. }
  2510. done_split = 1;
  2511. new_tune.split_nucleus_env = ix;
  2512. new_tune.split_nucleus_max = NextItem(tNUMBER);
  2513. new_tune.split_nucleus_min = NextItem(tNUMBER);
  2514. new_tune.split_tail_start = NextItem(tNUMBER);
  2515. new_tune.split_tail_end = NextItem(tNUMBER);
  2516. NextItem(tSTRING);
  2517. item_string[12] = 0;
  2518. for (ix = 0; ix < n_tune_names; ix++) {
  2519. if (strcmp(item_string, tune_names[ix]) == 0)
  2520. break;
  2521. }
  2522. if (ix == n_tune_names)
  2523. error("Tune '%s' not found", item_string);
  2524. else
  2525. new_tune.split_tune = ix;
  2526. break;
  2527. default:
  2528. error("Unexpected: '%s'", item_string);
  2529. break;
  2530. }
  2531. }
  2532. for (ix = 0; ix < n_preset_tunes; ix++) {
  2533. if (tune_data[ix].name[0] == 0)
  2534. error("Tune '%s' not defined", preset_tune_names[ix]);
  2535. }
  2536. fwrite(tune_data, n_tune_names, sizeof(TUNE), f_out);
  2537. free(tune_data);
  2538. fclose(f_in);
  2539. fclose(f_out);
  2540. fprintf(log, "Compiled %d intonation tunes: %d errors.\n", n_tune_names, error_count);
  2541. LoadPhData(NULL, NULL);
  2542. return error_count > 0 ? ENS_COMPILE_ERROR : ENS_OK;
  2543. }
  2544. #pragma GCC visibility pop
  2545. #endif