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

compiledata.c 70KB

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