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

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