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.

formantdlg.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. // For compilers that support precompilation, includes "wx/wx.h".
  20. #include "wx/wx.h"
  21. #include "wx/mdi.h"
  22. #include "speak_lib.h"
  23. #include "main.h"
  24. #include "speech.h"
  25. #include "phoneme.h"
  26. #include "synthesize.h"
  27. #include "voice.h"
  28. #include "spect.h"
  29. #include "wx/textctrl.h"
  30. #include "wx/checkbox.h"
  31. FormantDlg *formantdlg=NULL;
  32. ByteGraph *pitchgraph=NULL;
  33. BEGIN_EVENT_TABLE(FormantDlg, wxPanel)
  34. EVT_BUTTON(T_ZOOMOUT,FormantDlg::OnCommand)
  35. EVT_BUTTON(T_ZOOMIN,FormantDlg::OnCommand)
  36. EVT_SPINCTRL(-1,FormantDlg::OnSpin)
  37. // EVT_SPINCTRL(T_TIMESEQ,FormantDlg::OnSpin)
  38. // EVT_SPINCTRL(T_AMPFRAME,FormantDlg::OnSpin)
  39. // EVT_SPINCTRL(T_TIMEFRAME,FormantDlg::OnSpin)
  40. END_EVENT_TABLE()
  41. void FormantDlg::OnCommand(wxCommandEvent& event)
  42. {//=============================================
  43. int id;
  44. switch(id = event.GetId())
  45. {
  46. case T_ZOOMIN:
  47. case T_ZOOMOUT:
  48. currentcanvas->OnZoom(id);
  49. break;
  50. case T_AMPLITUDE:
  51. currentcanvas->RefreshDialogValues(0);
  52. break;
  53. case T_TIMEFRAME:
  54. case T_AMPFRAME:
  55. currentcanvas->RefreshDialogValues(1);
  56. break;
  57. }
  58. currentcanvas->SetFocus();
  59. }
  60. void FormantDlg::OnSpin(wxSpinEvent& event)
  61. {//=============================================
  62. int id;
  63. switch(id = event.GetId())
  64. {
  65. case T_AMPFRAME:
  66. currentcanvas->RefreshDialogValues(1);
  67. break;
  68. default:
  69. currentcanvas->RefreshDialogValues(0);
  70. break;
  71. }
  72. currentcanvas->SetFocus();
  73. }
  74. FormantDlg::FormantDlg(wxWindow *parent) : wxPanel(parent,-1,wxDefaultPosition,wxSize(400,1000))
  75. {//=============================================================================================
  76. int ix;
  77. int y;
  78. int xplace;
  79. int id;
  80. wxString string;
  81. y = 24;
  82. xplace = 28;
  83. id = 201;
  84. new wxStaticText(this,-1,_T("Formants"),wxPoint(4,5));
  85. t_lab[4] = new wxStaticText(this,-1,_T(" BW BWp Ap"),wxPoint(78,5),wxSize(300,20));
  86. for(ix=0; ix<N_PEAKS; ix++)
  87. {
  88. string.Printf(_T("%d"),ix);
  89. t_labpk[ix] = new wxStaticText(this,id++,string,
  90. wxPoint(xplace-22,y+1+24*ix));
  91. t_pkfreq[ix] = new wxTextCtrl(this,id++,_T(""),
  92. wxPoint(xplace,y+24*ix),wxSize(48,20),
  93. wxTE_CENTRE+wxTE_READONLY);
  94. t_pkheight[ix] = new wxTextCtrl(this,id++,_T(""),
  95. wxPoint(xplace+50,y+24*ix),wxSize(36,20),
  96. wxTE_CENTRE+wxTE_READONLY);
  97. t_pkwidth[ix] = new wxTextCtrl(this,id++,_T(""),
  98. wxPoint(xplace+88,y+24*ix),wxSize(48,20),
  99. wxTE_CENTRE+wxTE_READONLY);
  100. t_pkright[ix] = new wxTextCtrl(this,id++,_T(""),
  101. wxPoint(xplace+138,y+24*ix),wxSize(36,20),
  102. wxTE_CENTRE+wxTE_READONLY);
  103. if(ix>5) t_pkwidth[ix]->Hide();
  104. }
  105. y=240;
  106. t_timeframe = new wxSpinCtrl(this,T_TIMEFRAME,_T(""), wxPoint(6,y+0), wxSize(52,24), wxTE_CENTRE,0,500);
  107. t_orig_frame = new wxStaticText(this,-1,_T("mS"),wxPoint(57,y+8));
  108. t_ampframe = new wxSpinCtrl(this,T_AMPFRAME,_T(""), wxPoint(100,y+0), wxSize(52,24), wxTE_CENTRE,0,500);
  109. t_lab[3] = new wxStaticText(this,-1,_T("% amp - Frame"),wxPoint(151,y+8));
  110. s_klatt[KLATT_AV] = new wxSpinCtrl(this,T_AV,_T(""), wxPoint(6,y+32), wxSize(52,24), wxTE_CENTRE,0,500);
  111. t_klatt[KLATT_AV] = new wxStaticText(this,-1,_T("AV"),wxPoint(61,y+36));
  112. s_klatt[KLATT_AVp] = new wxSpinCtrl(this,T_AVP,_T(""), wxPoint(100,y+32), wxSize(52,24), wxTE_CENTRE,0,500);
  113. t_klatt[KLATT_AVp] = new wxStaticText(this,-1,_T("AVp"),wxPoint(155,y+36));
  114. s_klatt[KLATT_Fric] = new wxSpinCtrl(this,T_FRIC,_T(""), wxPoint(6,y+60), wxSize(52,24), wxTE_CENTRE,0,500);
  115. t_klatt[KLATT_Fric] = new wxStaticText(this,-1,_T("Fric"),wxPoint(61,y+64));
  116. s_klatt[KLATT_FricBP] = new wxSpinCtrl(this,T_FRICBP,_T(""), wxPoint(100,y+60), wxSize(52,24), wxTE_CENTRE,0,500);
  117. t_klatt[KLATT_FricBP] = new wxStaticText(this,-1,_T("FricBP"),wxPoint(155,y+64));
  118. s_klatt[KLATT_Aspr] = new wxSpinCtrl(this,T_ASPR,_T(""), wxPoint(6,y+88), wxSize(52,24), wxTE_CENTRE,0,500);
  119. t_klatt[KLATT_Aspr] = new wxStaticText(this,-1,_T("Aspr"),wxPoint(61,y+92));
  120. s_klatt[KLATT_Turb] = new wxSpinCtrl(this,T_TURB,_T(""), wxPoint(100,y+88), wxSize(52,24), wxTE_CENTRE,0,500);
  121. t_klatt[KLATT_Turb] = new wxStaticText(this,-1,_T("Turb"),wxPoint(155,y+92));
  122. s_klatt[KLATT_Skew] = new wxSpinCtrl(this,T_SKEW,_T(""), wxPoint(6,y+116), wxSize(52,24), wxTE_CENTRE,0,500);
  123. t_klatt[KLATT_Skew] = new wxStaticText(this,-1,_T("Skew"),wxPoint(61,y+120));
  124. s_klatt[KLATT_Tilt] = new wxSpinCtrl(this,T_TILT,_T(""), wxPoint(100,y+116), wxSize(52,24), wxTE_CENTRE,0,500);
  125. t_klatt[KLATT_Tilt] = new wxStaticText(this,-1,_T("Tilt"),wxPoint(155,y+120));
  126. s_klatt[KLATT_Kopen] = new wxSpinCtrl(this,T_KOPEN,_T(""), wxPoint(6,y+144), wxSize(52,24), wxTE_CENTRE,0,500);
  127. t_klatt[KLATT_Kopen] = new wxStaticText(this,-1,_T("kopen"),wxPoint(61,y+148));
  128. t_zoomout = new wxButton(this,T_ZOOMOUT,_T("Zoom-"),wxPoint(16,420));
  129. t_zoomin = new wxButton(this,T_ZOOMIN,_T("Zoom+"),wxPoint(106,420));
  130. y=468;
  131. t_amplitude = new wxSpinCtrl(this,T_AMPLITUDE,_T(""),
  132. wxPoint(6,y),wxSize(52,24),wxTE_CENTRE,0,y+130);
  133. t_lab[2] = new wxStaticText(this,-1,_T("% amp - Sequence"),wxPoint(61,y+4));
  134. // t_timeseq = new wxSpinCtrl(this,T_TIMESEQ,_T(""),
  135. // wxPoint(6,400),wxSize(52,24),wxTE_CENTRE,0,500);
  136. t_orig_seq = new wxStaticText(this,-1,_T("mS"),wxPoint(61,y+30));
  137. t_pitch = new wxStaticText(this,-1,_T(""),wxPoint(4,520),wxSize(192,24));
  138. pitchgraph = new ByteGraph(this,wxPoint(0,538),wxSize(200,140));
  139. pitchgraph->SetData(128,env_fall);
  140. pitchgraph->ShowSpectrum(1);
  141. pitchgraph->Show();
  142. }
  143. void FormantDlg::HideFields(int synth_type)
  144. {//=======================================
  145. int ix;
  146. static int prev_synth_type = -1;
  147. if(synth_type == prev_synth_type)
  148. return; // no change
  149. if(synth_type == 0)
  150. {
  151. for(ix=0; ix<9; ix++)
  152. {
  153. s_klatt[ix]->Hide();
  154. t_klatt[ix]->Hide();
  155. }
  156. for(ix=6; ix<9; ix++)
  157. {
  158. t_pkfreq[ix]->Show();
  159. t_pkheight[ix]->Show();
  160. t_pkwidth[ix]->Hide();
  161. t_pkright[ix]->Hide();
  162. }
  163. t_ampframe->Show();
  164. t_amplitude->Show();
  165. t_lab[2]->Show();
  166. t_lab[3]->Show();
  167. t_labpk[7]->Show();
  168. t_labpk[8]->Show();
  169. t_lab[4]->Hide();
  170. }
  171. else
  172. {
  173. for(ix=0; ix<9; ix++)
  174. {
  175. s_klatt[ix]->Show();
  176. t_klatt[ix]->Show();
  177. }
  178. for(ix=7; ix<9; ix++)
  179. {
  180. t_pkfreq[ix]->Hide();
  181. t_pkheight[ix]->Hide();
  182. t_pkwidth[ix]->Hide();
  183. t_pkright[ix]->Hide();
  184. }
  185. t_pkwidth[6]->Show(1);
  186. t_pkright[6]->Show(1);
  187. t_ampframe->Hide();
  188. t_amplitude->Hide();
  189. t_lab[2]->Hide();
  190. t_lab[3]->Hide();
  191. t_labpk[7]->Hide();
  192. t_labpk[8]->Hide();
  193. t_lab[4]->Show();
  194. }
  195. prev_synth_type = synth_type;
  196. }
  197. void FormantDlg::ShowFrame(SpectSeq *spectseq, int frame, int pk, int field)
  198. {//==============================================================
  199. int ix;
  200. SpectFrame *sf;
  201. wxString value;
  202. int original_mS;
  203. if(spectseq->frames == NULL)
  204. return;
  205. sf = spectseq->frames[frame];
  206. HideFields(spectseq->synthesizer_type);
  207. if(field == 0xff)
  208. {
  209. // indicate the selected peak
  210. // t_select_peak[pk]->SetValue(TRUE);
  211. }
  212. for(ix=0; ix<N_PEAKS; ix++)
  213. {
  214. if(field != 0xff && pk!=ix)
  215. continue;
  216. if(field == 0xff)
  217. {
  218. if(pk==ix)
  219. value.Printf(_T("%d="),ix);
  220. else
  221. value.Printf(_T("%d"),ix);
  222. t_labpk[ix]->SetLabel(value);
  223. }
  224. if(field & 1)
  225. {
  226. value.Printf(_T("%4d"),sf->peaks[ix].pkfreq);
  227. t_pkfreq[ix]->SetValue(value);
  228. }
  229. if(field & 2)
  230. {
  231. value.Printf(_T("%3d"),sf->peaks[ix].pkheight >> 6);
  232. t_pkheight[ix]->SetValue(value);
  233. }
  234. if(field & 4)
  235. {
  236. value.Printf(_T("%3d"),sf->peaks[ix].pkwidth);
  237. t_pkwidth[ix]->SetValue(value);
  238. value.Printf(_T("%3d"),sf->peaks[ix].pkright);
  239. t_pkright[ix]->SetValue(value);
  240. }
  241. }
  242. // find the time until the next keyframe
  243. SetSpinCtrl(t_timeframe,int(spectseq->GetFrameLength(frame,1,&original_mS)+0.5)); // round to nearest integer
  244. value.Printf(_T("%d mS"),original_mS);
  245. t_orig_frame->SetLabel(value);
  246. value.Printf(_T("%3d"),sf->amp_adjust);
  247. t_ampframe->SetValue(value);
  248. if(spectseq->synthesizer_type == 1)
  249. {
  250. for(ix=0; ix<9; ix++)
  251. {
  252. SetSpinCtrl(s_klatt[ix], sf->klatt_param[ix]);
  253. }
  254. }
  255. } // end of FormantDlg::ShowFrame
  256. BEGIN_EVENT_TABLE(ByteGraph,wxScrolledWindow)
  257. EVT_LEFT_DOWN(ByteGraph::OnMouse)
  258. END_EVENT_TABLE()
  259. static wxBrush BRUSH_FORMANT(wxColour(255,100,50),wxSOLID);
  260. static wxPen PEN_KEYFRAME(wxColour(255,220,100),2,wxSOLID);
  261. static wxPen PEN_KEYFORMANT(wxColour(0,0,0),2,wxSOLID);
  262. ByteGraph::ByteGraph(wxWindow *parent, const wxPoint& pos, const wxSize &size):
  263. wxScrolledWindow(parent,-1,pos,size,wxSUNKEN_BORDER)
  264. {//============================================================
  265. npoints = 0;
  266. show_spectrum = 0;
  267. } // end of ByteGraph::ByteGraph
  268. void ByteGraph::SetData(int nx, unsigned char *data)
  269. {//=================================================
  270. npoints = nx;
  271. graph = data;
  272. Refresh();
  273. } // end of ByteGraph::SetData
  274. void ByteGraph::ShowSpectrum(int yes)
  275. {//==================================
  276. show_spectrum = yes;
  277. }
  278. void ByteGraph::DrawSpectrum(wxDC& dc)
  279. {//===================================
  280. SpectSeq *seq;
  281. SpectFrame *sf;
  282. int ix;
  283. int x, y;
  284. int pk;
  285. double length;
  286. int numframes;
  287. int width, height;
  288. double xscale, yscale;
  289. spectrum_scale = 0;
  290. if(currentcanvas==NULL) return;
  291. if((seq = currentcanvas->spectseq) == NULL) return;
  292. numframes = seq->numframes;
  293. if(numframes == 0) return;
  294. GetClientSize(&width,&height);
  295. if(((length = seq->frames[numframes-1]->time)==0) && (numframes>1))
  296. length = seq->frames[numframes-2]->time;
  297. length = length - seq->frames[0]->time;
  298. yscale = height/3000.0;
  299. spectrum_scale = xscale = width/length;
  300. dc.SetBrush(BRUSH_FORMANT);
  301. dc.SetPen(*wxTRANSPARENT_PEN);
  302. for(ix=0; ix<numframes; ix++)
  303. {
  304. sf = seq->frames[ix];
  305. x = int(sf->time*xscale);
  306. if(sf->keyframe)
  307. {
  308. dc.SetPen(PEN_KEYFRAME);
  309. dc.DrawLine(x,0,x,height);
  310. }
  311. dc.SetPen(*wxTRANSPARENT_PEN);
  312. dc.DrawCircle(x,int(height-(sf->formants[1].freq-200)*yscale),3);
  313. dc.DrawCircle(x,int(height-(sf->formants[2].freq-200)*yscale),3);
  314. dc.DrawCircle(x,int(height-(sf->formants[3].freq-200)*yscale),3);
  315. if(sf->keyframe)
  316. {
  317. dc.SetPen(PEN_KEYFORMANT);
  318. for(pk=1; pk<6; pk++)
  319. {
  320. y = height-int((sf->peaks[pk].pkfreq-200)*yscale);
  321. dc.DrawLine(x-3,y,x+3,y);
  322. }
  323. }
  324. }
  325. }
  326. void ByteGraph::OnDraw(wxDC &dc)
  327. {//============================
  328. int x, y, x2, y2, ix;
  329. int width, height;
  330. double xscale, yscale;
  331. if(show_spectrum) DrawSpectrum(dc);
  332. if(npoints==0) return;
  333. GetClientSize(&width,&height);
  334. xscale = double(width) / npoints;
  335. yscale = double(height) / 256.0;
  336. if(npoints > 8)
  337. {
  338. // draw pitch profile
  339. dc.SetPen(*wxMEDIUM_GREY_PEN);
  340. x = 0;
  341. y = height - int(graph[0]*yscale);
  342. for(ix=1; ix<npoints; ix++)
  343. {
  344. dc.DrawLine(x,y,x2=int(ix*xscale),y2=height - int(graph[ix]*yscale));
  345. x = x2;
  346. y = y2;
  347. }
  348. }
  349. } // end of ByteGraph::OnDraw
  350. void ByteGraph::OnMouse(wxMouseEvent& event)
  351. {//========================================
  352. int ix;
  353. SpectSeq *seq;
  354. double time;
  355. double t0, t1;
  356. double t2=0;
  357. if(currentcanvas==NULL) return;
  358. if((seq = currentcanvas->spectseq) == NULL) return;
  359. wxClientDC dc(this);
  360. PrepareDC(dc);
  361. wxPoint pt(event.GetLogicalPosition(dc));
  362. t0 = t2 = seq->frames[0]->time;
  363. time = (double(pt.x) / spectrum_scale) + t0;
  364. for(ix=1; ix<seq->numframes; ix++)
  365. {
  366. t1=t2;
  367. t2 = seq->frames[ix]->time;
  368. if(time <= t1 + (t2-t1)/2)
  369. {
  370. currentcanvas->SelectFrame(ix-1);
  371. break;
  372. }
  373. }
  374. } // end of ByteGraph::OnMouse