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

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