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

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