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

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