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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  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',PH('A',':'),'*',PH('_','!'),PH('_','|'),PH('O','I'),PH('Y',':'),PH('p','F'),0};
  167. const char *replace_ph2[] = {NULL,NULL, "9","@r","Y", "a:", "r", "?", "?", "OY", "2:", "pf" ,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 > 3)
  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. char *p;
  232. int stress;
  233. int count=0;
  234. int start;
  235. int matched=0;
  236. char buf[120];
  237. char word[80];
  238. char word2[80];
  239. char type[80];
  240. char pronounce[80];
  241. char pronounce2[80];
  242. char phonemes[80];
  243. WORD_TAB winfo;
  244. static char *vowels = "aeiouyAEIOU29@";
  245. wxString fname = wxFileSelector(_T("German Lexicon"),wxString(path_home,wxConvLocal),
  246. _T(""),_T(""),_T("*"),wxOPEN);
  247. strcpy(buf,fname.mb_str(wxConvLocal));
  248. if((f_in = fopen(buf,"r")) == NULL)
  249. {
  250. wxLogError(_T("Can't read file ")+fname);
  251. return;
  252. }
  253. if((f_out = fopen("compare_de","w")) == NULL)
  254. {
  255. wxLogError(_T("Can't write file "));
  256. return;
  257. }
  258. LoadVoice("de",0);
  259. word2[0] = ' ';
  260. while(!feof(f_in))
  261. {
  262. count++;
  263. if(fgets(buf,sizeof(buf),f_in) == NULL)
  264. break;
  265. sscanf(buf,"%s\t%s\t%s",word,type,pronounce);
  266. // convert word to lower-case
  267. for(ix=0, p=&word2[1];;)
  268. {
  269. ix += utf8_in(&c,&word[ix],0);
  270. c = towlower(c);
  271. p += utf8_out(c,p);
  272. if(c == 0)
  273. break;
  274. }
  275. strcpy(word,&word2[1]);
  276. strcat(&word2[1]," ");
  277. // remove | syllable boundaries
  278. stress=0;
  279. start=1;
  280. for(ix=0, p=pronounce2;;ix++)
  281. {
  282. c = pronounce[ix];
  283. if(c == '\'')
  284. {
  285. stress=4;
  286. continue;
  287. }
  288. if(c == ',')
  289. {
  290. stress=3;
  291. continue;
  292. }
  293. if(c == '|')
  294. continue;
  295. if((c == '?') && start)
  296. continue; // omit initial [?]
  297. start =0;
  298. if(stress && (strchr(vowels,c) != NULL))
  299. {
  300. if(stress == 4)
  301. *p++ = '\'';
  302. if(stress == 3)
  303. *p++ = ',';
  304. stress = 0;
  305. }
  306. *p++ = c;
  307. if(c == 0)
  308. break;
  309. if(strchr("eiouy",c) && pronounce[ix+1] != ':')
  310. *p++ = ':'; // ensure [;] after these vowels
  311. }
  312. // translate
  313. memset(&winfo,0,sizeof(winfo));
  314. translator->TranslateWord(&word2[1],0,&winfo);
  315. DecodePhonemes2(translator->word_phonemes,phonemes); // also need to change some phoneme names
  316. if(strcmp(phonemes,pronounce2) == 0)
  317. {
  318. matched++;
  319. }
  320. else
  321. {
  322. if(strlen(word) < 8)
  323. strcat(word,"\t");
  324. fprintf(f_out,"%s\t%s\t%s\n",word,phonemes,pronounce2);
  325. }
  326. }
  327. fclose(f_in);
  328. fclose(f_out);
  329. wxLogStatus(_T("Completed, equal=%d different=%d"),matched,count-matched);
  330. }
  331. void Lexicon_Ru()
  332. {//==============
  333. // compare stress markings in Russian RuLex file with lookup in ru_rules
  334. int ix;
  335. char *p;
  336. int c;
  337. FILE *f_in;
  338. FILE *f_out;
  339. FILE *f_log;
  340. FILE *f_roots;
  341. PHONEME_TAB *ph;
  342. int vcount;
  343. int ru_stress;
  344. int max_stress;
  345. int max_stress_posn;
  346. int n_words=0;
  347. int n_wrong=0;
  348. int wlength;
  349. int input_length;
  350. int sfx;
  351. char *suffix;
  352. int wlen;
  353. int len;
  354. int check_root;
  355. WORD_TAB winfo;
  356. char word[80];
  357. char word2[80];
  358. int counts[20][20][10];
  359. char phonemes[N_WORD_PHONEMES];
  360. char buf[100];
  361. // KOI8-R codes for Russian vowels
  362. static char vowels[] = {0xa3,0xc0,0xc1,0xc5,0xc9,0xcf,0xd1,0xd5,0xd9,0xdc,0};
  363. typedef struct {
  364. char *suffix;
  365. int syllables;
  366. } SUFFIX;
  367. static SUFFIX suffixes[] = {
  368. {NULL,0},
  369. {"ичу",2},
  370. {"ского",2},
  371. {"ская",2},
  372. {"ски",1},
  373. {"ские",2},
  374. {"ский",1},
  375. {"ским",1},
  376. {"ское",2},
  377. {"ской",1},
  378. {"ском",1},
  379. {"скую",2},
  380. {"а",1},
  381. {"е",1},
  382. {"и",1},
  383. {NULL,0}};
  384. memset(counts,0,sizeof(counts));
  385. if(gui_flag)
  386. {
  387. wxString fname = wxFileSelector(_T("Read lexicon.dict"),path_dictsource,
  388. _T(""),_T(""),_T("*"),wxOPEN);
  389. if(fname.IsEmpty())
  390. return;
  391. strcpy(buf,fname.mb_str(wxConvLocal));
  392. }
  393. else
  394. {
  395. strcpy(buf,"lexicon.dict");
  396. }
  397. if((f_in = fopen(buf,"r")) == NULL)
  398. {
  399. if(gui_flag)
  400. wxLogError(_T("Can't read file: ") + wxString(buf,wxConvLocal));
  401. else
  402. fprintf(stderr,"Can't read file: %s\n",buf);
  403. return;
  404. }
  405. input_length = GetFileLength(buf);
  406. sprintf(buf,"%s%c%s",path_dsource,PATHSEP,"ru_listx_1");
  407. if((f_out = fopen(buf,"w")) == NULL)
  408. {
  409. wxLogError(_T("Can't write to: ")+wxString(buf,wxConvLocal));
  410. fclose(f_in);
  411. return;
  412. }
  413. sprintf(buf,"%s%c%s",path_dsource,PATHSEP,"ru_log");
  414. f_log = fopen(buf,"w");
  415. sprintf(buf,"%s%c%s",path_dsource,PATHSEP,"ru_roots_1");
  416. f_roots = fopen(buf,"w");
  417. LoadVoice("ru",0);
  418. if(gui_flag)
  419. progress = new wxProgressDialog(_T("Lexicon"),_T(""),input_length);
  420. else
  421. fprintf(stderr,"Processing lexicon.dict\n");
  422. for(;;)
  423. {
  424. if(((n_words & 0x3ff) == 0) && gui_flag)
  425. {
  426. progress->Update(ftell(f_in));
  427. }
  428. if(fgets(buf,sizeof(buf),f_in) == NULL)
  429. break;
  430. if(isspace2(buf[0]))
  431. continue;
  432. // convert word from KOI8-R to UTF8
  433. p = buf;
  434. ix = 0;
  435. wlength = 0;
  436. p_unicode = unicode;
  437. while(!isspace2(c = (*p++ & 0xff)))
  438. {
  439. if(c >= 0xa0)
  440. {
  441. c = KOI8_R[c-0xa0];
  442. *p_unicode++ = c;
  443. }
  444. wlength++;
  445. ix += utf8_out(c,&word[ix]);
  446. }
  447. word[ix] = 0;
  448. *p_unicode=0;
  449. sprintf(word2," %s ",word);
  450. // find the marked stress position
  451. vcount = 0;
  452. ru_stress = 0;
  453. while(*p == ' ') p++;
  454. while((c = (*p++ & 0xff)) != '\n')
  455. {
  456. if(c == '+')
  457. {
  458. ru_stress = vcount;
  459. break;
  460. }
  461. if(strchr(vowels,c) != NULL)
  462. {
  463. vcount++;
  464. }
  465. }
  466. // translate
  467. memset(&winfo,0,sizeof(winfo));
  468. translator->TranslateWord(&word2[1],0,&winfo);
  469. DecodePhonemes(translator->word_phonemes,phonemes);
  470. // find the stress position in the translation
  471. max_stress = 0;
  472. max_stress_posn = -1;
  473. vcount = 0;
  474. check_root = 0;
  475. ph = phoneme_tab[phonPAUSE];
  476. for(p=translator->word_phonemes; *p != 0; p++)
  477. {
  478. ph = phoneme_tab[(unsigned int)*p];
  479. if(ph == NULL)
  480. continue;
  481. if(ph->type == phVOWEL)
  482. vcount++;
  483. if(ph->type == phSTRESS)
  484. {
  485. if(ph->std_length > max_stress)
  486. {
  487. max_stress = ph->std_length;
  488. max_stress_posn = vcount+1;
  489. }
  490. }
  491. }
  492. n_words++;
  493. if(ru_stress > vcount)
  494. {
  495. if(f_log != NULL)
  496. {
  497. fprintf(f_log,"%s\t $%d\t // %s\n",word,ru_stress,phonemes);
  498. }
  499. }
  500. else
  501. {
  502. counts[vcount][ru_stress][ph->type]++;
  503. if((vcount > 1) && (ru_stress != max_stress_posn))
  504. {
  505. n_wrong++;
  506. if((ru_stress==0) || (ru_stress > 7))
  507. fprintf(f_out,"// "); // we only have $1 to $7 to indicate stress position
  508. else
  509. check_root = 1;
  510. #define X_COMPACT
  511. fprintf(f_out,"%s",word);
  512. #ifdef X_COMPACT
  513. if(wlength < 8) fputc('\t',f_out);
  514. if(wlength < 16) fputc('\t',f_out);
  515. fprintf(f_out," $%d\n",ru_stress);
  516. #else
  517. while(wlength++ < 20)
  518. fputc(' ',f_out);
  519. fprintf(f_out," $%d //%d %s\n",ru_stress,max_stress_posn,phonemes);
  520. #endif
  521. //CharStats();
  522. }
  523. }
  524. if(check_root)
  525. {
  526. // does this word match any suffixes ?
  527. wlen = strlen(word);
  528. for(sfx=0;(suffix = suffixes[sfx].suffix) != NULL; sfx++)
  529. {
  530. len = strlen(suffix);
  531. if(len >= (wlen-2))
  532. continue;
  533. if(ru_stress > (vcount - suffixes[sfx].syllables))
  534. continue;
  535. if(strcmp(suffix,&word[wlen-len])==0)
  536. {
  537. strcpy(word2,word);
  538. word2[wlen-len] = 0;
  539. // fprintf(f_roots,"%s\t $%d\t\\ %s\n",word2,ru_stress,suffix);
  540. fprintf(f_roots,"%s\t $%d\n",word2,ru_stress);
  541. }
  542. }
  543. }
  544. }
  545. fclose(f_in);
  546. fclose(f_out);
  547. fclose(f_roots);
  548. sprintf(buf,"Lexicon: Total %d OK %d wrong %d",n_words,n_words-n_wrong,n_wrong);
  549. if(gui_flag)
  550. {
  551. delete progress;
  552. wxLogStatus(wxString(buf,wxConvLocal));
  553. }
  554. else
  555. {
  556. fprintf(stderr,"%s\n",buf);
  557. }
  558. if(f_log != NULL)
  559. {
  560. #ifdef deleted
  561. // list tables of frequency of stress position for words of different syllable lengths
  562. int j,k;
  563. for(ix=0; ix<12; ix++)
  564. {
  565. fprintf(f_log,"%2d syl: ",ix);
  566. for(k=0; k<10; k++)
  567. {
  568. fprintf(f_log," %2d :",k);
  569. for(j=0; j<10; j++)
  570. {
  571. fprintf(f_log,"%6d ",counts[ix][j][k]);
  572. }
  573. fprintf(f_log,"\n");
  574. }
  575. fprintf(f_log,"\n\n");
  576. }
  577. #endif
  578. fclose(f_log);
  579. }
  580. } // end of Lexicon_Ru
  581. void CompareLexicon(int id)
  582. {//========================
  583. switch(id)
  584. {
  585. case MENU_LEXICON_RU:
  586. Lexicon_Ru();
  587. break;
  588. case MENU_LEXICON_DE:
  589. Lexicon_De();
  590. break;
  591. }
  592. } // end of CompareLexicon
  593. //******************************************************************************************************
  594. extern int HashDictionary(const char *string);
  595. static int n_words;
  596. struct wcount {
  597. struct wcount *link;
  598. int count;
  599. char *word;
  600. };
  601. static int wfreq_sorter(wcount **p1, wcount **p2)
  602. {//==============================================
  603. int x;
  604. wcount *a, *b;
  605. a = *p1;
  606. b = *p2;
  607. if((x = b->count - a->count) != 0)
  608. return(x);
  609. return(strcmp(a->word,b->word));
  610. }
  611. static void wfreq_add(const char *word, wcount **hashtab)
  612. {//======================================================
  613. wcount *p;
  614. wcount **p2;
  615. int len;
  616. int hash;
  617. hash = HashDictionary(word);
  618. p2 = &hashtab[hash];
  619. p = *p2;
  620. while(p != NULL)
  621. {
  622. if(strcmp(p->word,word)==0)
  623. {
  624. p->count++;
  625. return;
  626. }
  627. p2 = &p->link;
  628. p = *p2;
  629. }
  630. // word not found, add it to the list
  631. len = strlen(word) + 1;
  632. if((p = (wcount *)malloc(sizeof(wcount)+len)) == NULL)
  633. return;
  634. p->count = 1;
  635. p->link = NULL;
  636. p->word = (char *)p + sizeof(wcount);
  637. strcpy(p->word,word);
  638. *p2 = p;
  639. n_words++;
  640. }
  641. void CountWordFreq(wxString path, wcount **hashtab)
  642. {//================================================
  643. // Count the occurances of words in this file
  644. FILE *f_in;
  645. unsigned char c;
  646. int wc;
  647. unsigned int ix, j, k;
  648. int n_chars;
  649. char buf[80];
  650. char wbuf[80];
  651. if((f_in = fopen(path.mb_str(wxConvLocal),"rb")) == NULL)
  652. return;
  653. while(!feof(f_in))
  654. {
  655. while((c = fgetc(f_in)) < 'A')
  656. {
  657. // skip leading spaces, numbers, etc
  658. if(feof(f_in)) break;
  659. }
  660. // read utf8 bytes until a space, number or punctuation
  661. ix = 0;
  662. while(!feof(f_in) && (c >= 'A') && (ix < sizeof(buf)-1))
  663. {
  664. buf[ix++] = c;
  665. c = fgetc(f_in);
  666. }
  667. buf[ix++] = 0;
  668. buf[ix] = 0;
  669. // the buf may contain non-alphabetic characters
  670. j = 0;
  671. n_chars = 0;
  672. for(k=0; k<ix; )
  673. {
  674. k += utf8_in(&wc,&buf[k],0);
  675. wc = towlower(wc); // convert to lower case
  676. if(iswalpha(wc))
  677. {
  678. j += utf8_out(wc,&wbuf[j]);
  679. n_chars++;
  680. }
  681. else
  682. {
  683. wbuf[j] = 0;
  684. if(n_chars > 2)
  685. {
  686. wfreq_add(wbuf,hashtab);
  687. }
  688. j = 0;
  689. n_chars = 0;
  690. }
  691. }
  692. }
  693. fclose(f_in);
  694. } // end of CountWordFreq
  695. void MakeWordFreqList()
  696. {//====================
  697. // Read text files from a specified directory and make a list of the most frequently occuring words.
  698. struct wcount *whashtab[N_HASH_DICT];
  699. wcount **w_list;
  700. int ix;
  701. int j;
  702. int hash;
  703. wcount *p;
  704. FILE *f_out;
  705. char buf[200];
  706. char buf2[200];
  707. wxString dir = wxDirSelector(_T("Directory of text files"),path_speaktext);
  708. if(dir.IsEmpty()) return;
  709. memset(whashtab,0,sizeof(whashtab));
  710. wxString path = wxFindFirstFile(dir+_T("/*"),wxFILE);
  711. while (!path.empty())
  712. {
  713. if(path.AfterLast(PATHSEP) != _T("!wordcounts"))
  714. {
  715. CountWordFreq(path,whashtab);
  716. path = wxFindNextFile();
  717. }
  718. }
  719. // put all the words into a list and then sort it
  720. w_list = (wcount **)malloc(sizeof(wcount *) * n_words);
  721. ix = 0;
  722. for(hash=0; hash < N_HASH_DICT; hash++)
  723. {
  724. p = whashtab[hash];
  725. while((p != NULL) && (ix < n_words))
  726. {
  727. w_list[ix++] = p;
  728. p = p->link;
  729. }
  730. }
  731. qsort((void *)w_list,ix,sizeof(wcount *),(int(*)(const void *,const void *))wfreq_sorter);
  732. // write out the sorted list
  733. strcpy(buf,dir.mb_str(wxConvLocal));
  734. sprintf(buf2,"%s/!wordcounts",buf);
  735. if((f_out = fopen(buf2,"w")) == NULL)
  736. return;
  737. for(j=0; j<ix; j++)
  738. {
  739. p = w_list[j];
  740. fprintf(f_out,"%5d %s\n",p->count,p->word);
  741. free(p);
  742. }
  743. fclose(f_out);
  744. } // end of Make WorkFreqList
  745. //******************************************************************************************************
  746. void ConvertToUtf8()
  747. {//=================
  748. // Convert a file from 8bit to UTF8, according to the current voice
  749. unsigned int c;
  750. int ix;
  751. FILE *f_in;
  752. FILE *f_out;
  753. char buf[80];
  754. wxString fname = wxFileSelector(_T("Convert file to UTF8"),wxString(path_home,wxConvLocal),
  755. _T(""),_T(""),_T("*"),wxOPEN);
  756. if(fname.IsEmpty())
  757. return;
  758. strcpy(buf,fname.mb_str(wxConvLocal));
  759. f_in = fopen(buf,"r");
  760. if(f_in == NULL)
  761. {
  762. wxLogError(_T("Can't read file: ")+fname);
  763. return;
  764. }
  765. strcat(buf,"_1");
  766. f_out = fopen(buf,"w");
  767. if(f_out == NULL)
  768. {
  769. wxLogError(_T("Can't create file: ")+wxString(buf,wxConvLocal));
  770. fclose(f_in);
  771. return;
  772. }
  773. while(!feof(f_in))
  774. {
  775. c = fgetc(f_in);
  776. if(c >= 0xa0)
  777. c = translator->charset_a0[c-0xa0];
  778. ix = utf8_out(c,buf);
  779. fwrite(buf,ix,1,f_out);
  780. }
  781. fclose(f_in);
  782. fclose(f_out);
  783. } // end of ConvertToItf8
  784. //#define calcspeedtab
  785. #ifdef calcspeedtab
  786. // used to set up the presets in the speed_lookup table
  787. // interpolate between a set of measured wpm values
  788. void SetSpeedTab(void)
  789. {//===================
  790. #define N_WPM 19
  791. // Interpolation table to translate from words-per-minute to internal speed
  792. // words-per-minute values (measured)
  793. static float wpm1[N_WPM] =
  794. {0, 82, 96, 108, 124, 134, 147, 162, 174, 189, 224, 259, 273, 289, 307, 326, 346, 361, 370 };
  795. // corresponding internal speed values
  796. static float wpm2[N_WPM] =
  797. {0,253,200, 170, 140, 125, 110, 95, 85, 75, 55, 40, 35, 30, 25, 20, 15, 10, 5 };
  798. unsigned char speed_lookup[290];
  799. unsigned int ix;
  800. float x;
  801. int speed_wpm;
  802. FILE *f;
  803. // convert from word-per-minute to internal speed code
  804. for(speed_wpm=80; speed_wpm<370; speed_wpm++)
  805. {
  806. for(ix=2; ix<N_WPM-2; ix++)
  807. {
  808. if(speed_wpm < wpm1[ix])
  809. break;
  810. }
  811. x = polint(&wpm1[ix-1], &wpm2[ix-1], 3, speed_wpm);
  812. speed_lookup[speed_wpm-80] = (unsigned char)x;
  813. }
  814. f = fopen("speed_lookup","w");
  815. if(f == NULL) return;
  816. for(ix=0; ix<sizeof(speed_lookup); ix++)
  817. {
  818. fprintf(f,"%4d,",speed_lookup[ix]);
  819. if((ix % 5) == 4)
  820. fprintf(f,"\t//%4d\n\t",(ix / 5)*5 + 80);
  821. }
  822. fclose(f);
  823. } // end of SetSpeedTab
  824. #endif
  825. //#define xcharset
  826. #ifdef xcharset
  827. #include "iconv.h"
  828. void CharsetToUnicode(const char *charset)
  829. {//=======================================
  830. // write a 8bit charset to unicode translation table to file
  831. // charset: eg. "ISO-8859-1"
  832. iconv_t cd;
  833. unsigned char inbuf[4];
  834. size_t n_inbuf;
  835. unsigned char outbuf[12];
  836. size_t n_outbuf;
  837. int n;
  838. int ix;
  839. int x, y;
  840. FILE *f;
  841. char *p_inbuf;
  842. char *p_outbuf;
  843. f = fopen("/home/jsd1/tmp1/unicode1","a");
  844. cd = iconv_open("WCHAR_T",charset);
  845. if (cd == (iconv_t) -1)
  846. {
  847. fprintf(stderr,"Error - iconv_open\n");
  848. return;
  849. }
  850. fprintf(f,"towlower_tab\n ");
  851. for(ix=0x80; ix<=0x241; ix++)
  852. {
  853. y = 0;
  854. if(iswalpha(ix))
  855. {
  856. x = towlower(ix);
  857. if(x == ix)
  858. y = 0xff;
  859. else
  860. y = x - ix;
  861. }
  862. if((y == 0xff) || (y < 0))
  863. fprintf(f,"0xff,"); // ignore the 5 obscure cases where uc > lc
  864. else
  865. {
  866. fprintf(f,"%4d,",y);
  867. }
  868. if((ix&15)==15)
  869. fprintf(f," // %x\n ",ix & ~15);
  870. }
  871. fprintf(f,"\n%s\n ",charset);
  872. for(ix=0x80; ix<0x100; ix++)
  873. {
  874. inbuf[0] = ix;
  875. inbuf[1] = 0;
  876. inbuf[2] = 0;
  877. outbuf[0] = 0;
  878. outbuf[1] = 0;
  879. n_inbuf = 2;
  880. n_outbuf = sizeof(outbuf);
  881. p_inbuf = (char *)inbuf;
  882. p_outbuf = (char *)outbuf;
  883. n = iconv(cd, &p_inbuf, &n_inbuf, &p_outbuf, &n_outbuf);
  884. fprintf(f,"0x%.2x%.2x, ",outbuf[1],outbuf[0]);
  885. if((ix&7)==7)
  886. fprintf(f,"// %.2x\n ",ix & ~7);
  887. }
  888. fclose(f);
  889. iconv_close(cd);
  890. }
  891. #endif
  892. #ifdef deleted
  893. void Test2()
  894. {
  895. //
  896. char buf[120];
  897. FILE *f;
  898. FILE *f_out;
  899. unsigned char *p;
  900. f = fopen("/home/jsd1/tmp1/list","r");
  901. if(f == NULL) return;
  902. f_out = fopen("/home/jsd1/tmp1/list_out","w");
  903. if(f_out == NULL) return;
  904. while(!feof(f))
  905. {
  906. if(fgets(buf,sizeof(buf),f) == NULL)
  907. break;
  908. p = (unsigned char *)buf;
  909. while(*p > ' ') p++;
  910. *p = 0;
  911. fprintf(f_out,"%s . . .\n",buf);
  912. }
  913. fclose(f);
  914. fclose(f_out);
  915. }
  916. void Test3()
  917. {
  918. espeak_VOICE voicespec;
  919. espeak_VOICE *newvoice;
  920. int x;
  921. espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS,100,NULL,0);
  922. memset(&voicespec,0,sizeof(voicespec));
  923. voicespec.languages = "de";
  924. espeak_SetVoiceByProperties(&voicespec);
  925. newvoice = espeak_GetCurrentVoice();
  926. x = 1;
  927. }
  928. #endif
  929. char* text1[]=
  930. {
  931. "Hello World. This is the second sentence",
  932. "Testing"
  933. };
  934. void TestTest(int control)
  935. {//=======================
  936. FILE *f;
  937. unsigned int c;
  938. unsigned int ix=0;
  939. char textbuf[2000];
  940. espeak_VOICE voice;
  941. espeak_VOICE *voice2;
  942. //FindPhonemesUsed();
  943. //return;
  944. //CharsetToUnicode("ISO-8859-4");
  945. //CharsetToUnicode("ISCII");
  946. //return;
  947. if(control==2)
  948. {
  949. return;
  950. }
  951. memset(&voice,0,sizeof(voice));
  952. f = fopen("/home/jsd1/speechdata/text/test","r");
  953. if(f==NULL)
  954. return;
  955. while(!feof(f) && (ix < sizeof(textbuf)-2))
  956. {
  957. c = fgetc(f);
  958. if(!feof(f))
  959. textbuf[ix++] = c;
  960. }
  961. textbuf[ix] = 0;
  962. fclose(f);
  963. OpenWaveFile3("/home/jsd1/speechdata/text/test.wav",samplerate);
  964. f_events = fopen("/home/jsd1/speechdata/text/events","w");
  965. fprintf(f_events,"Audio Text Length Type Id\n");
  966. espeak_Initialize(AUDIO_OUTPUT_RETRIEVAL,0,NULL,1);
  967. espeak_SetSynthCallback(TestSynthCallback);
  968. unsigned int unique_identifier=0;
  969. int index=0;
  970. espeak_Synth(text1[index], strlen(text1[index])+1, 0, POS_CHARACTER, 0, espeakSSML|espeakCHARS_UTF8, &unique_identifier, NULL);
  971. espeak_SetParameter(espeakPUNCTUATION, 1, 0);
  972. espeak_Synchronize();
  973. // espeak_Cancel();
  974. espeak_SetParameter(espeakPUNCTUATION, 1, 0);
  975. index++;
  976. espeak_Synth(text1[index], strlen(text1[index])+1, 0, POS_CHARACTER, 0, espeakSSML|espeakCHARS_UTF8, &unique_identifier, NULL);
  977. }