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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784
  1. /*
  2. * Copyright (C) 2005 to 2014 by Jonathan Duddington
  3. * email: [email protected]
  4. * Copyright (C) 2013-2015 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 <stdio.h>
  21. #include <string.h>
  22. #include <strings.h>
  23. #include <ctype.h>
  24. #include <stdlib.h>
  25. #include <time.h>
  26. #if HAVE_STDINT_H
  27. #include <stdint.h>
  28. #endif
  29. #include "speak_lib.h"
  30. #include "espeak_ng.h"
  31. #include "speech.h"
  32. #include "phoneme.h"
  33. #include "synthesize.h"
  34. #include "voice.h"
  35. #include "spect.h"
  36. #include <sys/stat.h>
  37. #ifdef PLATFORM_POSIX
  38. #include <unistd.h>
  39. #endif
  40. typedef struct {
  41. unsigned int value;
  42. char *name;
  43. } NAMETAB;
  44. NAMETAB *manifest = NULL;
  45. int n_manifest;
  46. extern int utf8_in(int *c, const char *buf);
  47. extern int utf8_out(unsigned int c, char *buf);
  48. extern void DrawEnvelopes();
  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. {"prevPhW", tWHICH_PHONEME, 5},
  77. {"thisPh", tWHICH_PHONEME, 1},
  78. {"nextPh", tWHICH_PHONEME, 2},
  79. {"next2Ph", tWHICH_PHONEME, 3},
  80. {"nextPhW", tWHICH_PHONEME, 4},
  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. {
  347. for(ix=0; ix < n_manifest; ix++)
  348. free(manifest[ix].name);
  349. }
  350. if((manifest = (NAMETAB *)realloc(manifest, n_lines * sizeof(NAMETAB))) == NULL)
  351. {
  352. fclose(f);
  353. return;
  354. }
  355. n_manifest = 0;
  356. while(fgets(buf, sizeof(buf), f) != NULL)
  357. {
  358. if(!isalpha(buf[0]))
  359. continue;
  360. if(sscanf(&buf[2], "%x %s", &value, name) == 2)
  361. {
  362. if((p = (char *)malloc(strlen(name)+1)) != NULL)
  363. {
  364. strcpy(p, name);
  365. manifest[n_manifest].value = value;
  366. manifest[n_manifest].name = p;
  367. n_manifest++;
  368. }
  369. }
  370. }
  371. fclose(f);
  372. }
  373. static const char *KeyToMnem(keywtab_t *ktab, int type, int value)
  374. {
  375. while(ktab->mnem != NULL)
  376. {
  377. if(ktab->data == value)
  378. {
  379. if((type == -1) || (type == ktab->type))
  380. return(ktab->mnem);
  381. }
  382. ktab++;
  383. }
  384. return(NULL);
  385. }
  386. static void DecompilePhoneme(FILE *f_out, PHONEME_TAB *ph, int compile_phoneme)
  387. {
  388. USHORT *pc;
  389. int instn;
  390. int instn_category;
  391. int address, address2;
  392. int data1;
  393. int type2;
  394. int ix;
  395. int any;
  396. const char *name;
  397. char buf[120];
  398. static const char *instn_category_string[16] = {
  399. "", "", "IF", "IF OR",
  400. "", "", "", "",
  401. "", "", "", "FMT",
  402. "WAV", "NextVowelStart", "PrevVowelEnd", "+wav"
  403. };
  404. static const char *nextPh_string[6] = {
  405. "prevPh", "thisPh", "nextPh", "next2Ph", "nextPhW", "**",
  406. };
  407. static const char *instn0_string[] = {
  408. "invalid", "RETURN", "Continue", "DeleteNextPhoneme",
  409. };
  410. static const char *instn10_string[] = {
  411. "", "VowelIn", "VowelOut", "Tone", "",
  412. };
  413. static const char *instn_jumps[] = {
  414. "JMP", "Invalid", "Invalid", "Invalid",
  415. "JMP false", "SwitchNextVowelType", "SwitchPrevVowelType", "Invalid"
  416. };
  417. static char instn1_paramtype[] = {
  418. 0, 3, 3, 3, 3, 3, 3, 1,
  419. 1, 1, 1, 1, 1, 0, 0, 0,
  420. 3, 3, 3, 3, 3, 3, 3, 3,
  421. 0, 0, 0, 0, 0, 0, 0, 0
  422. };
  423. return;
  424. if(compile_phoneme)
  425. {
  426. fprintf(f_out,"\nPhoneme %s (%d)\n",WordToString(ph->mnemonic),ph->code);
  427. }
  428. else
  429. {
  430. fprintf(f_out,"\nProcedure %s\n",proc_names[n_procs]);
  431. }
  432. pc = prog_buf;
  433. while(pc < prog_out)
  434. {
  435. instn = *pc++;
  436. instn_category = (instn >> 12) & 0xf;
  437. data1 = instn & 0xff;
  438. type2 = (instn >> 8) & 0xf;
  439. fprintf(f_out, " %.3x: %.4x %s",(unsigned int)(pc-prog_buf),instn,instn_category_string[instn_category]);
  440. switch(instn_category)
  441. {
  442. case 0:
  443. case 1:
  444. type2 = instn >> 8;
  445. if(instn < 0x100)
  446. {
  447. if(data1 > 2)
  448. data1 = 0;
  449. fprintf(f_out,"%s",instn0_string[data1]);
  450. }
  451. else
  452. if(type2 == i_IPA_NAME)
  453. {
  454. for(ix = 0; ix < data1; ix += 2)
  455. {
  456. instn = *pc++;
  457. buf[ix] = instn >> 8;
  458. buf[ix+1] = instn & 0xff;
  459. }
  460. buf[ix] = 0;
  461. fprintf(f_out,"ipa %s",buf);
  462. }
  463. else
  464. {
  465. fprintf(f_out,"%s(",KeyToMnem(keywords, tINSTRN1, type2));
  466. switch(instn1_paramtype[type2])
  467. {
  468. case 0:
  469. fprintf(f_out,"%.4x",instn);
  470. break;
  471. case 1:
  472. fprintf(f_out,"%d",data1);
  473. break;
  474. case 3:
  475. fprintf(f_out,"%s",WordToString(phoneme_tab2[data1].mnemonic));
  476. break;
  477. }
  478. fprintf(f_out,")");
  479. }
  480. break;
  481. case 2:
  482. case 3:
  483. if(type2 < 12)
  484. {
  485. fprintf(f_out," %s(",nextPh_string[type2 % 6]);
  486. if(type2 >= 6)
  487. {
  488. switch(data1 >> 5)
  489. {
  490. case 0:
  491. name = KeyToMnem(keywords, tPHONEME_TYPE, (data1 & 0x1f));
  492. if(name != NULL)
  493. fprintf(f_out,"is%s",name);
  494. else
  495. fprintf(f_out,"%d %d",(data1 >> 5),(data1 & 0x1f));
  496. break;
  497. case 1:
  498. fprintf(f_out,"%d %d",(data1 >> 5),(data1 & 0x1f));
  499. break;
  500. case 2:
  501. fprintf(f_out,"%d %d",(data1 >> 5),(data1 & 0x1f));
  502. break;
  503. case 4:
  504. name = KeyToMnem(k_properties, -1, 0x80+(data1 & 0x1f));
  505. if(name != NULL)
  506. fprintf(f_out,"%s",name);
  507. else
  508. fprintf(f_out,"%d %d",(data1 >> 5),(data1 & 0x1f));
  509. break;
  510. default:
  511. fprintf(f_out,"%d %d",(data1 >> 5),(data1 & 0x1f));
  512. break;
  513. }
  514. }
  515. else
  516. {
  517. fprintf(f_out,"%s",WordToString(phoneme_tab2[data1].mnemonic));
  518. }
  519. }
  520. else
  521. if(type2 == 8)
  522. {
  523. // list of numbers
  524. fprintf(f_out," StressLevel(");
  525. any = 0;
  526. for(ix=0; ix<8; ix++)
  527. {
  528. if(data1 & (1 << ix))
  529. {
  530. if(any)
  531. fputc(',',f_out);
  532. any = 1;
  533. fprintf(f_out,"%d",ix);
  534. }
  535. }
  536. }
  537. else
  538. {
  539. name = KeyToMnem(k_conditions, tTEST, instn & 0xfff);
  540. fprintf(f_out,"%s(",name);
  541. }
  542. fprintf(f_out,")");
  543. break;
  544. case 6:
  545. fprintf(f_out,"%s",instn_jumps[(instn >> 9) & 7]);
  546. fprintf(f_out," %d",instn & 0x1ff);
  547. break;
  548. case 9:
  549. address = ((data1 & 0xf) << 4) + *pc++;
  550. fprintf(f_out,"CALL %.5x",address);
  551. break;
  552. case 10:
  553. fprintf(f_out,"%s",instn10_string[type2]);
  554. switch(type2)
  555. {
  556. case 1:
  557. case 2:
  558. address = (data1 << 16) + pc[0];
  559. address2 = (pc[1] << 16) + pc[2];
  560. pc += 3;
  561. fprintf(f_out, " %.6x %.8x",address,address2);
  562. break;
  563. case 3:
  564. address = ((instn & 0xf) << 16) + *pc++;
  565. address2 = ((instn & 0xf0) << 12) + *pc++;
  566. fprintf(f_out, " %.5x %.5x",address,address2);
  567. break;
  568. }
  569. break;
  570. case 11:
  571. case 12:
  572. case 13:
  573. case 14:
  574. case 15:
  575. address = ((instn & 0xf) << 16) + *pc++;
  576. fprintf(f_out, " %d %.5x",(instn >> 4) & 0xff,address*4);
  577. break;
  578. }
  579. fprintf(f_out,"\n");
  580. }
  581. }
  582. static int n_phoneme_tabs;
  583. static int n_phcodes;
  584. // outout files
  585. static FILE *f_phdata;
  586. static FILE *f_phindex;
  587. static FILE *f_phtab;
  588. static FILE *f_phcontents;
  589. static FILE *f_errors = NULL;
  590. static FILE *f_prog_log = NULL;
  591. static FILE *f_report;
  592. static FILE *f_in;
  593. static int f_in_linenum;
  594. static int f_in_displ;
  595. static int linenum;
  596. static int count_references = 0;
  597. static int duplicate_references = 0;
  598. static int count_frames = 0;
  599. static int error_count = 0;
  600. static int resample_count = 0;
  601. static int resample_fails = 0;
  602. static int then_count = 0;
  603. static int after_if = 0;
  604. static char current_fname[80];
  605. static int markers_used[8];
  606. typedef struct {
  607. void *link;
  608. int value;
  609. int ph_mnemonic;
  610. short ph_table;
  611. char string[1];
  612. } REF_HASH_TAB;
  613. static REF_HASH_TAB *ref_hash_tab[256];
  614. #define N_ENVELOPES 30
  615. int n_envelopes = 0;
  616. char envelope_paths[N_ENVELOPES][80];
  617. unsigned char envelope_dat[N_ENVELOPES][ENV_LEN];
  618. typedef struct {
  619. FILE *file;
  620. int linenum;
  621. char fname[80];
  622. } STACK;
  623. #define N_STACK 12
  624. int stack_ix;
  625. STACK stack[N_STACK];
  626. #define N_IF_STACK 12
  627. int if_level;
  628. typedef struct {
  629. USHORT *p_then;
  630. USHORT *p_else;
  631. int returned;
  632. } IF_STACK;
  633. IF_STACK if_stack[N_IF_STACK];
  634. enum {
  635. tENDFILE = 1,
  636. tSTRING,
  637. tNUMBER,
  638. tSIGNEDNUMBER,
  639. tPHONEMEMNEM,
  640. tOPENBRACKET,
  641. tKEYWORD,
  642. tCONDITION,
  643. tPROPERTIES,
  644. tINTONATION,
  645. };
  646. int item_type;
  647. int item_terminator;
  648. #define N_ITEM_STRING 256
  649. char item_string[N_ITEM_STRING];
  650. static int ref_sorter(char **a, char **b)
  651. {
  652. int ix;
  653. REF_HASH_TAB *p1 = (REF_HASH_TAB *)(*a);
  654. REF_HASH_TAB *p2 = (REF_HASH_TAB *)(*b);
  655. ix = strcoll(p1->string,p2->string);
  656. if(ix != 0)
  657. return ix;
  658. ix = p1->ph_table - p2->ph_table;
  659. if(ix != 0)
  660. return ix;
  661. return(p1->ph_mnemonic - p2->ph_mnemonic);
  662. }
  663. static void CompileReport(void)
  664. {
  665. int ix;
  666. int hash;
  667. int n;
  668. REF_HASH_TAB *p;
  669. REF_HASH_TAB **list;
  670. const char *data_path;
  671. int prev_table;
  672. int procedure_num;
  673. int prev_mnemonic;
  674. if(f_report == NULL)
  675. return;
  676. // make a list of all the references and sort it
  677. list = (REF_HASH_TAB **)malloc((count_references)* sizeof(REF_HASH_TAB *));
  678. if(list == NULL)
  679. return;
  680. fprintf(f_report,"\n%d phoneme tables\n",n_phoneme_tabs);
  681. fprintf(f_report," new total\n");
  682. for(ix=0; ix<n_phoneme_tabs; ix++)
  683. {
  684. fprintf(f_report,"%8s %3d %4d\n",phoneme_tab_list2[ix].name, phoneme_tab_list2[ix].n_phonemes, n_phcodes_list[ix]+1);
  685. }
  686. fputc('\n',f_report);
  687. fprintf(f_report,"Data file Used by\n");
  688. ix = 0;
  689. for(hash=0; (hash < 256) && (ix < count_references); hash++)
  690. {
  691. p = ref_hash_tab[hash];
  692. while(p != NULL)
  693. {
  694. list[ix++] = p;
  695. p = (REF_HASH_TAB *)(p->link);
  696. }
  697. }
  698. n = ix;
  699. qsort((void *)list,n,sizeof(REF_HASH_TAB *),(int (*)(const void *,const void *))ref_sorter);
  700. data_path = "";
  701. prev_mnemonic = 0;
  702. prev_table = 0;
  703. for(ix=0; ix<n; ix++)
  704. {
  705. int j = 0;
  706. if(strcmp(list[ix]->string, data_path) != 0)
  707. {
  708. data_path = list[ix]->string;
  709. j = strlen(data_path);
  710. fprintf(f_report,"%s",data_path);
  711. }
  712. else
  713. {
  714. if((list[ix]->ph_table == prev_table) && (list[ix]->ph_mnemonic == prev_mnemonic))
  715. continue; // same phoneme, don't list twice
  716. }
  717. while(j < 14)
  718. {
  719. fputc(' ',f_report); // pad filename with spaces
  720. j++;
  721. }
  722. prev_mnemonic = list[ix]->ph_mnemonic;
  723. if((prev_mnemonic >> 24) == 'P')
  724. {
  725. // a procedure, not a phoneme
  726. procedure_num = atoi(WordToString(prev_mnemonic));
  727. fprintf(f_report," %s %s", phoneme_tab_list2[prev_table = list[ix]->ph_table].name, proc_names[procedure_num]);
  728. }
  729. else
  730. {
  731. fprintf(f_report," [%s] %s",WordToString(prev_mnemonic), phoneme_tab_list2[prev_table = list[ix]->ph_table].name);
  732. }
  733. fputc('\n',f_report);
  734. }
  735. for(ix=0; ix<n; ix++)
  736. {
  737. free(list[ix]);
  738. list[ix] = NULL;
  739. }
  740. free(list);
  741. list = NULL;
  742. fclose(f_report);
  743. }
  744. #ifdef PLATFORM_WINDOWS
  745. int strcasecmp(const char *s1, const char *s2)
  746. {
  747. int ix=0;
  748. int diff;
  749. for(;; )
  750. {
  751. if((diff = (tolower(s1[ix]) - tolower(s2[ix]))) != 0)
  752. return(diff);
  753. if((s1[ix] == 0) || (s2[ix] == 0))
  754. return(diff);
  755. ix++;
  756. }
  757. }
  758. #endif
  759. static void error(const char *format, const char *string)
  760. {
  761. if(string==NULL)
  762. string = "";
  763. fprintf(f_errors,"%4d: ",linenum-1);
  764. fprintf(f_errors,format,string);
  765. fprintf(f_errors,"\n");
  766. error_count++;
  767. }
  768. static void Error(const char *string)
  769. {
  770. error("%s",string);
  771. }
  772. static FILE *fopen_log(FILE *f_log, const char *fname,const char *access)
  773. {
  774. // performs fopen, but produces error message to f_log if it fails
  775. FILE *f;
  776. if((f = fopen(fname,access)) == NULL)
  777. {
  778. if(f_log != NULL)
  779. fprintf(f_log,"Can't access (%s) file '%s'\n",access,fname);
  780. }
  781. return(f);
  782. }
  783. static unsigned int StringToWord(const char *string)
  784. {
  785. // Pack 4 characters into a word
  786. int ix;
  787. unsigned char c;
  788. unsigned int word;
  789. if(string==NULL)
  790. return(0);
  791. word = 0;
  792. for(ix=0; ix<4; ix++)
  793. {
  794. if(string[ix]==0) break;
  795. c = string[ix];
  796. word |= (c << (ix*8));
  797. }
  798. return(word);
  799. }
  800. static MNEM_TAB reserved_phonemes[] = {
  801. {"_\001", phonCONTROL}, // NOT USED
  802. {"%", phonSTRESS_U},
  803. {"%%", phonSTRESS_D},
  804. {",", phonSTRESS_2},
  805. {",,", phonSTRESS_3},
  806. {"'", phonSTRESS_P},
  807. {"''", phonSTRESS_P2},
  808. {"=", phonSTRESS_PREV}, // stress previous syllable
  809. {"_:", phonPAUSE}, // pause
  810. {"_", phonPAUSE_SHORT}, // short pause
  811. {"_!", phonPAUSE_NOLINK}, // short pause, no link
  812. {":", phonLENGTHEN},
  813. {"@", phonSCHWA},
  814. {"@-", phonSCHWA_SHORT},
  815. {"||", phonEND_WORD},
  816. {"1", phonDEFAULTTONE}, // (numeral 1) default tone (for tone language)
  817. {"#X1",phonCAPITAL}, // capital letter indication
  818. {"?", phonGLOTTALSTOP}, // glottal stop
  819. {"-", phonSYLLABIC}, // syllabic consonant
  820. {"_^_",phonSWITCH}, // Change language
  821. {"_X1",phonX1}, // a language specific action
  822. {"_|", phonPAUSE_VSHORT}, // very short pause
  823. {"_::",phonPAUSE_LONG}, // long pause
  824. {"t#", phonT_REDUCED}, // reduced version of [t]
  825. {"'!", phonSTRESS_TONIC}, // stress - emphasized
  826. {"_;_",phonPAUSE_CLAUSE}, // clause pause
  827. {"#@", phonVOWELTYPES}, // vowel type groups, these must be consecutive
  828. {"#a", phonVOWELTYPES+1},
  829. {"#e", phonVOWELTYPES+2},
  830. {"#i", phonVOWELTYPES+3},
  831. {"#o", phonVOWELTYPES+4},
  832. {"#u", phonVOWELTYPES+5},
  833. {NULL, 0}
  834. };
  835. static void ReservePhCodes()
  836. {
  837. // Reserve phoneme codes which have fixed numbers so that they can be
  838. // referred to from the program code.
  839. unsigned int word;
  840. MNEM_TAB *p;
  841. p = reserved_phonemes;
  842. while(p->mnem != NULL)
  843. {
  844. word = StringToWord(p->mnem);
  845. phoneme_tab2[p->value].mnemonic = word;
  846. phoneme_tab2[p->value].code = p->value;
  847. if(n_phcodes <= p->value)
  848. n_phcodes = p->value+1;
  849. p++;
  850. }
  851. }
  852. static int LookupPhoneme(const char *string, int control)
  853. {
  854. // control = 0 explicit declaration
  855. // control = 1 declare phoneme if not found
  856. // control = 2 start looking after control & stress phonemes
  857. int ix;
  858. int start;
  859. int use;
  860. unsigned int word;
  861. if(strcmp(string,"NULL")==0)
  862. return(1);
  863. ix = strlen(string);
  864. if((ix==0) || (ix> 4))
  865. {
  866. error("Bad phoneme name '%s'",string);
  867. }
  868. word = StringToWord(string);
  869. // don't use phoneme number 0, reserved for string terminator
  870. start = 1;
  871. if(control==2)
  872. start = 8; // don't look for control and stress phonemes (allows these characters to be
  873. // used for other purposes)
  874. use = 0;
  875. for(ix=start; ix<n_phcodes; ix++)
  876. {
  877. if(phoneme_tab2[ix].mnemonic == word)
  878. return(ix);
  879. if((use==0) && (phoneme_tab2[ix].mnemonic == 0))
  880. {
  881. use = ix;
  882. }
  883. }
  884. if(use == 0)
  885. {
  886. if(control == 0)
  887. return(-1);
  888. if(n_phcodes >= N_PHONEME_TAB-1)
  889. return(-1); // phoneme table is full
  890. use = n_phcodes++;
  891. }
  892. // add this phoneme to the phoneme table
  893. phoneme_tab2[use].mnemonic = word;
  894. phoneme_tab2[use].type = phINVALID;
  895. phoneme_tab2[use].program = linenum; // for error report if the phoneme remains undeclared
  896. return(use);
  897. }
  898. static unsigned int get_char()
  899. {
  900. unsigned int c;
  901. c = fgetc(f_in);
  902. if(c == '\n')
  903. linenum++;
  904. return(c);
  905. }
  906. static void unget_char(unsigned int c)
  907. {
  908. ungetc(c,f_in);
  909. if(c == '\n')
  910. linenum--;
  911. }
  912. int CheckNextChar()
  913. {
  914. int c;
  915. while(((c = get_char()) == ' ') || (c == '\t'));
  916. unget_char(c);
  917. return(c);
  918. }
  919. static int NextItem(int type)
  920. {
  921. int acc;
  922. unsigned char c=0;
  923. unsigned char c2;
  924. int ix;
  925. int sign;
  926. char *p;
  927. keywtab_t *pk;
  928. item_type = -1;
  929. f_in_displ = ftell(f_in);
  930. f_in_linenum = linenum;
  931. while(!feof(f_in))
  932. {
  933. c = get_char();
  934. if(c=='/')
  935. {
  936. if((c2 = get_char())=='/')
  937. {
  938. // comment, ignore to end of line
  939. while(!feof(f_in) && ((c = get_char()) != '\n'));
  940. }
  941. else
  942. {
  943. unget_char(c2);
  944. }
  945. }
  946. if(!isspace(c))
  947. {
  948. break;
  949. }
  950. }
  951. if(feof(f_in))
  952. return(-2);
  953. if(c == '(')
  954. {
  955. if(type == tOPENBRACKET)
  956. return(1);
  957. return(-1);
  958. }
  959. ix = 0;
  960. while(!feof(f_in) && !isspace(c) && (c != '(') && (c != ')') && (c != ','))
  961. {
  962. if(c == '\\')
  963. c = get_char();
  964. item_string[ix++] = c;
  965. c = get_char();
  966. if(feof(f_in))
  967. break;
  968. if(item_string[ix-1] == '=')
  969. break;
  970. }
  971. item_string[ix] = 0;
  972. while(isspace(c))
  973. {
  974. c = get_char();
  975. }
  976. item_terminator = ' ';
  977. if((c == ')') || (c == '(') || (c == ','))
  978. item_terminator = c;
  979. if((c == ')') || (c == ','))
  980. c = ' ';
  981. if(!feof(f_in))
  982. unget_char(c);
  983. if(type == tSTRING)
  984. {
  985. return(0);
  986. }
  987. if((type == tNUMBER) || (type == tSIGNEDNUMBER))
  988. {
  989. acc = 0;
  990. sign = 1;
  991. p = item_string;
  992. if((*p == '-') && (type == tSIGNEDNUMBER))
  993. {
  994. sign = -1;
  995. p++;
  996. }
  997. if(!isdigit(*p))
  998. {
  999. if((type == tNUMBER) && (*p == '-'))
  1000. error("Expected an unsigned number",NULL);
  1001. else
  1002. error("Expected a number",NULL);
  1003. }
  1004. while(isdigit(*p))
  1005. {
  1006. acc *= 10;
  1007. acc += (*p - '0');
  1008. p++;
  1009. }
  1010. return(acc * sign);
  1011. }
  1012. if((type >= tKEYWORD) && (type <= tINTONATION))
  1013. {
  1014. pk = keyword_tabs[type-tKEYWORD];
  1015. while(pk->mnem != NULL)
  1016. {
  1017. if(strcmp(item_string,pk->mnem)==0)
  1018. {
  1019. item_type = pk->type;
  1020. return(pk->data);
  1021. }
  1022. pk++;
  1023. }
  1024. item_type = -1;
  1025. return(-1); // keyword not found
  1026. }
  1027. if(type == tPHONEMEMNEM)
  1028. {
  1029. return(LookupPhoneme(item_string,2));
  1030. }
  1031. return(-1);
  1032. }
  1033. static int NextItemMax(int max)
  1034. {
  1035. // Get a number, but restrict value to max
  1036. int value;
  1037. char msg[80];
  1038. value = NextItem(tNUMBER);
  1039. if(value > max)
  1040. {
  1041. sprintf(msg, "Value %d is greater than maximum %d", value, max);
  1042. error(msg, NULL);
  1043. value = max;
  1044. }
  1045. return(value);
  1046. }
  1047. static int NextItemBrackets(int type, int control)
  1048. {
  1049. // Expect a parameter inside parantheses
  1050. // control: bit 0 0= need (
  1051. // bit 1 1= allow comma
  1052. int value;
  1053. if((control & 1) == 0)
  1054. {
  1055. if(!NextItem(tOPENBRACKET))
  1056. {
  1057. error("Expected '('", NULL);
  1058. }
  1059. }
  1060. value = NextItem(type);
  1061. if((control & 2) && (item_terminator == ','))
  1062. return(value);
  1063. if(item_terminator != ')')
  1064. {
  1065. error("Expected ')'", NULL);
  1066. }
  1067. return(value);
  1068. }
  1069. static void UngetItem()
  1070. {
  1071. fseek(f_in,f_in_displ,SEEK_SET);
  1072. linenum = f_in_linenum;
  1073. }
  1074. static int Range(int value, int divide, int min, int max)
  1075. {
  1076. if(value < 0)
  1077. value -= divide/2;
  1078. else
  1079. value += divide/2;
  1080. value = value / divide;
  1081. if(value > max)
  1082. value = max;
  1083. if(value < min)
  1084. value = min;
  1085. return(value - min);
  1086. }
  1087. int CompileVowelTransition(int which)
  1088. {
  1089. // Compile a vowel transition
  1090. int key;
  1091. int len=0;
  1092. int rms=0;
  1093. int f1=0;
  1094. int f2=0;
  1095. int f2_min=0;
  1096. int f2_max=0;
  1097. int f3_adj=0;
  1098. int f3_amp=0;
  1099. int flags=0;
  1100. int vcolour=0;
  1101. int x;
  1102. int instn = i_VOWELIN;
  1103. int word1;
  1104. int word2;
  1105. if(which==1)
  1106. {
  1107. len = 50 / 2; // defaults for transition into vowel
  1108. rms = 25 / 2;
  1109. if(phoneme_out->type == phSTOP)
  1110. {
  1111. len = 42 / 2; // defaults for transition into vowel
  1112. rms = 30 / 2;
  1113. }
  1114. }
  1115. else
  1116. if(which==2)
  1117. {
  1118. instn = i_VOWELOUT;
  1119. len = 36 / 2; // defaults for transition out of vowel
  1120. rms = 16 / 2;
  1121. }
  1122. for(;; )
  1123. {
  1124. key = NextItem(tKEYWORD);
  1125. if(item_type != tTRANSITION)
  1126. {
  1127. UngetItem();
  1128. break;
  1129. }
  1130. switch(key & 0xf)
  1131. {
  1132. case 1:
  1133. len = Range(NextItem(tNUMBER), 2, 0, 63) & 0x3f;
  1134. flags |= 1;
  1135. break;
  1136. case 2:
  1137. rms = Range(NextItem(tNUMBER), 2, 0, 31) & 0x1f;
  1138. flags |= 1;
  1139. break;
  1140. case 3:
  1141. f1 = NextItem(tNUMBER);
  1142. break;
  1143. case 4:
  1144. f2 = Range(NextItem(tNUMBER), 50, 0, 63) & 0x3f;
  1145. f2_min = Range(NextItem(tSIGNEDNUMBER), 50, -15, 15) & 0x1f;
  1146. f2_max = Range(NextItem(tSIGNEDNUMBER), 50, -15, 15) & 0x1f;
  1147. if(f2_min > f2_max)
  1148. {
  1149. x = f2_min;
  1150. f2_min = f2_max;
  1151. f2_max = x;
  1152. }
  1153. break;
  1154. case 5:
  1155. f3_adj = Range(NextItem(tSIGNEDNUMBER), 50, -15, 15) & 0x1f;
  1156. f3_amp = Range(NextItem(tNUMBER), 8, 0, 15) & 0x1f;
  1157. break;
  1158. case 6:
  1159. flags |= 2; // break
  1160. break;
  1161. case 7:
  1162. flags |= 4; // rate
  1163. break;
  1164. case 8:
  1165. flags |= 8; // glstop
  1166. break;
  1167. case 9:
  1168. flags |= 16; // lenadd
  1169. break;
  1170. case 10:
  1171. flags |= 32; // f4
  1172. break;
  1173. case 11:
  1174. flags |= 64; // paus
  1175. break;
  1176. case 12:
  1177. vcolour = NextItem(tNUMBER);
  1178. break;
  1179. case 13:
  1180. // set rms of 1st frame as fraction of rms of 2nd frame (1/30ths)
  1181. rms = (Range(NextItem(tNUMBER), 1, 0, 31) & 0x1f) | 0x20;
  1182. flags |= 1;
  1183. break;
  1184. }
  1185. }
  1186. word1 = len + (rms << 6) + (flags << 12);
  1187. word2 = f2 + (f2_min << 6) + (f2_max << 11) + (f3_adj << 16) + (f3_amp << 21) + (f1 << 26) + (vcolour << 29);
  1188. prog_out[0] = instn + ((word1 >> 16) & 0xff);
  1189. prog_out[1] = word1;
  1190. prog_out[2] = word2 >> 16;
  1191. prog_out[3] = word2;
  1192. prog_out += 4;
  1193. return(0);
  1194. }
  1195. int LoadSpect(const char *path, int control)
  1196. {
  1197. SpectSeq *spectseq;
  1198. int peak;
  1199. int displ;
  1200. int frame;
  1201. int n_frames;
  1202. int ix;
  1203. int x, x2;
  1204. int rms;
  1205. float total;
  1206. float pkheight;
  1207. int marker1_set=0;
  1208. int frame_vowelbreak=0;
  1209. int klatt_flag=0;
  1210. SpectFrame *fr;
  1211. frame_t *fr_out;
  1212. char filename[sizeof(path_home)+20];
  1213. SPECT_SEQ seq_out;
  1214. SPECT_SEQK seqk_out;
  1215. // create SpectSeq and import data
  1216. spectseq = SpectSeqCreate();
  1217. if(spectseq == NULL)
  1218. {
  1219. Error("Failed to create SpectSeq");
  1220. return(0);
  1221. }
  1222. snprintf(filename, sizeof(filename), "%s/../phsource/%s", path_home, path);
  1223. LoadSpectSeq(spectseq, filename);
  1224. if(spectseq->frames == NULL)
  1225. {
  1226. error("Bad vowel file, no frames: '%s'",path);
  1227. SpectSeqDestroy(spectseq);
  1228. return(0);
  1229. }
  1230. // do we need additional klatt data ?
  1231. for(frame=0; frame < spectseq->numframes; frame++)
  1232. {
  1233. for(ix=5; ix<N_KLATTP2; ix++)
  1234. {
  1235. if(spectseq->frames[frame]->klatt_param[ix] != 0)
  1236. klatt_flag = FRFLAG_KLATT;
  1237. }
  1238. }
  1239. displ = ftell(f_phdata);
  1240. seq_out.n_frames=0;
  1241. seq_out.sqflags=0;
  1242. seq_out.length_total=0;
  1243. total = 0;
  1244. for(frame=0; frame < spectseq->numframes; frame++)
  1245. {
  1246. if(spectseq->frames[frame]->keyframe)
  1247. {
  1248. if(seq_out.n_frames == 1)
  1249. {
  1250. frame_vowelbreak = frame;
  1251. }
  1252. if(spectseq->frames[frame]->markers & 0x2)
  1253. {
  1254. // marker 1 is set
  1255. marker1_set = 1;
  1256. }
  1257. seq_out.n_frames++;
  1258. if(frame > 0)
  1259. total += spectseq->frames[frame-1]->length;
  1260. }
  1261. }
  1262. seq_out.length_total = (int)total;
  1263. if((control & 1) && (marker1_set == 0))
  1264. {
  1265. // This is a vowel, but no Vowel Break marker is set
  1266. // set a marker flag for the second frame of a vowel
  1267. spectseq->frames[frame_vowelbreak]->markers |= FRFLAG_VOWEL_CENTRE;
  1268. }
  1269. n_frames = 0;
  1270. for(frame=0; frame < spectseq->numframes; frame++)
  1271. {
  1272. fr = spectseq->frames[frame];
  1273. if(fr->keyframe)
  1274. {
  1275. if(klatt_flag)
  1276. fr_out = &seqk_out.frame[n_frames];
  1277. else
  1278. fr_out = (frame_t *)&seq_out.frame[n_frames];
  1279. x = (int)(fr->length + 0.5); // round to nearest mS
  1280. if(x > 255) x = 255;
  1281. fr_out->length = x;
  1282. fr_out->frflags = fr->markers | klatt_flag;
  1283. rms = (int)GetFrameRms(fr, spectseq->amplitude);
  1284. if(rms > 255) rms = 255;
  1285. fr_out->rms = rms;
  1286. if(n_frames == (seq_out.n_frames-1))
  1287. fr_out->length = 0; // give last frame zero length
  1288. // write: peak data
  1289. count_frames++;
  1290. for(peak=0; peak < 8; peak++)
  1291. {
  1292. if(peak < 7)
  1293. fr_out->ffreq[peak] = fr->peaks[peak].pkfreq;
  1294. pkheight = spectseq->amplitude * fr->amp_adjust * fr->peaks[peak].pkheight;
  1295. pkheight = pkheight/640000;
  1296. if(pkheight > 255) pkheight = 255;
  1297. fr_out->fheight[peak] = (int)pkheight;
  1298. if(peak < 6)
  1299. {
  1300. x = fr->peaks[peak].pkwidth/4;
  1301. if(x > 255) x = 255;
  1302. fr_out->fwidth[peak] = x;
  1303. if(peak < 3)
  1304. {
  1305. x2 = fr->peaks[peak].pkright/4;
  1306. if(x2 > 255) x2 = 255;
  1307. fr_out->fright[peak] = x2;
  1308. }
  1309. }
  1310. if(peak < 4)
  1311. {
  1312. x = fr->peaks[peak].klt_bw / 2;
  1313. if(x > 255) x = 255;
  1314. fr_out->bw[peak] = x;
  1315. }
  1316. }
  1317. for(ix=0; ix<5; ix++)
  1318. {
  1319. fr_out->klattp[ix] = fr->klatt_param[ix];
  1320. fr_out->klattp[KLATT_FNZ] = fr->klatt_param[KLATT_FNZ] / 2;
  1321. }
  1322. if(klatt_flag)
  1323. {
  1324. // additional klatt parameters
  1325. for(ix=0; ix<5; ix++)
  1326. {
  1327. fr_out->klattp2[ix] = fr->klatt_param[ix+5];
  1328. }
  1329. for(peak=0; peak<7; peak++)
  1330. {
  1331. fr_out->klatt_ap[ix] = fr->peaks[peak].klt_ap;
  1332. x = fr->peaks[peak].klt_bp / 2;
  1333. if(x > 255) x = 255;
  1334. fr_out->klatt_bp[ix] = x;
  1335. }
  1336. }
  1337. if(fr_out->bw[1] == 0)
  1338. {
  1339. fr_out->bw[0] = 89 / 2;
  1340. fr_out->bw[1] = 90 / 2;
  1341. fr_out->bw[2] = 140 / 2;
  1342. fr_out->bw[3] = 260 / 2;
  1343. }
  1344. n_frames++;
  1345. }
  1346. }
  1347. if(klatt_flag)
  1348. {
  1349. seqk_out.n_frames = seq_out.n_frames;
  1350. seqk_out.sqflags = seq_out.sqflags;
  1351. seqk_out.length_total = seq_out.length_total;
  1352. ix = (char *)(&seqk_out.frame[seqk_out.n_frames]) - (char *)(&seqk_out);
  1353. ix = (ix+3) & 0xfffc; // round up to multiple of 4 bytes
  1354. fwrite(&seqk_out,ix,1,f_phdata);
  1355. }
  1356. else
  1357. {
  1358. ix = (char *)(&seq_out.frame[seq_out.n_frames]) - (char *)(&seq_out);
  1359. ix = (ix+3) & 0xfffc; // round up to multiple of 4 bytes
  1360. fwrite(&seq_out,ix,1,f_phdata);
  1361. }
  1362. SpectSeqDestroy(spectseq);
  1363. return(displ);
  1364. }
  1365. static int LoadWavefile(FILE *f, const char *fname)
  1366. {
  1367. int displ;
  1368. unsigned char c1;
  1369. unsigned char c3;
  1370. int c2;
  1371. int sample;
  1372. int sample2;
  1373. float x;
  1374. int max = 0;
  1375. int length;
  1376. int sr1, sr2;
  1377. int failed;
  1378. int len;
  1379. int resample_wav = 0;
  1380. const char *fname2;
  1381. char fname_temp[100];
  1382. char msg[120];
  1383. int scale_factor=0;
  1384. fseek(f,24,SEEK_SET);
  1385. sr1 = Read4Bytes(f);
  1386. sr2 = Read4Bytes(f);
  1387. fseek(f,40,SEEK_SET);
  1388. if((sr1 != samplerate_native) || (sr2 != sr1*2))
  1389. {
  1390. int fd_temp;
  1391. char command[sizeof(path_home)+250];
  1392. failed = 0;
  1393. #ifdef PLATFORM_POSIX
  1394. strcpy(fname_temp,"/tmp/espeakXXXXXX");
  1395. if((fd_temp = mkstemp(fname_temp)) >= 0)
  1396. {
  1397. close(fd_temp);
  1398. }
  1399. #else
  1400. strcpy(fname_temp,tmpnam(NULL));
  1401. #endif
  1402. fname2 = fname;
  1403. len = strlen(fname);
  1404. if(strcmp(&fname[len-4], ".wav") == 0)
  1405. {
  1406. strcpy(msg, fname);
  1407. msg[len-4] = 0;
  1408. fname2 = msg;
  1409. }
  1410. sprintf(command,"sox \"%s/../phsource/%s.wav\" -r %d -c1 -t wav %s\n",path_home,fname2,samplerate_native, fname_temp);
  1411. if(system(command) != 0)
  1412. {
  1413. failed = 1;
  1414. }
  1415. if(failed || (GetFileLength(fname_temp) <= 0))
  1416. {
  1417. if(resample_fails < 2)
  1418. error("Resample command failed: %s", command);
  1419. resample_fails++;
  1420. if(sr1 != samplerate_native)
  1421. {
  1422. sprintf(msg, "Can't resample (%d to %d): %s", sr1, samplerate_native, fname);
  1423. error("%s", msg);
  1424. }
  1425. else
  1426. {
  1427. error("WAV file is not mono: %s", fname);
  1428. }
  1429. remove(fname_temp);
  1430. return(0);
  1431. }
  1432. f = fopen(fname_temp,"rb");
  1433. if(f == NULL)
  1434. {
  1435. error("Can't read temp file: %s",fname_temp);
  1436. return(0);
  1437. }
  1438. if(f_report != NULL)
  1439. fprintf(f_report, "resampled %s\n", fname);
  1440. resample_count++;
  1441. resample_wav = 1;
  1442. fseek(f,40,SEEK_SET); // skip past the WAV header, up to before "data length"
  1443. }
  1444. displ = ftell(f_phdata);
  1445. // data contains: 4 bytes of length (n_samples * 2), followed by 2-byte samples (lsb byte first)
  1446. length = Read4Bytes(f);
  1447. while(!feof(f))
  1448. {
  1449. c1 = fgetc(f);
  1450. c3 = fgetc(f);
  1451. if(feof(f)) break;
  1452. c2 = c3 << 24;
  1453. c2 = c2 >> 16; // sign extend
  1454. sample = (c1 & 0xff) + c2;
  1455. if(sample > max)
  1456. max = sample;
  1457. else
  1458. if(sample < -max)
  1459. max = -sample;
  1460. }
  1461. scale_factor = (max / 127) + 1;
  1462. #define MIN_FACTOR -1 // was 6, disable use of 16 bit samples
  1463. if(scale_factor > MIN_FACTOR)
  1464. {
  1465. length = length/2 + (scale_factor << 16);
  1466. }
  1467. Write4Bytes(f_phdata,length);
  1468. fseek(f,44,SEEK_SET);
  1469. while(!feof(f))
  1470. {
  1471. c1 = fgetc(f);
  1472. c3 = fgetc(f);
  1473. c2 = c3 << 24;
  1474. c2 = c2 >> 16; // sign extend
  1475. sample = (c1 & 0xff) + c2;
  1476. if(feof(f)) break;
  1477. if(scale_factor <= MIN_FACTOR)
  1478. {
  1479. fputc(sample & 0xff,f_phdata);
  1480. fputc(sample >> 8,f_phdata);
  1481. }
  1482. else
  1483. {
  1484. x = ((float)sample / scale_factor) + 0.5;
  1485. sample2= (int)x;
  1486. if(sample2 > 127)
  1487. sample2 = 127;
  1488. if(sample2 < -128)
  1489. sample2 = -128;
  1490. fputc(sample2,f_phdata);
  1491. }
  1492. }
  1493. length = ftell(f_phdata);
  1494. while((length & 3) != 0)
  1495. {
  1496. // pad to a multiple of 4 bytes
  1497. fputc(0,f_phdata);
  1498. length++;
  1499. }
  1500. if(resample_wav != 0)
  1501. {
  1502. fclose(f);
  1503. remove(fname_temp);
  1504. }
  1505. return(displ | 0x800000); // set bit 23 to indicate a wave file rather than a spectrum
  1506. }
  1507. static int LoadEnvelope(FILE *f, const char *fname)
  1508. {
  1509. int displ;
  1510. char buf[128];
  1511. displ = ftell(f_phdata);
  1512. fseek(f,12,SEEK_SET);
  1513. if(fread(buf,128,1,f) == 0)
  1514. {
  1515. error("Failed to read envelope: %s",fname);
  1516. }
  1517. fwrite(buf,128,1,f_phdata);
  1518. if(n_envelopes < N_ENVELOPES)
  1519. {
  1520. strncpy0(envelope_paths[n_envelopes],fname,sizeof(envelope_paths[0]));
  1521. memcpy(envelope_dat[n_envelopes],buf,sizeof(envelope_dat[0]));
  1522. n_envelopes++;
  1523. }
  1524. return(displ);
  1525. }
  1526. /* Generate a hash code from the specified string */
  1527. static int Hash8(const char *string)
  1528. {
  1529. int c;
  1530. int chars=0;
  1531. int hash=0;
  1532. while((c = *string++) != 0)
  1533. {
  1534. c = tolower(c) - 'a';
  1535. hash = hash * 8 + c;
  1536. hash = (hash & 0x1ff) ^ (hash >> 8); /* exclusive or */
  1537. chars++;
  1538. }
  1539. return((hash+chars) & 0xff);
  1540. }
  1541. static int LoadEnvelope2(FILE *f, const char *fname)
  1542. {
  1543. int ix, ix2;
  1544. int n;
  1545. int x, y;
  1546. int displ;
  1547. int n_points;
  1548. double yy;
  1549. char line_buf[128];
  1550. float env_x[20];
  1551. float env_y[20];
  1552. int env_lin[20];
  1553. unsigned char env[ENV_LEN];
  1554. n_points = 0;
  1555. fgets(line_buf,sizeof(line_buf),f); // skip first line
  1556. while(!feof(f))
  1557. {
  1558. if(fgets(line_buf,sizeof(line_buf),f) == NULL)
  1559. break;
  1560. env_lin[n_points] = 0;
  1561. n = sscanf(line_buf,"%f %f %d",&env_x[n_points],&env_y[n_points],&env_lin[n_points]);
  1562. if(n >= 2)
  1563. {
  1564. env_x[n_points] *= (float)1.28; // convert range 0-100 to 0-128
  1565. n_points++;
  1566. }
  1567. }
  1568. env_x[n_points] = env_x[n_points-1];
  1569. env_y[n_points] = env_y[n_points-1];
  1570. ix = -1;
  1571. ix2 = 0;
  1572. for(x=0; x<ENV_LEN; x++)
  1573. {
  1574. if(x > env_x[ix+4])
  1575. ix++;
  1576. if(x >= env_x[ix2+1])
  1577. ix2++;
  1578. if(env_lin[ix2] > 0)
  1579. {
  1580. yy = env_y[ix2] + (env_y[ix2+1] - env_y[ix2]) * ((float)x - env_x[ix2]) / (env_x[ix2+1] - env_x[ix2]);
  1581. y = (int)(yy * 2.55);
  1582. }
  1583. else
  1584. if(n_points > 3)
  1585. y = (int)(polint(&env_x[ix],&env_y[ix],4,x) * 2.55); // convert to range 0-255
  1586. else
  1587. y = (int)(polint(&env_x[ix],&env_y[ix],3,x) * 2.55);
  1588. if(y < 0) y = 0;
  1589. if(y > 255) y = 255;
  1590. env[x] = y;
  1591. }
  1592. if(n_envelopes < N_ENVELOPES)
  1593. {
  1594. strncpy0(envelope_paths[n_envelopes],fname,sizeof(envelope_paths[0]));
  1595. memcpy(envelope_dat[n_envelopes],env,ENV_LEN);
  1596. n_envelopes++;
  1597. }
  1598. displ = ftell(f_phdata);
  1599. fwrite(env,1,128,f_phdata);
  1600. return(displ);
  1601. }
  1602. static int LoadDataFile(const char *path, int control)
  1603. {
  1604. // load spectrum sequence or sample data from a file.
  1605. // return index into spect or sample data area. bit 23=1 if a sample
  1606. FILE *f;
  1607. int id;
  1608. int hash;
  1609. int addr = 0;
  1610. int type_code=' ';
  1611. REF_HASH_TAB *p, *p2;
  1612. char buf[sizeof(path_home)+150];
  1613. if(strcmp(path,"NULL")==0)
  1614. return(0);
  1615. if(strcmp(path,"DFT")==0)
  1616. return(1);
  1617. count_references++;
  1618. hash = Hash8(path);
  1619. p = ref_hash_tab[hash];
  1620. while(p != NULL)
  1621. {
  1622. if(strcmp(path,p->string)==0)
  1623. {
  1624. duplicate_references++;
  1625. addr = p->value; // already loaded this data
  1626. break;
  1627. }
  1628. p = (REF_HASH_TAB *)p->link;
  1629. }
  1630. if(addr == 0)
  1631. {
  1632. sprintf(buf,"%s/../phsource/%s",path_home,path);
  1633. if((f = fopen(buf,"rb")) == NULL)
  1634. {
  1635. sprintf(buf,"%s/../phsource/%s.wav",path_home,path);
  1636. if((f = fopen(buf,"rb")) == NULL)
  1637. {
  1638. error("Can't read file: %s",path);
  1639. return(0);
  1640. }
  1641. }
  1642. id = Read4Bytes(f);
  1643. rewind(f);
  1644. if(id == 0x43455053)
  1645. {
  1646. addr = LoadSpect(path, control);
  1647. type_code = 'S';
  1648. }
  1649. else
  1650. if(id == 0x46464952)
  1651. {
  1652. addr = LoadWavefile(f,path);
  1653. type_code = 'W';
  1654. }
  1655. else
  1656. if(id == 0x43544950)
  1657. {
  1658. addr = LoadEnvelope(f,path);
  1659. type_code = 'E';
  1660. }
  1661. else
  1662. if(id == 0x45564E45)
  1663. {
  1664. addr = LoadEnvelope2(f,path);
  1665. type_code = 'E';
  1666. }
  1667. else
  1668. {
  1669. error("File not SPEC or RIFF: %s",path);
  1670. addr = -1;
  1671. }
  1672. fclose(f);
  1673. if(addr > 0)
  1674. {
  1675. fprintf(f_phcontents,"%c 0x%.5x %s\n",type_code,addr & 0x7fffff,path);
  1676. }
  1677. }
  1678. // add this item to the hash table
  1679. if(addr > 0)
  1680. {
  1681. p = ref_hash_tab[hash];
  1682. p2 = (REF_HASH_TAB *)malloc(sizeof(REF_HASH_TAB)+strlen(path)+1);
  1683. p2->value = addr;
  1684. p2->ph_mnemonic = phoneme_out->mnemonic; // phoneme which uses this file
  1685. p2->ph_table = n_phoneme_tabs-1;
  1686. strcpy(p2->string,path);
  1687. p2->link = (char *)p;
  1688. ref_hash_tab[hash] = p2;
  1689. }
  1690. return(addr);
  1691. }
  1692. static int CompileToneSpec(void)
  1693. {
  1694. int pitch1=0;
  1695. int pitch2=0;
  1696. int pitch_env = 0;
  1697. int amp_env = 0;
  1698. pitch1 = NextItemBrackets(tNUMBER,2);
  1699. pitch2 = NextItemBrackets(tNUMBER,3);
  1700. if(item_terminator == ',')
  1701. {
  1702. NextItemBrackets(tSTRING,3);
  1703. pitch_env = LoadDataFile(item_string, 0);
  1704. }
  1705. if(item_terminator == ',')
  1706. {
  1707. NextItemBrackets(tSTRING,1);
  1708. amp_env = LoadDataFile(item_string, 0);
  1709. }
  1710. if(pitch1 < pitch2)
  1711. {
  1712. phoneme_out->start_type = pitch1;
  1713. phoneme_out->end_type = pitch2;
  1714. }
  1715. else
  1716. {
  1717. phoneme_out->start_type = pitch2;
  1718. phoneme_out->end_type = pitch1;
  1719. }
  1720. if(pitch_env != 0)
  1721. {
  1722. *prog_out++ = i_PITCHENV + ((pitch_env >> 16) & 0xf);
  1723. *prog_out++ = pitch_env;
  1724. }
  1725. if(amp_env != 0)
  1726. {
  1727. *prog_out++ = i_AMPENV + ((amp_env >> 16) & 0xf);
  1728. *prog_out++ = amp_env;
  1729. }
  1730. return(0);
  1731. }
  1732. int CompileSound(int keyword, int isvowel)
  1733. {
  1734. int addr;
  1735. int value = 0;
  1736. char path[N_ITEM_STRING];
  1737. static int sound_instns[] = {i_FMT, i_WAV, i_VWLSTART, i_VWLENDING, i_WAVADD};
  1738. NextItemBrackets(tSTRING,2);
  1739. strcpy(path, item_string);
  1740. if(item_terminator == ',')
  1741. {
  1742. if((keyword == kVOWELSTART) || (keyword == kVOWELENDING))
  1743. {
  1744. value = NextItemBrackets(tSIGNEDNUMBER,1);
  1745. if(value > 127)
  1746. {
  1747. value = 127;
  1748. error("Parameter > 127",NULL);
  1749. }
  1750. if(value < -128)
  1751. {
  1752. value = -128;
  1753. error("Parameter < -128",NULL);
  1754. }
  1755. }
  1756. else
  1757. {
  1758. value = NextItemBrackets(tNUMBER,1);
  1759. if(value > 255)
  1760. {
  1761. value = 255;
  1762. error("Parameter > 255",NULL);
  1763. }
  1764. }
  1765. }
  1766. addr = LoadDataFile(path, isvowel);
  1767. addr = addr / 4; // addr is words not bytes
  1768. *prog_out++ = sound_instns[keyword-kFMT] + ((value & 0xff) << 4) + ((addr >> 16) & 0xf);
  1769. *prog_out++ = addr & 0xffff;
  1770. return(0);
  1771. }
  1772. /*
  1773. Condition
  1774. bits 14,15 1
  1775. bit 13 1 = AND, 0 = OR
  1776. bit 12 spare
  1777. bit 8-11
  1778. =0-3 p,t,n,n2 data=phoneme code
  1779. =4-7 p,t,n,n2 data=(bits5-7: phtype, place, property, special) (bits0-4: data)
  1780. =8 data = stress bitmap
  1781. =9 special tests
  1782. */
  1783. int CompileIf(int elif)
  1784. {
  1785. int key;
  1786. int finish = 0;
  1787. int word = 0;
  1788. int word2;
  1789. int data;
  1790. int bitmap;
  1791. int brackets;
  1792. int not_flag;
  1793. USHORT *prog_last_if = NULL;
  1794. then_count = 2;
  1795. after_if = 1;
  1796. while(!finish)
  1797. {
  1798. not_flag = 0;
  1799. word2 = 0;
  1800. if(prog_out >= prog_out_max)
  1801. {
  1802. error("Phoneme program too large", NULL);
  1803. return(0);
  1804. }
  1805. if((key = NextItem(tCONDITION)) < 0)
  1806. error("Expected a condition, not '%s'",item_string);
  1807. if((item_type == 0) && (key == k_NOT))
  1808. {
  1809. not_flag = 1;
  1810. if((key = NextItem(tCONDITION)) < 0)
  1811. error("Expected a condition, not '%s'",item_string);
  1812. }
  1813. if(item_type == tWHICH_PHONEME)
  1814. {
  1815. // prevPh(), thisPh(), nextPh(), next2Ph() etc
  1816. if(key >= 6)
  1817. {
  1818. // put the 'which' code in the next instruction
  1819. word2 = key;
  1820. key = 6;
  1821. }
  1822. key = key << 8;
  1823. data = NextItemBrackets(tPROPERTIES,0);
  1824. if(data >= 0)
  1825. {
  1826. word = key + data + 0x700;
  1827. }
  1828. else
  1829. {
  1830. data = LookupPhoneme(item_string,2);
  1831. word = key + data;
  1832. }
  1833. }
  1834. else
  1835. if(item_type == tTEST)
  1836. {
  1837. if(key == kTHISSTRESS)
  1838. {
  1839. bitmap = 0;
  1840. brackets = 2;
  1841. do {
  1842. data = NextItemBrackets(tNUMBER,brackets);
  1843. if(data > 7)
  1844. error("Expected list of stress levels",NULL);
  1845. bitmap |= (1 << data);
  1846. brackets = 3;
  1847. } while(item_terminator == ',');
  1848. word = i_StressLevel | bitmap;
  1849. }
  1850. else
  1851. {
  1852. word = key;
  1853. }
  1854. }
  1855. else
  1856. {
  1857. error("Unexpected keyword '%s'",item_string);
  1858. if((strcmp(item_string, "phoneme") == 0) || (strcmp(item_string, "endphoneme") == 0))
  1859. return(-1);
  1860. }
  1861. // output the word
  1862. prog_last_if = prog_out;
  1863. *prog_out++ = word | i_CONDITION;
  1864. if(word2 != 0)
  1865. *prog_out++ = word2;
  1866. if(not_flag)
  1867. *prog_out++ = i_NOT;
  1868. // expect AND, OR, THEN
  1869. switch(NextItem(tCONDITION))
  1870. {
  1871. case k_AND:
  1872. break;
  1873. case k_OR:
  1874. if(prog_last_if != NULL)
  1875. *prog_last_if |= i_OR;
  1876. break;
  1877. case k_THEN:
  1878. finish = 1;
  1879. break;
  1880. default:
  1881. error("Expected AND, OR, THEN",NULL);
  1882. break;
  1883. }
  1884. }
  1885. if(finish != 1)
  1886. {
  1887. }
  1888. if(elif == 0)
  1889. {
  1890. if_level++;
  1891. if_stack[if_level].p_else = NULL;
  1892. }
  1893. if_stack[if_level].returned = 0;
  1894. if_stack[if_level].p_then = prog_out;
  1895. *prog_out++ = i_JUMP_FALSE;
  1896. return(0);
  1897. }
  1898. void FillThen(int add)
  1899. {
  1900. USHORT *p;
  1901. int offset;
  1902. p = if_stack[if_level].p_then;
  1903. if(p != NULL)
  1904. {
  1905. offset = prog_out - p + add;
  1906. if((then_count == 1) && (if_level == 1))
  1907. {
  1908. // The THEN part only contains one statement, we can remove the THEN jump
  1909. // and the interpreter will implicitly skip the statement.
  1910. while(p < prog_out)
  1911. {
  1912. p[0] = p[1];
  1913. p++;
  1914. }
  1915. prog_out--;
  1916. }
  1917. else
  1918. {
  1919. if(offset > MAX_JUMP)
  1920. {
  1921. error("IF block is too long",NULL);
  1922. }
  1923. *p = i_JUMP_FALSE + offset;
  1924. }
  1925. if_stack[if_level].p_then = NULL;
  1926. }
  1927. then_count = 0;
  1928. }
  1929. int CompileElse(void)
  1930. {
  1931. USHORT *ref;
  1932. USHORT *p;
  1933. if(if_level < 1)
  1934. {
  1935. error("ELSE not expected",NULL);
  1936. return(0);
  1937. }
  1938. if(if_stack[if_level].returned == 0)
  1939. {
  1940. FillThen(1);
  1941. }
  1942. else
  1943. {
  1944. FillThen(0);
  1945. }
  1946. if(if_stack[if_level].returned == 0)
  1947. {
  1948. ref = prog_out;
  1949. *prog_out++ = 0;
  1950. if((p = if_stack[if_level].p_else) != NULL)
  1951. {
  1952. *ref = ref - p; // backwards offset to the previous else
  1953. }
  1954. if_stack[if_level].p_else = ref;
  1955. }
  1956. return(0);
  1957. }
  1958. int CompileElif(void)
  1959. {
  1960. if(if_level < 1)
  1961. {
  1962. error("ELIF not expected",NULL);
  1963. return(0);
  1964. }
  1965. CompileElse();
  1966. CompileIf(1);
  1967. return(0);
  1968. }
  1969. int CompileEndif(void)
  1970. {
  1971. USHORT *p;
  1972. int chain;
  1973. int offset;
  1974. if(if_level < 1)
  1975. {
  1976. error("ENDIF not expected",NULL);
  1977. return(0);
  1978. }
  1979. FillThen(0);
  1980. if((p = if_stack[if_level].p_else) != NULL)
  1981. {
  1982. do
  1983. {
  1984. chain = *p; // a chain of previous else links
  1985. offset = prog_out - p;
  1986. if(offset > MAX_JUMP)
  1987. {
  1988. error("IF block is too long",NULL);
  1989. }
  1990. *p = i_JUMP + offset;
  1991. p -= chain;
  1992. } while (chain > 0);
  1993. }
  1994. if_level--;
  1995. return(0);
  1996. }
  1997. static int CompileSwitch(int type)
  1998. {
  1999. // Type 0: EndSwitch
  2000. // 1: SwitchPrevVowelType
  2001. // 2: SwitchNextVowelType
  2002. if(type == 0)
  2003. {
  2004. // check the instructions in the Switch
  2005. return(0);
  2006. }
  2007. if(type == 1)
  2008. *prog_out++ = i_SWITCH_PREVVOWEL+6;
  2009. if(type == 2)
  2010. *prog_out++ = i_SWITCH_NEXTVOWEL+6;
  2011. return(0);
  2012. }
  2013. static PHONEME_TAB_LIST *FindPhonemeTable(const char *string)
  2014. {
  2015. int ix;
  2016. for(ix=0; ix<n_phoneme_tabs; ix++)
  2017. {
  2018. if(strcmp(phoneme_tab_list2[ix].name, string) == 0)
  2019. {
  2020. return(&phoneme_tab_list2[ix]);
  2021. }
  2022. }
  2023. error("Unknown phoneme table: '%s'",string);
  2024. return(NULL);
  2025. }
  2026. static PHONEME_TAB *FindPhoneme(const char *string)
  2027. {
  2028. PHONEME_TAB_LIST *phtab = NULL;
  2029. int ix;
  2030. unsigned int mnem;
  2031. char *phname;
  2032. char buf[200];
  2033. // is this the name of a phoneme which is in scope
  2034. if((strlen(string) <= 4) && ((ix = LookupPhoneme(string,0)) != -1))
  2035. {
  2036. return(&phoneme_tab2[ix]);
  2037. }
  2038. // no, treat the name as phonemetable/phoneme
  2039. strcpy(buf,string);
  2040. if((phname = strchr(buf,'/')) != 0)
  2041. {
  2042. *phname++ = 0;
  2043. }
  2044. phtab = FindPhonemeTable(buf);
  2045. if(phtab == NULL)
  2046. {
  2047. return(NULL); // phoneme table not found
  2048. }
  2049. mnem = StringToWord(phname);
  2050. for(ix=1; ix<256; ix++)
  2051. {
  2052. if(mnem == phtab->phoneme_tab_ptr[ix].mnemonic)
  2053. {
  2054. return(&phtab->phoneme_tab_ptr[ix]);
  2055. }
  2056. }
  2057. error("Phoneme reference not found: '%s'",string);
  2058. return(NULL);
  2059. }
  2060. static void ImportPhoneme(void)
  2061. {
  2062. unsigned int ph_mnem;
  2063. unsigned int ph_code;
  2064. PHONEME_TAB *ph;
  2065. NextItem(tSTRING);
  2066. if((ph = FindPhoneme(item_string)) == NULL)
  2067. return;
  2068. ph_mnem = phoneme_out->mnemonic;
  2069. ph_code = phoneme_out->code;
  2070. memcpy(phoneme_out,ph,sizeof(PHONEME_TAB));
  2071. phoneme_out->mnemonic = ph_mnem;
  2072. phoneme_out->code = ph_code;
  2073. if(phoneme_out->type != phVOWEL)
  2074. {
  2075. phoneme_out->end_type = 0; // voicingswitch, this must be set later to refer to a local phoneme
  2076. }
  2077. }
  2078. static void CallPhoneme(void)
  2079. {
  2080. PHONEME_TAB *ph;
  2081. int ix;
  2082. int addr = 0;
  2083. NextItem(tSTRING);
  2084. // first look for a procedure name
  2085. for(ix=0; ix<n_procs; ix++)
  2086. {
  2087. if(strcmp(proc_names[ix],item_string) == 0)
  2088. {
  2089. addr = proc_addr[ix];
  2090. break;
  2091. }
  2092. }
  2093. if(ix == n_procs)
  2094. {
  2095. // procedure not found, try a phoneme name
  2096. if((ph = FindPhoneme(item_string)) == NULL)
  2097. return;
  2098. addr = ph->program;
  2099. if(phoneme_out->type == phINVALID)
  2100. {
  2101. // Phoneme type has not been set. Copy it from the called phoneme
  2102. phoneme_out->type = ph->type;
  2103. phoneme_flags = ph->phflags & ~phARTICULATION;
  2104. place_articulation = (ph->phflags & phARTICULATION) >> 16;
  2105. phoneme_out->start_type = ph->start_type;
  2106. phoneme_out->end_type = ph->end_type;
  2107. phoneme_out->std_length = ph->std_length;
  2108. phoneme_out->length_mod = ph->length_mod;
  2109. }
  2110. }
  2111. *prog_out++ = i_CALLPH + (addr >> 16);
  2112. *prog_out++ = addr;
  2113. }
  2114. static void DecThenCount()
  2115. {
  2116. if(then_count >0)
  2117. then_count--;
  2118. }
  2119. static void InstnPlusPhoneme(int instn)
  2120. {
  2121. int phcode;
  2122. phcode = NextItemBrackets(tPHONEMEMNEM,0);
  2123. *prog_out++ = instn + phcode;
  2124. }
  2125. int CompilePhoneme(int compile_phoneme)
  2126. {
  2127. int endphoneme = 0;
  2128. int keyword;
  2129. int value;
  2130. int phcode = 0;
  2131. int flags;
  2132. int ix;
  2133. int start;
  2134. int count;
  2135. int c;
  2136. char *p;
  2137. int vowel_length_factor = 100; // for testing
  2138. char number_buf[12];
  2139. char ipa_buf[N_ITEM_STRING+1];
  2140. PHONEME_TAB phoneme_out2;
  2141. PHONEME_PROG_LOG phoneme_prog_log;
  2142. prog_out = prog_buf;
  2143. prog_out_max = &prog_buf[MAX_PROG_BUF-1];
  2144. if_level = 0;
  2145. if_stack[0].returned = 0;
  2146. after_if = 0;
  2147. phoneme_flags = 0;
  2148. place_articulation = 0;
  2149. NextItem(tSTRING);
  2150. if(compile_phoneme)
  2151. {
  2152. phcode = LookupPhoneme(item_string,1); // declare phoneme if not already there
  2153. if(phcode == -1) return(0);
  2154. phoneme_out = &phoneme_tab2[phcode];
  2155. }
  2156. else
  2157. {
  2158. // declare a procedure
  2159. if(n_procs >= N_PROCS)
  2160. {
  2161. error("Too many procedures",NULL);
  2162. return(0);
  2163. }
  2164. strcpy(proc_names[n_procs], item_string);
  2165. phoneme_out = &phoneme_out2;
  2166. sprintf(number_buf,"%.3dP", n_procs);
  2167. phoneme_out->mnemonic = StringToWord(number_buf);
  2168. }
  2169. phoneme_out->code = phcode;
  2170. phoneme_out->program = 0;
  2171. phoneme_out->type = phINVALID;
  2172. phoneme_out->std_length = 0;
  2173. phoneme_out->start_type = 0;
  2174. phoneme_out->end_type = 0;
  2175. phoneme_out->length_mod = 0;
  2176. while(!endphoneme && !feof(f_in))
  2177. {
  2178. if((keyword = NextItem(tKEYWORD)) < 0)
  2179. {
  2180. if(keyword == -2)
  2181. {
  2182. error("Missing 'endphoneme' before end-of-file",NULL); // end of file
  2183. break;
  2184. }
  2185. error("Bad keyword in phoneme definition '%s'",item_string);
  2186. continue;
  2187. }
  2188. switch(item_type)
  2189. {
  2190. case tPHONEME_TYPE:
  2191. if(phoneme_out->type != phINVALID)
  2192. error("More than one phoneme type: %s",item_string);
  2193. phoneme_out->type = keyword;
  2194. break;
  2195. case tPLACE:
  2196. if(place_articulation > 0)
  2197. error("Place of articulation has already been given: %s",item_string);
  2198. place_articulation = keyword;
  2199. break;
  2200. case tPHONEME_FLAG:
  2201. phoneme_flags |= keyword;
  2202. break;
  2203. case tINSTRN1:
  2204. // instruction group 0, with 8 bit operands which set data in PHONEME_DATA
  2205. switch(keyword)
  2206. {
  2207. case i_CHANGE_PHONEME:
  2208. case i_APPEND_PHONEME:
  2209. case i_APPEND_IFNEXTVOWEL:
  2210. case i_INSERT_PHONEME:
  2211. case i_REPLACE_NEXT_PHONEME:
  2212. case i_VOICING_SWITCH:
  2213. case i_CHANGE_IF+0:
  2214. case i_CHANGE_IF+1:
  2215. case i_CHANGE_IF+2:
  2216. case i_CHANGE_IF+3:
  2217. InstnPlusPhoneme(keyword << 8);
  2218. DecThenCount();
  2219. break;
  2220. case i_PAUSE_BEFORE:
  2221. value = NextItemMax(255);
  2222. *prog_out++ = (i_PAUSE_BEFORE << 8) + value;
  2223. DecThenCount();
  2224. break;
  2225. case i_PAUSE_AFTER:
  2226. value = NextItemMax(255);
  2227. *prog_out++ = (i_PAUSE_AFTER << 8) + value;
  2228. DecThenCount();
  2229. break;
  2230. case i_SET_LENGTH:
  2231. value = NextItemMax(511);
  2232. if(phoneme_out->type == phVOWEL)
  2233. {
  2234. value = (value * vowel_length_factor)/100;
  2235. }
  2236. if(after_if == 0)
  2237. {
  2238. phoneme_out->std_length = value/2;
  2239. }
  2240. else
  2241. {
  2242. *prog_out++ = (i_SET_LENGTH << 8) + value/2;
  2243. DecThenCount();
  2244. }
  2245. break;
  2246. case i_ADD_LENGTH:
  2247. value = NextItem(tSIGNEDNUMBER) / 2;
  2248. *prog_out++ = (i_ADD_LENGTH << 8) + (value & 0xff);
  2249. DecThenCount();
  2250. break;
  2251. case i_LENGTH_MOD:
  2252. value = NextItem(tNUMBER);
  2253. phoneme_out->length_mod = value;
  2254. break;
  2255. case i_IPA_NAME:
  2256. NextItem(tSTRING);
  2257. if(strcmp(item_string,"NULL")==0)
  2258. strcpy(item_string," ");
  2259. // copy the string, recognize characters in the form U+9999
  2260. flags = 0;
  2261. count = 0;
  2262. ix = 1;
  2263. for(p=item_string; *p != 0; )
  2264. {
  2265. p += utf8_in(&c, p);
  2266. if((c == '|') && (count > 0))
  2267. {
  2268. // '|' means don't allow a tie or joiner before this letter
  2269. flags |= (1 << (count -1));
  2270. }
  2271. else
  2272. if((c=='U') && (p[0]=='+'))
  2273. {
  2274. int j;
  2275. // U+9999
  2276. p++;
  2277. memcpy(number_buf,p,4); // U+ should be followed by 4 hex digits
  2278. number_buf[4] = 0;
  2279. c = '#';
  2280. sscanf(number_buf,"%x",&c);
  2281. // move past the 4 hexdecimal digits
  2282. for(j=0; j<4; j++)
  2283. {
  2284. if(!isalnum(*p))
  2285. break;
  2286. p++;
  2287. }
  2288. ix += utf8_out(c, &ipa_buf[ix]);
  2289. count++;
  2290. }
  2291. else
  2292. {
  2293. ix += utf8_out(c, &ipa_buf[ix]);
  2294. count++;
  2295. }
  2296. }
  2297. ipa_buf[0] = flags;
  2298. ipa_buf[ix] = 0;
  2299. start = 1;
  2300. if(flags != 0)
  2301. start = 0; // only include the flags byte if bits are set
  2302. value = strlen(&ipa_buf[start]); // number of UTF-8 bytes
  2303. *prog_out++ = (i_IPA_NAME << 8) + value;
  2304. for(ix=0; ix < value; ix += 2)
  2305. {
  2306. *prog_out++ = (ipa_buf[ix+start] << 8) + (ipa_buf[ix+start+1] & 0xff);
  2307. }
  2308. DecThenCount();
  2309. break;
  2310. }
  2311. break;
  2312. case tSTATEMENT:
  2313. switch(keyword)
  2314. {
  2315. case kIMPORT_PH:
  2316. ImportPhoneme();
  2317. phoneme_flags = phoneme_out->phflags;
  2318. break;
  2319. case kSTARTTYPE:
  2320. phcode = NextItem(tPHONEMEMNEM);
  2321. if(phcode == -1)
  2322. phcode = LookupPhoneme(item_string,1);
  2323. phoneme_out->start_type = phcode;
  2324. break;
  2325. case kENDTYPE:
  2326. phcode = NextItem(tPHONEMEMNEM);
  2327. if(phcode == -1)
  2328. phcode = LookupPhoneme(item_string,1);
  2329. if(phoneme_out->type == phVOWEL)
  2330. {
  2331. phoneme_out->end_type = phcode;
  2332. }
  2333. else
  2334. {
  2335. if(phcode != phoneme_out->start_type)
  2336. {
  2337. error("endtype must equal starttype for consonants",NULL);
  2338. }
  2339. }
  2340. break;
  2341. case kVOICINGSWITCH:
  2342. phcode = NextItem(tPHONEMEMNEM);
  2343. if(phcode == -1)
  2344. phcode = LookupPhoneme(item_string,1);
  2345. phoneme_out->end_type = phcode; // use end_type field for consonants as voicing_switch
  2346. break;
  2347. case kSTRESSTYPE:
  2348. value = NextItem(tNUMBER);
  2349. phoneme_out->std_length = value;
  2350. if(prog_out > prog_buf)
  2351. {
  2352. error("stress phonemes can't contain program instructions",NULL);
  2353. prog_out = prog_buf;
  2354. }
  2355. break;
  2356. case kIF:
  2357. endphoneme = CompileIf(0);
  2358. break;
  2359. case kELSE:
  2360. endphoneme = CompileElse();
  2361. break;
  2362. case kELIF:
  2363. endphoneme = CompileElif();
  2364. break;
  2365. case kENDIF:
  2366. endphoneme = CompileEndif();
  2367. break;
  2368. case kENDSWITCH:
  2369. break;
  2370. case kSWITCH_PREVVOWEL:
  2371. endphoneme = CompileSwitch(1);
  2372. break;
  2373. case kSWITCH_NEXTVOWEL:
  2374. endphoneme = CompileSwitch(2);
  2375. break;
  2376. case kCALLPH:
  2377. CallPhoneme();
  2378. DecThenCount();
  2379. break;
  2380. case kFMT:
  2381. if_stack[if_level].returned = 1;
  2382. DecThenCount();
  2383. if(phoneme_out->type == phVOWEL)
  2384. endphoneme = CompileSound(keyword,1);
  2385. else
  2386. endphoneme = CompileSound(keyword,0);
  2387. break;
  2388. case kWAV:
  2389. if_stack[if_level].returned = 1;
  2390. case kVOWELSTART:
  2391. case kVOWELENDING:
  2392. case kANDWAV:
  2393. DecThenCount();
  2394. endphoneme = CompileSound(keyword,0);
  2395. break;
  2396. case kVOWELIN:
  2397. DecThenCount();
  2398. endphoneme = CompileVowelTransition(1);
  2399. break;
  2400. case kVOWELOUT:
  2401. DecThenCount();
  2402. endphoneme = CompileVowelTransition(2);
  2403. break;
  2404. case kTONESPEC:
  2405. DecThenCount();
  2406. endphoneme = CompileToneSpec();
  2407. break;
  2408. case kCONTINUE:
  2409. *prog_out++ = i_CONTINUE;
  2410. DecThenCount();
  2411. break;
  2412. case kRETURN:
  2413. *prog_out++ = i_RETURN;
  2414. DecThenCount();
  2415. break;
  2416. case kINCLUDE:
  2417. case kPHONEMENUMBER:
  2418. case kPHONEMETABLE:
  2419. error("Missing 'endphoneme' before '%s'",item_string); // drop through to endphoneme
  2420. case kENDPHONEME:
  2421. case kENDPROCEDURE:
  2422. endphoneme = 1;
  2423. if(if_level > 0)
  2424. {
  2425. error("Missing ENDIF",NULL);
  2426. }
  2427. if((prog_out > prog_buf) && (if_stack[0].returned == 0))
  2428. {
  2429. *prog_out++ = i_RETURN;
  2430. }
  2431. break;
  2432. }
  2433. break;
  2434. }
  2435. }
  2436. if(endphoneme != 1)
  2437. {
  2438. error("'endphoneme' not expected here",NULL);
  2439. }
  2440. if(compile_phoneme)
  2441. {
  2442. if(phoneme_out->type == phINVALID)
  2443. {
  2444. error("Phoneme type is missing",NULL);
  2445. phoneme_out->type = 0;
  2446. }
  2447. phoneme_out->phflags = place_articulation << 16;
  2448. phoneme_out->phflags |= phoneme_flags;
  2449. if(phoneme_out->phflags & phVOICED)
  2450. {
  2451. if(phoneme_out->type == phSTOP)
  2452. phoneme_out->type = phVSTOP;
  2453. else
  2454. if(phoneme_out->type == phFRICATIVE)
  2455. phoneme_out->type = phVFRICATIVE;
  2456. }
  2457. if(phoneme_out->std_length == 0)
  2458. {
  2459. if(phoneme_out->type == phVOWEL)
  2460. phoneme_out->std_length = 180/2; // default length for vowel
  2461. }
  2462. phoneme_out->phflags |= phLOCAL; //declared in this phoneme table
  2463. if(phoneme_out->type == phDELETED)
  2464. {
  2465. phoneme_out->mnemonic = 0x01; // will not be recognised
  2466. }
  2467. }
  2468. DecompilePhoneme(f_errors, phoneme_out, compile_phoneme);
  2469. if(prog_out > prog_buf)
  2470. {
  2471. // write out the program for this phoneme
  2472. fflush(f_phindex);
  2473. phoneme_out->program = ftell(f_phindex) / sizeof(USHORT);
  2474. if(f_prog_log != NULL)
  2475. {
  2476. phoneme_prog_log.addr = phoneme_out->program;
  2477. phoneme_prog_log.length = prog_out - prog_buf;
  2478. fwrite(&phoneme_prog_log, 1, sizeof(phoneme_prog_log), f_prog_log);
  2479. }
  2480. if(compile_phoneme == 0)
  2481. {
  2482. proc_addr[n_procs++] = ftell(f_phindex) / sizeof(USHORT);
  2483. }
  2484. fwrite(prog_buf, sizeof(USHORT), prog_out - prog_buf, f_phindex);
  2485. }
  2486. return(0);
  2487. }
  2488. static void WritePhonemeTables()
  2489. {
  2490. int ix;
  2491. int j;
  2492. int n;
  2493. int value;
  2494. int count;
  2495. PHONEME_TAB *p;
  2496. value = n_phoneme_tabs;
  2497. fputc(value,f_phtab);
  2498. fputc(0,f_phtab);
  2499. fputc(0,f_phtab);
  2500. fputc(0,f_phtab);
  2501. for(ix=0; ix<n_phoneme_tabs; ix++)
  2502. {
  2503. p = phoneme_tab_list2[ix].phoneme_tab_ptr;
  2504. n = n_phcodes_list[ix];
  2505. p[n].mnemonic = 0; // terminate the phoneme table
  2506. // count number of locally declared phonemes
  2507. count=0;
  2508. for(j=0; j<n; j++)
  2509. {
  2510. if(ix==0)
  2511. p[j].phflags |= phLOCAL; // write all phonemes in the base phoneme table
  2512. if(p[j].phflags & phLOCAL)
  2513. count++;
  2514. }
  2515. phoneme_tab_list2[ix].n_phonemes = count+1;
  2516. fputc(count+1,f_phtab);
  2517. fputc(phoneme_tab_list2[ix].includes,f_phtab);
  2518. fputc(0,f_phtab);
  2519. fputc(0,f_phtab);
  2520. Write4Bytes(f_phtab, phoneme_tab_list2[ix].equivalence_tables); // byte index into phondata for equivalence tables
  2521. fwrite(phoneme_tab_list2[ix].name,1,N_PHONEME_TAB_NAME,f_phtab);
  2522. for(j=0; j<n; j++)
  2523. {
  2524. if(p[j].phflags & phLOCAL)
  2525. {
  2526. // this bit is set temporarily to incidate a local phoneme, declared in
  2527. // in the current phoneme file
  2528. p[j].phflags &= ~phLOCAL;
  2529. fwrite(&p[j],sizeof(PHONEME_TAB),1,f_phtab);
  2530. }
  2531. }
  2532. fwrite(&p[n],sizeof(PHONEME_TAB),1,f_phtab); // include the extra list-terminator phoneme entry
  2533. free(p);
  2534. }
  2535. }
  2536. static void EndPhonemeTable()
  2537. {
  2538. int ix;
  2539. int *pw;
  2540. int length;
  2541. if(n_phoneme_tabs == 0)
  2542. return;
  2543. fprintf(f_errors,"\n");
  2544. // check that all referenced phonemes have been declared
  2545. for(ix=0; ix<n_phcodes; ix++)
  2546. {
  2547. if(phoneme_tab2[ix].type == phINVALID)
  2548. {
  2549. fprintf(f_errors,"%3d: Phoneme [%s] not declared, referenced at line %d\n",linenum,
  2550. WordToString(phoneme_tab2[ix].mnemonic),(int)(phoneme_tab2[ix].program));
  2551. error_count++;
  2552. phoneme_tab2[ix].type = 0; // prevent the error message repeating
  2553. }
  2554. }
  2555. n_phcodes_list[n_phoneme_tabs-1] = n_phcodes;
  2556. if((length = p_equivalence - equivalence_buf) > 0)
  2557. {
  2558. // terminate the list of phoneme equivalence tables
  2559. pw = (int *)p_equivalence;
  2560. pw[0] = 0;
  2561. // write the equivalence data into phondata, and remember it's address
  2562. ix = ftell(f_phdata);
  2563. fprintf(f_phcontents,"Q 0x%.5x %s\n", ix, phoneme_tab_list2[n_phoneme_tabs-1].name);
  2564. phoneme_tab_list2[n_phoneme_tabs-1].equivalence_tables = ix;
  2565. fwrite(equivalence_buf, length+4, 1, f_phdata);
  2566. }
  2567. }
  2568. static void StartPhonemeTable(const char *name)
  2569. {
  2570. int ix;
  2571. int j;
  2572. PHONEME_TAB *p;
  2573. fprintf(f_errors,"______________________________\nPhoneme Table: '%s'\n",name);
  2574. if(n_phoneme_tabs >= N_PHONEME_TABS-1)
  2575. {
  2576. Error("Too many phonemetables");
  2577. return;
  2578. }
  2579. p = (PHONEME_TAB *)calloc(sizeof(PHONEME_TAB),N_PHONEME_TAB);
  2580. if(p == NULL)
  2581. {
  2582. Error("Out of memory");
  2583. return;
  2584. }
  2585. memset(&phoneme_tab_list2[n_phoneme_tabs], 0, sizeof(PHONEME_TAB_LIST));
  2586. phoneme_tab_list2[n_phoneme_tabs].phoneme_tab_ptr = phoneme_tab2 = p;
  2587. strncpy0(phoneme_tab_list2[n_phoneme_tabs].name, name, N_PHONEME_TAB_NAME);
  2588. n_phcodes = 1;
  2589. phoneme_tab_list2[n_phoneme_tabs].includes = 0;
  2590. p_equivalence = equivalence_buf;
  2591. if(n_phoneme_tabs > 0)
  2592. {
  2593. NextItem(tSTRING); // name of base phoneme table
  2594. for(ix=0; ix<n_phoneme_tabs; ix++)
  2595. {
  2596. if(strcmp(item_string,phoneme_tab_list2[ix].name)==0)
  2597. {
  2598. phoneme_tab_list2[n_phoneme_tabs].includes = ix+1;
  2599. // initialise the new phoneme table with the contents of this one
  2600. memcpy(phoneme_tab2,phoneme_tab_list2[ix].phoneme_tab_ptr,sizeof(PHONEME_TAB)*N_PHONEME_TAB);
  2601. n_phcodes = n_phcodes_list[ix];
  2602. // clear "local phoneme" bit"
  2603. for(j=0; j<n_phcodes; j++)
  2604. phoneme_tab2[j].phflags &= ~phLOCAL;
  2605. break;
  2606. }
  2607. }
  2608. if(ix == n_phoneme_tabs)
  2609. {
  2610. error("Can't find base phonemetable '%s'",item_string);
  2611. }
  2612. }
  2613. else
  2614. {
  2615. ReservePhCodes();
  2616. }
  2617. n_phoneme_tabs++;
  2618. }
  2619. static void CompileEquivalents()
  2620. {
  2621. // a list of phonemes in another language and the equivalent phoneme strings in this language
  2622. int ix;
  2623. int n_names;
  2624. int n_bytes;
  2625. int foreign_phoneme;
  2626. int foreign_error = 0;
  2627. int remove_stress = 0;
  2628. char *p_start;
  2629. char *p;
  2630. int foreign_table;
  2631. char foreign_table_name[40];
  2632. char line_buf[80];
  2633. char names[6][80];
  2634. char phcode[7];
  2635. NextItem(tSTRING);
  2636. strcpy(foreign_table_name, item_string);
  2637. if((foreign_table = SelectPhonemeTableName(foreign_table_name)) < 0)
  2638. {
  2639. if(strcmp(foreign_table_name, "NULL") != 0)
  2640. {
  2641. error("Unknown phoneme table '%s'", foreign_table_name);
  2642. }
  2643. foreign_error = 1;
  2644. foreign_phoneme = 0;
  2645. }
  2646. p_start = p_equivalence;
  2647. p_equivalence += 8;
  2648. p_start[0] = foreign_table;
  2649. linenum--;
  2650. while(!feof(f_in))
  2651. {
  2652. linenum++;
  2653. if(fgets(line_buf, sizeof(line_buf), f_in) == NULL)
  2654. break;
  2655. if((p = strstr(line_buf,"//")) != NULL)
  2656. *p = 0;
  2657. for(ix=0; ix<6; ix++)
  2658. names[ix][0] = 0;
  2659. n_names = sscanf(line_buf,"%s %s %s %s %s %s",names[0],names[1],names[2],names[3],names[4],names[5]);
  2660. if(n_names < 1)
  2661. continue;
  2662. if(strcmp(names[0],"endphoneme") == 0)
  2663. break;
  2664. if(foreign_error)
  2665. continue;
  2666. if(strcmp(names[0],"remove_stress") == 0)
  2667. {
  2668. remove_stress = 1;
  2669. continue;
  2670. }
  2671. if(p_equivalence > &equivalence_buf[sizeof(equivalence_buf) - 16])
  2672. {
  2673. error("'equivalents' tables are too large",NULL);
  2674. break;
  2675. }
  2676. if(foreign_error == 0)
  2677. {
  2678. phcode[0] = foreign_phoneme = LookupPhonemeString(names[0]);
  2679. if(foreign_phoneme == 0)
  2680. {
  2681. sprintf(line_buf,"%s/%s", foreign_table_name, names[0]);
  2682. error("Unknown phoneme '%s'", line_buf);
  2683. }
  2684. }
  2685. for(ix=1; ix<n_names; ix++)
  2686. {
  2687. phcode[ix] = LookupPhoneme(names[ix], 1);
  2688. }
  2689. // only write a translation if it has an effect
  2690. if((n_names > 2) || (phcode[0] != phcode[1]))
  2691. {
  2692. // write: foreign phoneme number, then a string of local phoneme numbers
  2693. memcpy(p_equivalence, phcode, n_names);
  2694. p_equivalence += n_names;
  2695. *p_equivalence++ = 0;
  2696. }
  2697. }
  2698. *p_equivalence++ = 0;
  2699. p_equivalence = (char *)((intptr_t)(p_equivalence + 3) & ~0x3); // align to word boundary
  2700. n_bytes = p_equivalence - p_start;
  2701. p_start[1] = remove_stress;
  2702. n_bytes = n_bytes / 4;
  2703. p_start[2] = n_bytes >> 8; // index of next table
  2704. p_start[3] = n_bytes;
  2705. }
  2706. static void CompilePhonemeFiles()
  2707. {
  2708. int item;
  2709. FILE *f;
  2710. char buf[sizeof(path_home)+120];
  2711. linenum = 1;
  2712. count_references = 0;
  2713. duplicate_references = 0;
  2714. count_frames = 0;
  2715. n_procs = 0;
  2716. for(;; )
  2717. {
  2718. if(feof(f_in))
  2719. {
  2720. // end of file, go back to previous from, from which this was included
  2721. if(stack_ix == 0)
  2722. break; // end of top level, finished
  2723. fclose(f_in);
  2724. f_in = stack[--stack_ix].file;
  2725. strcpy(current_fname,stack[stack_ix].fname);
  2726. linenum = stack[stack_ix].linenum;
  2727. fprintf(f_errors,"\n\n");
  2728. }
  2729. item = NextItem(tKEYWORD);
  2730. switch(item)
  2731. {
  2732. case kUTF8_BOM:
  2733. break; // ignore bytes 0xef 0xbb 0xbf
  2734. case kINCLUDE:
  2735. NextItem(tSTRING);
  2736. sprintf(buf,"%s/../phsource/%s",path_home,item_string);
  2737. if((stack_ix < N_STACK) && (f = fopen_log(f_errors,buf,"rb")) != NULL)
  2738. {
  2739. fprintf(f_errors,"include %s\n",item_string);
  2740. stack[stack_ix].linenum = linenum;
  2741. strcpy(stack[stack_ix].fname,current_fname);
  2742. stack[stack_ix++].file = f_in;
  2743. f_in = f;
  2744. strncpy0(current_fname,item_string,sizeof(current_fname));
  2745. linenum = 1;
  2746. }
  2747. else
  2748. {
  2749. error("Missing file: %s", item_string);
  2750. }
  2751. break;
  2752. case kPHONEMETABLE:
  2753. EndPhonemeTable();
  2754. NextItem(tSTRING); // name of the new phoneme table
  2755. StartPhonemeTable(item_string);
  2756. break;
  2757. case kPHONEMESTART:
  2758. if(n_phoneme_tabs == 0)
  2759. {
  2760. Error("phonemetable is missing");
  2761. return;
  2762. }
  2763. CompilePhoneme(1);
  2764. break;
  2765. case kPROCEDURE:
  2766. CompilePhoneme(0);
  2767. break;
  2768. case kEQUIVALENTS:
  2769. CompileEquivalents();
  2770. break;
  2771. default:
  2772. if(!feof(f_in))
  2773. Error("Keyword 'phoneme' expected");
  2774. break;
  2775. }
  2776. }
  2777. phoneme_tab2[n_phcodes+1].mnemonic = 0; // terminator
  2778. }
  2779. static espeak_ng_STATUS CompilePhonemeData2(const char *source, FILE *log)
  2780. {
  2781. char fname[sizeof(path_home)+40];
  2782. sprintf(fname,"%s/../phsource",path_home);
  2783. #ifdef MAKE_ENVELOPES
  2784. make_envs();
  2785. #endif
  2786. fprintf(log,"Compiling phoneme data: %s\n",fname);
  2787. n_envelopes = 0;
  2788. error_count = 0;
  2789. resample_count = 0;
  2790. memset(markers_used,0,sizeof(markers_used));
  2791. f_errors = log;
  2792. if(!access(fname, 755))
  2793. {
  2794. fprintf(log,"Can't find phoneme source directory: %s\n",fname);
  2795. return ENE_READ_ERROR;
  2796. }
  2797. strncpy0(current_fname,source,sizeof(current_fname));
  2798. sprintf(fname,"%s/../phsource/phonemes",path_home);
  2799. f_in = fopen_log(f_errors,fname,"rb");
  2800. if(f_in == NULL)
  2801. {
  2802. fprintf(log,"Can't read master phonemes file: %s\n",fname);
  2803. return ENE_READ_ERROR;
  2804. }
  2805. sprintf(fname,"%s/../phsource/%s",path_home,"error_log");
  2806. if((f_errors = fopen_log(f_errors,fname,"w")) == NULL)
  2807. f_errors = stderr;
  2808. sprintf(fname,"%s/../phsource/%s",path_home,"compile_report");
  2809. f_report = fopen_log(f_errors, fname,"w");
  2810. sprintf(fname,"%s/%s",path_home,"phondata-manifest");
  2811. if((f_phcontents = fopen_log(f_phcontents,fname,"w")) == NULL)
  2812. f_phcontents = stderr;
  2813. fprintf (f_phcontents,
  2814. "# This file lists the type of data that has been compiled into the\n"
  2815. "# phondata file\n"
  2816. "#\n"
  2817. "# The first character of a line indicates the type of data:\n"
  2818. "# S - A SPECT_SEQ structure\n"
  2819. "# W - A wavefile segment\n"
  2820. "# E - An envelope\n"
  2821. "# Q - Phoneme equivalence tables\n"
  2822. "#\n"
  2823. "# Address is the displacement within phondata of this item\n"
  2824. "#\n"
  2825. "# Address Data file\n"
  2826. "# ------- ---------\n");
  2827. fprintf(f_errors, "Source data path = '%s/../phsource'\n", path_home);
  2828. fprintf(f_errors, "Master phonemes file = '%s/../phsource/phonemes'\n", path_home);
  2829. fprintf(f_errors, "Output to '%s/'\n\n", path_home);
  2830. sprintf(fname,"%s/%s",path_home,"phondata");
  2831. f_phdata = fopen_log(f_errors,fname,"wb");
  2832. sprintf(fname,"%s/%s",path_home,"phonindex");
  2833. f_phindex = fopen_log(f_errors,fname,"wb");
  2834. sprintf(fname,"%s/%s",path_home,"phontab");
  2835. f_phtab = fopen_log(f_errors,fname,"wb");
  2836. if(f_phdata==NULL || f_phindex==NULL || f_phtab==NULL)
  2837. {
  2838. return ENE_WRITE_ERROR;
  2839. }
  2840. sprintf(fname,"%s/../phsource/compile_prog_log",path_home);
  2841. f_prog_log = fopen_log(f_errors,fname,"wb");
  2842. fprintf(log,"Compiling phoneme data: %s/../phsource\n",path_home);
  2843. // write a word so that further data doesn't start at displ=0
  2844. Write4Bytes(f_phdata,version_phdata);
  2845. Write4Bytes(f_phdata,samplerate_native);
  2846. Write4Bytes(f_phindex,version_phdata);
  2847. memset(ref_hash_tab,0,sizeof(ref_hash_tab));
  2848. n_phoneme_tabs = 0;
  2849. stack_ix = 0;
  2850. StartPhonemeTable("base");
  2851. CompilePhonemeFiles();
  2852. EndPhonemeTable();
  2853. WritePhonemeTables();
  2854. fprintf(f_errors,"\nRefs %d, Reused %d\n",count_references,duplicate_references);
  2855. fclose(f_in);
  2856. fclose(f_phdata);
  2857. if(f_prog_log != NULL)
  2858. fclose(f_prog_log);
  2859. fclose(f_phindex);
  2860. fclose(f_phtab);
  2861. fclose(f_phcontents);
  2862. LoadPhData(NULL);
  2863. CompileReport();
  2864. #ifdef MAKE_ENVELOPES
  2865. DrawEnvelopes();
  2866. #endif
  2867. if(resample_count > 0)
  2868. {
  2869. fprintf(f_errors, "\n%d WAV files resampled to %d Hz\n", resample_count, samplerate_native);
  2870. fprintf(log,"Compiled phonemes: %d errors, %d files resampled to %d Hz.\n",error_count, resample_count, samplerate_native);
  2871. }
  2872. else
  2873. {
  2874. fprintf(log,"Compiled phonemes: %d errors.\n",error_count);
  2875. }
  2876. if(f_errors != stderr)
  2877. fclose(f_errors);
  2878. ReadPhondataManifest();
  2879. return ENS_OK;
  2880. }
  2881. static const char *preset_tune_names[] = {
  2882. "s1", "c1", "q1", "e1", NULL
  2883. };
  2884. static const TUNE default_tune = {
  2885. {0,0,0,0,0,0,0,0,0,0,0,0},
  2886. {0,0,0,0},
  2887. {0, 40, 24, 8, 0, 0, 0, 0},
  2888. 46, 57, PITCHfall, 16, 0, 0,
  2889. 255, 78, 50, 255,
  2890. 3, 5,
  2891. {-7,-7,-7}, {-7,-7,-7},
  2892. PITCHfall, 64, 8,
  2893. PITCHfall, 70, 18, 24, 12,
  2894. PITCHfall, 70, 18, 24, 12, 0,
  2895. {0,0,0,0,0,0,0,0}, 0
  2896. };
  2897. #define N_TUNE_NAMES 100
  2898. MNEM_TAB envelope_names[] = {
  2899. {"fall", 0},
  2900. {"rise", 2},
  2901. {"fall-rise", 4},
  2902. {"fall-rise2", 6},
  2903. {"rise-fall", 8},
  2904. {"fall-rise3",10},
  2905. {"fall-rise4",12},
  2906. {"fall2", 14},
  2907. {"rise2", 16},
  2908. {"rise-fall-rise", 18},
  2909. {NULL, -1}
  2910. };
  2911. int LookupEnvelopeName(const char *name)
  2912. {
  2913. return(LookupMnem(envelope_names, name));
  2914. }
  2915. #pragma GCC visibility push(default)
  2916. espeak_ng_STATUS espeak_ng_CompileIntonation(FILE *log)
  2917. {
  2918. if(!log) log=stderr;
  2919. int ix;
  2920. char *p;
  2921. char c;
  2922. int keyword;
  2923. int compiling_tune = 0;
  2924. int n_tune_names = 0;
  2925. int done_split;
  2926. int done_onset = 0;
  2927. int done_last = 0;
  2928. int n_preset_tunes = 0;
  2929. int found;
  2930. int tune_number = 0;
  2931. FILE *f_out;
  2932. TUNE *tune_data;
  2933. TUNE new_tune;
  2934. char name[12];
  2935. char tune_names[N_TUNE_NAMES][12];
  2936. char buf[sizeof(path_home)+150];
  2937. error_count = 0;
  2938. f_errors = log;
  2939. sprintf(buf,"%s/../phsource/intonation.txt",path_home);
  2940. if((f_in = fopen(buf, "r")) == NULL)
  2941. {
  2942. sprintf(buf,"%s/../phsource/intonation",path_home);
  2943. if((f_in = fopen_log(f_errors, buf, "r")) == NULL)
  2944. {
  2945. fprintf(log,"Can't read file: %s\n",buf);
  2946. fclose(f_errors);
  2947. return ENE_READ_ERROR;
  2948. }
  2949. }
  2950. for(ix=0; preset_tune_names[ix] != NULL; ix++)
  2951. {
  2952. strcpy(tune_names[ix], preset_tune_names[ix]);
  2953. }
  2954. n_tune_names = ix;
  2955. n_preset_tunes = ix;
  2956. // make a list of the tune names
  2957. while(!feof(f_in))
  2958. {
  2959. if(fgets(buf,sizeof(buf),f_in) == NULL)
  2960. break;
  2961. if((memcmp(buf,"tune",4)==0) && isspace(buf[4]))
  2962. {
  2963. p = &buf[5];
  2964. while(isspace(*p)) p++;
  2965. ix = 0;
  2966. while((ix < (int)(sizeof(name) - 1)) && !isspace(*p))
  2967. {
  2968. name[ix++] = *p++;
  2969. }
  2970. name[ix] = 0;
  2971. found = 0;
  2972. for(ix = 0; ix < n_tune_names; ix++)
  2973. {
  2974. if(strcmp(name, tune_names[ix]) == 0)
  2975. {
  2976. found = 1;
  2977. break;
  2978. }
  2979. }
  2980. if(found == 0)
  2981. {
  2982. strncpy0(tune_names[n_tune_names++], name, sizeof(name));
  2983. if(n_tune_names >= N_TUNE_NAMES)
  2984. break;
  2985. }
  2986. }
  2987. }
  2988. rewind(f_in);
  2989. linenum = 1;
  2990. tune_data = (TUNE *)calloc(sizeof(TUNE), n_tune_names);
  2991. if(tune_data == NULL)
  2992. {
  2993. fprintf(f_errors, "Failed to allocate data for tunes\n");
  2994. fclose(f_in);
  2995. fclose(f_errors);
  2996. return ENE_OUT_OF_MEMORY;
  2997. }
  2998. sprintf(buf,"%s/intonations",path_home);
  2999. f_out = fopen_log(f_errors, buf, "wb");
  3000. if(f_out == NULL)
  3001. {
  3002. fclose(f_in);
  3003. fclose(f_errors);
  3004. free(tune_data);
  3005. return ENE_WRITE_ERROR;
  3006. }
  3007. while(!feof(f_in))
  3008. {
  3009. keyword = NextItem(tINTONATION);
  3010. switch(keyword)
  3011. {
  3012. case kTUNE:
  3013. if(compiling_tune)
  3014. {
  3015. }
  3016. compiling_tune = 1;
  3017. done_split = 0;
  3018. memcpy(&new_tune, &default_tune, sizeof(TUNE));
  3019. NextItem(tSTRING);
  3020. strncpy0(new_tune.name, item_string, sizeof(new_tune.name));
  3021. found = 0;
  3022. tune_number = 0;
  3023. for(ix=0; ix < n_tune_names; ix++)
  3024. {
  3025. if(strcmp(new_tune.name, tune_names[ix]) == 0)
  3026. {
  3027. found = 1;
  3028. tune_number = ix;
  3029. if(tune_data[ix].name[0] != 0)
  3030. found = 2;
  3031. break;
  3032. }
  3033. }
  3034. if(found == 2)
  3035. {
  3036. error("Duplicate tune name: '%s'",new_tune.name);
  3037. }
  3038. if(found == 0)
  3039. {
  3040. error("Bad tune name: '%s;", new_tune.name);
  3041. }
  3042. break;
  3043. case kENDTUNE:
  3044. compiling_tune = 0;
  3045. if(done_onset == 0)
  3046. {
  3047. new_tune.unstr_start[0] = new_tune.unstr_start[1];
  3048. new_tune.unstr_end[0] = new_tune.unstr_end[1];
  3049. }
  3050. if(done_last == 0)
  3051. {
  3052. new_tune.unstr_start[2] = new_tune.unstr_start[1];
  3053. new_tune.unstr_end[2] = new_tune.unstr_end[1];
  3054. }
  3055. memcpy(&tune_data[tune_number], &new_tune, sizeof(TUNE));
  3056. break;
  3057. case kTUNE_PREHEAD:
  3058. new_tune.prehead_start = NextItem(tNUMBER);
  3059. new_tune.prehead_end = NextItem(tNUMBER);
  3060. break;
  3061. case kTUNE_ONSET:
  3062. new_tune.onset = NextItem(tNUMBER);
  3063. new_tune.unstr_start[0] = NextItem(tSIGNEDNUMBER);
  3064. new_tune.unstr_end[0] = NextItem(tSIGNEDNUMBER);
  3065. done_onset = 1;
  3066. break;
  3067. case kTUNE_HEADLAST:
  3068. new_tune.head_last = NextItem(tNUMBER);
  3069. new_tune.unstr_start[2] = NextItem(tSIGNEDNUMBER);
  3070. new_tune.unstr_end[2] = NextItem(tSIGNEDNUMBER);
  3071. done_last = 1;
  3072. break;
  3073. case kTUNE_HEADENV:
  3074. NextItem(tSTRING);
  3075. if((ix = LookupEnvelopeName(item_string)) < 0)
  3076. error("Bad envelope name: '%s'",item_string);
  3077. else
  3078. new_tune.stressed_env = ix;
  3079. new_tune.stressed_drop = NextItem(tNUMBER);
  3080. break;
  3081. case kTUNE_HEAD:
  3082. new_tune.head_max_steps = NextItem(tNUMBER);
  3083. new_tune.head_start = NextItem(tNUMBER);
  3084. new_tune.head_end = NextItem(tNUMBER);
  3085. new_tune.unstr_start[1] = NextItem(tSIGNEDNUMBER);
  3086. new_tune.unstr_end[1] = NextItem(tSIGNEDNUMBER);
  3087. break;
  3088. case kTUNE_HEADEXTEND:
  3089. // up to 8 numbers
  3090. for(ix=0; ix < (int)(sizeof(new_tune.head_extend)); ix++)
  3091. {
  3092. if(!isdigit(c = CheckNextChar()) && (c != '-'))
  3093. break;
  3094. new_tune.head_extend[ix] = (NextItem(tSIGNEDNUMBER) * 64) / 100; // convert from percentage to 64ths
  3095. }
  3096. new_tune.n_head_extend = ix; // number of values
  3097. break;
  3098. case kTUNE_NUCLEUS0:
  3099. NextItem(tSTRING);
  3100. if((ix = LookupEnvelopeName(item_string)) < 0)
  3101. {
  3102. error("Bad envelope name: '%s'",item_string);
  3103. break;
  3104. }
  3105. new_tune.nucleus0_env = ix;
  3106. new_tune.nucleus0_max = NextItem(tNUMBER);
  3107. new_tune.nucleus0_min = NextItem(tNUMBER);
  3108. break;
  3109. case kTUNE_NUCLEUS1:
  3110. NextItem(tSTRING);
  3111. if((ix = LookupEnvelopeName(item_string)) < 0)
  3112. {
  3113. error("Bad envelope name: '%s'",item_string);
  3114. break;
  3115. }
  3116. new_tune.nucleus1_env = ix;
  3117. new_tune.nucleus1_max = NextItem(tNUMBER);
  3118. new_tune.nucleus1_min = NextItem(tNUMBER);
  3119. new_tune.tail_start = NextItem(tNUMBER);
  3120. new_tune.tail_end = NextItem(tNUMBER);
  3121. if(!done_split)
  3122. {
  3123. // also this as the default setting for 'split'
  3124. new_tune.split_nucleus_env = ix;
  3125. new_tune.split_nucleus_max = new_tune.nucleus1_max;
  3126. new_tune.split_nucleus_min = new_tune.nucleus1_min;
  3127. new_tune.split_tail_start = new_tune.tail_start;
  3128. new_tune.split_tail_end = new_tune.tail_end;
  3129. }
  3130. break;
  3131. case kTUNE_SPLIT:
  3132. NextItem(tSTRING);
  3133. if((ix = LookupEnvelopeName(item_string)) < 0)
  3134. {
  3135. error("Bad envelope name: '%s'",item_string);
  3136. break;
  3137. }
  3138. done_split = 1;
  3139. new_tune.split_nucleus_env = ix;
  3140. new_tune.split_nucleus_max = NextItem(tNUMBER);
  3141. new_tune.split_nucleus_min = NextItem(tNUMBER);
  3142. new_tune.split_tail_start = NextItem(tNUMBER);
  3143. new_tune.split_tail_end = NextItem(tNUMBER);
  3144. NextItem(tSTRING);
  3145. item_string[12] = 0;
  3146. for(ix=0; ix < n_tune_names; ix++)
  3147. {
  3148. if(strcmp(item_string, tune_names[ix]) == 0)
  3149. break;
  3150. }
  3151. if(ix == n_tune_names)
  3152. error("Tune '%s' not found",item_string);
  3153. else
  3154. new_tune.split_tune = ix;
  3155. break;
  3156. default:
  3157. error("Unexpected: '%s'",item_string);
  3158. break;
  3159. }
  3160. }
  3161. for(ix = 0; ix < n_preset_tunes; ix++)
  3162. {
  3163. if(tune_data[ix].name[0] == 0)
  3164. {
  3165. error("Tune '%s' not defined", preset_tune_names[ix]);
  3166. }
  3167. }
  3168. fwrite(tune_data, n_tune_names, sizeof(TUNE), f_out);
  3169. free(tune_data);
  3170. fclose(f_in);
  3171. fclose(f_out);
  3172. fprintf(log,"Compiled %d intonation tunes: %d errors.\n",n_tune_names, error_count);
  3173. LoadPhData(NULL);
  3174. return error_count > 0 ? ENE_COMPILE_ERRORS : ENS_OK;
  3175. }
  3176. espeak_ng_STATUS espeak_ng_CompilePhonemeData(long rate, FILE *log)
  3177. {
  3178. if(!log) log=stderr;
  3179. WavegenInit(rate, 0);
  3180. WavegenSetVoice(voice);
  3181. return CompilePhonemeData2("phonemes", log);
  3182. }
  3183. #pragma GCC visibility pop