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

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