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

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