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

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