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

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