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

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