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

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