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

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