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

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