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

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