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

compiledata.c 68KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952
  1. /*
  2. * Copyright (C) 2005 to 2014 by Jonathan Duddington
  3. * email: [email protected]
  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/espeak_ng.h>
  32. #include <espeak-ng/speak_lib.h>
  33. #include "error.h"
  34. #include "speech.h"
  35. #include "phoneme.h"
  36. #include "synthesize.h"
  37. #include "voice.h"
  38. #include "spect.h"
  39. typedef struct {
  40. unsigned int value;
  41. char *name;
  42. } NAMETAB;
  43. NAMETAB *manifest = NULL;
  44. int n_manifest;
  45. char phsrc[sizeof(path_home)+40]; // Source: path to the 'phonemes' source file.
  46. extern ESPEAK_NG_API int utf8_in(int *c, const char *buf);
  47. extern int utf8_out(unsigned int c, char *buf);
  48. typedef struct {
  49. const char *mnem;
  50. int type;
  51. int data;
  52. } keywtab_t;
  53. #define k_AND 1
  54. #define k_OR 2
  55. #define k_THEN 3
  56. #define k_NOT 4
  57. #define kTHISSTRESS 0x800
  58. // keyword types
  59. enum {
  60. tPHONEME_TYPE = 1,
  61. tPHONEME_FLAG,
  62. tTRANSITION,
  63. tSTATEMENT,
  64. tINSTRN1,
  65. tWHICH_PHONEME,
  66. tTEST,
  67. };
  68. static keywtab_t k_conditions[] = {
  69. { "AND", 0, k_AND },
  70. { "OR", 0, k_OR },
  71. { "THEN", 0, k_THEN },
  72. { "NOT", 0, k_NOT },
  73. { "prevPh", tWHICH_PHONEME, 0 },
  74. { "thisPh", tWHICH_PHONEME, 1 },
  75. { "nextPh", tWHICH_PHONEME, 2 },
  76. { "next2Ph", tWHICH_PHONEME, 3 },
  77. { "nextPhW", tWHICH_PHONEME, 4 },
  78. { "prevPhW", tWHICH_PHONEME, 5 },
  79. { "next2PhW", tWHICH_PHONEME, 6 },
  80. { "nextVowel", tWHICH_PHONEME, 7 },
  81. { "prevVowel", tWHICH_PHONEME, 8 },
  82. { "next3PhW", tWHICH_PHONEME, 9 },
  83. { "prev2PhW", tWHICH_PHONEME, 10 },
  84. { "PreVoicing", tTEST, 0xf01 },
  85. { "KlattSynth", tTEST, 0xf02 },
  86. { "MbrolaSynth", tTEST, 0xf03 },
  87. { NULL, 0, 0 }
  88. };
  89. static keywtab_t k_properties[] = {
  90. { "isPause", 0, CONDITION_IS_PHONEME_TYPE | phPAUSE },
  91. { "isVowel", 0, CONDITION_IS_PHONEME_TYPE | phVOWEL },
  92. { "isNasal", 0, CONDITION_IS_PHONEME_TYPE | phNASAL },
  93. { "isLiquid", 0, CONDITION_IS_PHONEME_TYPE | phLIQUID },
  94. { "isUStop", 0, CONDITION_IS_PHONEME_TYPE | phSTOP },
  95. { "isVStop", 0, CONDITION_IS_PHONEME_TYPE | phVSTOP },
  96. { "isVFricative", 0, CONDITION_IS_PHONEME_TYPE | phVFRICATIVE },
  97. { "isPalatal", 0, CONDITION_IS_PHFLAG_SET | phFLAGBIT_PALATAL },
  98. { "isLong", 0, CONDITION_IS_PHFLAG_SET | phFLAGBIT_LONG },
  99. { "isRhotic", 0, CONDITION_IS_PHFLAG_SET | phFLAGBIT_RHOTIC },
  100. { "isSibilant", 0, CONDITION_IS_PHFLAG_SET | phFLAGBIT_SIBILANT },
  101. { "isFlag1", 0, CONDITION_IS_PHFLAG_SET | phFLAGBIT_FLAG1 },
  102. { "isFlag2", 0, CONDITION_IS_PHFLAG_SET | phFLAGBIT_FLAG2 },
  103. { "isVel", 0, CONDITION_IS_PLACE_OF_ARTICULATION | phPLACE_VELAR },
  104. { "isDiminished", 0, CONDITION_IS_OTHER | isDiminished },
  105. { "isUnstressed", 0, CONDITION_IS_OTHER | isUnstressed },
  106. { "isNotStressed", 0, CONDITION_IS_OTHER | isNotStressed },
  107. { "isStressed", 0, CONDITION_IS_OTHER | isStressed },
  108. { "isMaxStress", 0, CONDITION_IS_OTHER | isMaxStress },
  109. { "isPause2", 0, CONDITION_IS_OTHER | isBreak },
  110. { "isWordStart", 0, CONDITION_IS_OTHER | isWordStart },
  111. { "isWordEnd", 0, CONDITION_IS_OTHER | isWordEnd },
  112. { "isAfterStress", 0, CONDITION_IS_OTHER | isAfterStress },
  113. { "isNotVowel", 0, CONDITION_IS_OTHER | isNotVowel },
  114. { "isFinalVowel", 0, CONDITION_IS_OTHER | isFinalVowel },
  115. { "isVoiced", 0, CONDITION_IS_OTHER | isVoiced },
  116. { "isFirstVowel", 0, CONDITION_IS_OTHER | isFirstVowel },
  117. { "isSecondVowel", 0, CONDITION_IS_OTHER | isSecondVowel },
  118. { "isTranslationGiven", 0, CONDITION_IS_OTHER | isTranslationGiven },
  119. { NULL, 0, 0 }
  120. };
  121. enum {
  122. kPHONEMESTART = 1,
  123. kUTF8_BOM,
  124. kPROCEDURE,
  125. kENDPHONEME,
  126. kENDPROCEDURE,
  127. kPHONEMETABLE,
  128. kINCLUDE,
  129. kIMPORT_PH,
  130. kSTARTTYPE,
  131. kENDTYPE,
  132. kSTRESSTYPE,
  133. kVOICINGSWITCH,
  134. kIF,
  135. kELSE,
  136. kELIF,
  137. kENDIF,
  138. kCALLPH,
  139. kSWITCH_PREVVOWEL,
  140. kSWITCH_NEXTVOWEL,
  141. kENDSWITCH,
  142. kFMT,
  143. kWAV,
  144. kVOWELSTART,
  145. kVOWELENDING,
  146. kANDWAV,
  147. kVOWELIN,
  148. kVOWELOUT,
  149. kTONESPEC,
  150. kRETURN,
  151. kCONTINUE,
  152. };
  153. enum {
  154. kTUNE = 1,
  155. kENDTUNE,
  156. kTUNE_PREHEAD,
  157. kTUNE_ONSET,
  158. kTUNE_HEAD,
  159. kTUNE_HEADENV,
  160. kTUNE_HEADEXTEND,
  161. kTUNE_HEADLAST,
  162. kTUNE_NUCLEUS0,
  163. kTUNE_NUCLEUS1,
  164. kTUNE_SPLIT,
  165. };
  166. static unsigned const char utf8_bom[] = { 0xef, 0xbb, 0xbf, 0 };
  167. static keywtab_t k_intonation[] = {
  168. { "tune", 0, kTUNE },
  169. { "endtune", 0, kENDTUNE },
  170. { "prehead", 0, kTUNE_PREHEAD },
  171. { "onset", 0, kTUNE_ONSET },
  172. { "head", 0, kTUNE_HEAD },
  173. { "headenv", 0, kTUNE_HEADENV },
  174. { "headextend", 0, kTUNE_HEADEXTEND },
  175. { "headlast", 0, kTUNE_HEADLAST },
  176. { "nucleus0", 0, kTUNE_NUCLEUS0 },
  177. { "nucleus", 0, kTUNE_NUCLEUS1 },
  178. { "split", 0, kTUNE_SPLIT },
  179. { NULL, 0, -1 }
  180. };
  181. static keywtab_t keywords[] = {
  182. { "liquid", tPHONEME_TYPE, phLIQUID },
  183. { "pause", tPHONEME_TYPE, phPAUSE },
  184. { "stress", tPHONEME_TYPE, phSTRESS },
  185. { "virtual", tPHONEME_TYPE, phVIRTUAL },
  186. { "delete_phoneme", tPHONEME_TYPE, phDELETED },
  187. // keywords
  188. { "phonemetable", tSTATEMENT, kPHONEMETABLE },
  189. { "include", tSTATEMENT, kINCLUDE },
  190. { (const char *)utf8_bom, tSTATEMENT, kUTF8_BOM },
  191. { "phoneme", tSTATEMENT, kPHONEMESTART },
  192. { "procedure", tSTATEMENT, kPROCEDURE },
  193. { "endphoneme", tSTATEMENT, kENDPHONEME },
  194. { "endprocedure", tSTATEMENT, kENDPROCEDURE },
  195. { "import_phoneme", tSTATEMENT, kIMPORT_PH },
  196. { "stress_type", tSTATEMENT, kSTRESSTYPE },
  197. { "starttype", tSTATEMENT, kSTARTTYPE },
  198. { "endtype", tSTATEMENT, kENDTYPE },
  199. { "voicingswitch", tSTATEMENT, kVOICINGSWITCH },
  200. { "IF", tSTATEMENT, kIF },
  201. { "ELSE", tSTATEMENT, kELSE },
  202. { "ELIF", tSTATEMENT, kELIF },
  203. { "ELSEIF", tSTATEMENT, kELIF }, // same as ELIF
  204. { "ENDIF", tSTATEMENT, kENDIF },
  205. { "CALL", tSTATEMENT, kCALLPH },
  206. { "RETURN", tSTATEMENT, kRETURN },
  207. { "PrevVowelEndings", tSTATEMENT, kSWITCH_PREVVOWEL },
  208. { "NextVowelStarts", tSTATEMENT, kSWITCH_NEXTVOWEL },
  209. { "EndSwitch", tSTATEMENT, kENDSWITCH },
  210. { "Tone", tSTATEMENT, kTONESPEC },
  211. { "FMT", tSTATEMENT, kFMT },
  212. { "WAV", tSTATEMENT, kWAV },
  213. { "VowelStart", tSTATEMENT, kVOWELSTART },
  214. { "VowelEnding", tSTATEMENT, kVOWELENDING },
  215. { "addWav", tSTATEMENT, kANDWAV },
  216. { "Vowelin", tSTATEMENT, kVOWELIN },
  217. { "Vowelout", tSTATEMENT, kVOWELOUT },
  218. { "Continue", tSTATEMENT, kCONTINUE },
  219. { "ChangePhoneme", tINSTRN1, i_CHANGE_PHONEME },
  220. { "ChangeNextPhoneme", tINSTRN1, i_REPLACE_NEXT_PHONEME },
  221. { "InsertPhoneme", tINSTRN1, i_INSERT_PHONEME },
  222. { "AppendPhoneme", tINSTRN1, i_APPEND_PHONEME },
  223. { "IfNextVowelAppend", tINSTRN1, i_APPEND_IFNEXTVOWEL },
  224. { "ChangeIfDiminished", tINSTRN1, i_CHANGE_IF | isDiminished },
  225. { "ChangeIfUnstressed", tINSTRN1, i_CHANGE_IF | isUnstressed },
  226. { "ChangeIfNotStressed", tINSTRN1, i_CHANGE_IF | isNotStressed },
  227. { "ChangeIfStressed", tINSTRN1, i_CHANGE_IF | isStressed },
  228. { "PauseBefore", tINSTRN1, i_PAUSE_BEFORE },
  229. { "PauseAfter", tINSTRN1, i_PAUSE_AFTER },
  230. { "length", tINSTRN1, i_SET_LENGTH },
  231. { "LongLength", tINSTRN1, i_LONG_LENGTH },
  232. { "LengthAdd", tINSTRN1, i_ADD_LENGTH },
  233. { "lengthmod", tINSTRN1, i_LENGTH_MOD },
  234. { "ipa", tINSTRN1, i_IPA_NAME },
  235. // flags
  236. { "unstressed", tPHONEME_FLAG, phUNSTRESSED },
  237. { "nolink", tPHONEME_FLAG, phNOLINK },
  238. { "brkafter", tPHONEME_FLAG, phBRKAFTER },
  239. { "rhotic", tPHONEME_FLAG, phRHOTIC },
  240. { "lengthenstop", tPHONEME_FLAG, phLENGTHENSTOP },
  241. { "nopause", tPHONEME_FLAG, phNOPAUSE },
  242. { "prevoice", tPHONEME_FLAG, phPREVOICE },
  243. { "flag1", tPHONEME_FLAG, phFLAG1 },
  244. { "flag2", tPHONEME_FLAG, phFLAG2 },
  245. // vowel transition attributes
  246. { "len=", tTRANSITION, 1 },
  247. { "rms=", tTRANSITION, 2 },
  248. { "f1=", tTRANSITION, 3 },
  249. { "f2=", tTRANSITION, 4 },
  250. { "f3=", tTRANSITION, 5 },
  251. { "brk", tTRANSITION, 6 },
  252. { "rate", tTRANSITION, 7 },
  253. { "glstop", tTRANSITION, 8 },
  254. { "lenadd", tTRANSITION, 9 },
  255. { "f4", tTRANSITION, 10 },
  256. { "gpaus", tTRANSITION, 11 },
  257. { "colr=", tTRANSITION, 12 },
  258. { "amp=", tTRANSITION, 13 }, // set rms of 1st frame as fraction of rms of 2nd frame (1/30ths)
  259. { NULL, 0, -1 }
  260. };
  261. static keywtab_t *keyword_tabs[] = {
  262. keywords, k_conditions, k_properties, k_intonation
  263. };
  264. static PHONEME_TAB *phoneme_out;
  265. static int n_phcodes_list[N_PHONEME_TABS];
  266. static PHONEME_TAB_LIST phoneme_tab_list2[N_PHONEME_TABS];
  267. static PHONEME_TAB *phoneme_tab2;
  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, 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 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 must 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. 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. 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. espeak_ng_STATUS LoadSpect(const char *path, int control, int *addr)
  857. {
  858. SpectSeq *spectseq;
  859. int peak;
  860. int frame;
  861. int n_frames;
  862. int ix;
  863. int x, x2;
  864. int rms;
  865. float total;
  866. float pkheight;
  867. int marker1_set = 0;
  868. int frame_vowelbreak = 0;
  869. int klatt_flag = 0;
  870. SpectFrame *fr;
  871. frame_t *fr_out;
  872. char filename[sizeof(path_home)+20];
  873. SPECT_SEQ seq_out;
  874. SPECT_SEQK seqk_out;
  875. // create SpectSeq and import data
  876. spectseq = SpectSeqCreate();
  877. if (spectseq == NULL)
  878. return ENOMEM;
  879. snprintf(filename, sizeof(filename), "%s/%s", phsrc, path);
  880. espeak_ng_STATUS status = LoadSpectSeq(spectseq, filename);
  881. if (status != ENS_OK) {
  882. error("Bad vowel file: '%s'", path);
  883. SpectSeqDestroy(spectseq);
  884. return status;
  885. }
  886. // do we need additional klatt data ?
  887. for (frame = 0; frame < spectseq->numframes; frame++) {
  888. for (ix = 5; ix < N_KLATTP2; ix++) {
  889. if (spectseq->frames[frame]->klatt_param[ix] != 0)
  890. klatt_flag = FRFLAG_KLATT;
  891. }
  892. }
  893. *addr = ftell(f_phdata);
  894. seq_out.n_frames = 0;
  895. seq_out.sqflags = 0;
  896. seq_out.length_total = 0;
  897. total = 0;
  898. for (frame = 0; frame < spectseq->numframes; frame++) {
  899. if (spectseq->frames[frame]->keyframe) {
  900. if (seq_out.n_frames == 1)
  901. frame_vowelbreak = frame;
  902. if (spectseq->frames[frame]->markers & 0x2) {
  903. // marker 1 is set
  904. marker1_set = 1;
  905. }
  906. seq_out.n_frames++;
  907. if (frame > 0)
  908. total += spectseq->frames[frame-1]->length;
  909. }
  910. }
  911. seq_out.length_total = (int)total;
  912. if ((control & 1) && (marker1_set == 0)) {
  913. // This is a vowel, but no Vowel Break marker is set
  914. // set a marker flag for the second frame of a vowel
  915. spectseq->frames[frame_vowelbreak]->markers |= FRFLAG_VOWEL_CENTRE;
  916. }
  917. n_frames = 0;
  918. for (frame = 0; frame < spectseq->numframes; frame++) {
  919. fr = spectseq->frames[frame];
  920. if (fr->keyframe) {
  921. if (klatt_flag)
  922. fr_out = &seqk_out.frame[n_frames];
  923. else
  924. fr_out = (frame_t *)&seq_out.frame[n_frames];
  925. x = (int)(fr->length + 0.5); // round to nearest mS
  926. if (x > 255) x = 255;
  927. fr_out->length = x;
  928. fr_out->frflags = fr->markers | klatt_flag;
  929. rms = (int)GetFrameRms(fr, spectseq->amplitude);
  930. if (rms > 255) rms = 255;
  931. fr_out->rms = rms;
  932. if (n_frames == (seq_out.n_frames-1))
  933. fr_out->length = 0; // give last frame zero length
  934. // write: peak data
  935. count_frames++;
  936. for (peak = 0; peak < 8; peak++) {
  937. if (peak < 7)
  938. fr_out->ffreq[peak] = fr->peaks[peak].pkfreq;
  939. pkheight = spectseq->amplitude * fr->amp_adjust * fr->peaks[peak].pkheight;
  940. pkheight = pkheight/640000;
  941. if (pkheight > 255) pkheight = 255;
  942. fr_out->fheight[peak] = (int)pkheight;
  943. if (peak < 6) {
  944. x = fr->peaks[peak].pkwidth/4;
  945. if (x > 255) x = 255;
  946. fr_out->fwidth[peak] = x;
  947. if (peak < 3) {
  948. x2 = fr->peaks[peak].pkright/4;
  949. if (x2 > 255) x2 = 255;
  950. fr_out->fright[peak] = x2;
  951. }
  952. }
  953. if (peak < 4) {
  954. x = fr->peaks[peak].klt_bw / 2;
  955. if (x > 255) x = 255;
  956. fr_out->bw[peak] = x;
  957. }
  958. }
  959. for (ix = 0; ix < 5; ix++) {
  960. fr_out->klattp[ix] = fr->klatt_param[ix];
  961. fr_out->klattp[KLATT_FNZ] = fr->klatt_param[KLATT_FNZ] / 2;
  962. }
  963. if (klatt_flag) {
  964. // additional klatt parameters
  965. for (ix = 0; ix < 5; ix++)
  966. fr_out->klattp2[ix] = fr->klatt_param[ix+5];
  967. for (peak = 0; peak < 7; peak++) {
  968. fr_out->klatt_ap[peak] = fr->peaks[peak].klt_ap;
  969. x = fr->peaks[peak].klt_bp / 2;
  970. if (x > 255) x = 255;
  971. fr_out->klatt_bp[peak] = x;
  972. }
  973. fr_out->spare = 0;
  974. }
  975. if (fr_out->bw[1] == 0) {
  976. fr_out->bw[0] = 89 / 2;
  977. fr_out->bw[1] = 90 / 2;
  978. fr_out->bw[2] = 140 / 2;
  979. fr_out->bw[3] = 260 / 2;
  980. }
  981. n_frames++;
  982. }
  983. }
  984. if (klatt_flag) {
  985. seqk_out.n_frames = seq_out.n_frames;
  986. seqk_out.sqflags = seq_out.sqflags;
  987. seqk_out.length_total = seq_out.length_total;
  988. ix = (char *)(&seqk_out.frame[seqk_out.n_frames]) - (char *)(&seqk_out);
  989. fwrite(&seqk_out, ix, 1, f_phdata);
  990. while (ix & 3)
  991. {
  992. // round up to multiple of 4 bytes
  993. fputc(0, f_phdata);
  994. ix++;
  995. }
  996. } else {
  997. ix = (char *)(&seq_out.frame[seq_out.n_frames]) - (char *)(&seq_out);
  998. fwrite(&seq_out, ix, 1, f_phdata);
  999. while (ix & 3)
  1000. {
  1001. // round up to multiple of 4 bytes
  1002. fputc(0, f_phdata);
  1003. ix++;
  1004. }
  1005. }
  1006. SpectSeqDestroy(spectseq);
  1007. return ENS_OK;
  1008. }
  1009. static int LoadWavefile(FILE *f, const char *fname)
  1010. {
  1011. int displ;
  1012. unsigned char c1;
  1013. unsigned char c3;
  1014. int c2;
  1015. int sample;
  1016. int sample2;
  1017. float x;
  1018. int max = 0;
  1019. int length;
  1020. int sr1, sr2;
  1021. int failed;
  1022. int len;
  1023. int resample_wav = 0;
  1024. const char *fname2;
  1025. char fname_temp[100];
  1026. char msg[120];
  1027. int scale_factor = 0;
  1028. fseek(f, 24, SEEK_SET);
  1029. sr1 = Read4Bytes(f);
  1030. sr2 = Read4Bytes(f);
  1031. fseek(f, 40, SEEK_SET);
  1032. if ((sr1 != samplerate_native) || (sr2 != sr1*2)) {
  1033. int fd_temp;
  1034. char command[sizeof(path_home)+250];
  1035. failed = 0;
  1036. #ifdef HAVE_MKSTEMP
  1037. strcpy(fname_temp, "/tmp/espeakXXXXXX");
  1038. if ((fd_temp = mkstemp(fname_temp)) >= 0)
  1039. close(fd_temp);
  1040. #else
  1041. strcpy(fname_temp, tmpnam(NULL));
  1042. #endif
  1043. fname2 = fname;
  1044. len = strlen(fname);
  1045. if (strcmp(&fname[len-4], ".wav") == 0) {
  1046. strcpy(msg, fname);
  1047. msg[len-4] = 0;
  1048. fname2 = msg;
  1049. }
  1050. sprintf(command, "sox \"%s/%s.wav\" -r %d -c1 -t wav %s\n", phsrc, fname2, samplerate_native, fname_temp);
  1051. if (system(command) != 0)
  1052. failed = 1;
  1053. if (failed || (GetFileLength(fname_temp) <= 0)) {
  1054. if (resample_fails < 2)
  1055. error("Resample command failed: %s", command);
  1056. resample_fails++;
  1057. if (sr1 != samplerate_native)
  1058. error("Can't resample (%d to %d): %s", sr1, samplerate_native, fname);
  1059. else
  1060. error("WAV file is not mono: %s", fname);
  1061. remove(fname_temp);
  1062. return 0;
  1063. }
  1064. f = fopen(fname_temp, "rb");
  1065. if (f == NULL) {
  1066. error("Can't read temp file: %s", fname_temp);
  1067. return 0;
  1068. }
  1069. if (f_report != NULL)
  1070. fprintf(f_report, "resampled %s\n", fname);
  1071. resample_count++;
  1072. resample_wav = 1;
  1073. fseek(f, 40, SEEK_SET); // skip past the WAV header, up to before "data length"
  1074. }
  1075. displ = ftell(f_phdata);
  1076. // data contains: 4 bytes of length (n_samples * 2), followed by 2-byte samples (lsb byte first)
  1077. length = Read4Bytes(f);
  1078. while (true) {
  1079. int c;
  1080. if ((c = fgetc(f)) == EOF)
  1081. break;
  1082. c1 = (unsigned char)c;
  1083. if ((c = fgetc(f)) == EOF)
  1084. break;
  1085. c3 = (unsigned char)c;
  1086. c2 = c3 << 24;
  1087. c2 = c2 >> 16; // sign extend
  1088. sample = (c1 & 0xff) + c2;
  1089. if (sample > max)
  1090. max = sample;
  1091. else if (sample < -max)
  1092. max = -sample;
  1093. }
  1094. scale_factor = (max / 127) + 1;
  1095. #define MIN_FACTOR -1 // was 6, disable use of 16 bit samples
  1096. if (scale_factor > MIN_FACTOR) {
  1097. length = length/2 + (scale_factor << 16);
  1098. }
  1099. Write4Bytes(f_phdata, length);
  1100. fseek(f, 44, SEEK_SET);
  1101. while (!feof(f)) {
  1102. c1 = fgetc(f);
  1103. c3 = fgetc(f);
  1104. c2 = c3 << 24;
  1105. c2 = c2 >> 16; // sign extend
  1106. sample = (c1 & 0xff) + c2;
  1107. if (feof(f)) break;
  1108. if (scale_factor <= MIN_FACTOR) {
  1109. fputc(sample & 0xff, f_phdata);
  1110. fputc(sample >> 8, f_phdata);
  1111. } else {
  1112. x = ((float)sample / scale_factor) + 0.5;
  1113. sample2 = (int)x;
  1114. if (sample2 > 127)
  1115. sample2 = 127;
  1116. if (sample2 < -128)
  1117. sample2 = -128;
  1118. fputc(sample2, f_phdata);
  1119. }
  1120. }
  1121. length = ftell(f_phdata);
  1122. while ((length & 3) != 0) {
  1123. // pad to a multiple of 4 bytes
  1124. fputc(0, f_phdata);
  1125. length++;
  1126. }
  1127. if (resample_wav != 0) {
  1128. fclose(f);
  1129. remove(fname_temp);
  1130. }
  1131. return displ | 0x800000; // set bit 23 to indicate a wave file rather than a spectrum
  1132. }
  1133. static espeak_ng_STATUS LoadEnvelope(FILE *f, const char *fname, int *displ)
  1134. {
  1135. char buf[128];
  1136. if (displ)
  1137. *displ = ftell(f_phdata);
  1138. if (fseek(f, 12, SEEK_SET) == -1)
  1139. return errno;
  1140. if (fread(buf, 128, 1, f) != 128)
  1141. return errno;
  1142. fwrite(buf, 128, 1, f_phdata);
  1143. if (n_envelopes < N_ENVELOPES) {
  1144. strncpy0(envelope_paths[n_envelopes], fname, sizeof(envelope_paths[0]));
  1145. memcpy(envelope_dat[n_envelopes], buf, sizeof(envelope_dat[0]));
  1146. n_envelopes++;
  1147. }
  1148. return ENS_OK;
  1149. }
  1150. // Generate a hash code from the specified string
  1151. static int Hash8(const char *string)
  1152. {
  1153. int c;
  1154. int chars = 0;
  1155. int hash = 0;
  1156. while ((c = *string++) != 0) {
  1157. c = tolower(c) - 'a';
  1158. hash = hash * 8 + c;
  1159. hash = (hash & 0x1ff) ^ (hash >> 8); // exclusive or
  1160. chars++;
  1161. }
  1162. return (hash+chars) & 0xff;
  1163. }
  1164. static int LoadEnvelope2(FILE *f, const char *fname)
  1165. {
  1166. int ix, ix2;
  1167. int n;
  1168. int x, y;
  1169. int displ;
  1170. int n_points;
  1171. double yy;
  1172. char line_buf[128];
  1173. float env_x[20];
  1174. float env_y[20];
  1175. int env_lin[20];
  1176. unsigned char env[ENV_LEN];
  1177. n_points = 0;
  1178. fgets(line_buf, sizeof(line_buf), f); // skip first line
  1179. while (!feof(f)) {
  1180. if (fgets(line_buf, sizeof(line_buf), f) == NULL)
  1181. break;
  1182. env_lin[n_points] = 0;
  1183. n = sscanf(line_buf, "%f %f %d", &env_x[n_points], &env_y[n_points], &env_lin[n_points]);
  1184. if (n >= 2) {
  1185. env_x[n_points] *= (float)1.28; // convert range 0-100 to 0-128
  1186. n_points++;
  1187. }
  1188. }
  1189. env_x[n_points] = env_x[n_points-1];
  1190. env_y[n_points] = env_y[n_points-1];
  1191. ix = -1;
  1192. ix2 = 0;
  1193. for (x = 0; x < ENV_LEN; x++) {
  1194. if (x > env_x[ix+4])
  1195. ix++;
  1196. if (x >= env_x[ix2+1])
  1197. ix2++;
  1198. if (env_lin[ix2] > 0) {
  1199. yy = env_y[ix2] + (env_y[ix2+1] - env_y[ix2]) * ((float)x - env_x[ix2]) / (env_x[ix2+1] - env_x[ix2]);
  1200. y = (int)(yy * 2.55);
  1201. } else if (n_points > 3)
  1202. y = (int)(polint(&env_x[ix], &env_y[ix], 4, x) * 2.55); // convert to range 0-255
  1203. else
  1204. y = (int)(polint(&env_x[ix], &env_y[ix], 3, x) * 2.55);
  1205. if (y < 0) y = 0;
  1206. if (y > 255) y = 255;
  1207. env[x] = y;
  1208. }
  1209. if (n_envelopes < N_ENVELOPES) {
  1210. strncpy0(envelope_paths[n_envelopes], fname, sizeof(envelope_paths[0]));
  1211. memcpy(envelope_dat[n_envelopes], env, ENV_LEN);
  1212. n_envelopes++;
  1213. }
  1214. displ = ftell(f_phdata);
  1215. fwrite(env, 1, 128, f_phdata);
  1216. return displ;
  1217. }
  1218. static espeak_ng_STATUS LoadDataFile(const char *path, int control, int *addr)
  1219. {
  1220. // load spectrum sequence or sample data from a file.
  1221. // return index into spect or sample data area. bit 23=1 if a sample
  1222. FILE *f;
  1223. int id;
  1224. int hash;
  1225. int type_code = ' ';
  1226. REF_HASH_TAB *p, *p2;
  1227. char buf[sizeof(path_home)+150];
  1228. if (strcmp(path, "NULL") == 0)
  1229. return ENS_OK;
  1230. if (strcmp(path, "DFT") == 0) {
  1231. *addr = 1;
  1232. return ENS_OK;
  1233. }
  1234. count_references++;
  1235. hash = Hash8(path);
  1236. p = ref_hash_tab[hash];
  1237. while (p != NULL) {
  1238. if (strcmp(path, p->string) == 0) {
  1239. duplicate_references++;
  1240. *addr = p->value; // already loaded this data
  1241. break;
  1242. }
  1243. p = (REF_HASH_TAB *)p->link;
  1244. }
  1245. if (*addr == 0) {
  1246. sprintf(buf, "%s/%s", phsrc, path);
  1247. if ((f = fopen(buf, "rb")) == NULL) {
  1248. sprintf(buf, "%s/%s.wav", phsrc, path);
  1249. if ((f = fopen(buf, "rb")) == NULL) {
  1250. error("Can't read file: %s", path);
  1251. return errno;
  1252. }
  1253. }
  1254. id = Read4Bytes(f);
  1255. rewind(f);
  1256. espeak_ng_STATUS status = ENS_OK;
  1257. if (id == 0x43455053) {
  1258. status = LoadSpect(path, control, addr);
  1259. type_code = 'S';
  1260. } else if (id == 0x46464952) {
  1261. *addr = LoadWavefile(f, path);
  1262. type_code = 'W';
  1263. } else if (id == 0x43544950) {
  1264. status = LoadEnvelope(f, path, addr);
  1265. type_code = 'E';
  1266. } else if (id == 0x45564E45) {
  1267. *addr = LoadEnvelope2(f, path);
  1268. type_code = 'E';
  1269. } else {
  1270. error("File not SPEC or RIFF: %s", path);
  1271. *addr = -1;
  1272. status = ENS_UNSUPPORTED_PHON_FORMAT;
  1273. }
  1274. fclose(f);
  1275. if (status != ENS_OK)
  1276. return status;
  1277. if (*addr > 0)
  1278. fprintf(f_phcontents, "%c 0x%.5x %s\n", type_code, *addr & 0x7fffff, path);
  1279. }
  1280. // add this item to the hash table
  1281. if (*addr > 0) {
  1282. p = ref_hash_tab[hash];
  1283. p2 = (REF_HASH_TAB *)malloc(sizeof(REF_HASH_TAB)+strlen(path)+1);
  1284. if (p2 == NULL)
  1285. return ENOMEM;
  1286. p2->value = *addr;
  1287. p2->ph_mnemonic = phoneme_out->mnemonic; // phoneme which uses this file
  1288. p2->ph_table = n_phoneme_tabs-1;
  1289. strcpy(p2->string, path);
  1290. p2->link = (char *)p;
  1291. ref_hash_tab[hash] = p2;
  1292. }
  1293. return ENS_OK;
  1294. }
  1295. static void CompileToneSpec(void)
  1296. {
  1297. int pitch1 = 0;
  1298. int pitch2 = 0;
  1299. int pitch_env = 0;
  1300. int amp_env = 0;
  1301. pitch1 = NextItemBrackets(tNUMBER, 2);
  1302. pitch2 = NextItemBrackets(tNUMBER, 3);
  1303. if (item_terminator == ',') {
  1304. NextItemBrackets(tSTRING, 3);
  1305. LoadDataFile(item_string, 0, &pitch_env);
  1306. }
  1307. if (item_terminator == ',') {
  1308. NextItemBrackets(tSTRING, 1);
  1309. LoadDataFile(item_string, 0, &amp_env);
  1310. }
  1311. if (pitch1 < pitch2) {
  1312. phoneme_out->start_type = pitch1;
  1313. phoneme_out->end_type = pitch2;
  1314. } else {
  1315. phoneme_out->start_type = pitch2;
  1316. phoneme_out->end_type = pitch1;
  1317. }
  1318. if (pitch_env != 0) {
  1319. *prog_out++ = i_PITCHENV + ((pitch_env >> 16) & 0xf);
  1320. *prog_out++ = pitch_env;
  1321. }
  1322. if (amp_env != 0) {
  1323. *prog_out++ = i_AMPENV + ((amp_env >> 16) & 0xf);
  1324. *prog_out++ = amp_env;
  1325. }
  1326. }
  1327. static void CompileSound(int keyword, int isvowel)
  1328. {
  1329. int addr = 0;
  1330. int value = 0;
  1331. char path[N_ITEM_STRING];
  1332. static int sound_instns[] = { i_FMT, i_WAV, i_VWLSTART, i_VWLENDING, i_WAVADD };
  1333. NextItemBrackets(tSTRING, 2);
  1334. strcpy(path, item_string);
  1335. if (item_terminator == ',') {
  1336. if ((keyword == kVOWELSTART) || (keyword == kVOWELENDING)) {
  1337. value = NextItemBrackets(tSIGNEDNUMBER, 1);
  1338. if (value > 127) {
  1339. value = 127;
  1340. error("Parameter > 127");
  1341. }
  1342. if (value < -128) {
  1343. value = -128;
  1344. error("Parameter < -128");
  1345. }
  1346. } else {
  1347. value = NextItemBrackets(tNUMBER, 1);
  1348. if (value > 255) {
  1349. value = 255;
  1350. error("Parameter > 255");
  1351. }
  1352. }
  1353. }
  1354. LoadDataFile(path, isvowel, &addr);
  1355. addr = addr / 4; // addr is words not bytes
  1356. *prog_out++ = sound_instns[keyword-kFMT] + ((value & 0xff) << 4) + ((addr >> 16) & 0xf);
  1357. *prog_out++ = addr & 0xffff;
  1358. }
  1359. /*
  1360. Condition
  1361. bits 14,15 1
  1362. bit 13 1 = AND, 0 = OR
  1363. bit 12 spare
  1364. bit 8-11
  1365. =0-3 p,t,n,n2 data=phoneme code
  1366. =4-7 p,t,n,n2 data=(bits5-7: phtype, place, property, special) (bits0-4: data)
  1367. =8 data = stress bitmap
  1368. =9 special tests
  1369. */
  1370. int CompileIf(int elif)
  1371. {
  1372. int key;
  1373. int finish = 0;
  1374. int word = 0;
  1375. int word2;
  1376. int data;
  1377. int bitmap;
  1378. int brackets;
  1379. int not_flag;
  1380. USHORT *prog_last_if = NULL;
  1381. then_count = 2;
  1382. after_if = 1;
  1383. while (!finish) {
  1384. not_flag = 0;
  1385. word2 = 0;
  1386. if (prog_out >= prog_out_max) {
  1387. error("Phoneme program too large");
  1388. return 0;
  1389. }
  1390. if ((key = NextItem(tCONDITION)) < 0)
  1391. error("Expected a condition, not '%s'", item_string);
  1392. if ((item_type == 0) && (key == k_NOT)) {
  1393. not_flag = 1;
  1394. if ((key = NextItem(tCONDITION)) < 0)
  1395. error("Expected a condition, not '%s'", item_string);
  1396. }
  1397. if (item_type == tWHICH_PHONEME) {
  1398. // prevPh(), thisPh(), nextPh(), next2Ph() etc
  1399. if (key >= 6) {
  1400. // put the 'which' code in the next instruction
  1401. word2 = key;
  1402. key = 6;
  1403. }
  1404. key = key << 8;
  1405. data = NextItemBrackets(tPROPERTIES, 0);
  1406. if (data >= 0)
  1407. word = key + data + 0x700;
  1408. else {
  1409. data = LookupPhoneme(item_string, 2);
  1410. word = key + data;
  1411. }
  1412. } else if (item_type == tTEST) {
  1413. if (key == kTHISSTRESS) {
  1414. bitmap = 0;
  1415. brackets = 2;
  1416. do {
  1417. data = NextItemBrackets(tNUMBER, brackets);
  1418. if (data > 7)
  1419. error("Expected list of stress levels");
  1420. bitmap |= (1 << data);
  1421. brackets = 3;
  1422. } while (item_terminator == ',');
  1423. word = i_StressLevel | bitmap;
  1424. } else
  1425. word = key;
  1426. } else {
  1427. error("Unexpected keyword '%s'", item_string);
  1428. if ((strcmp(item_string, "phoneme") == 0) || (strcmp(item_string, "endphoneme") == 0))
  1429. return -1;
  1430. }
  1431. // output the word
  1432. prog_last_if = prog_out;
  1433. *prog_out++ = word | i_CONDITION;
  1434. if (word2 != 0)
  1435. *prog_out++ = word2;
  1436. if (not_flag)
  1437. *prog_out++ = i_NOT;
  1438. // expect AND, OR, THEN
  1439. switch (NextItem(tCONDITION))
  1440. {
  1441. case k_AND:
  1442. break;
  1443. case k_OR:
  1444. if (prog_last_if != NULL)
  1445. *prog_last_if |= i_OR;
  1446. break;
  1447. case k_THEN:
  1448. finish = 1;
  1449. break;
  1450. default:
  1451. error("Expected AND, OR, THEN");
  1452. break;
  1453. }
  1454. }
  1455. if (elif == 0) {
  1456. if_level++;
  1457. if_stack[if_level].p_else = NULL;
  1458. }
  1459. if_stack[if_level].returned = 0;
  1460. if_stack[if_level].p_then = prog_out;
  1461. *prog_out++ = i_JUMP_FALSE;
  1462. return 0;
  1463. }
  1464. void FillThen(int add)
  1465. {
  1466. USHORT *p;
  1467. int offset;
  1468. p = if_stack[if_level].p_then;
  1469. if (p != NULL) {
  1470. offset = prog_out - p + add;
  1471. if ((then_count == 1) && (if_level == 1)) {
  1472. // The THEN part only contains one statement, we can remove the THEN jump
  1473. // and the interpreter will implicitly skip the statement.
  1474. while (p < prog_out) {
  1475. p[0] = p[1];
  1476. p++;
  1477. }
  1478. prog_out--;
  1479. } else {
  1480. if (offset > MAX_JUMP)
  1481. error("IF block is too long");
  1482. *p = i_JUMP_FALSE + offset;
  1483. }
  1484. if_stack[if_level].p_then = NULL;
  1485. }
  1486. then_count = 0;
  1487. }
  1488. int CompileElse(void)
  1489. {
  1490. USHORT *ref;
  1491. USHORT *p;
  1492. if (if_level < 1) {
  1493. error("ELSE not expected");
  1494. return 0;
  1495. }
  1496. if (if_stack[if_level].returned == 0)
  1497. FillThen(1);
  1498. else
  1499. FillThen(0);
  1500. if (if_stack[if_level].returned == 0) {
  1501. ref = prog_out;
  1502. *prog_out++ = 0;
  1503. if ((p = if_stack[if_level].p_else) != NULL)
  1504. *ref = ref - p; // backwards offset to the previous else
  1505. if_stack[if_level].p_else = ref;
  1506. }
  1507. return 0;
  1508. }
  1509. int CompileElif(void)
  1510. {
  1511. if (if_level < 1) {
  1512. error("ELIF not expected");
  1513. return 0;
  1514. }
  1515. CompileElse();
  1516. CompileIf(1);
  1517. return 0;
  1518. }
  1519. int CompileEndif(void)
  1520. {
  1521. USHORT *p;
  1522. int chain;
  1523. int offset;
  1524. if (if_level < 1) {
  1525. error("ENDIF not expected");
  1526. return 0;
  1527. }
  1528. FillThen(0);
  1529. if ((p = if_stack[if_level].p_else) != NULL) {
  1530. do {
  1531. chain = *p; // a chain of previous else links
  1532. offset = prog_out - p;
  1533. if (offset > MAX_JUMP)
  1534. error("IF block is too long");
  1535. *p = i_JUMP + offset;
  1536. p -= chain;
  1537. } while (chain > 0);
  1538. }
  1539. if_level--;
  1540. return 0;
  1541. }
  1542. static int CompileSwitch(int type)
  1543. {
  1544. // Type 0: EndSwitch
  1545. // 1: SwitchPrevVowelType
  1546. // 2: SwitchNextVowelType
  1547. if (type == 0) {
  1548. // check the instructions in the Switch
  1549. return 0;
  1550. }
  1551. if (type == 1)
  1552. *prog_out++ = i_SWITCH_PREVVOWEL+6;
  1553. if (type == 2)
  1554. *prog_out++ = i_SWITCH_NEXTVOWEL+6;
  1555. return 0;
  1556. }
  1557. static PHONEME_TAB_LIST *FindPhonemeTable(const char *string)
  1558. {
  1559. int ix;
  1560. for (ix = 0; ix < n_phoneme_tabs; ix++) {
  1561. if (strcmp(phoneme_tab_list2[ix].name, string) == 0)
  1562. return &phoneme_tab_list2[ix];
  1563. }
  1564. error("Unknown phoneme table: '%s'", string);
  1565. return NULL;
  1566. }
  1567. static PHONEME_TAB *FindPhoneme(const char *string)
  1568. {
  1569. PHONEME_TAB_LIST *phtab = NULL;
  1570. int ix;
  1571. unsigned int mnem;
  1572. char *phname;
  1573. char buf[200];
  1574. // is this the name of a phoneme which is in scope
  1575. if ((strlen(string) <= 4) && ((ix = LookupPhoneme(string, 0)) != -1))
  1576. return &phoneme_tab2[ix];
  1577. // no, treat the name as phonemetable/phoneme
  1578. strcpy(buf, string);
  1579. if ((phname = strchr(buf, '/')) != 0)
  1580. *phname++ = 0;
  1581. phtab = FindPhonemeTable(buf);
  1582. if (phtab == NULL)
  1583. return NULL; // phoneme table not found
  1584. mnem = StringToWord(phname);
  1585. for (ix = 1; ix < 256; ix++) {
  1586. if (mnem == phtab->phoneme_tab_ptr[ix].mnemonic)
  1587. return &phtab->phoneme_tab_ptr[ix];
  1588. }
  1589. error("Phoneme reference not found: '%s'", string);
  1590. return NULL;
  1591. }
  1592. static void ImportPhoneme(void)
  1593. {
  1594. unsigned int ph_mnem;
  1595. unsigned int ph_code;
  1596. PHONEME_TAB *ph;
  1597. NextItem(tSTRING);
  1598. if ((ph = FindPhoneme(item_string)) == NULL) {
  1599. error("Cannot find phoneme '%s' to import.", item_string);
  1600. return;
  1601. }
  1602. if (phoneme_out->phflags != 0 ||
  1603. phoneme_out->type != phINVALID ||
  1604. phoneme_out->start_type != 0 ||
  1605. phoneme_out->end_type != 0 ||
  1606. phoneme_out->std_length != 0 ||
  1607. phoneme_out->length_mod != 0) {
  1608. error("Phoneme import will override set properties.");
  1609. }
  1610. ph_mnem = phoneme_out->mnemonic;
  1611. ph_code = phoneme_out->code;
  1612. memcpy(phoneme_out, ph, sizeof(PHONEME_TAB));
  1613. phoneme_out->mnemonic = ph_mnem;
  1614. phoneme_out->code = ph_code;
  1615. if (phoneme_out->type != phVOWEL)
  1616. phoneme_out->end_type = 0; // voicingswitch, this must be set later to refer to a local phoneme
  1617. }
  1618. static void CallPhoneme(void)
  1619. {
  1620. PHONEME_TAB *ph;
  1621. int ix;
  1622. int addr = 0;
  1623. NextItem(tSTRING);
  1624. // first look for a procedure name
  1625. for (ix = 0; ix < n_procs; ix++) {
  1626. if (strcmp(proc_names[ix], item_string) == 0) {
  1627. addr = proc_addr[ix];
  1628. break;
  1629. }
  1630. }
  1631. if (ix == n_procs) {
  1632. // procedure not found, try a phoneme name
  1633. if ((ph = FindPhoneme(item_string)) == NULL)
  1634. return;
  1635. addr = ph->program;
  1636. if (phoneme_out->type == phINVALID) {
  1637. // Phoneme type has not been set. Copy it from the called phoneme
  1638. phoneme_out->type = ph->type;
  1639. phoneme_out->start_type = ph->start_type;
  1640. phoneme_out->end_type = ph->end_type;
  1641. phoneme_out->std_length = ph->std_length;
  1642. phoneme_out->length_mod = ph->length_mod;
  1643. }
  1644. }
  1645. *prog_out++ = i_CALLPH + (addr >> 16);
  1646. *prog_out++ = addr;
  1647. }
  1648. static void DecThenCount()
  1649. {
  1650. if (then_count > 0)
  1651. then_count--;
  1652. }
  1653. int CompilePhoneme(int compile_phoneme)
  1654. {
  1655. int endphoneme = 0;
  1656. int keyword;
  1657. int value;
  1658. int phcode = 0;
  1659. int flags;
  1660. int ix;
  1661. int start;
  1662. int count;
  1663. int c;
  1664. char *p;
  1665. int vowel_length_factor = 100; // for testing
  1666. char number_buf[12];
  1667. char ipa_buf[N_ITEM_STRING+1];
  1668. PHONEME_TAB phoneme_out2;
  1669. PHONEME_PROG_LOG phoneme_prog_log;
  1670. prog_out = prog_buf;
  1671. prog_out_max = &prog_buf[MAX_PROG_BUF-1];
  1672. if_level = 0;
  1673. if_stack[0].returned = 0;
  1674. after_if = 0;
  1675. int phoneme_flags = 0;
  1676. NextItem(tSTRING);
  1677. if (compile_phoneme) {
  1678. phcode = LookupPhoneme(item_string, 1); // declare phoneme if not already there
  1679. if (phcode == -1) return 0;
  1680. phoneme_out = &phoneme_tab2[phcode];
  1681. } else {
  1682. // declare a procedure
  1683. if (n_procs >= N_PROCS) {
  1684. error("Too many procedures");
  1685. return 0;
  1686. }
  1687. strcpy(proc_names[n_procs], item_string);
  1688. phoneme_out = &phoneme_out2;
  1689. sprintf(number_buf, "%.3dP", n_procs);
  1690. phoneme_out->mnemonic = StringToWord(number_buf);
  1691. }
  1692. phoneme_out->code = phcode;
  1693. phoneme_out->program = 0;
  1694. phoneme_out->type = phINVALID;
  1695. phoneme_out->std_length = 0;
  1696. phoneme_out->start_type = 0;
  1697. phoneme_out->end_type = 0;
  1698. phoneme_out->length_mod = 0;
  1699. phoneme_out->phflags = 0;
  1700. while (!endphoneme && !feof(f_in)) {
  1701. if ((keyword = NextItem(tKEYWORD)) < 0) {
  1702. if (keyword == -2) {
  1703. error("Missing 'endphoneme' before end-of-file"); // end of file
  1704. break;
  1705. }
  1706. phoneme_feature_t feature = phoneme_feature_from_string(item_string);
  1707. espeak_ng_STATUS status = phoneme_add_feature(phoneme_out, feature);
  1708. if (status == ENS_OK)
  1709. continue;
  1710. error_from_status(status, item_string);
  1711. continue;
  1712. }
  1713. switch (item_type)
  1714. {
  1715. case tPHONEME_TYPE:
  1716. if (phoneme_out->type != phINVALID)
  1717. error("More than one phoneme type: %s", item_string);
  1718. phoneme_out->type = keyword;
  1719. break;
  1720. case tPHONEME_FLAG:
  1721. phoneme_flags |= keyword;
  1722. break;
  1723. case tINSTRN1:
  1724. // instruction group 0, with 8 bit operands which set data in PHONEME_DATA
  1725. switch (keyword)
  1726. {
  1727. case i_CHANGE_PHONEME:
  1728. case i_APPEND_PHONEME:
  1729. case i_APPEND_IFNEXTVOWEL:
  1730. case i_INSERT_PHONEME:
  1731. case i_REPLACE_NEXT_PHONEME:
  1732. case i_VOICING_SWITCH:
  1733. case i_CHANGE_IF | isDiminished:
  1734. case i_CHANGE_IF | isUnstressed:
  1735. case i_CHANGE_IF | isNotStressed:
  1736. case i_CHANGE_IF | isStressed:
  1737. value = NextItemBrackets(tPHONEMEMNEM, 0);
  1738. *prog_out++ = (keyword << 8) + value;
  1739. DecThenCount();
  1740. break;
  1741. case i_PAUSE_BEFORE:
  1742. value = NextItemMax(255);
  1743. *prog_out++ = (i_PAUSE_BEFORE << 8) + value;
  1744. DecThenCount();
  1745. break;
  1746. case i_PAUSE_AFTER:
  1747. value = NextItemMax(255);
  1748. *prog_out++ = (i_PAUSE_AFTER << 8) + value;
  1749. DecThenCount();
  1750. break;
  1751. case i_SET_LENGTH:
  1752. value = NextItemMax(511);
  1753. if (phoneme_out->type == phVOWEL)
  1754. value = (value * vowel_length_factor)/100;
  1755. if (after_if == 0)
  1756. phoneme_out->std_length = value/2;
  1757. else {
  1758. *prog_out++ = (i_SET_LENGTH << 8) + value/2;
  1759. DecThenCount();
  1760. }
  1761. break;
  1762. case i_ADD_LENGTH:
  1763. value = NextItem(tSIGNEDNUMBER) / 2;
  1764. *prog_out++ = (i_ADD_LENGTH << 8) + (value & 0xff);
  1765. DecThenCount();
  1766. break;
  1767. case i_LENGTH_MOD:
  1768. value = NextItem(tNUMBER);
  1769. phoneme_out->length_mod = value;
  1770. break;
  1771. case i_IPA_NAME:
  1772. NextItem(tSTRING);
  1773. if (strcmp(item_string, "NULL") == 0)
  1774. strcpy(item_string, " ");
  1775. // copy the string, recognize characters in the form U+9999
  1776. flags = 0;
  1777. count = 0;
  1778. ix = 1;
  1779. for (p = item_string; *p != 0;) {
  1780. p += utf8_in(&c, p);
  1781. if ((c == '|') && (count > 0)) {
  1782. // '|' means don't allow a tie or joiner before this letter
  1783. flags |= (1 << (count -1));
  1784. } else if ((c == 'U') && (p[0] == '+')) {
  1785. int j;
  1786. // U+9999
  1787. p++;
  1788. memcpy(number_buf, p, 4); // U+ should be followed by 4 hex digits
  1789. number_buf[4] = 0;
  1790. c = '#';
  1791. sscanf(number_buf, "%x", (unsigned int *)&c);
  1792. // move past the 4 hexdecimal digits
  1793. for (j = 0; j < 4; j++) {
  1794. if (!isalnum(*p))
  1795. break;
  1796. p++;
  1797. }
  1798. ix += utf8_out(c, &ipa_buf[ix]);
  1799. count++;
  1800. } else {
  1801. ix += utf8_out(c, &ipa_buf[ix]);
  1802. count++;
  1803. }
  1804. }
  1805. ipa_buf[0] = flags;
  1806. ipa_buf[ix] = 0;
  1807. start = 1;
  1808. if (flags != 0)
  1809. start = 0; // only include the flags byte if bits are set
  1810. value = strlen(&ipa_buf[start]); // number of UTF-8 bytes
  1811. *prog_out++ = (i_IPA_NAME << 8) + value;
  1812. for (ix = 0; ix < value; ix += 2)
  1813. *prog_out++ = (ipa_buf[ix+start] << 8) + (ipa_buf[ix+start+1] & 0xff);
  1814. DecThenCount();
  1815. break;
  1816. }
  1817. break;
  1818. case tSTATEMENT:
  1819. switch (keyword)
  1820. {
  1821. case kIMPORT_PH:
  1822. ImportPhoneme();
  1823. phoneme_flags = phoneme_out->phflags;
  1824. break;
  1825. case kSTARTTYPE:
  1826. phcode = NextItem(tPHONEMEMNEM);
  1827. if (phcode == -1)
  1828. phcode = LookupPhoneme(item_string, 1);
  1829. phoneme_out->start_type = phcode;
  1830. if (phoneme_out->type == phINVALID)
  1831. error("a phoneme type or manner of articulation must be specified before starttype");
  1832. break;
  1833. case kENDTYPE:
  1834. phcode = NextItem(tPHONEMEMNEM);
  1835. if (phcode == -1)
  1836. phcode = LookupPhoneme(item_string, 1);
  1837. if (phoneme_out->type == phINVALID)
  1838. error("a phoneme type or manner of articulation must be specified before endtype");
  1839. else if (phoneme_out->type == phVOWEL)
  1840. phoneme_out->end_type = phcode;
  1841. else if (phcode != phoneme_out->start_type)
  1842. error("endtype must equal starttype for consonants");
  1843. break;
  1844. case kVOICINGSWITCH:
  1845. phcode = NextItem(tPHONEMEMNEM);
  1846. if (phcode == -1)
  1847. phcode = LookupPhoneme(item_string, 1);
  1848. if (phoneme_out->type == phVOWEL)
  1849. error("voicingswitch cannot be used on vowels");
  1850. else
  1851. phoneme_out->end_type = phcode; // use end_type field for consonants as voicing_switch
  1852. break;
  1853. case kSTRESSTYPE:
  1854. value = NextItem(tNUMBER);
  1855. phoneme_out->std_length = value;
  1856. if (prog_out > prog_buf) {
  1857. error("stress phonemes can't contain program instructions");
  1858. prog_out = prog_buf;
  1859. }
  1860. break;
  1861. case kIF:
  1862. endphoneme = CompileIf(0);
  1863. break;
  1864. case kELSE:
  1865. endphoneme = CompileElse();
  1866. break;
  1867. case kELIF:
  1868. endphoneme = CompileElif();
  1869. break;
  1870. case kENDIF:
  1871. endphoneme = CompileEndif();
  1872. break;
  1873. case kENDSWITCH:
  1874. break;
  1875. case kSWITCH_PREVVOWEL:
  1876. endphoneme = CompileSwitch(1);
  1877. break;
  1878. case kSWITCH_NEXTVOWEL:
  1879. endphoneme = CompileSwitch(2);
  1880. break;
  1881. case kCALLPH:
  1882. CallPhoneme();
  1883. DecThenCount();
  1884. break;
  1885. case kFMT:
  1886. if_stack[if_level].returned = 1;
  1887. DecThenCount();
  1888. if (phoneme_out->type == phVOWEL)
  1889. CompileSound(keyword, 1);
  1890. else
  1891. CompileSound(keyword, 0);
  1892. break;
  1893. case kWAV:
  1894. if_stack[if_level].returned = 1;
  1895. // fallthrough:
  1896. case kVOWELSTART:
  1897. case kVOWELENDING:
  1898. case kANDWAV:
  1899. DecThenCount();
  1900. CompileSound(keyword, 0);
  1901. break;
  1902. case kVOWELIN:
  1903. DecThenCount();
  1904. endphoneme = CompileVowelTransition(1);
  1905. break;
  1906. case kVOWELOUT:
  1907. DecThenCount();
  1908. endphoneme = CompileVowelTransition(2);
  1909. break;
  1910. case kTONESPEC:
  1911. DecThenCount();
  1912. CompileToneSpec();
  1913. break;
  1914. case kCONTINUE:
  1915. *prog_out++ = OPCODE_CONTINUE;
  1916. DecThenCount();
  1917. break;
  1918. case kRETURN:
  1919. *prog_out++ = OPCODE_RETURN;
  1920. DecThenCount();
  1921. break;
  1922. case kINCLUDE:
  1923. case kPHONEMETABLE:
  1924. error("Missing 'endphoneme' before '%s'", item_string); // drop through to endphoneme
  1925. // fallthrough:
  1926. case kENDPHONEME:
  1927. case kENDPROCEDURE:
  1928. endphoneme = 1;
  1929. if (if_level > 0)
  1930. error("Missing ENDIF");
  1931. if ((prog_out > prog_buf) && (if_stack[0].returned == 0))
  1932. *prog_out++ = OPCODE_RETURN;
  1933. break;
  1934. }
  1935. break;
  1936. }
  1937. }
  1938. if (endphoneme != 1)
  1939. error("'endphoneme' not expected here");
  1940. if (compile_phoneme) {
  1941. if (phoneme_out->type == phINVALID) {
  1942. error("Phoneme type is missing");
  1943. phoneme_out->type = 0;
  1944. }
  1945. phoneme_out->phflags |= phoneme_flags;
  1946. if (phoneme_out->phflags & phVOICED) {
  1947. if (phoneme_out->type == phSTOP)
  1948. phoneme_out->type = phVSTOP;
  1949. else if (phoneme_out->type == phFRICATIVE)
  1950. phoneme_out->type = phVFRICATIVE;
  1951. }
  1952. if (phoneme_out->std_length == 0) {
  1953. if (phoneme_out->type == phVOWEL)
  1954. phoneme_out->std_length = 180/2; // default length for vowel
  1955. }
  1956. phoneme_out->phflags |= phLOCAL; // declared in this phoneme table
  1957. if (phoneme_out->type == phDELETED)
  1958. phoneme_out->mnemonic = 0x01; // will not be recognised
  1959. }
  1960. if (prog_out > prog_buf) {
  1961. // write out the program for this phoneme
  1962. fflush(f_phindex);
  1963. phoneme_out->program = ftell(f_phindex) / sizeof(USHORT);
  1964. if (f_prog_log != NULL) {
  1965. phoneme_prog_log.addr = phoneme_out->program;
  1966. phoneme_prog_log.length = prog_out - prog_buf;
  1967. fwrite(&phoneme_prog_log, 1, sizeof(phoneme_prog_log), f_prog_log);
  1968. }
  1969. if (compile_phoneme == 0)
  1970. proc_addr[n_procs++] = ftell(f_phindex) / sizeof(USHORT);
  1971. fwrite(prog_buf, sizeof(USHORT), prog_out - prog_buf, f_phindex);
  1972. }
  1973. return 0;
  1974. }
  1975. static void WritePhonemeTables()
  1976. {
  1977. int ix;
  1978. int j;
  1979. int n;
  1980. int value;
  1981. int count;
  1982. PHONEME_TAB *p;
  1983. value = n_phoneme_tabs;
  1984. fputc(value, f_phtab);
  1985. fputc(0, f_phtab);
  1986. fputc(0, f_phtab);
  1987. fputc(0, f_phtab);
  1988. for (ix = 0; ix < n_phoneme_tabs; ix++) {
  1989. p = phoneme_tab_list2[ix].phoneme_tab_ptr;
  1990. n = n_phcodes_list[ix];
  1991. memset(&p[n], 0, sizeof(p[n]));
  1992. p[n].mnemonic = 0; // terminate the phoneme table
  1993. // count number of locally declared phonemes
  1994. count = 0;
  1995. for (j = 0; j < n; j++) {
  1996. if (ix == 0)
  1997. p[j].phflags |= phLOCAL; // write all phonemes in the base phoneme table
  1998. if (p[j].phflags & phLOCAL)
  1999. count++;
  2000. }
  2001. phoneme_tab_list2[ix].n_phonemes = count+1;
  2002. fputc(count+1, f_phtab);
  2003. fputc(phoneme_tab_list2[ix].includes, f_phtab);
  2004. fputc(0, f_phtab);
  2005. fputc(0, f_phtab);
  2006. fwrite(phoneme_tab_list2[ix].name, 1, N_PHONEME_TAB_NAME, f_phtab);
  2007. for (j = 0; j < n; j++) {
  2008. if (p[j].phflags & phLOCAL) {
  2009. // this bit is set temporarily to incidate a local phoneme, declared in
  2010. // in the current phoneme file
  2011. p[j].phflags &= ~phLOCAL;
  2012. fwrite(&p[j], sizeof(PHONEME_TAB), 1, f_phtab);
  2013. }
  2014. }
  2015. fwrite(&p[n], sizeof(PHONEME_TAB), 1, f_phtab); // include the extra list-terminator phoneme entry
  2016. free(p);
  2017. }
  2018. }
  2019. static void EndPhonemeTable()
  2020. {
  2021. int ix;
  2022. int *pw;
  2023. int length;
  2024. if (n_phoneme_tabs == 0)
  2025. return;
  2026. // check that all referenced phonemes have been declared
  2027. for (ix = 0; ix < n_phcodes; ix++) {
  2028. if (phoneme_tab2[ix].type == phINVALID) {
  2029. error("Phoneme [%s] not declared, referenced at line %d",
  2030. WordToString(phoneme_tab2[ix].mnemonic), (int)(phoneme_tab2[ix].program));
  2031. error_count++;
  2032. phoneme_tab2[ix].type = 0; // prevent the error message repeating
  2033. }
  2034. }
  2035. n_phcodes_list[n_phoneme_tabs-1] = n_phcodes;
  2036. }
  2037. static void StartPhonemeTable(const char *name)
  2038. {
  2039. int ix;
  2040. int j;
  2041. PHONEME_TAB *p;
  2042. if (n_phoneme_tabs >= N_PHONEME_TABS-1) {
  2043. error("Too many phonemetables");
  2044. return;
  2045. }
  2046. p = (PHONEME_TAB *)calloc(sizeof(PHONEME_TAB), N_PHONEME_TAB);
  2047. if (p == NULL) {
  2048. error("Out of memory");
  2049. return;
  2050. }
  2051. memset(&phoneme_tab_list2[n_phoneme_tabs], 0, sizeof(PHONEME_TAB_LIST));
  2052. phoneme_tab_list2[n_phoneme_tabs].phoneme_tab_ptr = phoneme_tab2 = p;
  2053. memset(phoneme_tab_list2[n_phoneme_tabs].name, 0, sizeof(phoneme_tab_list2[n_phoneme_tabs].name));
  2054. strncpy0(phoneme_tab_list2[n_phoneme_tabs].name, name, N_PHONEME_TAB_NAME);
  2055. n_phcodes = 1;
  2056. phoneme_tab_list2[n_phoneme_tabs].includes = 0;
  2057. if (n_phoneme_tabs > 0) {
  2058. NextItem(tSTRING); // name of base phoneme table
  2059. for (ix = 0; ix < n_phoneme_tabs; ix++) {
  2060. if (strcmp(item_string, phoneme_tab_list2[ix].name) == 0) {
  2061. phoneme_tab_list2[n_phoneme_tabs].includes = ix+1;
  2062. // initialise the new phoneme table with the contents of this one
  2063. memcpy(phoneme_tab2, phoneme_tab_list2[ix].phoneme_tab_ptr, sizeof(PHONEME_TAB)*N_PHONEME_TAB);
  2064. n_phcodes = n_phcodes_list[ix];
  2065. // clear "local phoneme" bit"
  2066. for (j = 0; j < n_phcodes; j++)
  2067. phoneme_tab2[j].phflags &= ~phLOCAL;
  2068. break;
  2069. }
  2070. }
  2071. if (ix == n_phoneme_tabs)
  2072. error("Can't find base phonemetable '%s'", item_string);
  2073. } else
  2074. ReservePhCodes();
  2075. n_phoneme_tabs++;
  2076. }
  2077. static void CompilePhonemeFiles()
  2078. {
  2079. int item;
  2080. FILE *f;
  2081. char buf[sizeof(path_home)+120];
  2082. linenum = 1;
  2083. count_references = 0;
  2084. duplicate_references = 0;
  2085. count_frames = 0;
  2086. n_procs = 0;
  2087. for (;;) {
  2088. if (feof(f_in)) {
  2089. // end of file, go back to previous from, from which this was included
  2090. if (stack_ix == 0)
  2091. break; // end of top level, finished
  2092. fclose(f_in);
  2093. f_in = stack[--stack_ix].file;
  2094. strcpy(current_fname, stack[stack_ix].fname);
  2095. linenum = stack[stack_ix].linenum;
  2096. }
  2097. item = NextItem(tKEYWORD);
  2098. switch (item)
  2099. {
  2100. case kUTF8_BOM:
  2101. break; // ignore bytes 0xef 0xbb 0xbf
  2102. case kINCLUDE:
  2103. NextItem(tSTRING);
  2104. sprintf(buf, "%s/%s", phsrc, item_string);
  2105. if ((stack_ix < N_STACK) && (f = fopen(buf, "rb")) != NULL) {
  2106. stack[stack_ix].linenum = linenum;
  2107. strcpy(stack[stack_ix].fname, current_fname);
  2108. stack[stack_ix++].file = f_in;
  2109. f_in = f;
  2110. strncpy0(current_fname, item_string, sizeof(current_fname));
  2111. linenum = 1;
  2112. } else
  2113. error("Missing file: %s", item_string);
  2114. break;
  2115. case kPHONEMETABLE:
  2116. EndPhonemeTable();
  2117. NextItem(tSTRING); // name of the new phoneme table
  2118. StartPhonemeTable(item_string);
  2119. break;
  2120. case kPHONEMESTART:
  2121. if (n_phoneme_tabs == 0) {
  2122. error("phonemetable is missing");
  2123. return;
  2124. }
  2125. CompilePhoneme(1);
  2126. break;
  2127. case kPROCEDURE:
  2128. CompilePhoneme(0);
  2129. break;
  2130. default:
  2131. if (!feof(f_in))
  2132. error("Keyword 'phoneme' expected");
  2133. break;
  2134. }
  2135. }
  2136. memset(&phoneme_tab2[n_phcodes+1], 0, sizeof(phoneme_tab2[n_phcodes+1]));
  2137. phoneme_tab2[n_phcodes+1].mnemonic = 0; // terminator
  2138. }
  2139. #pragma GCC visibility push(default)
  2140. espeak_ng_STATUS
  2141. espeak_ng_CompilePhonemeData(long rate,
  2142. FILE *log,
  2143. espeak_ng_ERROR_CONTEXT *context)
  2144. {
  2145. return espeak_ng_CompilePhonemeDataPath(rate, NULL, NULL, log, context);
  2146. }
  2147. espeak_ng_STATUS
  2148. espeak_ng_CompilePhonemeDataPath(long rate,
  2149. const char *source_path,
  2150. const char *destination_path,
  2151. FILE *log,
  2152. espeak_ng_ERROR_CONTEXT *context)
  2153. {
  2154. if (!log) log = stderr;
  2155. char fname[sizeof(path_home)+40];
  2156. char phdst[sizeof(path_home)+40]; // Destination: path to the phondata/phontab/phonindex output files.
  2157. if (source_path) {
  2158. sprintf(phsrc, "%s", source_path);
  2159. } else {
  2160. sprintf(phsrc, "%s/../phsource", path_home);
  2161. }
  2162. if (destination_path) {
  2163. sprintf(phdst, "%s", destination_path);
  2164. } else {
  2165. sprintf(phdst, "%s", path_home);
  2166. }
  2167. samplerate_native = samplerate = rate;
  2168. LoadPhData(NULL, NULL);
  2169. if (LoadVoice("", 0) == NULL)
  2170. return ENS_VOICE_NOT_FOUND;
  2171. WavegenInit(rate, 0);
  2172. WavegenSetVoice(voice);
  2173. n_envelopes = 0;
  2174. error_count = 0;
  2175. resample_count = 0;
  2176. memset(markers_used, 0, sizeof(markers_used));
  2177. f_errors = log;
  2178. strncpy0(current_fname, "phonemes", sizeof(current_fname));
  2179. sprintf(fname, "%s/phonemes", phsrc);
  2180. fprintf(log, "Compiling phoneme data: %s\n", fname);
  2181. f_in = fopen(fname, "rb");
  2182. if (f_in == NULL)
  2183. return create_file_error_context(context, errno, fname);
  2184. sprintf(fname, "%s/%s", phsrc, "compile_report");
  2185. f_report = fopen(fname, "w");
  2186. if (f_report == NULL) {
  2187. int error = errno;
  2188. fclose(f_in);
  2189. return create_file_error_context(context, error, fname);
  2190. }
  2191. sprintf(fname, "%s/%s", phdst, "phondata-manifest");
  2192. if ((f_phcontents = fopen(fname, "w")) == NULL)
  2193. f_phcontents = stderr;
  2194. fprintf(f_phcontents,
  2195. "# This file lists the type of data that has been compiled into the\n"
  2196. "# phondata file\n"
  2197. "#\n"
  2198. "# The first character of a line indicates the type of data:\n"
  2199. "# S - A SPECT_SEQ structure\n"
  2200. "# W - A wavefile segment\n"
  2201. "# E - An envelope\n"
  2202. "#\n"
  2203. "# Address is the displacement within phondata of this item\n"
  2204. "#\n"
  2205. "# Address Data file\n"
  2206. "# ------- ---------\n");
  2207. sprintf(fname, "%s/%s", phdst, "phondata");
  2208. f_phdata = fopen(fname, "wb");
  2209. if (f_phdata == NULL) {
  2210. int error = errno;
  2211. fclose(f_in);
  2212. fclose(f_report);
  2213. fclose(f_phcontents);
  2214. return create_file_error_context(context, error, fname);
  2215. }
  2216. sprintf(fname, "%s/%s", phdst, "phonindex");
  2217. f_phindex = fopen(fname, "wb");
  2218. if (f_phindex == NULL) {
  2219. int error = errno;
  2220. fclose(f_in);
  2221. fclose(f_report);
  2222. fclose(f_phcontents);
  2223. fclose(f_phdata);
  2224. return create_file_error_context(context, error, fname);
  2225. }
  2226. sprintf(fname, "%s/%s", phdst, "phontab");
  2227. f_phtab = fopen(fname, "wb");
  2228. if (f_phtab == NULL) {
  2229. int error = errno;
  2230. fclose(f_in);
  2231. fclose(f_report);
  2232. fclose(f_phcontents);
  2233. fclose(f_phdata);
  2234. fclose(f_phindex);
  2235. return create_file_error_context(context, error, fname);
  2236. }
  2237. sprintf(fname, "%s/compile_prog_log", phsrc);
  2238. f_prog_log = fopen(fname, "wb");
  2239. // write a word so that further data doesn't start at displ=0
  2240. Write4Bytes(f_phdata, version_phdata);
  2241. Write4Bytes(f_phdata, samplerate_native);
  2242. Write4Bytes(f_phindex, version_phdata);
  2243. memset(ref_hash_tab, 0, sizeof(ref_hash_tab));
  2244. n_phoneme_tabs = 0;
  2245. stack_ix = 0;
  2246. StartPhonemeTable("base");
  2247. CompilePhonemeFiles();
  2248. EndPhonemeTable();
  2249. WritePhonemeTables();
  2250. fprintf(f_errors, "\nRefs %d, Reused %d\n", count_references, duplicate_references);
  2251. fclose(f_in);
  2252. fclose(f_phcontents);
  2253. fclose(f_phdata);
  2254. fclose(f_phindex);
  2255. fclose(f_phtab);
  2256. if (f_prog_log != NULL)
  2257. fclose(f_prog_log);
  2258. LoadPhData(NULL, NULL);
  2259. CompileReport();
  2260. fclose(f_report);
  2261. if (resample_count > 0) {
  2262. fprintf(f_errors, "\n%d WAV files resampled to %d Hz\n", resample_count, samplerate_native);
  2263. fprintf(log, "Compiled phonemes: %d errors, %d files resampled to %d Hz.\n", error_count, resample_count, samplerate_native);
  2264. } else
  2265. fprintf(log, "Compiled phonemes: %d errors.\n", error_count);
  2266. if (f_errors != stderr && f_errors != stdout)
  2267. fclose(f_errors);
  2268. espeak_ng_STATUS status = ReadPhondataManifest(context);
  2269. if (status != ENS_OK)
  2270. return status;
  2271. return error_count > 0 ? ENS_COMPILE_ERROR : ENS_OK;
  2272. }
  2273. #pragma GCC visibility pop
  2274. static const char *preset_tune_names[] = {
  2275. "s1", "c1", "q1", "e1", NULL
  2276. };
  2277. static const TUNE default_tune = {
  2278. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2279. { 0, 0, 0, 0 },
  2280. { 0, 40, 24, 8, 0, 0, 0, 0 },
  2281. 46, 57, PITCHfall, 16, 0, 0,
  2282. 255, 78, 50, 255,
  2283. 3, 5,
  2284. { -7, -7, -7 }, { -7, -7, -7 },
  2285. PITCHfall, 64, 8,
  2286. PITCHfall, 70, 18, 24, 12,
  2287. PITCHfall, 70, 18, 24, 12, 0,
  2288. { 0, 0, 0, 0, 0, 0, 0, 0 }, 0
  2289. };
  2290. #define N_TUNE_NAMES 100
  2291. MNEM_TAB envelope_names[] = {
  2292. { "fall", 0 },
  2293. { "rise", 2 },
  2294. { "fall-rise", 4 },
  2295. { "fall-rise2", 6 },
  2296. { "rise-fall", 8 },
  2297. { "fall-rise3", 10 },
  2298. { "fall-rise4", 12 },
  2299. { "fall2", 14 },
  2300. { "rise2", 16 },
  2301. { "rise-fall-rise", 18 },
  2302. { NULL, -1 }
  2303. };
  2304. int LookupEnvelopeName(const char *name)
  2305. {
  2306. return LookupMnem(envelope_names, name);
  2307. }
  2308. #pragma GCC visibility push(default)
  2309. espeak_ng_STATUS espeak_ng_CompileIntonation(FILE *log, espeak_ng_ERROR_CONTEXT *context)
  2310. {
  2311. if (!log) log = stderr;
  2312. int ix;
  2313. char *p;
  2314. char c;
  2315. int keyword;
  2316. int n_tune_names = 0;
  2317. int done_split = 0;
  2318. int done_onset = 0;
  2319. int done_last = 0;
  2320. int n_preset_tunes = 0;
  2321. int found;
  2322. int tune_number = 0;
  2323. FILE *f_out;
  2324. TUNE *tune_data;
  2325. TUNE new_tune;
  2326. char name[12];
  2327. char tune_names[N_TUNE_NAMES][12];
  2328. char buf[sizeof(path_home)+150];
  2329. error_count = 0;
  2330. f_errors = log;
  2331. sprintf(buf, "%s/../phsource/intonation.txt", path_home);
  2332. if ((f_in = fopen(buf, "r")) == NULL) {
  2333. sprintf(buf, "%s/../phsource/intonation", path_home);
  2334. if ((f_in = fopen(buf, "r")) == NULL) {
  2335. int error = errno;
  2336. fclose(f_errors);
  2337. return create_file_error_context(context, error, buf);
  2338. }
  2339. }
  2340. for (ix = 0; preset_tune_names[ix] != NULL; ix++)
  2341. strcpy(tune_names[ix], preset_tune_names[ix]);
  2342. n_tune_names = ix;
  2343. n_preset_tunes = ix;
  2344. // make a list of the tune names
  2345. while (!feof(f_in)) {
  2346. if (fgets(buf, sizeof(buf), f_in) == NULL)
  2347. break;
  2348. if ((memcmp(buf, "tune", 4) == 0) && isspace(buf[4])) {
  2349. p = &buf[5];
  2350. while (isspace(*p)) p++;
  2351. ix = 0;
  2352. while ((ix < (int)(sizeof(name) - 1)) && !isspace(*p))
  2353. name[ix++] = *p++;
  2354. name[ix] = 0;
  2355. found = 0;
  2356. for (ix = 0; ix < n_tune_names; ix++) {
  2357. if (strcmp(name, tune_names[ix]) == 0) {
  2358. found = 1;
  2359. break;
  2360. }
  2361. }
  2362. if (found == 0) {
  2363. strncpy0(tune_names[n_tune_names++], name, sizeof(name));
  2364. if (n_tune_names >= N_TUNE_NAMES)
  2365. break;
  2366. }
  2367. }
  2368. }
  2369. rewind(f_in);
  2370. linenum = 1;
  2371. tune_data = (n_tune_names == 0) ? NULL : (TUNE *)calloc(n_tune_names, sizeof(TUNE));
  2372. if (tune_data == NULL) {
  2373. fclose(f_in);
  2374. fclose(f_errors);
  2375. return ENOMEM;
  2376. }
  2377. sprintf(buf, "%s/intonations", path_home);
  2378. f_out = fopen(buf, "wb");
  2379. if (f_out == NULL) {
  2380. int error = errno;
  2381. fclose(f_in);
  2382. fclose(f_errors);
  2383. free(tune_data);
  2384. return create_file_error_context(context, error, buf);
  2385. }
  2386. while (!feof(f_in)) {
  2387. keyword = NextItem(tINTONATION);
  2388. switch (keyword)
  2389. {
  2390. case kTUNE:
  2391. done_split = 0;
  2392. memcpy(&new_tune, &default_tune, sizeof(TUNE));
  2393. NextItem(tSTRING);
  2394. strncpy0(new_tune.name, item_string, sizeof(new_tune.name));
  2395. found = 0;
  2396. tune_number = 0;
  2397. for (ix = 0; ix < n_tune_names; ix++) {
  2398. if (strcmp(new_tune.name, tune_names[ix]) == 0) {
  2399. found = 1;
  2400. tune_number = ix;
  2401. if (tune_data[ix].name[0] != 0)
  2402. found = 2;
  2403. break;
  2404. }
  2405. }
  2406. if (found == 2)
  2407. error("Duplicate tune name: '%s'", new_tune.name);
  2408. if (found == 0)
  2409. error("Bad tune name: '%s;", new_tune.name);
  2410. break;
  2411. case kENDTUNE:
  2412. if (done_onset == 0) {
  2413. new_tune.unstr_start[0] = new_tune.unstr_start[1];
  2414. new_tune.unstr_end[0] = new_tune.unstr_end[1];
  2415. }
  2416. if (done_last == 0) {
  2417. new_tune.unstr_start[2] = new_tune.unstr_start[1];
  2418. new_tune.unstr_end[2] = new_tune.unstr_end[1];
  2419. }
  2420. memcpy(&tune_data[tune_number], &new_tune, sizeof(TUNE));
  2421. break;
  2422. case kTUNE_PREHEAD:
  2423. new_tune.prehead_start = NextItem(tNUMBER);
  2424. new_tune.prehead_end = NextItem(tNUMBER);
  2425. break;
  2426. case kTUNE_ONSET:
  2427. new_tune.onset = NextItem(tNUMBER);
  2428. new_tune.unstr_start[0] = NextItem(tSIGNEDNUMBER);
  2429. new_tune.unstr_end[0] = NextItem(tSIGNEDNUMBER);
  2430. done_onset = 1;
  2431. break;
  2432. case kTUNE_HEADLAST:
  2433. new_tune.head_last = NextItem(tNUMBER);
  2434. new_tune.unstr_start[2] = NextItem(tSIGNEDNUMBER);
  2435. new_tune.unstr_end[2] = NextItem(tSIGNEDNUMBER);
  2436. done_last = 1;
  2437. break;
  2438. case kTUNE_HEADENV:
  2439. NextItem(tSTRING);
  2440. if ((ix = LookupEnvelopeName(item_string)) < 0)
  2441. error("Bad envelope name: '%s'", item_string);
  2442. else
  2443. new_tune.stressed_env = ix;
  2444. new_tune.stressed_drop = NextItem(tNUMBER);
  2445. break;
  2446. case kTUNE_HEAD:
  2447. new_tune.head_max_steps = NextItem(tNUMBER);
  2448. new_tune.head_start = NextItem(tNUMBER);
  2449. new_tune.head_end = NextItem(tNUMBER);
  2450. new_tune.unstr_start[1] = NextItem(tSIGNEDNUMBER);
  2451. new_tune.unstr_end[1] = NextItem(tSIGNEDNUMBER);
  2452. break;
  2453. case kTUNE_HEADEXTEND:
  2454. // up to 8 numbers
  2455. for (ix = 0; ix < (int)(sizeof(new_tune.head_extend)); ix++) {
  2456. if (!isdigit(c = CheckNextChar()) && (c != '-'))
  2457. break;
  2458. new_tune.head_extend[ix] = (NextItem(tSIGNEDNUMBER) * 64) / 100; // convert from percentage to 64ths
  2459. }
  2460. new_tune.n_head_extend = ix; // number of values
  2461. break;
  2462. case kTUNE_NUCLEUS0:
  2463. NextItem(tSTRING);
  2464. if ((ix = LookupEnvelopeName(item_string)) < 0) {
  2465. error("Bad envelope name: '%s'", item_string);
  2466. break;
  2467. }
  2468. new_tune.nucleus0_env = ix;
  2469. new_tune.nucleus0_max = NextItem(tNUMBER);
  2470. new_tune.nucleus0_min = NextItem(tNUMBER);
  2471. break;
  2472. case kTUNE_NUCLEUS1:
  2473. NextItem(tSTRING);
  2474. if ((ix = LookupEnvelopeName(item_string)) < 0) {
  2475. error("Bad envelope name: '%s'", item_string);
  2476. break;
  2477. }
  2478. new_tune.nucleus1_env = ix;
  2479. new_tune.nucleus1_max = NextItem(tNUMBER);
  2480. new_tune.nucleus1_min = NextItem(tNUMBER);
  2481. new_tune.tail_start = NextItem(tNUMBER);
  2482. new_tune.tail_end = NextItem(tNUMBER);
  2483. if (!done_split) {
  2484. // also this as the default setting for 'split'
  2485. new_tune.split_nucleus_env = ix;
  2486. new_tune.split_nucleus_max = new_tune.nucleus1_max;
  2487. new_tune.split_nucleus_min = new_tune.nucleus1_min;
  2488. new_tune.split_tail_start = new_tune.tail_start;
  2489. new_tune.split_tail_end = new_tune.tail_end;
  2490. }
  2491. break;
  2492. case kTUNE_SPLIT:
  2493. NextItem(tSTRING);
  2494. if ((ix = LookupEnvelopeName(item_string)) < 0) {
  2495. error("Bad envelope name: '%s'", item_string);
  2496. break;
  2497. }
  2498. done_split = 1;
  2499. new_tune.split_nucleus_env = ix;
  2500. new_tune.split_nucleus_max = NextItem(tNUMBER);
  2501. new_tune.split_nucleus_min = NextItem(tNUMBER);
  2502. new_tune.split_tail_start = NextItem(tNUMBER);
  2503. new_tune.split_tail_end = NextItem(tNUMBER);
  2504. NextItem(tSTRING);
  2505. item_string[12] = 0;
  2506. for (ix = 0; ix < n_tune_names; ix++) {
  2507. if (strcmp(item_string, tune_names[ix]) == 0)
  2508. break;
  2509. }
  2510. if (ix == n_tune_names)
  2511. error("Tune '%s' not found", item_string);
  2512. else
  2513. new_tune.split_tune = ix;
  2514. break;
  2515. default:
  2516. error("Unexpected: '%s'", item_string);
  2517. break;
  2518. }
  2519. }
  2520. for (ix = 0; ix < n_preset_tunes; ix++) {
  2521. if (tune_data[ix].name[0] == 0)
  2522. error("Tune '%s' not defined", preset_tune_names[ix]);
  2523. }
  2524. fwrite(tune_data, n_tune_names, sizeof(TUNE), f_out);
  2525. free(tune_data);
  2526. fclose(f_in);
  2527. fclose(f_out);
  2528. fprintf(log, "Compiled %d intonation tunes: %d errors.\n", n_tune_names, error_count);
  2529. LoadPhData(NULL, NULL);
  2530. return error_count > 0 ? ENS_COMPILE_ERROR : ENS_OK;
  2531. }
  2532. #pragma GCC visibility pop