eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.

compiledata.cpp 50KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2007 by Jonathan Duddington *
  3. * email: jonsd@users.sourceforge.net *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 3 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, see: *
  17. * <http://www.gnu.org/licenses/>. *
  18. ***************************************************************************/
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22. #include <stdlib.h>
  23. #include "wx/wx.h"
  24. #include "wx/wfstream.h"
  25. #include "wx/dir.h"
  26. #include "wx/filename.h"
  27. #include "speak_lib.h"
  28. #include "main.h"
  29. #include "speech.h"
  30. #include "options.h"
  31. #include "phoneme.h"
  32. #include "synthesize.h"
  33. #include "voice.h"
  34. #include "spect.h"
  35. #define tNUMBER 1
  36. #define tSTRING 2
  37. #define tPHONEMEMNEM 3
  38. #define tKEYWORD 4
  39. #define tSIGNEDNUMBER 5
  40. #define tPHONEMESTART 1
  41. #define tEND 2
  42. #define tSPECT 3
  43. #define tWAVE 4
  44. #define tSTARTTYPE 5
  45. #define tENDTYPE 6
  46. #define tBEFORE 7
  47. #define tAFTER 8
  48. #define tTONESPEC 9
  49. #define tLENGTHMOD 11
  50. #define tLENGTH 12
  51. #define tLONGLENGTH 13
  52. #define tOLDNAME 14
  53. #define tREDUCETO 15
  54. #define tFIXEDCODE 16
  55. #define tBEFOREVOWEL 17
  56. #define tBEFOREVOWELPAUSE 18
  57. #define tBEFORENOTVOWEL 19
  58. #define tLINKOUT 20
  59. #define tSWITCHVOICING 21
  60. #define tVOWELIN 22
  61. #define tVOWELOUT 23
  62. #define tAPPENDPH 24 // always insert another phoneme (linkout) after this one
  63. #define tIMPORTPH 25
  64. #define tPHONEMENUMBER 29
  65. #define tPHONEMETABLE 30
  66. #define tINCLUDE 31
  67. extern void Write4Bytes(FILE *f, int value);
  68. extern void MakeVowelLists(void);
  69. extern void FindPhonemesUsed(void);
  70. extern void DrawEnvelopes();
  71. extern int CompileDictionary(const char *dsource, const char *dict_name, FILE *log, char *fname);
  72. static int markers_used[8];
  73. typedef struct {
  74. void *link;
  75. int value;
  76. int ph_mnemonic;
  77. short ph_table;
  78. char string[1];
  79. } REF_HASH_TAB;
  80. #define N_ENVELOPES 30
  81. #define ENV_LEN 128
  82. int n_envelopes = 0;
  83. char envelope_paths[N_ENVELOPES][80];
  84. unsigned char envelope_dat[N_ENVELOPES][ENV_LEN];
  85. class Compile
  86. {//==========
  87. public:
  88. Compile();
  89. void CPhonemeTab(const char *path);
  90. private:
  91. int CPhoneme();
  92. void StartPhonemeTable(const char *name);
  93. void EndPhonemeTable();
  94. void CPhonemeFiles();
  95. int NextItem(int type);
  96. void UngetItem();
  97. void Error(const char *msg);
  98. void Error(const char *msg, const char *msg2);
  99. void Summary(FILE *f);
  100. void SummarySpect(FILE *f,unsigned int value);
  101. void SummarySpectList(FILE *f,int ref,const char *label);
  102. const char *PhonemeMnem(int phcode);
  103. void WritePhonemeTable();
  104. void Report();
  105. int LookupPhoneme(const char *string, int declare);
  106. void ReservePhCode(const char *string, int value);
  107. const char *GetKeyMnem(int value);
  108. int LoadSpect(const char *path, int control);
  109. int LoadWavefile(FILE *f, const char *fname);
  110. int LoadEnvelope(FILE *f, const char *fname);
  111. int LoadEnvelope2(FILE *f, const char *fname);
  112. int LoadDataFile(const char *path, int control);
  113. int AddSpect(int phcode, int *list, int control);
  114. void AddSpectList(int *list, int control);
  115. void AddEnvelope(int *list);
  116. void VowelTransition(int which, unsigned int *trans);
  117. void ImportPhoneme(void);
  118. FILE *f_in;
  119. FILE *f_phdata;
  120. FILE *f_phcontents;
  121. FILE *f_errors;
  122. FILE *f_phindex;
  123. FILE *f_phtab;
  124. int f_in_displ;
  125. int f_in_linenum;
  126. PHONEME_TAB *ph;
  127. int linenum;
  128. int item_type;
  129. char current_fname[80];
  130. char item_string[256];
  131. char phoneme_tab_name[N_PHONEME_TAB_NAME];
  132. unsigned int vowel_in[2];
  133. unsigned int vowel_out[2];
  134. int n_phcodes_list[N_PHONEME_TABS];
  135. PHONEME_TAB_LIST phoneme_tab_list2[N_PHONEME_TABS];
  136. PHONEME_TAB *phoneme_tab2;
  137. int n_phoneme_tabs;
  138. int n_phcodes;
  139. int count_references;
  140. int duplicate_references;
  141. int count_frames;
  142. int error_count;
  143. REF_HASH_TAB *ref_hash_tab[256];
  144. typedef struct {
  145. FILE *file;
  146. int linenum;
  147. char fname[80];
  148. } STACK;
  149. #define N_STACK 12
  150. int stack_ix;
  151. STACK stack[N_STACK];
  152. };
  153. char path_source[sizeof(path_home)+20];
  154. Compile *compile;
  155. typedef struct {
  156. const char *mnem;
  157. int data;
  158. } keywtab_t;
  159. static keywtab_t keywords[] = {
  160. {"vowel", 0x1000000+phVOWEL},
  161. {"liquid", 0x1000000+phLIQUID},
  162. {"pause", 0x1000000+phPAUSE},
  163. {"stress", 0x1000000+phSTRESS},
  164. {"virtual", 0x1000000+phVIRTUAL},
  165. {"fricative", 0x1000000+phFRICATIVE},
  166. {"vstop", 0x1000000+phVSTOP},
  167. {"vfricative",0x1000000+phVFRICATIVE},
  168. {"delete_phoneme", 0x1000000+phDELETED},
  169. // type of consonant
  170. {"stop", 0x1000000+phSTOP},
  171. {"frc", 0x1000000+phFRICATIVE},
  172. {"nasal", 0x1000000+phNASAL},
  173. {"flp", 0x1000000+phVSTOP},
  174. {"afr", 0x1000000+phSTOP}, // treat as stop
  175. {"apr", 0x1000000+phFRICATIVE}, // [h] voiceless approximant
  176. // keywords
  177. {"phonemenumber", 29},
  178. {"phonemetable", 30},
  179. {"include", 31},
  180. {"phoneme", 1},
  181. {"endphoneme", 2},
  182. {"formants", 3},
  183. {"wave", 4},
  184. {"starttype",5},
  185. {"endtype", 6},
  186. {"before", 7},
  187. {"after", 8},
  188. {"tone", 9},
  189. {"lengthmod",11},
  190. {"length", 12},
  191. {"longlength", 13},
  192. {"reduceto", 15},
  193. {"beforevowel", 17},
  194. {"beforevowelpause", 18},
  195. {"beforenotvowel",19},
  196. {"linkout",20},
  197. {"switchvoicing",21},
  198. {"vowelin",22},
  199. {"vowelout",23},
  200. {"appendph",24},
  201. {"import_phoneme",25},
  202. // flags
  203. {"wavef", 0x2000000+phWAVE},
  204. {"unstressed", 0x2000000+phUNSTRESSED},
  205. {"fortis", 0x2000000+phFORTIS},
  206. {"sibilant", 0x2000000+phSIBILANT},
  207. {"nolink", 0x2000000+phNOLINK},
  208. {"trill", 0x2000000+phTRILL},
  209. {"vowel2", 0x2000000+phVOWEL2},
  210. {"palatal", 0x2000000+phPALATAL},
  211. {"long", 0x2000000+phLONG},
  212. {"brkafter", 0x2000000+phBRKAFTER},
  213. {"nonsyllabic",0x2000000+phNONSYLLABIC},
  214. // voiced / unvoiced
  215. {"vcd", 0x2000000+phVOICED},
  216. {"vls", 0x2000000+phFORTIS},
  217. // place of articulation
  218. {"blb", 0x2010000},
  219. {"lbd", 0x2020000},
  220. {"dnt", 0x2030000},
  221. {"alv", 0x2040000},
  222. {"rfx", 0x2050000},
  223. {"pla", 0x2060000},
  224. {"pal", 0x2070000},
  225. {"vel", 0x2080000},
  226. {"lbv", 0x2090000},
  227. {"uvl", 0x20a0000},
  228. {"phr", 0x20b0000},
  229. {"glt", 0x20c0000},
  230. // vowel transition attributes
  231. {"len=", 0x3000001},
  232. {"rms=", 0x3000002},
  233. {"f1=", 0x3000003},
  234. {"f2=", 0x3000004},
  235. {"f3=", 0x3000005},
  236. {"brk", 0x3000006},
  237. {"rate", 0x3000007},
  238. {"glstop", 0x3000008},
  239. {"lenadd", 0x3000009},
  240. {"f4", 0x300000a},
  241. {"paus", 0x300000b},
  242. {"colr=",0x300000c},
  243. {"amp=", 0x300000d},
  244. {"", -1}
  245. };
  246. extern void strncpy0(char *to,const char *from, int size);
  247. static unsigned int StringToWord(const char *string)
  248. {//=================================================
  249. // Pack 4 characters into a word
  250. int ix;
  251. unsigned char c;
  252. unsigned int word;
  253. word = 0;
  254. for(ix=0; ix<4; ix++)
  255. {
  256. if(string[ix]==0) break;
  257. c = string[ix];
  258. word |= (c << (ix*8));
  259. }
  260. return(word);
  261. }
  262. int Read4Bytes(FILE *f)
  263. {//====================
  264. // Read 4 bytes (least significant first) into a word
  265. int ix;
  266. unsigned char c;
  267. int acc=0;
  268. for(ix=0; ix<4; ix++)
  269. {
  270. c = fgetc(f) & 0xff;
  271. acc += (c << (ix*8));
  272. }
  273. return(acc);
  274. }
  275. static FILE *fopen_log(FILE *f_log, const char *fname,const char *access)
  276. {//==================================================
  277. // performs fopen, but produces error message to f_log if it fails
  278. FILE *f;
  279. if((f = fopen(fname,access)) == NULL)
  280. {
  281. if(f_log != NULL)
  282. fprintf(f_log,"Can't access (%s) file '%s'\n",access,fname);
  283. }
  284. return(f);
  285. }
  286. int Hash8(const char *string)
  287. //===========================
  288. /* Generate a hash code from the specified string */
  289. {
  290. int c;
  291. int chars=0;
  292. int hash=0;
  293. while((c = *string++) != 0)
  294. {
  295. c = tolower(c) - 'a';
  296. hash = hash * 8 + c;
  297. hash = (hash & 0x1ff) ^ (hash >> 8); /* exclusive or */
  298. chars++;
  299. }
  300. return((hash+chars) & 0xff);
  301. } // end of Hash8
  302. Compile::Compile()
  303. {//===============
  304. linenum = 0;
  305. }
  306. void Compile::Error(const char *msg)
  307. {//=================================
  308. fprintf(f_errors,"%3d: %s\n",linenum,msg);
  309. error_count++;
  310. }
  311. void Compile::Error(const char *msg, const char *msg2)
  312. {//=================================
  313. fprintf(f_errors,"%3d: %s: '%s'\n",linenum,msg,msg2);
  314. error_count++;
  315. }
  316. const char *Compile::PhonemeMnem(int phcode)
  317. {//==========================================
  318. if((phcode >= 0) && (phcode < N_PHONEME_TAB))
  319. return(WordToString(phoneme_tab2[phcode].mnemonic));
  320. else
  321. return("???");
  322. }
  323. const char *Compile::GetKeyMnem(int value)
  324. {//=======================================
  325. keywtab_t *p;
  326. p = keywords;
  327. while(p->data != -1)
  328. {
  329. if(p->data == value)
  330. return(p->mnem);
  331. p++;
  332. }
  333. return("???");
  334. }
  335. void Compile::ReservePhCode(const char *string, int value)
  336. {//=======================================================
  337. // Use a fixed number for this phoneme. Must be used before any
  338. // phonemes are declared
  339. unsigned int word;
  340. word = StringToWord(string);
  341. phoneme_tab2[value].mnemonic = word;
  342. phoneme_tab2[value].code = value;
  343. if(n_phcodes <= value)
  344. n_phcodes = value+1;
  345. }
  346. int Compile::LookupPhoneme(const char *string, int control)
  347. {//========================================================
  348. // control = 0 explicit declaration
  349. // control = 1 declare phoneme if not found
  350. // control = 2 start looking after control & stress phonemes
  351. int ix;
  352. int start;
  353. int use;
  354. unsigned int word;
  355. if(strcmp(string,"NULL")==0)
  356. return(1);
  357. ix = strlen(string);
  358. if((ix==0) || (ix> 4))
  359. {
  360. Error("Bad phoneme",string);
  361. }
  362. word = StringToWord(string);
  363. // don't use phoneme number 0, reserved for string terminator
  364. start = 1;
  365. if(control==2)
  366. start = 8; // don't look for control and stress phonemes (allows these characters to be
  367. // used for other purposes)
  368. use = 0;
  369. for(ix=start; ix<n_phcodes; ix++)
  370. {
  371. if(phoneme_tab2[ix].mnemonic == word)
  372. return(ix);
  373. if((use==0) && (phoneme_tab2[ix].mnemonic == 0))
  374. {
  375. use = ix;
  376. }
  377. }
  378. // if(control != 1) return(-1); // not found
  379. if(use == 0)
  380. {
  381. if(n_phcodes >= N_PHONEME_TAB-1)
  382. return(-1); // phoneme table is full
  383. use = n_phcodes++;
  384. }
  385. // add this phoneme to the phoneme table
  386. phoneme_tab2[use].mnemonic = word;
  387. phoneme_tab2[use].type = phINVALID;
  388. phoneme_tab2[use].spect = linenum; // for error report if the phoneme remains undeclared
  389. return(use);
  390. } // end of Compile::LookupPhoneme
  391. int Compile::NextItem(int type)
  392. {//============================
  393. int acc;
  394. unsigned char c=0;
  395. unsigned char c2;
  396. int ix;
  397. int sign;
  398. int keyword;
  399. keywtab_t *p;
  400. f_in_displ = ftell(f_in);
  401. f_in_linenum = linenum;
  402. while(!feof(f_in))
  403. {
  404. c = fgetc(f_in);
  405. if(c=='/')
  406. {
  407. if((c2 = fgetc(f_in))=='/')
  408. {
  409. // comment, ignore to end of line
  410. while(!feof(f_in) && ((c=fgetc(f_in)) != '\n'));
  411. }
  412. else
  413. {
  414. ungetc(c2,f_in);
  415. }
  416. }
  417. if(c == '\n') linenum++;
  418. if(!isspace(c))
  419. {
  420. break;
  421. }
  422. }
  423. if(feof(f_in))
  424. return(tEND);
  425. if((type == tNUMBER) || (type == tSIGNEDNUMBER))
  426. {
  427. acc = 0;
  428. sign = 1;
  429. if((c == '-') && (type == tSIGNEDNUMBER))
  430. {
  431. sign = -1;
  432. c = fgetc(f_in);
  433. }
  434. if(!isdigit(c))
  435. Error("Expected number");
  436. while(isdigit(c))
  437. {
  438. acc *= 10;
  439. acc += (c - '0');
  440. c = fgetc(f_in);
  441. }
  442. ungetc(c,f_in);
  443. return(acc * sign);
  444. }
  445. ix = 0;
  446. while(!feof(f_in) && !isspace(c))
  447. {
  448. item_string[ix++] = c;
  449. c = fgetc(f_in);
  450. if(feof(f_in))
  451. break;
  452. if(item_string[ix-1] == '=')
  453. break;
  454. }
  455. item_string[ix] = 0;
  456. if(!feof(f_in))
  457. ungetc(c,f_in);
  458. keyword = -1;
  459. if(type == tSTRING)
  460. {
  461. return(0);
  462. }
  463. if(type == tKEYWORD)
  464. {
  465. p = keywords;
  466. while(p->data != -1)
  467. {
  468. if(strcmp(item_string,p->mnem)==0)
  469. {
  470. item_type = p->data >> 24;
  471. return(p->data & 0xffffff);
  472. }
  473. p++;
  474. }
  475. item_type = -1;
  476. return(-1); // keyword not found
  477. }
  478. if(type == tPHONEMEMNEM)
  479. {
  480. return(LookupPhoneme(item_string,2));
  481. }
  482. return(-1);
  483. } // end of Compile::NextItem
  484. void Compile::UngetItem()
  485. {//======================
  486. fseek(f_in,f_in_displ,SEEK_SET);
  487. linenum = f_in_linenum;
  488. } // end of Compile::UngetItem
  489. int Compile::LoadSpect(const char *path, int control)
  490. {//=================================================
  491. SpectSeq *spectseq;
  492. int peak;
  493. int displ;
  494. int frame;
  495. int ix;
  496. int x;
  497. int rms;
  498. float total;
  499. float pkheight;
  500. SpectFrame *fr;
  501. wxString path_sep = _T("/");
  502. SPECT_SEQ seq_out;
  503. // create SpectSeq and import data
  504. spectseq = new SpectSeq;
  505. if(spectseq == NULL)
  506. {
  507. Error("Failed to create SpectSeq");
  508. return(0);
  509. }
  510. wxString filename = path_phsource + path_sep + wxString(path,wxConvLocal);
  511. wxFileInputStream stream(filename);
  512. if(stream.Ok() == FALSE)
  513. {
  514. Error("Failed to open",path);
  515. return(0);
  516. }
  517. spectseq->Load(stream);
  518. displ = ftell(f_phdata);
  519. seq_out.n_frames=0;
  520. seq_out.flags=0;
  521. seq_out.length=0;
  522. total = 0;
  523. for(frame=0; frame < spectseq->numframes; frame++)
  524. {
  525. #ifdef deleted
  526. for(ix=0; ix<8; ix++)
  527. {
  528. // find which files have certain markers set
  529. if(spectseq->frames[frame]->markers & (1<<ix))
  530. {
  531. markers_used[ix]++;
  532. if((ix==3) || (ix==4))
  533. {
  534. fprintf(f_errors,"Marker %d: %s\n",ix,path);
  535. }
  536. }
  537. }
  538. #endif
  539. if(spectseq->frames[frame]->keyframe)
  540. {
  541. seq_out.n_frames++;
  542. if(frame > 0)
  543. total += spectseq->frames[frame-1]->length;
  544. }
  545. }
  546. seq_out.length = int(total);
  547. if((control & 1) && (spectseq->numframes > 2))
  548. {
  549. // set a marker flag for the second frame of a vowel
  550. spectseq->frames[1]->markers |= FRFLAG_VOWEL_CENTRE;
  551. }
  552. ix = 0;
  553. for(frame=0; frame < spectseq->numframes; frame++)
  554. {
  555. fr = spectseq->frames[frame];
  556. if(fr->keyframe)
  557. {
  558. x = int(fr->length + 0.5); // round to nearest mS
  559. if(x > 255) x = 255;
  560. seq_out.frame[ix].length = x;
  561. seq_out.frame[ix].frflags = fr->markers;
  562. rms = int(fr->GetRms(spectseq->amplitude));
  563. if(rms > 255) rms = 255;
  564. seq_out.frame[ix].rms = rms;
  565. if(ix == (seq_out.n_frames-1))
  566. seq_out.frame[ix].length = 0; // give last frame zero length
  567. // write: peak data
  568. count_frames++;
  569. for(peak=0; peak<N_PEAKS; peak++)
  570. {
  571. seq_out.frame[ix].ffreq[peak] = fr->peaks[peak].pkfreq;
  572. pkheight = spectseq->amplitude * fr->amp_adjust * fr->peaks[peak].pkheight;
  573. pkheight = pkheight/640000;
  574. if(pkheight > 255) pkheight = 255;
  575. seq_out.frame[ix].fheight[peak] = int(pkheight);
  576. if(peak < 6)
  577. {
  578. x = fr->peaks[peak].pkwidth/4;
  579. if(x > 255) x = 255;
  580. seq_out.frame[ix].fwidth[peak] = x;
  581. x = fr->peaks[peak].pkright/4;
  582. if(x > 255) x = 255;
  583. seq_out.frame[ix].fright[peak] = x;
  584. }
  585. }
  586. #ifdef LOG_DETAIL
  587. fprintf(f_errors,"Frame %d %3dmS rms=%3d flags=%2d pk=%4d %4d %4d",ix,seq_out.frame[ix].length,
  588. seq_out.frame[ix].rms,seq_out.frame[ix].flags,
  589. seq_out.frame[ix].peaks[1].pkfreq,seq_out.frame[ix].peaks[2].pkfreq,seq_out.frame[ix].peaks[3].pkfreq);
  590. if(fr->markers != 0)
  591. {
  592. fprintf(f_errors," [%x]",fr->markers);
  593. }
  594. fputc('\n',f_errors);
  595. #endif
  596. ix++;
  597. }
  598. }
  599. ix = (char *)(&seq_out.frame[seq_out.n_frames]) - (char *)(&seq_out);
  600. ix = (ix+3) & 0xfffc; // round up to multiple of 4 bytes
  601. fwrite(&seq_out,ix,1,f_phdata);
  602. delete spectseq;
  603. return(displ);
  604. } // end of Compile::LoadSpect
  605. int Compile::LoadWavefile(FILE *f, const char *fname)
  606. {//==================================================
  607. int displ;
  608. unsigned char c1;
  609. unsigned char c3;
  610. int c2;
  611. int sample;
  612. int sample2;
  613. float x;
  614. int max = 0;
  615. int length;
  616. int sr1, sr2;
  617. int resample_wav = 0;
  618. char fname_temp[100];
  619. int scale_factor=0;
  620. char command[sizeof(path_source)+200];
  621. fseek(f,24,SEEK_SET);
  622. sr1 = Read4Bytes(f);
  623. sr2 = Read4Bytes(f);
  624. fseek(f,40,SEEK_SET);
  625. if((sr1 != samplerate) || (sr2 != sr1*2))
  626. {
  627. #ifdef PLATFORM_WINDOWS
  628. if(sr1 != samplerate)
  629. {
  630. fprintf(f_errors,"Wrong samplerate %d, wants %d\n",sr1,samplerate);
  631. Error("Wrong samplerate: ",fname);
  632. }
  633. if(sr2 != sr1*2)
  634. {
  635. Error("Not mono: ",fname);
  636. }
  637. #else
  638. sprintf(fname_temp,"%s.wav",tmpnam(NULL));
  639. sprintf(command,"sox \"%s%s.wav\" -r %d -c 1 -w %s polyphase\n",path_source,fname,samplerate,fname_temp);
  640. if(system(command) < 0)
  641. {
  642. Error("Failed to resample: ",command);
  643. return(0);
  644. }
  645. f = fopen(fname_temp,"rb");
  646. if(f == NULL)
  647. {
  648. Error("Can't read temp file: ",fname_temp);
  649. return(0);
  650. }
  651. resample_wav = 1;
  652. fseek(f,40,SEEK_SET); // skip past the WAV header, up to before "data length"
  653. #endif
  654. }
  655. displ = ftell(f_phdata);
  656. // data contains: 4 bytes of length (n_samples * 2), followed by 2-byte samples (lsb byte first)
  657. length = Read4Bytes(f);
  658. while(!feof(f))
  659. {
  660. c1 = fgetc(f);
  661. c3 = fgetc(f);
  662. if(feof(f)) break;
  663. c2 = c3 << 24;
  664. c2 = c2 >> 16; // sign extend
  665. sample = (c1 & 0xff) + c2;;
  666. if(sample > max)
  667. max = sample;
  668. else
  669. if(sample < -max)
  670. max = -sample;
  671. }
  672. if(max > 1)
  673. {
  674. scale_factor = (max / 127) + 1;
  675. }
  676. else
  677. scale_factor = 0;
  678. //fprintf(f_errors," sample len=%d max=%4x shift=%d\n",length,max,scale_factor);
  679. #define MIN_FACTOR 6
  680. if(scale_factor > MIN_FACTOR)
  681. {
  682. length = length/2 + (scale_factor << 16);
  683. }
  684. Write4Bytes(f_phdata,length);
  685. // fwrite(&length,4,1,f_phdata);
  686. fseek(f,44,SEEK_SET);
  687. while(!feof(f))
  688. {
  689. c1 = fgetc(f);
  690. c3 = fgetc(f);
  691. c2 = c3 << 24;
  692. c2 = c2 >> 16; // sign extend
  693. sample = (c1 & 0xff) + c2;
  694. if(feof(f)) break;
  695. if(scale_factor <= MIN_FACTOR)
  696. {
  697. fputc(sample & 0xff,f_phdata);
  698. fputc(sample >> 8,f_phdata);
  699. }
  700. else
  701. {
  702. x = (float(sample) / scale_factor) + 0.5;
  703. sample2= int(x);
  704. if(sample2 > 127)
  705. sample2 = 127;
  706. if(sample2 < -128)
  707. sample2 = -128;
  708. fputc(sample2,f_phdata);
  709. }
  710. }
  711. length = ftell(f_phdata);
  712. while((length & 3) != 0)
  713. {
  714. // pad to a multiple of 4 bytes
  715. fputc(0,f_phdata);
  716. length++;
  717. }
  718. if(resample_wav != 0)
  719. {
  720. fclose(f);
  721. remove(fname_temp);
  722. }
  723. return(displ | 0x800000); // set bit 23 to indicate a wave file rather than a spectrum
  724. } // end of Compile::LoadWavefile
  725. int Compile::LoadEnvelope(FILE *f, const char *fname)
  726. {//==================================================
  727. int displ;
  728. char buf[128];
  729. displ = ftell(f_phdata);
  730. fseek(f,12,SEEK_SET);
  731. fread(buf,1,128,f);
  732. fwrite(buf,1,128,f_phdata);
  733. if(n_envelopes < N_ENVELOPES)
  734. {
  735. strncpy0(envelope_paths[n_envelopes],fname,sizeof(envelope_paths[0]));
  736. memcpy(envelope_dat[n_envelopes],buf,sizeof(envelope_dat[0]));
  737. n_envelopes++;
  738. }
  739. return(displ);
  740. }
  741. int Compile::LoadDataFile(const char *path, int control)
  742. {//====================================================
  743. // load spectrum sequence or sample data from a file.
  744. // return index into spect or sample data area. bit 23=1 if a sample
  745. FILE *f;
  746. int id;
  747. int hash;
  748. int addr = 0;
  749. int type_code=' ';
  750. REF_HASH_TAB *p, *p2;
  751. char buf[sizeof(path_source)+120];
  752. if(strcmp(path,"NULL")==0)
  753. return(0);
  754. if(strcmp(path,"DFT")==0)
  755. return(1);
  756. count_references++;
  757. hash = Hash8(path);
  758. p = ref_hash_tab[hash];
  759. while(p != NULL)
  760. {
  761. if(strcmp(path,p->string)==0)
  762. {
  763. duplicate_references++;
  764. addr = p->value; // already loaded this data
  765. break;
  766. }
  767. p = (REF_HASH_TAB *)p->link;
  768. }
  769. if(addr == 0)
  770. {
  771. sprintf(buf,"%s%s",path_source,path);
  772. if((f = fopen(buf,"rb")) == NULL)
  773. {
  774. sprintf(buf,"%s%s.wav",path_source,path);
  775. if((f = fopen(buf,"rb")) == NULL)
  776. {
  777. Error("Can't read file",path);
  778. return(0);
  779. }
  780. }
  781. id = Read4Bytes(f);
  782. rewind(f);
  783. if(id == 0x43455053)
  784. {
  785. addr = LoadSpect(path, control);
  786. type_code = 'S';
  787. }
  788. else
  789. if(id == 0x46464952)
  790. {
  791. addr = LoadWavefile(f,path);
  792. type_code = 'W';
  793. }
  794. else
  795. if(id == 0x43544950)
  796. {
  797. addr = LoadEnvelope(f,path);
  798. type_code = 'E';
  799. }
  800. else
  801. if(id == 0x45564E45)
  802. {
  803. addr = LoadEnvelope2(f,path);
  804. type_code = 'E';
  805. }
  806. else
  807. {
  808. Error("File not SPEC or RIFF",path);
  809. addr = -1;
  810. }
  811. fclose(f);
  812. if(addr > 0)
  813. {
  814. fprintf(f_phcontents,"%c 0x%.5x %s\n",type_code,addr & 0x7fffff,path);
  815. }
  816. }
  817. // add this item to the hash table
  818. if(addr > 0)
  819. {
  820. p = ref_hash_tab[hash];
  821. p2 = (REF_HASH_TAB *)malloc(sizeof(REF_HASH_TAB)+strlen(path)+1);
  822. p2->value = addr;
  823. p2->ph_mnemonic = ph->mnemonic; // phoneme which uses this file
  824. p2->ph_table = n_phoneme_tabs-1;
  825. strcpy(p2->string,path);
  826. p2->link = (char *)p;
  827. ref_hash_tab[hash] = p2;
  828. }
  829. return(addr);
  830. } // end of Compile::LoadDataFile
  831. void Compile::AddEnvelope(int *list)
  832. {//================================
  833. NextItem(tSTRING);
  834. *list++ = LoadDataFile(item_string,0);
  835. list[0] = 0;
  836. }
  837. int Compile::AddSpect(int phcode, int *list, int control)
  838. {//=====================================================
  839. int sign;
  840. int sign2;
  841. int all_digits;
  842. char *p;
  843. char *p2;
  844. int spect;
  845. int value;
  846. int count = 0;
  847. int v_in = vowel_in[0];
  848. int v_out = vowel_out[0];
  849. sign = 0;
  850. all_digits=0;
  851. p = p2 = item_string;
  852. for(;;)
  853. {
  854. if((*p == 0) || (*p == '+') || (((*p == '-') || (*p == '%')) && isdigit(p[1])))
  855. {
  856. sign2 = *p;
  857. *p = 0;
  858. if(all_digits)
  859. {
  860. value = atoi(p2);
  861. if(sign == '+')
  862. *list++ = 1 + (value<<8);
  863. else
  864. if(sign == '-')
  865. *list++ = 2 + (value<<8);
  866. else
  867. {
  868. value = (value * 32)/100; // convert from % to 1/32s
  869. *list++ = 3 + (value<<8);
  870. }
  871. count++;
  872. }
  873. else
  874. {
  875. spect = LoadDataFile(p2, control);
  876. if(spect != -1)
  877. {
  878. *list++ = phcode + (spect<<8);
  879. count++;
  880. // vowel formant transitions specified ?
  881. if(v_in != 0)
  882. {
  883. *list++ = 0x4 + (vowel_in[0] << 8);
  884. *list++ = vowel_in[1];
  885. v_in = 0;
  886. count+=2;
  887. }
  888. if(v_out != 0)
  889. {
  890. *list++ = 0x5 + (vowel_out[0] << 8);
  891. *list++ = vowel_out[1];
  892. v_out = 0;
  893. count+=2;
  894. }
  895. }
  896. phcode = 0;
  897. }
  898. if((sign = sign2)==0) break;
  899. p2 = p+1;
  900. all_digits=1;
  901. }
  902. else
  903. {
  904. if(!isdigit(*p))
  905. all_digits=0;
  906. }
  907. p++;
  908. }
  909. *list = 0;
  910. return(count);
  911. } // end of AddSpect
  912. void Compile::AddSpectList(int *list, int control)
  913. {//==============================================
  914. int phcode;
  915. int key;
  916. while(*list != 0) list++; // find end of the list
  917. for(;;)
  918. {
  919. key = NextItem(tKEYWORD);
  920. UngetItem();
  921. if(key != -1)
  922. break;
  923. phcode = NextItem(tPHONEMEMNEM);
  924. if(phcode == -1)
  925. Error("Undeclared phoneme",item_string);
  926. if(NextItem(tSTRING) == -1)
  927. break;
  928. list += AddSpect(phcode, list, control);
  929. }
  930. *list++ = 0;
  931. } // end of Compile::AddSpectList
  932. static int Range(int value, int divide, int min, int max)
  933. {//======================================================
  934. if(value < 0)
  935. value -= divide/2;
  936. else
  937. value += divide/2;
  938. value = value / divide;
  939. if(value > max)
  940. value = max;
  941. if(value < min)
  942. value = min;
  943. return(value - min);
  944. }
  945. void Compile::VowelTransition(int which, unsigned int *trans)
  946. {//==========================================================
  947. // Compile a vowel transition
  948. int key;
  949. int len=0;
  950. int rms=0;
  951. int f1=0;
  952. int f2=0;
  953. int f2_min=0;
  954. int f2_max=0;
  955. int f3_adj=0;
  956. int f3_amp=0;
  957. int flags=0;
  958. int vcolour=0;
  959. int x;
  960. if(which==1)
  961. {
  962. len = 50 / 2; // defaults for transition into vowel
  963. rms = 25 / 2;
  964. }
  965. else
  966. if(which==2)
  967. {
  968. len = 36 / 2; // defaults for transition out of vowel
  969. rms = 16 / 2;
  970. }
  971. for(;;)
  972. {
  973. key = NextItem(tKEYWORD);
  974. if(item_type != 3)
  975. {
  976. UngetItem();
  977. break;
  978. }
  979. switch(key & 0xf)
  980. {
  981. case 1:
  982. len = Range(NextItem(tNUMBER), 2, 0, 63) & 0x3f;
  983. flags |= 1;
  984. break;
  985. case 2:
  986. rms = Range(NextItem(tNUMBER), 2, 0, 31) & 0x1f;
  987. flags |= 1;
  988. break;
  989. case 3:
  990. f1 = NextItem(tNUMBER);
  991. break;
  992. case 4:
  993. f2 = Range(NextItem(tNUMBER), 50, 0, 63) & 0x3f;
  994. f2_min = Range(NextItem(tSIGNEDNUMBER), 50, -15, 15) & 0x1f;
  995. f2_max = Range(NextItem(tSIGNEDNUMBER), 50, -15, 15) & 0x1f;
  996. if(f2_min > f2_max)
  997. {
  998. x = f2_min;
  999. f2_min = f2_max;
  1000. f2_max = x;
  1001. }
  1002. break;
  1003. case 5:
  1004. f3_adj = Range(NextItem(tSIGNEDNUMBER), 50, -15, 15) & 0x1f;
  1005. f3_amp = Range(NextItem(tNUMBER), 8, 0, 15) & 0x1f;
  1006. break;
  1007. case 6:
  1008. flags |= 2; // break
  1009. break;
  1010. case 7:
  1011. flags |= 4; // rate
  1012. break;
  1013. case 8:
  1014. flags |= 8; // glstop
  1015. break;
  1016. case 9:
  1017. flags |= 16; // lenadd
  1018. break;
  1019. case 10:
  1020. flags |= 32; // f4
  1021. break;
  1022. case 11:
  1023. flags |= 64; // paus
  1024. break;
  1025. case 12:
  1026. vcolour = NextItem(tNUMBER);
  1027. break;
  1028. case 13:
  1029. rms = (Range(NextItem(tNUMBER), 1, 0, 31) & 0x1f) | 0x20;
  1030. flags |= 1;
  1031. break;
  1032. }
  1033. }
  1034. trans[0] = len + (rms << 6) + (flags << 12) + 0x80000000;
  1035. trans[1] = f2 + (f2_min << 6) + (f2_max << 11) + (f3_adj << 16) + (f3_amp << 21) + (f1 << 26) + (vcolour << 29);
  1036. } // end of VowelTransition
  1037. void Compile::ImportPhoneme(void)
  1038. {//==============================
  1039. int ix;
  1040. unsigned int mnem;
  1041. unsigned int ph_mnem;
  1042. unsigned int ph_code;
  1043. PHONEME_TAB_LIST *phtab = NULL;
  1044. NextItem(tSTRING);
  1045. mnem = StringToWord(item_string);
  1046. NextItem(tSTRING);
  1047. for(ix=0; ix<n_phoneme_tabs; ix++)
  1048. {
  1049. if(strcmp(phoneme_tab_list2[ix].name,item_string) == 0)
  1050. {
  1051. phtab = &phoneme_tab_list2[ix];
  1052. break;
  1053. }
  1054. }
  1055. if(phtab == NULL)
  1056. {
  1057. Error("Unknown phoneme table",item_string);
  1058. return; // phoneme table not found
  1059. }
  1060. for(ix=1; ix<256; ix++)
  1061. {
  1062. if(mnem == phtab->phoneme_tab_ptr[ix].mnemonic)
  1063. {
  1064. ph_mnem = ph->mnemonic;
  1065. ph_code = ph->code;
  1066. memcpy(ph,&phtab->phoneme_tab_ptr[ix],sizeof(PHONEME_TAB));
  1067. ph->mnemonic = ph_mnem;
  1068. ph->code = ph_code;
  1069. break;
  1070. }
  1071. }
  1072. if(ix == 256)
  1073. {
  1074. Error("Import phoneme not found",WordToString(mnem));
  1075. }
  1076. }
  1077. int Compile::CPhoneme()
  1078. {//====================
  1079. int phcode;
  1080. int phindex;
  1081. int value;
  1082. int item;
  1083. int finish=0;
  1084. int *intp;
  1085. int before_tab[N_PHONEME_TAB];
  1086. int after_tab[N_PHONEME_TAB];
  1087. int default_spect[N_PHONEME_TAB];
  1088. NextItem(tSTRING);
  1089. phindex = LookupPhoneme(item_string,1); // declare phoneme if not already there
  1090. if(phindex == -1) return(0);
  1091. ph = &phoneme_tab2[phindex];
  1092. memset(ph,0,sizeof(PHONEME_TAB));
  1093. ph->mnemonic = StringToWord(item_string);
  1094. ph->type = 0xff; // undefined
  1095. ph->code = phindex;
  1096. before_tab[0] = 0;
  1097. after_tab[0] = 0;
  1098. default_spect[0] = 0;
  1099. vowel_in[0] = 0;
  1100. vowel_in[1] = 0;
  1101. vowel_out[0] = 0;
  1102. vowel_out[1] = 0;
  1103. while(!finish)
  1104. {
  1105. item = NextItem(tKEYWORD);
  1106. if(item_type==2)
  1107. ph->phflags |= item & 0xffffff;
  1108. else
  1109. if(item_type==1)
  1110. {
  1111. if(ph->type != 0xff)
  1112. {
  1113. Error("Phoneme type already set",item_string);
  1114. }
  1115. ph->type = item & 0xff;
  1116. }
  1117. else
  1118. switch(item)
  1119. {
  1120. case tLENGTHMOD:
  1121. ph->length_mod = NextItem(tNUMBER);
  1122. break;
  1123. case tLENGTH:
  1124. ph->std_length = NextItem(tNUMBER);
  1125. break;
  1126. case tWAVE:
  1127. ph->phflags |= phWAVE; // drop through to tSPECT
  1128. case tSPECT:
  1129. if(NextItem(tSTRING) == -1)
  1130. Error("Syntax error",item_string);
  1131. else
  1132. {
  1133. if(ph->type == phVOWEL)
  1134. AddSpect(phonPAUSE,default_spect,1);
  1135. else
  1136. AddSpect(phonPAUSE,default_spect,0);
  1137. }
  1138. break;
  1139. case tSTARTTYPE:
  1140. phcode = NextItem(tPHONEMEMNEM);
  1141. if(phcode == -1)
  1142. phcode = LookupPhoneme(item_string,1);
  1143. ph->start_type = phcode;
  1144. break;
  1145. case tENDTYPE:
  1146. phcode = NextItem(tPHONEMEMNEM);
  1147. if(phcode == -1)
  1148. phcode = LookupPhoneme(item_string,1);
  1149. ph->end_type = phcode;
  1150. break;
  1151. case tTONESPEC:
  1152. ph->start_type = NextItem(tNUMBER); // tone's min pitch (range 0-50)
  1153. ph->end_type = NextItem(tNUMBER); // tone's max pitch (range 0-50)
  1154. if(ph->start_type > ph->end_type)
  1155. {
  1156. // ensure pitch1 < pitch2
  1157. value = ph->start_type;
  1158. ph->start_type = ph->end_type;
  1159. ph->end_type = value;
  1160. }
  1161. AddEnvelope(default_spect); // envelope for pitch change, rise or fall or fall/rise, etc
  1162. AddEnvelope(after_tab); // envelope for amplitude change
  1163. break;
  1164. case tREDUCETO:
  1165. phcode = NextItem(tPHONEMEMNEM);
  1166. if(phcode == -1)
  1167. phcode = LookupPhoneme(item_string,1);
  1168. ph->reduce_to = phcode;
  1169. value = NextItem(tNUMBER);
  1170. ph->phflags = (ph->phflags & 0xfffffff) + (value << 28);
  1171. break;
  1172. case tBEFORENOTVOWEL:
  1173. ph->phflags |= phBEFORENOTVOWEL; // and drop through to tBEFOREVOWEL
  1174. case tBEFOREVOWELPAUSE:
  1175. ph->phflags |= phBEFOREVOWELPAUSE;
  1176. case tBEFOREVOWEL:
  1177. if((phcode = NextItem(tPHONEMEMNEM)) == -1)
  1178. phcode = LookupPhoneme(item_string,1);
  1179. ph->alternative_ph = phcode;
  1180. break;
  1181. case tSWITCHVOICING:
  1182. ph->phflags |= phSWITCHVOICING;
  1183. if((phcode = NextItem(tPHONEMEMNEM)) == -1)
  1184. phcode = LookupPhoneme(item_string,1);
  1185. ph->alternative_ph = phcode;
  1186. break;
  1187. case tAPPENDPH:
  1188. ph->phflags |= phAPPENDPH; // drop through to tLINKOUT
  1189. case tLINKOUT:
  1190. phcode = NextItem(tPHONEMEMNEM);
  1191. if(phcode == -1)
  1192. phcode = LookupPhoneme(item_string,1);
  1193. ph->link_out = phcode;
  1194. break;
  1195. case tBEFORE:
  1196. AddSpectList(before_tab,0);
  1197. break;
  1198. case tAFTER:
  1199. AddSpectList(after_tab,0);
  1200. break;
  1201. case tEND:
  1202. finish = 1;
  1203. break;
  1204. case tVOWELIN:
  1205. VowelTransition(1,vowel_in);
  1206. break;
  1207. case tVOWELOUT:
  1208. VowelTransition(2,vowel_out);
  1209. break;
  1210. case tIMPORTPH:
  1211. ImportPhoneme();
  1212. break;
  1213. default:
  1214. Error("Syntax error",item_string);
  1215. break;
  1216. }
  1217. }
  1218. // write out indices
  1219. if(before_tab[0] != 0)
  1220. {
  1221. ph->before = ftell(f_phindex) / 4;
  1222. intp = before_tab;
  1223. for(;;)
  1224. {
  1225. fwrite(intp,4,1,f_phindex);
  1226. if(*intp++ == 0) break;
  1227. }
  1228. }
  1229. if(after_tab[0] != 0)
  1230. {
  1231. ph->after = ftell(f_phindex) / 4;
  1232. intp = after_tab;
  1233. for(;;)
  1234. {
  1235. fwrite(intp,4,1,f_phindex);
  1236. if(*intp++ == 0) break;
  1237. }
  1238. }
  1239. if(default_spect[0] != 0)
  1240. {
  1241. ph->spect = ftell(f_phindex) / 4;
  1242. intp = default_spect;
  1243. for(;;)
  1244. {
  1245. fwrite(intp,4,1,f_phindex);
  1246. if(*intp++ == 0) break;
  1247. }
  1248. }
  1249. if(ph->phflags & phVOICED)
  1250. {
  1251. if(ph->type == phSTOP)
  1252. ph->type = phVSTOP;
  1253. else
  1254. if(ph->type == phFRICATIVE)
  1255. ph->type = phVFRICATIVE;
  1256. }
  1257. ph->std_length |= 0x8000; // 'locally declared' indicator
  1258. if(ph->type == phDELETED)
  1259. {
  1260. ph->mnemonic = 0x01; // will not be recognised
  1261. }
  1262. return(phindex);
  1263. } // end of Compile::CPhoneme
  1264. void Compile::WritePhonemeTable()
  1265. {//==============================
  1266. int ix;
  1267. int j;
  1268. int n;
  1269. int value;
  1270. int count;
  1271. PHONEME_TAB *p;
  1272. value = n_phoneme_tabs;
  1273. fputc(value,f_phtab);
  1274. fputc(0,f_phtab);
  1275. fputc(0,f_phtab);
  1276. fputc(0,f_phtab);
  1277. for(ix=0; ix<n_phoneme_tabs; ix++)
  1278. {
  1279. p = phoneme_tab_list2[ix].phoneme_tab_ptr;
  1280. n = n_phcodes_list[ix];
  1281. p[n].mnemonic = 0; // terminate the phoneme table
  1282. // count number of locally declared phonemes
  1283. count=0;
  1284. for(j=0; j<n; j++)
  1285. {
  1286. if(ix==0)
  1287. p[j].std_length |= 0x8000; // write all phonemes in the base phoneme table
  1288. if(p[j].std_length & 0x8000)
  1289. count++;
  1290. }
  1291. phoneme_tab_list2[ix].n_phonemes = count+1;
  1292. fputc(count+1,f_phtab);
  1293. fputc(phoneme_tab_list2[ix].includes,f_phtab);
  1294. fputc(0,f_phtab);
  1295. fputc(0,f_phtab);
  1296. fwrite(phoneme_tab_list2[ix].name,1,N_PHONEME_TAB_NAME,f_phtab);
  1297. for(j=0; j<n; j++)
  1298. {
  1299. if(p[j].std_length & 0x8000)
  1300. {
  1301. // this bit is set temporarily to incidate a local phoneme, declared in
  1302. // in the current phoneme file
  1303. p[j].std_length &= 0x7fff;
  1304. fwrite(&p[j],sizeof(PHONEME_TAB),1,f_phtab);
  1305. }
  1306. }
  1307. fwrite(&p[n],sizeof(PHONEME_TAB),1,f_phtab); // include the extra list-terminator phoneme entry
  1308. free(p);
  1309. }
  1310. }
  1311. void Compile::EndPhonemeTable()
  1312. {//============================
  1313. int ix;
  1314. if(n_phoneme_tabs == 0)
  1315. return;
  1316. // check that all referenced phonemes have been declared
  1317. for(ix=0; ix<n_phcodes; ix++)
  1318. {
  1319. if(phoneme_tab2[ix].type == phINVALID)
  1320. {
  1321. fprintf(f_errors,"%3d: Phoneme [%s] not declared, referenced at line %d\n",linenum,
  1322. WordToString(phoneme_tab2[ix].mnemonic),int(phoneme_tab2[ix].spect));
  1323. error_count++;
  1324. }
  1325. }
  1326. n_phcodes_list[n_phoneme_tabs-1] = n_phcodes;
  1327. }
  1328. void Compile::StartPhonemeTable(const char *name)
  1329. {//==============================================
  1330. int ix;
  1331. int j;
  1332. PHONEME_TAB *p;
  1333. fprintf(f_errors,"______________________________\nPhoneme Table: '%s'\n",name);
  1334. if(n_phoneme_tabs >= N_PHONEME_TABS-1)
  1335. {
  1336. Error("Too many phonemetables");
  1337. return;
  1338. }
  1339. p = (PHONEME_TAB *)calloc(sizeof(PHONEME_TAB),N_PHONEME_TAB);
  1340. if(p == NULL)
  1341. {
  1342. Error("Out of memory");
  1343. return;
  1344. }
  1345. if(gui_flag)
  1346. progress->Update(n_phoneme_tabs);
  1347. phoneme_tab_list2[n_phoneme_tabs].phoneme_tab_ptr = phoneme_tab2 = p;
  1348. memset(phoneme_tab_list2[n_phoneme_tabs].name, 0, N_PHONEME_TAB_NAME);
  1349. strncpy0(phoneme_tab_list2[n_phoneme_tabs].name, name, N_PHONEME_TAB_NAME);
  1350. n_phcodes = 1;
  1351. phoneme_tab_list2[n_phoneme_tabs].includes = 0;
  1352. if(n_phoneme_tabs > 0)
  1353. {
  1354. NextItem(tSTRING); // name of base phoneme table
  1355. for(ix=0; ix<n_phoneme_tabs; ix++)
  1356. {
  1357. if(strcmp(item_string,phoneme_tab_list2[ix].name)==0)
  1358. {
  1359. phoneme_tab_list2[n_phoneme_tabs].includes = ix+1;
  1360. // initialise the new phoneme table with the contents of this one
  1361. memcpy(phoneme_tab2,phoneme_tab_list2[ix].phoneme_tab_ptr,sizeof(PHONEME_TAB)*N_PHONEME_TAB);
  1362. n_phcodes = n_phcodes_list[ix];
  1363. // clear "local phoneme" bit"
  1364. for(j=0; j<n_phcodes; j++)
  1365. phoneme_tab2[j].std_length = phoneme_tab2[j].std_length & 0x7fff;
  1366. break;
  1367. }
  1368. }
  1369. if(ix == n_phoneme_tabs)
  1370. {
  1371. Error("Can't find base phonemetable '%s'",item_string);
  1372. }
  1373. }
  1374. n_phoneme_tabs++;
  1375. } // end of StartPhonemeTable
  1376. void Compile::CPhonemeFiles()
  1377. {//==========================
  1378. int phcode;
  1379. int item;
  1380. FILE *f;
  1381. char buf[sizeof(path_source)+120];
  1382. linenum = 1;
  1383. n_phcodes = 1; // don't use phoneme code=0, it's used as string terminator
  1384. count_references = 0;
  1385. duplicate_references = 0;
  1386. count_frames = 0;
  1387. for(;;)
  1388. {
  1389. if(feof(f_in))
  1390. {
  1391. // end of file, go back to previous from, from which this was included
  1392. if(stack_ix == 0)
  1393. break; // end of top level, finished
  1394. fclose(f_in);
  1395. f_in = stack[--stack_ix].file;
  1396. strcpy(current_fname,stack[stack_ix].fname);
  1397. linenum = stack[stack_ix].linenum;
  1398. fprintf(f_errors,"\n\n");
  1399. }
  1400. item = NextItem(tKEYWORD);
  1401. if(item == tINCLUDE)
  1402. {
  1403. NextItem(tSTRING);
  1404. sprintf(buf,"%s%s",path_source,item_string);
  1405. if((stack_ix < N_STACK) && (f = fopen_log(f_errors,buf,"rb")) != NULL)
  1406. {
  1407. fprintf(f_errors,"include %s\n",item_string);
  1408. stack[stack_ix].linenum = linenum;
  1409. strcpy(stack[stack_ix].fname,current_fname);
  1410. stack[stack_ix++].file = f_in;
  1411. f_in = f;
  1412. strncpy0(current_fname,item_string,sizeof(current_fname));
  1413. linenum = 1;
  1414. }
  1415. }
  1416. else
  1417. if(item == tPHONEMETABLE)
  1418. {
  1419. EndPhonemeTable();
  1420. NextItem(tSTRING); // name of the new phoneme table
  1421. StartPhonemeTable(item_string);
  1422. }
  1423. else
  1424. if(item == tPHONEMENUMBER)
  1425. {
  1426. // reserve a specified number for this phoneme
  1427. phcode = NextItem(tNUMBER);
  1428. NextItem(tSTRING);
  1429. ReservePhCode(item_string,phcode);
  1430. }
  1431. else
  1432. if(item == tPHONEMESTART)
  1433. {
  1434. if(n_phoneme_tabs == 0)
  1435. {
  1436. Error("phonemetable is missing");
  1437. return;
  1438. }
  1439. phcode = CPhoneme();
  1440. }
  1441. else
  1442. if(!feof(f_in))
  1443. Error("Keyword 'phoneme' expected");
  1444. }
  1445. phoneme_tab2[n_phcodes+1].mnemonic = 0; // terminator
  1446. } // end of CPhonemeFiles
  1447. //#define MAKE_ENVELOPES
  1448. #ifdef MAKE_ENVELOPES
  1449. #define ENV_LEN 128
  1450. void MakeEnvelope(unsigned char *env, float *points_x, float *points_y)
  1451. {//====================================================================
  1452. int ix = -1;
  1453. int x,y;
  1454. for(x=0; x<ENV_LEN; x++)
  1455. {
  1456. if(x > points_x[ix+4])
  1457. ix++;
  1458. y = (int)polint(&points_x[ix],&points_y[ix],4,x);
  1459. if(y < 0) y = 0;
  1460. if(y > 255) y = 255;
  1461. env[x] = y;
  1462. }
  1463. }
  1464. static float env1_x[]={0,0x30,0x40,ENV_LEN};
  1465. static float env1_y[]={0,200,255,255};
  1466. static float env2_x[]={0,0x28,0x48,0x60,ENV_LEN};
  1467. static float env2_y[]={255,0xe0,0xc0,0x60,0};
  1468. static float env3_x[]={0,0x18,0x30,ENV_LEN};
  1469. static float env3_y[]={0,0x28,0x50,255};
  1470. static float env4_x[]={0,0x20,0x40,0x60,ENV_LEN};
  1471. static float env4_y[]={255,0x70,0,0x70,255};
  1472. static float env5_x[]={0,0x20,0x40,0x58,0x70,ENV_LEN};
  1473. static float env5_y[]={0,0x28,0x50,0xa0,0xf0,255};
  1474. static float env6_x[]={0,0x20,0x38,0x60,ENV_LEN};
  1475. static float env6_y[]={255,0xe8,0xd0,0x68,0};
  1476. static float env7_x[]={0,0x20,0x40,0x60,ENV_LEN}; // 214
  1477. static float env7_y[]={85,42,0,128,255};
  1478. static float env8_x[]={0,0x20,0x40,0x60,ENV_LEN}; // 211
  1479. static float env8_y[]={255,130,20,10,0};
  1480. static float env9_x[]={0,0x20,0x40,0x60,ENV_LEN}; // 51 fall
  1481. static float env9_y[]={255,210,140,70,0};
  1482. static float enva3_x[]={0,44,64,84,ENV_LEN}; // amp env for broken tone
  1483. static float enva3_y[]={255,255,160,255,255};
  1484. static float enva6_x[]={0,44,64,80,ENV_LEN}; // amp env for drop tone
  1485. static float enva6_y[]={255,255,255,250,50};
  1486. unsigned char env_test[ENV_LEN];
  1487. void MakeEnvFile(char *fname, float *x, float *y, int source)
  1488. {//==========================================================
  1489. static char hdr[12] = {'P','I','T','C','H','E','N','V',80,0,120,0};
  1490. FILE *f;
  1491. int ix;
  1492. MakeEnvelope(env_test,x,y);
  1493. f = fopen(fname,"wb");
  1494. if(source)
  1495. {
  1496. for(ix=0; ix<128; ix++)
  1497. {
  1498. fprintf(f,"0x%.2x,",env_test[ix]);
  1499. if((ix & 7) == 7)
  1500. fprintf(f,"\n");
  1501. }
  1502. }
  1503. else
  1504. {
  1505. fwrite(hdr,12,1,f);
  1506. fwrite(env_test,128,1,f);
  1507. }
  1508. fclose(f);
  1509. }
  1510. void make_envs()
  1511. {//=============
  1512. MakeEnvFile("p_level",env1_x,env1_y,0);
  1513. MakeEnvFile("p_rise",env5_x,env5_y,0);
  1514. MakeEnvFile("p_fall",env9_x,env9_y,0);
  1515. MakeEnvFile("p_214",env7_x,env7_y,0);
  1516. MakeEnvFile("p_211",env8_x,env8_y,0);
  1517. MakeEnvFile("vi_2",env2_x,env2_y,0);
  1518. MakeEnvFile("vi_5",env3_x,env3_y,0);
  1519. MakeEnvFile("p_fallrise",env4_x,env4_y,0);
  1520. MakeEnvFile("vi_6",env6_x,env6_y,0);
  1521. MakeEnvFile("vi_5amp",enva3_x,enva3_y,0);
  1522. MakeEnvFile("vi_6amp",enva6_x,enva6_y,0);
  1523. }
  1524. #endif
  1525. int Compile::LoadEnvelope2(FILE *f, const char *fname)
  1526. {//===================================================
  1527. int ix;
  1528. int n;
  1529. int x, y;
  1530. int displ;
  1531. int n_points;
  1532. char line_buf[128];
  1533. float env_x[20];
  1534. float env_y[20];
  1535. int env_lin[20];
  1536. unsigned char env[ENV_LEN];
  1537. n_points = 0;
  1538. fgets(line_buf,sizeof(line_buf),f); // skip first line
  1539. while(!feof(f))
  1540. {
  1541. if(fgets(line_buf,sizeof(line_buf),f) == NULL)
  1542. break;
  1543. env_lin[n_points] = 0;
  1544. n = sscanf(line_buf,"%f %f %d",&env_x[n_points],&env_y[n_points],&env_lin[n_points]);
  1545. if(n >= 2)
  1546. {
  1547. env_x[n_points] *= 1.28; // convert range 0-100 to 0-128
  1548. n_points++;
  1549. }
  1550. }
  1551. env_x[n_points] = env_x[n_points-1];
  1552. env_y[n_points] = env_y[n_points-1];
  1553. ix = -1;
  1554. for(x=0; x<ENV_LEN; x++)
  1555. {
  1556. if(x > env_x[ix+4])
  1557. ix++;
  1558. if(n_points > 3)
  1559. y = (int)(polint(&env_x[ix],&env_y[ix],4,x) * 2.55); // convert to range 0-255
  1560. else
  1561. y = (int)(polint(&env_x[ix],&env_y[ix],3,x) * 2.55);
  1562. if(y < 0) y = 0;
  1563. if(y > 255) y = 255;
  1564. env[x] = y;
  1565. }
  1566. if(n_envelopes < N_ENVELOPES)
  1567. {
  1568. strncpy0(envelope_paths[n_envelopes],fname,sizeof(envelope_paths[0]));
  1569. memcpy(envelope_dat[n_envelopes],env,ENV_LEN);
  1570. n_envelopes++;
  1571. }
  1572. displ = ftell(f_phdata);
  1573. fwrite(env,1,128,f_phdata);
  1574. return(displ);
  1575. }
  1576. static int ref_sorter(char **a, char **b)
  1577. {//======================================
  1578. int ix;
  1579. REF_HASH_TAB *p1 = (REF_HASH_TAB *)(*a);
  1580. REF_HASH_TAB *p2 = (REF_HASH_TAB *)(*b);
  1581. ix = strcoll(p1->string,p2->string);
  1582. if(ix != 0)
  1583. return ix;
  1584. ix = p1->ph_table - p2->ph_table;
  1585. if(ix != 0)
  1586. return ix;
  1587. return(p1->ph_mnemonic - p2->ph_mnemonic);
  1588. } /* end of ref_sorter */
  1589. void Compile::Report(void)
  1590. {//=======================
  1591. int ix;
  1592. int hash;
  1593. int n;
  1594. REF_HASH_TAB *p;
  1595. REF_HASH_TAB **list;
  1596. FILE *f_report;
  1597. const char *data_path;
  1598. int prev_table;
  1599. int prev_mnemonic;
  1600. char fname[sizeof(path_source)+20];
  1601. // make a list of all the references and sort it
  1602. list = (REF_HASH_TAB **)malloc((count_references)* sizeof(REF_HASH_TAB *));
  1603. if(list == NULL)
  1604. return;
  1605. sprintf(fname,"%scompile_report",path_source);
  1606. f_report = fopen(fname,"w");
  1607. if(f_report == NULL)
  1608. {
  1609. free(list);
  1610. return;
  1611. }
  1612. fprintf(f_report,"%d phoneme tables\n",n_phoneme_tabs);
  1613. fprintf(f_report," new total\n");
  1614. for(ix=0; ix<n_phoneme_tabs; ix++)
  1615. {
  1616. fprintf(f_report,"%8s %3d %4d\n",phoneme_tab_list2[ix].name, phoneme_tab_list2[ix].n_phonemes, n_phcodes_list[ix]+1);
  1617. }
  1618. fputc('\n',f_report);
  1619. fprintf(f_report,"Data file Used by\n");
  1620. ix = 0;
  1621. for(hash=0; (hash < 256) && (ix < count_references); hash++)
  1622. {
  1623. p = ref_hash_tab[hash];
  1624. while(p != NULL)
  1625. {
  1626. list[ix++] = p;
  1627. p = (REF_HASH_TAB *)(p->link);
  1628. }
  1629. }
  1630. n = ix;
  1631. qsort((void *)list,n,sizeof(REF_HASH_TAB *),(int (*)(const void *,const void *))ref_sorter);
  1632. data_path = "";
  1633. prev_mnemonic = 0;
  1634. prev_table = 0;
  1635. for(ix=0; ix<n; ix++)
  1636. {
  1637. int j = 0;
  1638. if(strcmp(list[ix]->string, data_path) != 0)
  1639. {
  1640. data_path = list[ix]->string;
  1641. j = strlen(data_path);
  1642. fprintf(f_report,"%s",data_path);
  1643. }
  1644. else
  1645. {
  1646. if((list[ix]->ph_table == prev_table) && (list[ix]->ph_mnemonic == prev_mnemonic))
  1647. continue; // same phoneme, don't list twice
  1648. }
  1649. while(j < 14)
  1650. {
  1651. fputc(' ',f_report); // pad filename with spaces
  1652. j++;
  1653. }
  1654. fprintf(f_report," [%s] %s",WordToString(prev_mnemonic = list[ix]->ph_mnemonic), phoneme_tab_list2[prev_table = list[ix]->ph_table].name);
  1655. fputc('\n',f_report);
  1656. free(list[ix]);
  1657. }
  1658. free(list);
  1659. fclose(f_report);
  1660. }
  1661. #ifdef PLATFORM_WINDOWS
  1662. int strcasecmp(const char *s1, const char *s2)
  1663. {
  1664. int ix=0;
  1665. int diff;
  1666. for(;;)
  1667. {
  1668. if((diff = (tolower(s1[ix]) - tolower(s2[ix]))) != 0)
  1669. return(diff);
  1670. if((s1[ix] == 0) || (s2[ix] == 0))
  1671. return(diff);
  1672. ix++;
  1673. }
  1674. }
  1675. #endif
  1676. static int ph_sorter(char **a, char **b)
  1677. {//======================================
  1678. int ix;
  1679. int t1, t2;
  1680. char mnem1[6];
  1681. PHONEME_TAB *p1 = (PHONEME_TAB *)(*a);
  1682. PHONEME_TAB *p2 = (PHONEME_TAB *)(*b);
  1683. t1 = p1->type;
  1684. if(t1 > phVOWEL) t1 = phVOWEL+1;
  1685. t2 = p2->type;
  1686. if(t2 > phVOWEL) t2 = phVOWEL+1;
  1687. if((ix = t1 - t2) != 0)
  1688. return(ix);
  1689. strcpy(mnem1,WordToString(p1->mnemonic));
  1690. return(strcasecmp(mnem1,WordToString(p2->mnemonic)));
  1691. } /* end of ph_sorter */
  1692. void PrintPhonemesUsed(FILE *f, const char *dictname)
  1693. {//==================================================
  1694. int ix;
  1695. PHONEME_TAB *ph;
  1696. PHONEME_TAB *ph_tab[N_PHONEME_TAB];
  1697. int count = 0;
  1698. int n_ph = 0;
  1699. int section = 0;
  1700. fprintf(f,"\n\nDictionary %s_dict\n",dictname);
  1701. fflush(f);
  1702. for(ix=0; (ix<N_PHONEME_TAB) && (phoneme_tab[ix] != NULL); ix++)
  1703. {
  1704. if(phoneme_tab_flags[ix] & 2)
  1705. {
  1706. ph_tab[n_ph++] = phoneme_tab[ix];
  1707. }
  1708. }
  1709. qsort((void *)ph_tab,n_ph,sizeof(PHONEME_TAB *),(int (*)(const void *,const void *))ph_sorter);
  1710. for(ix=0; ix<n_ph; ix++)
  1711. {
  1712. ph = ph_tab[ix];
  1713. if(ph->type > 1)
  1714. {
  1715. if((ph->type > phVOWEL) && (section == 0))
  1716. {
  1717. section = 1;
  1718. count = 0;
  1719. fputc('\n',f);
  1720. }
  1721. if((count & 0x7) == 0)
  1722. fputc('\n',f);
  1723. fprintf(f,"%-4s ",WordToString(ph->mnemonic));
  1724. count++;
  1725. }
  1726. }
  1727. fputc('\n',f);
  1728. } // end of PrintPhonemesUsed
  1729. wxString CompileAllDictionaries()
  1730. {//==============================
  1731. wxString filename;
  1732. wxFileName fname;
  1733. wxString dictstr;
  1734. wxString report = _T("");
  1735. int err;
  1736. int errors = 0;
  1737. int dict_count = 0;
  1738. FILE *log;
  1739. FILE *f_phused;
  1740. char dictname[80];
  1741. char fname_log[sizeof(path_dsource)+20];
  1742. char save_voice_name[80];
  1743. if(!wxDirExists(path_dictsource))
  1744. {
  1745. if(gui_flag)
  1746. {
  1747. wxString dirname = wxDirSelector(_T("Directory of dictionary files"),path_phsource);
  1748. if(!dirname.IsEmpty())
  1749. {
  1750. path_dictsource = dirname;
  1751. strncpy0(path_dsource,path_dictsource.mb_str(wxConvLocal),sizeof(path_dsource)-1);
  1752. strcat(path_dsource,"/");
  1753. }
  1754. }
  1755. else
  1756. {
  1757. fprintf(stderr,"Can't find dictionary files: %s\n",path_dsource);
  1758. }
  1759. }
  1760. wxDir dir(path_dictsource);
  1761. if(!dir.IsOpened())
  1762. {
  1763. return(_T(" No dictionaries"));
  1764. }
  1765. strcpy(save_voice_name,voice_name2);
  1766. sprintf(fname_log,"%s%s",path_dsource,"dict_log");
  1767. log = fopen(fname_log,"w");
  1768. sprintf(fname_log,"%s%s",path_dsource,"dict_phonemes");
  1769. f_phused = fopen(fname_log,"w");
  1770. if(f_phused)
  1771. {
  1772. fprintf(f_phused,"Phonemes which are used in the *_rules and *_list files\n");
  1773. }
  1774. bool cont = dir.GetFirst(&filename, _T("*_rules"), wxDIR_FILES);
  1775. while ( cont )
  1776. {
  1777. fname = wxFileName(filename);
  1778. dictstr = fname.GetName().BeforeLast('_');
  1779. strcpy(dictname,dictstr.mb_str(wxConvLocal));
  1780. dict_count++;
  1781. LoadVoice(dictname,0);
  1782. if((err = CompileDictionary(path_dsource, dictname,log,NULL)) > 0)
  1783. {
  1784. report = report + dictstr + wxString::Format(_T(" %d, "),err);
  1785. errors += err;
  1786. }
  1787. if(f_phused != NULL)
  1788. {
  1789. memset(phoneme_tab_flags,0,sizeof(phoneme_tab_flags));
  1790. FindPhonemesUsed();
  1791. PrintPhonemesUsed(f_phused,dictname);
  1792. }
  1793. cont = dir.GetNext(&filename);
  1794. }
  1795. if(log != NULL)
  1796. fclose(log);
  1797. if(f_phused != NULL)
  1798. fclose(f_phused);
  1799. LoadVoice(save_voice_name,1);
  1800. if(errors == 0)
  1801. return(wxString::Format(_T(" Compiled %d dictionaries"),dict_count));
  1802. else
  1803. {
  1804. return(_T(" Dictionary errors: ") + report);
  1805. }
  1806. } // end of CompileAllDictionaries
  1807. void Compile::CPhonemeTab(const char *source)
  1808. {//========================================
  1809. char fname[sizeof(path_source)+40];
  1810. wxString report;
  1811. wxString report_dict;
  1812. #ifdef MAKE_ENVELOPES
  1813. make_envs();
  1814. #endif
  1815. n_envelopes = 0;
  1816. error_count = 0;
  1817. memset(markers_used,0,sizeof(markers_used));
  1818. f_errors = stderr;
  1819. strncpy0(current_fname,source,sizeof(current_fname));
  1820. strncpy0(fname,path_phfile.mb_str(wxConvLocal),sizeof(fname));
  1821. f_in = fopen_log(f_errors,fname,"rb");
  1822. if(f_in == NULL)
  1823. {
  1824. if(gui_flag)
  1825. {
  1826. wxString phfile = wxFileSelector(_T("Master phonemes file"),path_phsource,
  1827. _T(""),_T(""),_T("*"),wxOPEN);
  1828. if(!phfile.IsEmpty())
  1829. {
  1830. path_phfile = phfile;
  1831. }
  1832. }
  1833. strncpy0(fname,path_phfile.mb_str(wxConvLocal),sizeof(fname));
  1834. f_in = fopen_log(f_errors,fname,"rb");
  1835. if(f_in == NULL)
  1836. {
  1837. wxLogError(_T("Can't read master phonemes file:\n") + wxString(fname,wxConvLocal));
  1838. return;
  1839. }
  1840. }
  1841. progress_max = 0;
  1842. while(fgets(fname,sizeof(fname),f_in) != NULL)
  1843. {
  1844. // count the number of phoneme tables declared in the master phonemes file
  1845. if(memcmp(fname,"phonemetable",12)==0)
  1846. progress_max++;
  1847. }
  1848. rewind(f_in);
  1849. sprintf(fname,"%s%s",path_source,"error_log");
  1850. if((f_errors = fopen_log(f_errors,fname,"w")) == NULL)
  1851. f_errors = stderr;
  1852. sprintf(fname,"%s/%s",path_home,"phondata-manifest");
  1853. if((f_phcontents = fopen_log(f_phcontents,fname,"w")) == NULL)
  1854. f_phcontents = stderr;
  1855. fprintf (f_phcontents,
  1856. "# This file lists the type of data that has been compiled into the\n"
  1857. "# phondata file\n"
  1858. "#\n"
  1859. "# The first character of a line indicates the type of data:\n"
  1860. "# S - A SPECT_SEQ structure\n"
  1861. "# W - A wavefile segment\n"
  1862. "# E - An envelope\n"
  1863. "#\n"
  1864. "# Address is the displacement within phondata of this item\n"
  1865. "#\n"
  1866. "# Address Data file\n"
  1867. "# ------- ---------\n");
  1868. sprintf(fname,"%s/%s",path_home,"phondata");
  1869. f_phdata = fopen_log(f_errors,fname,"wb");
  1870. sprintf(fname,"%s/%s",path_home,"phonindex");
  1871. f_phindex = fopen_log(f_errors,fname,"wb");
  1872. sprintf(fname,"%s/%s",path_home,"phontab");
  1873. f_phtab = fopen_log(f_errors,fname,"wb");
  1874. if(f_phdata==NULL || f_phindex==NULL || f_phtab==NULL)
  1875. {
  1876. return;
  1877. }
  1878. if(gui_flag)
  1879. {
  1880. progress = new wxProgressDialog(_T("Phonemes"),_T(""),progress_max);
  1881. }
  1882. else
  1883. {
  1884. fprintf(stderr,"Compiling phoneme data: %s\n",path_source);
  1885. }
  1886. // write a word so that further data doesn't start at displ=0
  1887. Write4Bytes(f_phdata,version_phdata);
  1888. Write4Bytes(f_phindex,version_phdata);
  1889. memset(ref_hash_tab,0,sizeof(ref_hash_tab));
  1890. n_phoneme_tabs = 0;
  1891. stack_ix = 0;
  1892. StartPhonemeTable("base");
  1893. CPhonemeFiles();
  1894. EndPhonemeTable();
  1895. WritePhonemeTable();
  1896. fprintf(f_errors,"Refs %d, Reused %d\n",count_references,duplicate_references);
  1897. fclose(f_in);
  1898. fclose(f_phdata);
  1899. fclose(f_errors);
  1900. fclose(f_phindex);
  1901. fclose(f_phtab);
  1902. fclose(f_phcontents);
  1903. LoadPhData();
  1904. LoadVoice(voice_name2,0);
  1905. Report();
  1906. DrawEnvelopes();
  1907. report_dict = CompileAllDictionaries();
  1908. if(gui_flag)
  1909. {
  1910. delete progress;
  1911. }
  1912. report.Printf(_T("Compiled phonemes: %d errors."),error_count);
  1913. if(error_count > 0)
  1914. {
  1915. report += _T(" See file: 'error_log'.");
  1916. wxLogError(report);
  1917. }
  1918. wxLogStatus(report + report_dict);
  1919. if(gui_flag == 0)
  1920. {
  1921. strncpy0(fname,(report+report_dict).mb_str(wxConvLocal),sizeof(fname));
  1922. fprintf(stderr,"%s\n",fname);
  1923. }
  1924. } // end of Compile::CPhonemeTab
  1925. void CompileMbrola()
  1926. {//=================
  1927. char *p;
  1928. FILE *f_in;
  1929. FILE *f_out;
  1930. int percent;
  1931. int n;
  1932. int count = 0;
  1933. int control;
  1934. char phoneme[40];
  1935. char phoneme2[40];
  1936. char name1[40];
  1937. char name2[40];
  1938. char mbrola_voice[40];
  1939. char buf[sizeof(path_home)+30];
  1940. int mbrola_ctrl = 20; // volume in 1/16 ths
  1941. MBROLA_TAB data[N_PHONEME_TAB];
  1942. wxString filepath = wxFileSelector(_T("Read Mbrola phonemes file"),path_phsource+_T("/mbrola"),_T(""),_T(""),_T("*"),wxOPEN);
  1943. strcpy(buf,filepath.mb_str(wxConvLocal));
  1944. if((f_in = fopen(buf,"r")) == NULL)
  1945. {
  1946. wxLogError(_T("Can't read: ")+filepath);
  1947. return;
  1948. }
  1949. while(fgets(buf,sizeof(phoneme),f_in) != NULL)
  1950. {
  1951. buf[sizeof(phoneme)-1] = 0;
  1952. if((p = strstr(buf,"//")) != NULL)
  1953. *p = 0; // truncate line at comment
  1954. if(memcmp(buf,"volume",6)==0)
  1955. {
  1956. mbrola_ctrl = atoi(&buf[6]);
  1957. continue;
  1958. }
  1959. n = sscanf(buf,"%d %s %s %d %s %s",&control,phoneme,phoneme2,&percent,name1,name2);
  1960. if(n >= 5)
  1961. {
  1962. data[count].name = StringToWord(phoneme);
  1963. if(strcmp(phoneme2,"NULL")==0)
  1964. data[count].next_phoneme = 0;
  1965. else
  1966. if(strcmp(phoneme2,"VWL")==0)
  1967. data[count].next_phoneme = 2;
  1968. else
  1969. data[count].next_phoneme = StringToWord(phoneme2);
  1970. data[count].mbr_name = 0;
  1971. data[count].mbr_name2 = 0;
  1972. data[count].percent = percent;
  1973. data[count].control = control;
  1974. if(strcmp(name1,"NULL")!=0)
  1975. data[count].mbr_name = StringToWord(name1);
  1976. if(n == 6)
  1977. data[count].mbr_name2 = StringToWord(name2);
  1978. count++;
  1979. }
  1980. }
  1981. fclose(f_in);
  1982. wxFileName filename = wxFileName(filepath);
  1983. strcpy(mbrola_voice,filename.GetName().mb_str(wxConvLocal));
  1984. sprintf(buf,"%s/mbrola_ph/%s_phtrans",path_home,mbrola_voice);
  1985. if((f_out = fopen(buf,"w")) == NULL)
  1986. {
  1987. wxLogError(_T("Can't write to: ")+wxString(buf,wxConvLocal));
  1988. return;
  1989. }
  1990. data[count].name = 0; // list terminator
  1991. fwrite(&mbrola_ctrl,4,1,f_out);
  1992. fwrite(data,sizeof(MBROLA_TAB),count+1,f_out);
  1993. fclose(f_out);
  1994. wxLogStatus(_T("Mbrola translation file: %d phonemes"),count);
  1995. } // end of CompileMbrola
  1996. void CompileInit(void)
  1997. {
  1998. compile = new Compile;
  1999. compile->CPhonemeTab("phonemes");
  2000. delete compile;
  2001. }