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 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /***************************************************************************
  2. * Copyright (C) 2005 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, 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 "voice.h"
  24. #include "main.h"
  25. #include "synthesize.h"
  26. #include "translate.h"
  27. #include "prosodydisplay.h"
  28. #include "wx/font.h"
  29. #include "wx/textctrl.h"
  30. #include "wx/button.h"
  31. #include "wx/checkbox.h"
  32. #include "wx/filename.h"
  33. #include "wx/sound.h"
  34. #define T_SOURCE 501
  35. #define T_PHONETIC 502
  36. #define T_TRANSLATE 503
  37. #define T_PROCESS 504
  38. #define T_RULES 505
  39. TranslDlg *transldlg = NULL;
  40. extern char *speech_to_phonemes(char *data, char *phout);
  41. extern ProsodyDisplay *prosodycanvas;
  42. BEGIN_EVENT_TABLE(TranslDlg, wxPanel)
  43. EVT_BUTTON(T_TRANSLATE,TranslDlg::OnCommand)
  44. EVT_BUTTON(T_PROCESS,TranslDlg::OnCommand)
  45. EVT_BUTTON(T_RULES,TranslDlg::OnCommand)
  46. END_EVENT_TABLE()
  47. class IPATextCtrl : public wxTextCtrl
  48. {
  49. public:
  50. void OnKey(wxKeyEvent& event);
  51. IPATextCtrl(wxWindow *parent,wxWindowID id,const wxPoint& pos,const wxSize& size);
  52. DECLARE_EVENT_TABLE()
  53. };
  54. BEGIN_EVENT_TABLE(IPATextCtrl, wxTextCtrl)
  55. EVT_CHAR(IPATextCtrl::OnKey)
  56. END_EVENT_TABLE()
  57. // using Kirschenbaum scheme to enter IPA from the keyboard
  58. wchar_t ipa1[256] = {
  59. 0,0x250,32,32,32,32,32,32,32,32,0xa,0xb,0xc,0x26f,0x272,0x275,
  60. 32,32,0x25a,32,32,32,32,0x28d,32,32,32,32,32,32,32,32,
  61. 0x20,0x21,0x22,0x23,0x24,0x25,0x0e6,0x27,0x28,0x27e,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
  62. 0x252,0x31,0x32,0x25c,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x294,
  63. 0x259,0x251,0x3b2,0xe7,0xf0,0x25b,0x46,0x262,0x127,0x26a,0x25f,0x4b,0x26c,0x271,0x14b,0x254,
  64. 0x3a6,0x263,0x280,0x283,0x3b8,0x28a,0x28c,0x153,0x3c7,0xf8,0x292,0x32a,0x5c,0x5d,0x5e,0x5f,
  65. 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,
  66. 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x303,0x7f,
  67. 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,
  68. 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,
  69. 0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,
  70. 0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,
  71. 0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,
  72. 0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf,
  73. 0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef,
  74. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  75. };
  76. IPATextCtrl::IPATextCtrl(wxWindow *parent,wxWindowID id,const wxPoint& pos,const wxSize& size) :
  77. wxTextCtrl(parent,id,_T(""),pos,size,wxTE_MULTILINE)
  78. {
  79. wxTextAttr attr;
  80. wxFont font = wxFont(12,wxFONTFAMILY_ROMAN,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_LIGHT);
  81. attr.SetFont(font);
  82. SetDefaultStyle(attr);
  83. }
  84. void IPATextCtrl::OnKey(wxKeyEvent& event)
  85. {
  86. long key;
  87. wchar_t wbuf[3];
  88. key = event.m_keyCode;
  89. key = event.GetKeyCode();
  90. if(event.ControlDown())
  91. key = key & 0x1f;
  92. if(key==0xd && !event.ControlDown())
  93. event.Skip(); // ENTER
  94. else
  95. if(key != 8 && key < 0x7f)
  96. {
  97. wbuf[0] = ipa1[key];
  98. wbuf[1] = 0;
  99. WriteText(wxString(wbuf));
  100. }
  101. else
  102. event.Skip();
  103. }
  104. void PlayWavFile(const char *fname)
  105. {//================================
  106. char command[120];
  107. #ifdef PLATFORM_WINDOWS
  108. wxSound(wxString(fname,wxConvLocal)).Play(wxSOUND_SYNC);
  109. #else
  110. sprintf(command,"play %s",fname);
  111. if(system(command) == -1)
  112. {
  113. wxLogError(_T("Failed to run system command:\n\n"+wxString(command,wxConvLocal)));
  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 *fname)
  129. {//=================================
  130. int result;
  131. if((result = OpenWaveFile(fname, samplerate)) != 0)
  132. {
  133. wxLogStatus(_T("Can't write to WAV file: '"+wxString(fname,wxConvLocal))+_T("'"));
  134. }
  135. return(result);
  136. }
  137. void MakeWave2(PHONEME_LIST *p, int n_phonemes)
  138. {//============================================
  139. int result;
  140. char *fname_speech;
  141. int n_ph;
  142. n_ph = n_phonemes;
  143. fname_speech = WavFileName();
  144. OpenWaveFile2(fname_speech);
  145. Generate(p,&n_ph,0);
  146. for(;;)
  147. {
  148. result = WavegenFile();
  149. if(result != 0)
  150. break;
  151. Generate(p,&n_ph,1);
  152. }
  153. CloseWaveFile(samplerate);
  154. PlayWavFile(fname_speech);
  155. } // end of MakeWave2
  156. TranslDlg::TranslDlg(wxWindow *parent) : wxPanel(parent)
  157. {//=====================================================
  158. t_source = new wxTextCtrl(this,T_SOURCE,_T(""),wxPoint(0,4),
  159. wxSize(200,250),wxTE_MULTILINE);
  160. t_phonetic = new wxTextCtrl(this,T_PHONETIC,_T(""),wxPoint(0,262),
  161. wxSize(200,350),wxTE_MULTILINE);
  162. t_translate = new wxButton(this,T_TRANSLATE,_T("Translate"),wxPoint(4,620));
  163. t_translate = new wxButton(this,T_RULES,_T("Show Rules"),wxPoint(4,652));
  164. t_process = new wxButton(this,T_PROCESS,_T("Speak"),wxPoint(100,620));
  165. t_source->SetFocus();
  166. } // end of TransDlg::TransDlg
  167. void TranslDlg::SpeakFile(void)
  168. {//============================
  169. wxString file;
  170. wxFileName fname;
  171. FILE *f_text;
  172. char buf[200];
  173. fname = wxFileName(path_speaktext);
  174. file = wxFileSelector(_T("Text file to speak"),fname.GetPath(),fname.GetName(),_T(""),_T("*"),wxOPEN);
  175. if(file == wxEmptyString) return;
  176. strcpy(buf,file.mb_str(wxConvLocal));
  177. f_text = fopen(buf,"r");
  178. if(f_text == NULL)
  179. {
  180. wxLogError(_T("Failed to read: ")+file);
  181. return;
  182. }
  183. path_speaktext = file;
  184. InitText(0);
  185. SpeakNextClause(f_text,NULL,0);
  186. return;
  187. } // end of SpeakFile
  188. void TranslDlg::ReadVoice()
  189. {//========================
  190. wxString path;
  191. wxString filename;
  192. char fname[sizeof(path_home)+10];
  193. sprintf(fname,"%s/voices",path_home);
  194. path = wxFileSelector(_T("Load voice"),wxString(fname,wxConvLocal),_T(""),_T(""),_T("*"),wxOPEN);
  195. if(path.IsEmpty())
  196. return;
  197. filename = path.Mid(strlen(fname)+1);
  198. strcpy(fname,filename.mb_str(wxConvLocal));
  199. if(SetVoiceByName(fname) != EE_OK)
  200. {
  201. wxLogError(_T("Failed to load voice data"));
  202. }
  203. WavegenSetVoice(voice);
  204. }
  205. void TranslDlg::OnCommand(wxCommandEvent& event)
  206. {//=============================================
  207. #define N_PH_LIST N_PHONEME_LIST
  208. void *vp;
  209. char buf[500];
  210. char phon_out[N_PH_LIST*2];
  211. int clause_tone;
  212. int clause_count;
  213. FILE *f;
  214. char fname_temp[100];
  215. static int n_ph_list;
  216. static PHONEME_LIST ph_list[N_PH_LIST+1];
  217. option_phonemes = 0;
  218. switch(event.GetId())
  219. {
  220. case T_RULES:
  221. case MENU_SPEAK_RULES:
  222. option_phonemes = 2;
  223. strcpy(fname_temp,tmpnam(NULL));
  224. if((f = fopen(fname_temp,"w+")) != NULL)
  225. {
  226. f_trans = f; // write translation rule trace to a temp file
  227. }
  228. case T_TRANSLATE:
  229. case MENU_SPEAK_TRANSLATE:
  230. SpeakNextClause(NULL,NULL,2); // stop speaking file
  231. strncpy0(buf,t_source->GetValue().mb_str(wxConvUTF8),sizeof(buf));
  232. phon_out[0] = 0;
  233. n_ph_list = 0;
  234. clause_count = 0;
  235. vp = buf;
  236. InitText(0);
  237. while((vp != NULL) && (n_ph_list < N_PH_LIST))
  238. {
  239. vp = translator->TranslateClause(NULL,vp,&clause_tone,NULL);
  240. if(clause_count++ > 0)
  241. strcat(phon_out," ||");
  242. strcat(phon_out,translator->phon_out);
  243. t_phonetic->SetValue(wxString(translator->phon_out,wxConvLocal));
  244. translator->CalcPitches(clause_tone);
  245. translator->CalcLengths();
  246. if((n_ph_list + n_phoneme_list) >= N_PH_LIST)
  247. {
  248. n_phoneme_list = N_PH_LIST - n_ph_list - n_phoneme_list;
  249. }
  250. memcpy(&ph_list[n_ph_list],phoneme_list,sizeof(PHONEME_LIST)*n_phoneme_list);
  251. n_ph_list += n_phoneme_list;
  252. }
  253. t_phonetic->Clear();
  254. if(option_phonemes == 2)
  255. {
  256. option_phonemes=0;
  257. rewind(f_trans);
  258. while(fgets(buf,sizeof(buf),f_trans) != NULL)
  259. {
  260. t_phonetic->AppendText(wxString(buf,wxConvUTF8));
  261. }
  262. t_phonetic->AppendText(_T("---\n"));
  263. fclose(f_trans);
  264. remove(fname_temp);
  265. }
  266. t_phonetic->AppendText(wxString(phon_out,wxConvLocal));
  267. break;
  268. case T_PROCESS:
  269. case MENU_SPEAK_TEXT:
  270. if(prosodycanvas != NULL)
  271. {
  272. prosodycanvas->LayoutData(ph_list,n_ph_list);
  273. }
  274. else
  275. {
  276. myframe->OnProsody(event);
  277. }
  278. option_phoneme_events = 1;
  279. option_log_frames = 1;
  280. MakeWave2(ph_list,n_ph_list);
  281. option_log_frames = 0;
  282. break;
  283. }
  284. } // end of TranslDlg::OnCommand