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.

transldlg.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2013 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, see: *
  17. * <http://www.gnu.org/licenses/>. *
  18. ***************************************************************************/
  19. #include "wx/wx.h"
  20. #include "speak_lib.h"
  21. #include "speech.h"
  22. #include "phoneme.h"
  23. #include "main.h"
  24. #include "synthesize.h"
  25. #include "voice.h"
  26. #include "translate.h"
  27. #include "prosodydisplay.h"
  28. #ifdef PLATFORM_POSIX
  29. #include <unistd.h>
  30. #endif
  31. #include "wx/font.h"
  32. #include "wx/textctrl.h"
  33. #include "wx/button.h"
  34. #include "wx/checkbox.h"
  35. #include "wx/filename.h"
  36. #include "wx/sound.h"
  37. #define T_SOURCE 501
  38. #define T_PHONETIC 502
  39. #define T_TRANSLATE 503
  40. #define T_PROCESS 504
  41. #define T_RULES 505
  42. #define T_TRANSLATE_IPA 506
  43. TranslDlg *transldlg = NULL;
  44. extern char *speech_to_phonemes(char *data, char *phout);
  45. extern ProsodyDisplay *prosodycanvas;
  46. extern void Write4Bytes(FILE *f, int value);
  47. BEGIN_EVENT_TABLE(TranslDlg, wxPanel)
  48. EVT_BUTTON(T_TRANSLATE,TranslDlg::OnCommand)
  49. EVT_BUTTON(T_PROCESS,TranslDlg::OnCommand)
  50. EVT_BUTTON(T_RULES,TranslDlg::OnCommand)
  51. EVT_BUTTON(T_TRANSLATE_IPA,TranslDlg::OnCommand)
  52. END_EVENT_TABLE()
  53. FILE *f_wave = NULL;
  54. wxFont font_phonetic;
  55. wxTextAttr style_phonetic;
  56. wxFont font_phonetic_large;
  57. wxTextAttr style_phonetic_large;
  58. #ifdef deleted
  59. class IPATextCtrl : public wxTextCtrl
  60. {
  61. public:
  62. void OnKey(wxKeyEvent& event);
  63. IPATextCtrl(wxWindow *parent,wxWindowID id,const wxPoint& pos,const wxSize& size);
  64. DECLARE_EVENT_TABLE()
  65. };
  66. BEGIN_EVENT_TABLE(IPATextCtrl, wxTextCtrl)
  67. EVT_CHAR(IPATextCtrl::OnKey)
  68. END_EVENT_TABLE()
  69. IPATextCtrl::IPATextCtrl(wxWindow *parent,wxWindowID id,const wxPoint& pos,const wxSize& size) :
  70. wxTextCtrl(parent,id,_T(""),pos,size,wxTE_MULTILINE)
  71. {
  72. wxTextAttr attr;
  73. wxFont font = wxFont(12,wxFONTFAMILY_ROMAN,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_LIGHT);
  74. attr.SetFont(font);
  75. SetDefaultStyle(attr);
  76. }
  77. void IPATextCtrl::OnKey(wxKeyEvent& event)
  78. {
  79. long key;
  80. wchar_t wbuf[3];
  81. key = event.m_keyCode;
  82. key = event.GetKeyCode();
  83. if(event.ControlDown())
  84. key = key & 0x1f;
  85. if(key==0xd && !event.ControlDown())
  86. event.Skip(); // ENTER
  87. else
  88. if(key != 8 && key < 0x7f)
  89. {
  90. wbuf[0] = ipa1[key];
  91. wbuf[1] = 0;
  92. WriteText(wxString(wbuf));
  93. }
  94. else
  95. event.Skip();
  96. }
  97. #endif
  98. void PlayWavFile(const char *fname)
  99. {//================================
  100. char command[120];
  101. #ifdef PLATFORM_WINDOWS
  102. wxSound(wxString(fname,wxConvLocal)).Play(wxSOUND_SYNC);
  103. #else
  104. // wxSound(wxString(fname,wxConvLocal)).Play(wxSOUND_SYNC); // This gives a click at the start of each play
  105. sprintf(command,"aplay %s",fname);
  106. if(system(command) == -1)
  107. {
  108. sprintf(command,"play %s",fname);
  109. if(system(command) == -1)
  110. {
  111. wxLogError(_T("Failed to run system command:\n\n"+wxString(command,wxConvLocal)));
  112. }
  113. }
  114. #endif
  115. }
  116. char *WavFileName(void)
  117. {//====================
  118. static char f_speech[120];
  119. if(!wxDirExists(wxPathOnly(path_speech)))
  120. {
  121. path_speech = wxFileSelector(_T("Speech output file"),
  122. path_phsource,_T("speech.wav"),_T("*"),_T("*"),wxSAVE);
  123. }
  124. strcpy(f_speech,path_speech.mb_str(wxConvLocal));
  125. return(f_speech);
  126. }
  127. int OpenWaveFile2(const char *path)
  128. /*********************************/
  129. {
  130. // Set the length of 0x7fffffff for --stdout
  131. // This will be changed to the correct length for -w (write to file)
  132. static unsigned char wave_hdr[44] = {
  133. 'R','I','F','F',0,0,0,0,'W','A','V','E','f','m','t',' ',
  134. 0x10,0,0,0,1,0,1,0, 9,0x3d,0,0,0x12,0x7a,0,0,
  135. 2,0,0x10,0,'d','a','t','a', 0xff,0xff,0xff,0x7f};
  136. if(path == NULL)
  137. return(2);
  138. if(strcmp(path,"stdout")==0)
  139. f_wave = stdout;
  140. else
  141. f_wave = fopen(path,"wb");
  142. if(f_wave != NULL)
  143. {
  144. fwrite(wave_hdr,1,24,f_wave);
  145. Write4Bytes(f_wave,samplerate);
  146. Write4Bytes(f_wave,samplerate * 2);
  147. fwrite(&wave_hdr[32],1,12,f_wave);
  148. return(0);
  149. }
  150. wxLogStatus(_T("Can't write to WAV file: '"+wxString(path,wxConvLocal))+_T("'"));
  151. return(1);
  152. } // end of OpenWaveFile
  153. void CloseWaveFile2()
  154. /******************/
  155. {
  156. unsigned int pos;
  157. if((f_wave == NULL) || (f_wave == stdout))
  158. return;
  159. fflush(f_wave);
  160. pos = ftell(f_wave);
  161. fseek(f_wave,4,SEEK_SET);
  162. Write4Bytes(f_wave,pos - 8);
  163. fseek(f_wave,40,SEEK_SET);
  164. Write4Bytes(f_wave,pos - 44);
  165. fclose(f_wave);
  166. f_wave = NULL;
  167. } // end of CloseWaveFile
  168. int WavegenFile2(void)
  169. {//==================
  170. int finished;
  171. unsigned char wav_outbuf[1024];
  172. out_ptr = out_start = wav_outbuf;
  173. out_end = wav_outbuf + sizeof(wav_outbuf);
  174. finished = WavegenFill(0);
  175. if(f_wave != NULL)
  176. {
  177. fwrite(wav_outbuf, 1, out_ptr-wav_outbuf, f_wave);
  178. }
  179. return(finished);
  180. } // end of WavegenFile
  181. void MakeWave2(PHONEME_LIST *p, int n_phonemes)
  182. {//============================================
  183. int result;
  184. char *fname_speech;
  185. int n_ph;
  186. #ifdef KLATT_TEST
  187. KlattInit();
  188. #endif
  189. n_ph = n_phonemes;
  190. fname_speech = WavFileName();
  191. OpenWaveFile2(fname_speech);
  192. Generate(p,&n_ph,0);
  193. for(;;)
  194. {
  195. result = WavegenFile2();
  196. if(result != 0)
  197. break;
  198. Generate(p,&n_ph,1);
  199. }
  200. CloseWaveFile2();
  201. PlayWavFile(fname_speech);
  202. } // end of MakeWave2
  203. TranslDlg::TranslDlg(wxWindow *parent) : wxPanel(parent)
  204. {//=====================================================
  205. int height;
  206. int width;
  207. int x,y;
  208. int font_size;
  209. int height_ph = 350;
  210. wxTextAttr attr;
  211. wxFont font = wxFont(12,wxFONTFAMILY_ROMAN,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_LIGHT,false,_T(""),wxFONTENCODING_SYSTEM);
  212. attr.SetFont(font);
  213. wxClientDisplayRect(&x,&y,&width, &height);
  214. #ifdef PLATFORM_WINDOWS
  215. if(height <= 768)
  216. height_ph = height - 416;
  217. #else
  218. if(height <= 800)
  219. height_ph = 280;
  220. #endif
  221. t_source = new wxTextCtrl(this,T_SOURCE,_T(""),wxPoint(0,4),
  222. wxSize(298,250),wxTE_MULTILINE,wxDefaultValidator,_T("Text input window"));
  223. t_source->SetDefaultStyle(attr);
  224. t_phonetic = new wxTextCtrl(this,T_PHONETIC,_T(""),wxPoint(0,262),
  225. wxSize(298,height_ph),wxTE_MULTILINE | wxTE_READONLY, wxDefaultValidator,_T("Phoneme translation window"));
  226. style_phonetic = t_phonetic->GetDefaultStyle();
  227. font_phonetic = style_phonetic.GetFont();
  228. font_size = font_phonetic.GetPointSize();
  229. font_phonetic_large = font_phonetic;
  230. style_phonetic_large = style_phonetic;
  231. //font_phonetic_large.SetFamily(wxFONTFAMILY_SWISS);
  232. font_phonetic_large.SetPointSize(font_size+1);
  233. style_phonetic_large.SetFont(font_phonetic_large);
  234. y = height_ph + 270;
  235. t_translate = new wxButton(this,T_TRANSLATE,_T("Translate"),wxPoint(4,y));
  236. t_translate = new wxButton(this,T_RULES,_T("Show Rules"),wxPoint(4,y+32));
  237. t_translate = new wxButton(this,T_TRANSLATE_IPA,_T("Show IPA"),wxPoint(100,y+32));
  238. t_process = new wxButton(this,T_PROCESS,_T("Speak"),wxPoint(100,y));
  239. t_source->SetFocus();
  240. } // end of TransDlg::TransDlg
  241. void TranslDlg::SpeakFile(void)
  242. {//============================
  243. wxString file;
  244. wxFileName fname;
  245. FILE *f_text;
  246. char buf[200];
  247. fname = wxFileName(path_speaktext);
  248. file = wxFileSelector(_T("Text file to speak"),fname.GetPath(),fname.GetName(),_T(""),_T("*"),wxOPEN);
  249. if(file == wxEmptyString) return;
  250. strcpy(buf,file.mb_str(wxConvLocal));
  251. f_text = fopen(buf,"r");
  252. if(f_text == NULL)
  253. {
  254. wxLogError(_T("Failed to read: ")+file);
  255. return;
  256. }
  257. path_speaktext = file;
  258. InitText(0);
  259. SpeakNextClause(f_text,NULL,0);
  260. return;
  261. } // end of SpeakFile
  262. void TranslDlg::ReadVoice(int variant)
  263. {//===================================
  264. wxString path;
  265. wxString filename;
  266. char *p;
  267. char vname[40];
  268. char fname[sizeof(path_home)+30];
  269. if(variant)
  270. {
  271. // remove variant from the previous voice name
  272. if((p = strchr(voice_name2,'+')) != NULL)
  273. *p = 0;
  274. sprintf(fname,"%s/voices/!v",path_home);
  275. path = wxFileSelector(_T("Load voice variant"),wxString(fname,wxConvLocal),_T(""),_T(""),_T("*"),wxOPEN);
  276. if(path.IsEmpty())
  277. {
  278. strcpy(fname,voice_name2);
  279. }
  280. else
  281. {
  282. filename = path.Mid(strlen(fname)+1);
  283. strcpy(vname,filename.mb_str(wxConvLocal));
  284. sprintf(fname,"%s+%s",voice_name2,vname);
  285. }
  286. }
  287. else
  288. {
  289. sprintf(fname,"%s/voices",path_home);
  290. path = wxFileSelector(_T("Load voice"),wxString(fname,wxConvLocal),_T(""),_T(""),_T("*"),wxOPEN);
  291. if(path.IsEmpty())
  292. return;
  293. filename = path.Mid(strlen(fname)+1);
  294. strcpy(fname,filename.mb_str(wxConvLocal));
  295. }
  296. if(SetVoiceByName(fname) != EE_OK)
  297. {
  298. wxLogError(_T("Failed to load voice data"));
  299. }
  300. else
  301. {
  302. strcpy(voice_name2,fname);
  303. }
  304. WavegenSetVoice(voice);
  305. }
  306. void TranslDlg::OnCommand(wxCommandEvent& event)
  307. {//=============================================
  308. #define N_PH_LIST N_PHONEME_LIST
  309. void *vp;
  310. int translate_text = 0;
  311. char buf[1000];
  312. char phon_out[N_PH_LIST*2];
  313. const char *phon_out2;
  314. int clause_tone;
  315. int clause_count;
  316. int use_ipa = 0;
  317. FILE *f;
  318. int fd_temp;
  319. char fname_temp[100];
  320. static int n_ph_list;
  321. static PHONEME_LIST ph_list[N_PH_LIST+1];
  322. if(translator==NULL)
  323. {
  324. wxLogError(_T("Voice not set"));
  325. return;
  326. }
  327. option_phonemes = 0;
  328. switch(event.GetId())
  329. {
  330. case T_RULES:
  331. case MENU_SPEAK_RULES:
  332. #ifdef PLATFORM_POSIX
  333. strcpy(fname_temp,"/tmp/espeakXXXXXX");
  334. if((fd_temp = mkstemp(fname_temp)) >= 0)
  335. {
  336. close(fd_temp);
  337. if((f = fopen(fname_temp,"w+")) != NULL)
  338. {
  339. f_trans = f; // write translation rule trace to a temp file
  340. }
  341. }
  342. #else
  343. strcpy(fname_temp,tmpnam(NULL));
  344. if((f = fopen(fname_temp,"w+")) != NULL)
  345. {
  346. f_trans = f; // write translation rule trace to a temp file
  347. }
  348. #endif
  349. t_phonetic->SetDefaultStyle(style_phonetic);
  350. translate_text = 2;
  351. break;
  352. case T_TRANSLATE:
  353. case MENU_SPEAK_TRANSLATE:
  354. t_phonetic->SetDefaultStyle(style_phonetic);
  355. translate_text = 1;
  356. break;
  357. case T_TRANSLATE_IPA:
  358. case MENU_SPEAK_IPA:
  359. t_phonetic->SetDefaultStyle(style_phonetic_large);
  360. translate_text = 3;
  361. use_ipa = 0x10;
  362. break;
  363. case T_PROCESS:
  364. case MENU_SPEAK_TEXT:
  365. myframe->OnProsody(event);
  366. prosodycanvas->LayoutData(ph_list,n_ph_list);
  367. myframe->Refresh();
  368. option_phoneme_events = espeakINITIALIZE_PHONEME_EVENTS;
  369. option_log_frames = 1;
  370. MakeWave2(ph_list,n_ph_list);
  371. option_log_frames = 0;
  372. break;
  373. }
  374. if(translate_text)
  375. {
  376. option_phonemes = translate_text;
  377. option_multibyte = espeakCHARS_AUTO;
  378. SpeakNextClause(NULL,NULL,2); // stop speaking file
  379. strncpy0(buf,t_source->GetValue().mb_str(wxConvUTF8),sizeof(buf));
  380. phon_out[0] = 0;
  381. n_ph_list = 0;
  382. clause_count = 0;
  383. vp = buf;
  384. InitText(0);
  385. while((vp != NULL) && (n_ph_list < N_PH_LIST))
  386. {
  387. vp = TranslateClause(translator,NULL,vp,&clause_tone,NULL);
  388. CalcPitches(translator,clause_tone);
  389. CalcLengths(translator);
  390. phon_out2 = GetTranslatedPhonemeString(use_ipa);
  391. if(clause_count++ > 0)
  392. strcat(phon_out," ||");
  393. strcat(phon_out, phon_out2);
  394. t_phonetic->SetValue(wxString(phon_out2, wxConvUTF8));
  395. if((n_ph_list + n_phoneme_list) >= N_PH_LIST)
  396. {
  397. n_phoneme_list = N_PH_LIST - n_ph_list;
  398. }
  399. memcpy(&ph_list[n_ph_list],phoneme_list,sizeof(PHONEME_LIST)*n_phoneme_list);
  400. n_ph_list += n_phoneme_list;
  401. }
  402. ph_list[N_PH_LIST].ph = NULL; // to recognize overrun off list (in Generate() )
  403. t_phonetic->Clear();
  404. if(option_phonemes == 2)
  405. {
  406. option_phonemes=0;
  407. rewind(f_trans);
  408. while(fgets(buf,sizeof(buf),f_trans) != NULL)
  409. {
  410. t_phonetic->AppendText(wxString(buf,wxConvUTF8));
  411. }
  412. t_phonetic->AppendText(_T("---\n"));
  413. if(f_trans != NULL)
  414. fclose(f_trans);
  415. remove(fname_temp);
  416. }
  417. t_phonetic->AppendText(wxString(phon_out,wxConvUTF8));
  418. }
  419. } // end of TranslDlg::OnCommand