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

compiledata.cpp 51KB

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