eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

extras.cpp 24KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  1. /***************************************************************************
  2. * Copyright (C) 2006 by Jonathan Duddington *
  3. * [email protected] *
  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 2 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, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include "wx/wx.h"
  21. #include <wx/dirdlg.h>
  22. #include "wx/filename.h"
  23. #include "wx/sound.h"
  24. #include "sys/stat.h"
  25. #include "speak_lib.h"
  26. #include "main.h"
  27. #include "speech.h"
  28. #include "voice.h"
  29. #include "spect.h"
  30. #include "phoneme.h"
  31. #include "synthesize.h"
  32. #include "translate.h"
  33. #include "options.h"
  34. //******************************************************************************************************
  35. FILE *f_wavtest = NULL;
  36. FILE *f_events = NULL;
  37. int OpenWaveFile3(const char *path, int rate)
  38. /******************************************/
  39. {
  40. int *p;
  41. static unsigned char wave_hdr[44] = {
  42. 'R','I','F','F',0,0,0,0,'W','A','V','E','f','m','t',' ',
  43. 0x10,0,0,0,1,0,1,0, 9,0x3d,0,0,0x12,0x7a,0,0,
  44. 2,0,0x10,0,'d','a','t','a', 0,0,0,0 };
  45. if(path == NULL)
  46. return(2);
  47. // set the sample rate in the header
  48. p = (int *)(&wave_hdr[24]);
  49. p[0] = rate;
  50. p[1] = rate * 2;
  51. f_wavtest = fopen(path,"wb");
  52. if(f_wavtest != NULL)
  53. {
  54. fwrite(wave_hdr,1,sizeof(wave_hdr),f_wavtest);
  55. return(0);
  56. }
  57. return(1);
  58. } // end of OpenWaveFile
  59. void CloseWaveFile3(int rate)
  60. /******************/
  61. {
  62. unsigned int pos;
  63. static int value;
  64. fflush(f_wavtest);
  65. pos = ftell(f_wavtest);
  66. value = pos - 8;
  67. fseek(f_wavtest,4,SEEK_SET);
  68. fwrite(&value,4,1,f_wavtest);
  69. value = rate;
  70. fseek(f_wavtest,24,SEEK_SET);
  71. fwrite(&value,4,1,f_wavtest);
  72. value = rate*2;
  73. fseek(f_wavtest,28,SEEK_SET);
  74. fwrite(&value,4,1,f_wavtest);
  75. value = pos - 44;
  76. fseek(f_wavtest,40,SEEK_SET);
  77. fwrite(&value,4,1,f_wavtest);
  78. fclose(f_wavtest);
  79. f_wavtest = NULL;
  80. } // end of CloseWaveFile2
  81. int TestUriCallback(int type, const char *uri, const char *base)
  82. {//=============================================================
  83. if(strcmp(uri,"hello")==0)
  84. return(1);
  85. return(0);
  86. }
  87. int TestSynthCallback(short *wav, int numsamples, espeak_EVENT *events)
  88. {//====================================================================
  89. int type;
  90. fprintf(f_events,"--\n");
  91. if(f_wavtest == NULL) return(0);
  92. if(wav == NULL)
  93. {
  94. fprintf(f_events,"Finished\n");
  95. CloseWaveFile3(samplerate);
  96. fclose(f_events);
  97. return(0);
  98. }
  99. fwrite(wav,numsamples*2,1,f_wavtest);
  100. while((type = events->type) != 0)
  101. {
  102. fprintf(f_events,"%5d %4d (%2d) %d ",events->audio_position,events->text_position,events->length,type);
  103. if((type==3) || (type==4))
  104. fprintf(f_events,"'%s'\n",events->id.name);
  105. else
  106. if(type==espeakEVENT_PHONEME)
  107. fprintf(f_events,"[%s]\n",WordToString(events->id.number));
  108. else
  109. fprintf(f_events,"%d\n",events->id.number);
  110. events++;
  111. }
  112. return(0);
  113. }
  114. //******************************************************************************************************
  115. #ifdef deleted
  116. static int RuLex_sorter(char **a, char **b)
  117. {//=======================================
  118. char *pa, *pb;
  119. int xa, xb;
  120. int ix;
  121. pa = *a;
  122. pb = *b;
  123. xa = strlen(pa)-1;
  124. xb = strlen(pb)-1;
  125. while((xa >= 0) && (xb >= 0))
  126. {
  127. if((ix = (pa[xa] - pb[xb])) != 0)
  128. return(ix);
  129. xa--;
  130. xb--;
  131. }
  132. return(pa - pb);
  133. } /* end of strcmp2 */
  134. #endif
  135. static const unsigned short KOI8_R[0x60] = {
  136. 0x2550, 0x2551, 0x2552, 0x0451, 0x2553, 0x2554, 0x2555, 0x2556, // a0
  137. 0x2557, 0x2558, 0x2559, 0x255a, 0x255b, 0x255c, 0x255d, 0x255e, // a8
  138. 0x255f, 0x2560, 0x2561, 0x0401, 0x2562, 0x2563, 0x2564, 0x2565, // b0
  139. 0x2566, 0x2567, 0x2568, 0x2569, 0x256a, 0x256b, 0x256c, 0x00a9, // b8
  140. 0x044e, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433, // c0
  141. 0x0445, 0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e, // c8
  142. 0x043f, 0x044f, 0x0440, 0x0441, 0x0442, 0x0443, 0x0436, 0x0432, // d0
  143. 0x044c, 0x044b, 0x0437, 0x0448, 0x044d, 0x0449, 0x0447, 0x044a, // d8
  144. 0x042e, 0x0410, 0x0411, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413, // e0
  145. 0x0425, 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, // e8
  146. 0x041f, 0x042f, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412, // f0
  147. 0x042c, 0x042b, 0x0417, 0x0428, 0x042d, 0x0429, 0x0427, 0x042a, // f8
  148. };
  149. #define N_CHARS 34
  150. int *p_unicode;
  151. int unicode[80];
  152. #define PH(c1,c2) (c2<<8)+c1 // combine two characters into an integer for phoneme name
  153. void DecodePhonemes2(const char *inptr, char *outptr)
  154. //===================================================
  155. // Translate from internal phoneme codes into phoneme mnemonics
  156. {
  157. unsigned char phcode;
  158. unsigned char c;
  159. unsigned int mnem;
  160. PHONEME_TAB *ph;
  161. const char *p;
  162. int ix;
  163. int j;
  164. int start;
  165. static const char *stress_chars = "==,,'* ";
  166. unsigned int replace_ph[] = {',',PH('@','-'),'W','3','y','A',PH('A',':'),'*',PH('_','!'),PH('_','|'),PH('O','I'),PH('Y',':'),PH('p','F'),PH('E','2'),0};
  167. const char *replace_ph2[] = {NULL,NULL,"9","@r","Y","a:", "a:", "r", "?", "?", "OY", "2:", "pf" ,"E",NULL};
  168. start = 1;
  169. for(ix=0; (phcode = inptr[ix]) != 0; ix++)
  170. {
  171. if(phcode == 255)
  172. continue; /* indicates unrecognised phoneme */
  173. if((ph = phoneme_tab[phcode]) == NULL)
  174. continue;
  175. if((ph->type == phSTRESS) && (ph->std_length <= 4) && (ph->spect == 0))
  176. {
  177. if(ph->std_length > 2)
  178. *outptr++ = stress_chars[ph->std_length];
  179. }
  180. else
  181. {
  182. mnem = ph->mnemonic;
  183. if(ph->type == phPAUSE)
  184. {
  185. if(start)
  186. continue; // omit initial [?]
  187. if(inptr[ix+1] == phonSCHWA_SHORT)
  188. continue; // omit [?] before [@-*]
  189. }
  190. start = 0;
  191. p = NULL;
  192. for(j=0;;j++)
  193. {
  194. if(replace_ph[j] == 0)
  195. break;
  196. if(mnem == replace_ph[j])
  197. {
  198. p = replace_ph2[j];
  199. if(p == NULL)
  200. mnem = 0;
  201. break;
  202. }
  203. }
  204. if(p != NULL)
  205. {
  206. while((c = *p++) != 0)
  207. {
  208. *outptr++ = c;
  209. }
  210. }
  211. else
  212. if(mnem != 0)
  213. {
  214. while((c = (mnem & 0xff)) != 0)
  215. {
  216. *outptr++ = c;
  217. mnem = mnem >> 8;
  218. }
  219. }
  220. }
  221. }
  222. *outptr = 0; /* string terminator */
  223. } // end of DecodePhonemes
  224. void Lexicon_De()
  225. {//==============
  226. // Compare eSpeak's translation of German words with a pronunciation lexicon
  227. FILE *f_in;
  228. FILE *f_out;
  229. int ix;
  230. int c;
  231. int c2;
  232. char *p;
  233. int stress;
  234. int count=0;
  235. int start;
  236. int matched=0;
  237. int defer_stress = 0;
  238. char buf[120];
  239. char word[80];
  240. char word2[80];
  241. char type[80];
  242. char pronounce[80];
  243. char pronounce2[80];
  244. char phonemes[80];
  245. char phonemes2[80];
  246. WORD_TAB winfo;
  247. static char *vowels = "aeiouyAEIOUY29@";
  248. wxString fname = wxFileSelector(_T("German Lexicon"),path_dir1,_T(""),_T(""),_T("*"),wxOPEN);
  249. strcpy(buf,fname.mb_str(wxConvLocal));
  250. if((f_in = fopen(buf,"r")) == NULL)
  251. {
  252. wxLogError(_T("Can't read file ")+fname);
  253. return;
  254. }
  255. path_dir1 = wxFileName(fname).GetPath();
  256. if((f_out = fopen("compare_de","w")) == NULL)
  257. {
  258. wxLogError(_T("Can't write file "));
  259. return;
  260. }
  261. LoadVoice("de",0);
  262. word2[0] = ' ';
  263. while(!feof(f_in))
  264. {
  265. count++;
  266. if(fgets(buf,sizeof(buf),f_in) == NULL)
  267. break;
  268. sscanf(buf,"%s %s %s",word,type,pronounce);
  269. // convert word to lower-case
  270. for(ix=0, p=&word2[1];;)
  271. {
  272. ix += utf8_in(&c,&word[ix],0);
  273. c = towlower(c);
  274. p += utf8_out(c,p);
  275. if(c == 0)
  276. break;
  277. }
  278. strcpy(word,&word2[1]);
  279. strcat(&word2[1]," ");
  280. // remove | syllable boundaries
  281. stress=0;
  282. start=1;
  283. for(ix=0, p=pronounce2;;ix++)
  284. {
  285. c = pronounce[ix];
  286. if(c == '\'')
  287. {
  288. stress=4;
  289. continue;
  290. }
  291. if(c == ',')
  292. {
  293. stress=3;
  294. continue;
  295. }
  296. if(c == '|')
  297. continue;
  298. if((c == '?') && start)
  299. continue; // omit initial [?]
  300. if(c == '<')
  301. {
  302. if((c2 = pronounce[ix+1]) == 'i')
  303. {
  304. defer_stress =1;
  305. #ifdef deleted
  306. if(stress == 4)
  307. {
  308. *p++ = 'i';
  309. c =':';
  310. }
  311. else
  312. #endif
  313. {
  314. c = 'I';
  315. }
  316. ix++;
  317. }
  318. }
  319. start =0;
  320. if(defer_stress)
  321. {
  322. defer_stress = 0;
  323. }
  324. else
  325. if(stress && (strchr(vowels,c) != NULL))
  326. {
  327. if(stress == 4)
  328. *p++ = '\'';
  329. if(stress == 3)
  330. *p++ = ',';
  331. stress = 0;
  332. }
  333. *p++ = c;
  334. if(c == 0)
  335. break;
  336. if(strchr("eiouy",c) && pronounce[ix+1] != ':')
  337. *p++ = ':'; // ensure [;] after these vowels
  338. }
  339. // translate
  340. memset(&winfo,0,sizeof(winfo));
  341. translator->TranslateWord(&word2[1],0,&winfo);
  342. DecodePhonemes2(translator->word_phonemes,phonemes); // also need to change some phoneme names
  343. if(strcmp(phonemes,pronounce2) == 0)
  344. {
  345. matched++;
  346. }
  347. else
  348. {
  349. // remove secondary stress
  350. strcpy(phonemes2,phonemes);
  351. p = phonemes;
  352. for(ix=0; ;ix++)
  353. {
  354. if((c = phonemes2[ix]) != ',')
  355. *p++ = c;
  356. if(c == 0)
  357. break;
  358. }
  359. if(strcmp(phonemes,pronounce2) == 0)
  360. {
  361. matched++;
  362. }
  363. else
  364. {
  365. if(strlen(word) < 8)
  366. strcat(word,"\t");
  367. fprintf(f_out,"%s\t%s\t%s\n",word,phonemes,pronounce2);
  368. }
  369. }
  370. }
  371. fclose(f_in);
  372. fclose(f_out);
  373. wxLogStatus(_T("Completed, equal=%d different=%d"),matched,count-matched);
  374. }
  375. void Lexicon_Ru()
  376. {//==============
  377. // compare stress markings in Russian RuLex file with lookup in ru_rules
  378. int ix;
  379. char *p;
  380. int c;
  381. FILE *f_in;
  382. FILE *f_out;
  383. FILE *f_log;
  384. FILE *f_roots;
  385. PHONEME_TAB *ph;
  386. int vcount;
  387. int ru_stress;
  388. int max_stress;
  389. int max_stress_posn;
  390. int n_words=0;
  391. int n_wrong=0;
  392. int wlength;
  393. int input_length;
  394. int sfx;
  395. char *suffix;
  396. int wlen;
  397. int len;
  398. int check_root;
  399. WORD_TAB winfo;
  400. char word[80];
  401. char word2[80];
  402. int counts[20][20][10];
  403. char phonemes[N_WORD_PHONEMES];
  404. char buf[100];
  405. // KOI8-R codes for Russian vowels
  406. static char vowels[] = {0xa3,0xc0,0xc1,0xc5,0xc9,0xcf,0xd1,0xd5,0xd9,0xdc,0};
  407. typedef struct {
  408. char *suffix;
  409. int syllables;
  410. } SUFFIX;
  411. static SUFFIX suffixes[] = {
  412. {NULL,0},
  413. {"ичу",2},
  414. {"ского",2},
  415. {"ская",2},
  416. {"ски",1},
  417. {"ские",2},
  418. {"ский",1},
  419. {"ским",1},
  420. {"ское",2},
  421. {"ской",1},
  422. {"ском",1},
  423. {"скую",2},
  424. {"а",1},
  425. {"е",1},
  426. {"и",1},
  427. {NULL,0}};
  428. memset(counts,0,sizeof(counts));
  429. if(gui_flag)
  430. {
  431. wxString fname = wxFileSelector(_T("Read lexicon.dict"),path_dictsource,
  432. _T(""),_T(""),_T("*"),wxOPEN);
  433. if(fname.IsEmpty())
  434. return;
  435. strcpy(buf,fname.mb_str(wxConvLocal));
  436. }
  437. else
  438. {
  439. strcpy(buf,"lexicon.dict");
  440. }
  441. if((f_in = fopen(buf,"r")) == NULL)
  442. {
  443. if(gui_flag)
  444. wxLogError(_T("Can't read file: ") + wxString(buf,wxConvLocal));
  445. else
  446. fprintf(stderr,"Can't read file: %s\n",buf);
  447. return;
  448. }
  449. input_length = GetFileLength(buf);
  450. sprintf(buf,"%s%c%s",path_dsource,PATHSEP,"ru_listx_1");
  451. if((f_out = fopen(buf,"w")) == NULL)
  452. {
  453. wxLogError(_T("Can't write to: ")+wxString(buf,wxConvLocal));
  454. fclose(f_in);
  455. return;
  456. }
  457. sprintf(buf,"%s%c%s",path_dsource,PATHSEP,"ru_log");
  458. f_log = fopen(buf,"w");
  459. sprintf(buf,"%s%c%s",path_dsource,PATHSEP,"ru_roots_1");
  460. f_roots = fopen(buf,"w");
  461. LoadVoice("ru",0);
  462. if(gui_flag)
  463. progress = new wxProgressDialog(_T("Lexicon"),_T(""),input_length);
  464. else
  465. fprintf(stderr,"Processing lexicon.dict\n");
  466. for(;;)
  467. {
  468. if(((n_words & 0x3ff) == 0) && gui_flag)
  469. {
  470. progress->Update(ftell(f_in));
  471. }
  472. if(fgets(buf,sizeof(buf),f_in) == NULL)
  473. break;
  474. if(isspace2(buf[0]))
  475. continue;
  476. // convert word from KOI8-R to UTF8
  477. p = buf;
  478. ix = 0;
  479. wlength = 0;
  480. p_unicode = unicode;
  481. while(!isspace2(c = (*p++ & 0xff)))
  482. {
  483. if(c >= 0xa0)
  484. {
  485. c = KOI8_R[c-0xa0];
  486. *p_unicode++ = c;
  487. }
  488. wlength++;
  489. ix += utf8_out(c,&word[ix]);
  490. }
  491. word[ix] = 0;
  492. *p_unicode=0;
  493. sprintf(word2," %s ",word);
  494. // find the marked stress position
  495. vcount = 0;
  496. ru_stress = 0;
  497. while(*p == ' ') p++;
  498. while((c = (*p++ & 0xff)) != '\n')
  499. {
  500. if(c == '+')
  501. {
  502. ru_stress = vcount;
  503. break;
  504. }
  505. if(strchr(vowels,c) != NULL)
  506. {
  507. vcount++;
  508. }
  509. }
  510. // translate
  511. memset(&winfo,0,sizeof(winfo));
  512. translator->TranslateWord(&word2[1],0,&winfo);
  513. DecodePhonemes(translator->word_phonemes,phonemes);
  514. // find the stress position in the translation
  515. max_stress = 0;
  516. max_stress_posn = -1;
  517. vcount = 0;
  518. check_root = 0;
  519. ph = phoneme_tab[phonPAUSE];
  520. for(p=translator->word_phonemes; *p != 0; p++)
  521. {
  522. ph = phoneme_tab[(unsigned int)*p];
  523. if(ph == NULL)
  524. continue;
  525. if(ph->type == phVOWEL)
  526. vcount++;
  527. if(ph->type == phSTRESS)
  528. {
  529. if(ph->std_length > max_stress)
  530. {
  531. max_stress = ph->std_length;
  532. max_stress_posn = vcount+1;
  533. }
  534. }
  535. }
  536. n_words++;
  537. if(ru_stress > vcount)
  538. {
  539. if(f_log != NULL)
  540. {
  541. fprintf(f_log,"%s\t $%d\t // %s\n",word,ru_stress,phonemes);
  542. }
  543. }
  544. else
  545. {
  546. counts[vcount][ru_stress][ph->type]++;
  547. if((vcount > 1) && (ru_stress != max_stress_posn))
  548. {
  549. n_wrong++;
  550. if((ru_stress==0) || (ru_stress > 7))
  551. fprintf(f_out,"// "); // we only have $1 to $7 to indicate stress position
  552. else
  553. check_root = 1;
  554. #define X_COMPACT
  555. fprintf(f_out,"%s",word);
  556. #ifdef X_COMPACT
  557. if(wlength < 8) fputc('\t',f_out);
  558. if(wlength < 16) fputc('\t',f_out);
  559. fprintf(f_out," $%d\n",ru_stress);
  560. #else
  561. while(wlength++ < 20)
  562. fputc(' ',f_out);
  563. fprintf(f_out," $%d //%d %s\n",ru_stress,max_stress_posn,phonemes);
  564. #endif
  565. //CharStats();
  566. }
  567. }
  568. if(check_root)
  569. {
  570. // does this word match any suffixes ?
  571. wlen = strlen(word);
  572. for(sfx=0;(suffix = suffixes[sfx].suffix) != NULL; sfx++)
  573. {
  574. len = strlen(suffix);
  575. if(len >= (wlen-2))
  576. continue;
  577. if(ru_stress > (vcount - suffixes[sfx].syllables))
  578. continue;
  579. if(strcmp(suffix,&word[wlen-len])==0)
  580. {
  581. strcpy(word2,word);
  582. word2[wlen-len] = 0;
  583. // fprintf(f_roots,"%s\t $%d\t\\ %s\n",word2,ru_stress,suffix);
  584. fprintf(f_roots,"%s\t $%d\n",word2,ru_stress);
  585. }
  586. }
  587. }
  588. }
  589. fclose(f_in);
  590. fclose(f_out);
  591. fclose(f_roots);
  592. sprintf(buf,"Lexicon: Total %d OK %d wrong %d",n_words,n_words-n_wrong,n_wrong);
  593. if(gui_flag)
  594. {
  595. delete progress;
  596. wxLogStatus(wxString(buf,wxConvLocal));
  597. }
  598. else
  599. {
  600. fprintf(stderr,"%s\n",buf);
  601. }
  602. if(f_log != NULL)
  603. {
  604. #ifdef deleted
  605. // list tables of frequency of stress position for words of different syllable lengths
  606. int j,k;
  607. for(ix=0; ix<12; ix++)
  608. {
  609. fprintf(f_log,"%2d syl: ",ix);
  610. for(k=0; k<10; k++)
  611. {
  612. fprintf(f_log," %2d :",k);
  613. for(j=0; j<10; j++)
  614. {
  615. fprintf(f_log,"%6d ",counts[ix][j][k]);
  616. }
  617. fprintf(f_log,"\n");
  618. }
  619. fprintf(f_log,"\n\n");
  620. }
  621. #endif
  622. fclose(f_log);
  623. }
  624. } // end of Lexicon_Ru
  625. void CompareLexicon(int id)
  626. {//========================
  627. switch(id)
  628. {
  629. case MENU_LEXICON_RU:
  630. Lexicon_Ru();
  631. break;
  632. case MENU_LEXICON_DE:
  633. Lexicon_De();
  634. break;
  635. }
  636. } // end of CompareLexicon
  637. //******************************************************************************************************
  638. extern int HashDictionary(const char *string);
  639. static int n_words;
  640. struct wcount {
  641. struct wcount *link;
  642. int count;
  643. char *word;
  644. };
  645. static int wfreq_sorter(wcount **p1, wcount **p2)
  646. {//==============================================
  647. int x;
  648. wcount *a, *b;
  649. a = *p1;
  650. b = *p2;
  651. if((x = b->count - a->count) != 0)
  652. return(x);
  653. return(strcmp(a->word,b->word));
  654. }
  655. static void wfreq_add(const char *word, wcount **hashtab)
  656. {//======================================================
  657. wcount *p;
  658. wcount **p2;
  659. int len;
  660. int hash;
  661. hash = HashDictionary(word);
  662. p2 = &hashtab[hash];
  663. p = *p2;
  664. while(p != NULL)
  665. {
  666. if(strcmp(p->word,word)==0)
  667. {
  668. p->count++;
  669. return;
  670. }
  671. p2 = &p->link;
  672. p = *p2;
  673. }
  674. // word not found, add it to the list
  675. len = strlen(word) + 1;
  676. if((p = (wcount *)malloc(sizeof(wcount)+len)) == NULL)
  677. return;
  678. p->count = 1;
  679. p->link = NULL;
  680. p->word = (char *)p + sizeof(wcount);
  681. strcpy(p->word,word);
  682. *p2 = p;
  683. n_words++;
  684. }
  685. void CountWordFreq(wxString path, wcount **hashtab)
  686. {//================================================
  687. // Count the occurances of words in this file
  688. FILE *f_in;
  689. unsigned char c;
  690. int wc;
  691. unsigned int ix, j, k;
  692. int n_chars;
  693. char buf[80];
  694. char wbuf[80];
  695. if((f_in = fopen(path.mb_str(wxConvLocal),"rb")) == NULL)
  696. return;
  697. while(!feof(f_in))
  698. {
  699. while((c = fgetc(f_in)) < 'A')
  700. {
  701. // skip leading spaces, numbers, etc
  702. if(feof(f_in)) break;
  703. }
  704. // read utf8 bytes until a space, number or punctuation
  705. ix = 0;
  706. while(!feof(f_in) && (c >= 'A') && (ix < sizeof(buf)-1))
  707. {
  708. buf[ix++] = c;
  709. c = fgetc(f_in);
  710. }
  711. buf[ix++] = 0;
  712. buf[ix] = 0;
  713. // the buf may contain non-alphabetic characters
  714. j = 0;
  715. n_chars = 0;
  716. for(k=0; k<ix; )
  717. {
  718. k += utf8_in(&wc,&buf[k],0);
  719. wc = towlower(wc); // convert to lower case
  720. if(iswalpha(wc))
  721. {
  722. j += utf8_out(wc,&wbuf[j]);
  723. n_chars++;
  724. }
  725. else
  726. {
  727. wbuf[j] = 0;
  728. if(n_chars > 2)
  729. {
  730. wfreq_add(wbuf,hashtab);
  731. }
  732. j = 0;
  733. n_chars = 0;
  734. }
  735. }
  736. }
  737. fclose(f_in);
  738. } // end of CountWordFreq
  739. void MakeWordFreqList()
  740. {//====================
  741. // Read text files from a specified directory and make a list of the most frequently occuring words.
  742. struct wcount *whashtab[N_HASH_DICT];
  743. wcount **w_list;
  744. int ix;
  745. int j;
  746. int hash;
  747. wcount *p;
  748. FILE *f_out;
  749. char buf[200];
  750. char buf2[200];
  751. wxString dir = wxDirSelector(_T("Directory of text files"),path_speaktext);
  752. if(dir.IsEmpty()) return;
  753. memset(whashtab,0,sizeof(whashtab));
  754. wxString path = wxFindFirstFile(dir+_T("/*"),wxFILE);
  755. while (!path.empty())
  756. {
  757. if(path.AfterLast(PATHSEP) != _T("!wordcounts"))
  758. {
  759. CountWordFreq(path,whashtab);
  760. path = wxFindNextFile();
  761. }
  762. }
  763. // put all the words into a list and then sort it
  764. w_list = (wcount **)malloc(sizeof(wcount *) * n_words);
  765. ix = 0;
  766. for(hash=0; hash < N_HASH_DICT; hash++)
  767. {
  768. p = whashtab[hash];
  769. while((p != NULL) && (ix < n_words))
  770. {
  771. w_list[ix++] = p;
  772. p = p->link;
  773. }
  774. }
  775. qsort((void *)w_list,ix,sizeof(wcount *),(int(*)(const void *,const void *))wfreq_sorter);
  776. // write out the sorted list
  777. strcpy(buf,dir.mb_str(wxConvLocal));
  778. sprintf(buf2,"%s/!wordcounts",buf);
  779. if((f_out = fopen(buf2,"w")) == NULL)
  780. return;
  781. for(j=0; j<ix; j++)
  782. {
  783. p = w_list[j];
  784. fprintf(f_out,"%5d %s\n",p->count,p->word);
  785. free(p);
  786. }
  787. fclose(f_out);
  788. } // end of Make WorkFreqList
  789. //******************************************************************************************************
  790. void ConvertToUtf8()
  791. {//=================
  792. // Convert a file from 8bit to UTF8, according to the current voice
  793. unsigned int c;
  794. int ix;
  795. FILE *f_in;
  796. FILE *f_out;
  797. char buf[80];
  798. wxString fname = wxFileSelector(_T("Convert file to UTF8"),wxString(path_home,wxConvLocal),
  799. _T(""),_T(""),_T("*"),wxOPEN);
  800. if(fname.IsEmpty())
  801. return;
  802. strcpy(buf,fname.mb_str(wxConvLocal));
  803. f_in = fopen(buf,"r");
  804. if(f_in == NULL)
  805. {
  806. wxLogError(_T("Can't read file: ")+fname);
  807. return;
  808. }
  809. strcat(buf,"_1");
  810. f_out = fopen(buf,"w");
  811. if(f_out == NULL)
  812. {
  813. wxLogError(_T("Can't create file: ")+wxString(buf,wxConvLocal));
  814. fclose(f_in);
  815. return;
  816. }
  817. while(!feof(f_in))
  818. {
  819. c = fgetc(f_in);
  820. if(c >= 0xa0)
  821. c = translator->charset_a0[c-0xa0];
  822. ix = utf8_out(c,buf);
  823. fwrite(buf,ix,1,f_out);
  824. }
  825. fclose(f_in);
  826. fclose(f_out);
  827. } // end of ConvertToItf8
  828. //#define calcspeedtab
  829. #ifdef calcspeedtab
  830. // used to set up the presets in the speed_lookup table
  831. // interpolate between a set of measured wpm values
  832. void SetSpeedTab(void)
  833. {//===================
  834. #define N_WPM 19
  835. // Interpolation table to translate from words-per-minute to internal speed
  836. // words-per-minute values (measured)
  837. static float wpm1[N_WPM] =
  838. {0, 82, 96, 108, 124, 134, 147, 162, 174, 189, 224, 259, 273, 289, 307, 326, 346, 361, 370 };
  839. // corresponding internal speed values
  840. static float wpm2[N_WPM] =
  841. {0,253,200, 170, 140, 125, 110, 95, 85, 75, 55, 40, 35, 30, 25, 20, 15, 10, 5 };
  842. unsigned char speed_lookup[290];
  843. unsigned int ix;
  844. float x;
  845. int speed_wpm;
  846. FILE *f;
  847. // convert from word-per-minute to internal speed code
  848. for(speed_wpm=80; speed_wpm<370; speed_wpm++)
  849. {
  850. for(ix=2; ix<N_WPM-2; ix++)
  851. {
  852. if(speed_wpm < wpm1[ix])
  853. break;
  854. }
  855. x = polint(&wpm1[ix-1], &wpm2[ix-1], 3, speed_wpm);
  856. speed_lookup[speed_wpm-80] = (unsigned char)x;
  857. }
  858. f = fopen("speed_lookup","w");
  859. if(f == NULL) return;
  860. for(ix=0; ix<sizeof(speed_lookup); ix++)
  861. {
  862. fprintf(f,"%4d,",speed_lookup[ix]);
  863. if((ix % 5) == 4)
  864. fprintf(f,"\t//%4d\n\t",(ix / 5)*5 + 80);
  865. }
  866. fclose(f);
  867. } // end of SetSpeedTab
  868. #endif
  869. //#define xcharset
  870. #ifdef xcharset
  871. #include "iconv.h"
  872. void CharsetToUnicode(const char *charset)
  873. {//=======================================
  874. // write a 8bit charset to unicode translation table to file
  875. // charset: eg. "ISO-8859-1"
  876. iconv_t cd;
  877. unsigned char inbuf[4];
  878. size_t n_inbuf;
  879. unsigned char outbuf[12];
  880. size_t n_outbuf;
  881. int n;
  882. int ix;
  883. int x, y;
  884. FILE *f;
  885. char *p_inbuf;
  886. char *p_outbuf;
  887. f = fopen("/home/jsd1/tmp1/unicode1","a");
  888. cd = iconv_open("WCHAR_T",charset);
  889. if (cd == (iconv_t) -1)
  890. {
  891. fprintf(stderr,"Error - iconv_open\n");
  892. return;
  893. }
  894. fprintf(f,"towlower_tab\n ");
  895. for(ix=0x80; ix<=0x241; ix++)
  896. {
  897. y = 0;
  898. if(iswalpha(ix))
  899. {
  900. x = towlower(ix);
  901. if(x == ix)
  902. y = 0xff;
  903. else
  904. y = x - ix;
  905. }
  906. if((y == 0xff) || (y < 0))
  907. fprintf(f,"0xff,"); // ignore the 5 obscure cases where uc > lc
  908. else
  909. {
  910. fprintf(f,"%4d,",y);
  911. }
  912. if((ix&15)==15)
  913. fprintf(f," // %x\n ",ix & ~15);
  914. }
  915. fprintf(f,"\n%s\n ",charset);
  916. for(ix=0x80; ix<0x100; ix++)
  917. {
  918. inbuf[0] = ix;
  919. inbuf[1] = 0;
  920. inbuf[2] = 0;
  921. outbuf[0] = 0;
  922. outbuf[1] = 0;
  923. n_inbuf = 2;
  924. n_outbuf = sizeof(outbuf);
  925. p_inbuf = (char *)inbuf;
  926. p_outbuf = (char *)outbuf;
  927. n = iconv(cd, &p_inbuf, &n_inbuf, &p_outbuf, &n_outbuf);
  928. fprintf(f,"0x%.2x%.2x, ",outbuf[1],outbuf[0]);
  929. if((ix&7)==7)
  930. fprintf(f,"// %.2x\n ",ix & ~7);
  931. }
  932. fclose(f);
  933. iconv_close(cd);
  934. }
  935. #endif
  936. #ifdef deleted
  937. void Test2()
  938. {
  939. //
  940. char buf[120];
  941. FILE *f;
  942. FILE *f_out;
  943. unsigned char *p;
  944. f = fopen("/home/jsd1/tmp1/list","r");
  945. if(f == NULL) return;
  946. f_out = fopen("/home/jsd1/tmp1/list_out","w");
  947. if(f_out == NULL) return;
  948. while(!feof(f))
  949. {
  950. if(fgets(buf,sizeof(buf),f) == NULL)
  951. break;
  952. p = (unsigned char *)buf;
  953. while(*p > ' ') p++;
  954. *p = 0;
  955. fprintf(f_out,"%s . . .\n",buf);
  956. }
  957. fclose(f);
  958. fclose(f_out);
  959. }
  960. void Test3()
  961. {
  962. espeak_VOICE voicespec;
  963. espeak_VOICE *newvoice;
  964. int x;
  965. espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS,100,NULL,0);
  966. memset(&voicespec,0,sizeof(voicespec));
  967. voicespec.languages = "de";
  968. espeak_SetVoiceByProperties(&voicespec);
  969. newvoice = espeak_GetCurrentVoice();
  970. x = 1;
  971. }
  972. #endif
  973. char* text1[]=
  974. {
  975. "Hello World. This is the second sentence",
  976. "Testing"
  977. };
  978. void TestTest(int control)
  979. {//=======================
  980. FILE *f;
  981. unsigned int c;
  982. unsigned int ix=0;
  983. char textbuf[2000];
  984. espeak_VOICE voice;
  985. espeak_VOICE *voice2;
  986. //FindPhonemesUsed();
  987. //return;
  988. //CharsetToUnicode("ISO-8859-4");
  989. //CharsetToUnicode("ISCII");
  990. //return;
  991. if(control==2)
  992. {
  993. return;
  994. }
  995. memset(&voice,0,sizeof(voice));
  996. f = fopen("/home/jsd1/speechdata/text/test","r");
  997. if(f==NULL)
  998. return;
  999. while(!feof(f) && (ix < sizeof(textbuf)-2))
  1000. {
  1001. c = fgetc(f);
  1002. if(!feof(f))
  1003. textbuf[ix++] = c;
  1004. }
  1005. textbuf[ix] = 0;
  1006. fclose(f);
  1007. OpenWaveFile3("/home/jsd1/speechdata/text/test.wav",samplerate);
  1008. f_events = fopen("/home/jsd1/speechdata/text/events","w");
  1009. fprintf(f_events,"Audio Text Length Type Id\n");
  1010. espeak_Initialize(AUDIO_OUTPUT_RETRIEVAL,0,NULL,1);
  1011. espeak_SetSynthCallback(TestSynthCallback);
  1012. unsigned int unique_identifier=0;
  1013. int index=0;
  1014. espeak_Synth(text1[index], strlen(text1[index])+1, 0, POS_CHARACTER, 0, espeakSSML|espeakCHARS_UTF8, &unique_identifier, NULL);
  1015. espeak_SetParameter(espeakPUNCTUATION, 1, 0);
  1016. espeak_Synchronize();
  1017. // espeak_Cancel();
  1018. espeak_SetParameter(espeakPUNCTUATION, 1, 0);
  1019. index++;
  1020. espeak_Synth(text1[index], strlen(text1[index])+1, 0, POS_CHARACTER, 0, espeakSSML|espeakCHARS_UTF8, &unique_identifier, NULL);
  1021. }