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

compiledata.c 69KB

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