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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /***************************************************************************
  2. * Copyright (C) 2005, 2006 by Jonathan Duddington *
  3. * [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 2 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 to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. // For compilers that support precompilation, includes "wx/wx.h".
  21. #include "wx/wx.h"
  22. #include "wx/mdi.h"
  23. #include "main.h"
  24. #include "speech.h"
  25. #include "voice.h"
  26. #include "spect.h"
  27. #include "wx/textctrl.h"
  28. #include "wx/checkbox.h"
  29. FormantDlg *formantdlg=NULL;
  30. ByteGraph *pitchgraph=NULL;
  31. BEGIN_EVENT_TABLE(FormantDlg, wxPanel)
  32. EVT_BUTTON(T_ZOOMOUT,FormantDlg::OnCommand)
  33. EVT_BUTTON(T_ZOOMIN,FormantDlg::OnCommand)
  34. EVT_SPINCTRL(T_AMPLITUDE,FormantDlg::OnSpin)
  35. EVT_SPINCTRL(T_TIMESEQ,FormantDlg::OnSpin)
  36. EVT_SPINCTRL(T_AMPFRAME,FormantDlg::OnSpin)
  37. EVT_SPINCTRL(T_TIMEFRAME,FormantDlg::OnSpin)
  38. END_EVENT_TABLE()
  39. void FormantDlg::OnCommand(wxCommandEvent& event)
  40. {//=============================================
  41. int id;
  42. switch(id = event.GetId())
  43. {
  44. case T_ZOOMIN:
  45. case T_ZOOMOUT:
  46. currentcanvas->OnZoom(id);
  47. break;
  48. case T_AMPLITUDE:
  49. currentcanvas->RefreshDialogValues(0);
  50. break;
  51. case T_TIMEFRAME:
  52. case T_AMPFRAME:
  53. currentcanvas->RefreshDialogValues(1);
  54. break;
  55. }
  56. currentcanvas->SetFocus();
  57. }
  58. void FormantDlg::OnSpin(wxSpinEvent& event)
  59. {//=============================================
  60. int id;
  61. switch(id = event.GetId())
  62. {
  63. case T_AMPLITUDE:
  64. case T_TIMESEQ:
  65. case T_TIMEFRAME:
  66. currentcanvas->RefreshDialogValues(0);
  67. break;
  68. case T_AMPFRAME:
  69. currentcanvas->RefreshDialogValues(1);
  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. for(ix=0; ix<N_PEAKS; ix++)
  86. {
  87. string.Printf(_T("%d"),ix);
  88. t_labpk[ix] = new wxStaticText(this,id++,string,
  89. wxPoint(xplace-22,y+1+24*ix));
  90. t_pkfreq[ix] = new wxTextCtrl(this,id++,_T(""),
  91. wxPoint(xplace,y+24*ix),wxSize(48,20),
  92. wxTE_CENTRE+wxTE_READONLY);
  93. t_pkheight[ix] = new wxTextCtrl(this,id++,_T(""),
  94. wxPoint(xplace+52,y+24*ix),wxSize(36,20),
  95. wxTE_CENTRE+wxTE_READONLY);
  96. t_pkwidth[ix] = new wxTextCtrl(this,id++,_T(""),
  97. wxPoint(xplace+92,y+24*ix),wxSize(48,20),
  98. wxTE_CENTRE+wxTE_READONLY);
  99. #ifdef WIDTH2
  100. t_pkright[ix] = new wxTextCtrl(this,id++,_T(""),
  101. wxPoint(xplace+132,y+24*ix),wxSize(36,20),
  102. wxTE_CENTRE+wxTE_READONLY);
  103. #endif
  104. if(ix>5) t_pkwidth[ix]->Hide();
  105. }
  106. t_ampframe = new wxSpinCtrl(this,T_AMPFRAME,_T(""),
  107. wxPoint(6,244),wxSize(52,24),wxTE_CENTRE,0,500);
  108. t_lab[3] = new wxStaticText(this,-1,_T("% amp - Frame"),wxPoint(61,248));
  109. t_timeframe = new wxSpinCtrl(this,T_TIMEFRAME,_T(""),
  110. wxPoint(6,272),wxSize(52,24),wxTE_CENTRE,0,500);
  111. t_orig_frame = new wxStaticText(this,-1,_T("mS"),wxPoint(61,276));
  112. t_zoomout = new wxButton(this,T_ZOOMOUT,_T("Zoom-"),wxPoint(16,310));
  113. t_zoomin = new wxButton(this,T_ZOOMIN,_T("Zoom+"),wxPoint(106,310));
  114. // usepitchenv = new wxCheckBox(this,T_USEPITCHENV,_T("???"),wxPoint(20,320));
  115. t_amplitude = new wxSpinCtrl(this,T_AMPLITUDE,_T(""),
  116. wxPoint(6,370),wxSize(52,24),wxTE_CENTRE,0,500);
  117. t_lab[2] = new wxStaticText(this,-1,_T("% amp - Sequence"),wxPoint(61,374));
  118. // t_timeseq = new wxSpinCtrl(this,T_TIMESEQ,_T(""),
  119. // wxPoint(6,400),wxSize(52,24),wxTE_CENTRE,0,500);
  120. t_orig_seq = new wxStaticText(this,-1,_T("mS"),wxPoint(61,404));
  121. t_pitch = new wxStaticText(this,-1,_T(""),wxPoint(4,440),wxSize(192,24));
  122. pitchgraph = new ByteGraph(this,wxPoint(0,460),wxSize(200,140));
  123. pitchgraph->SetData(128,env_fall);
  124. pitchgraph->ShowSpectrum(1);
  125. pitchgraph->Show();
  126. }
  127. void FormantDlg::ShowFrame(SpectSeq *spectseq, int frame, int pk, int field)
  128. {//==============================================================
  129. int ix;
  130. SpectFrame *sf;
  131. wxString value;
  132. int original_mS;
  133. if(spectseq->frames == NULL)
  134. return;
  135. sf = spectseq->frames[frame];
  136. if(field == 0xff)
  137. {
  138. // indicate the selected peak
  139. // t_select_peak[pk]->SetValue(TRUE);
  140. }
  141. for(ix=0; ix<N_PEAKS; ix++)
  142. {
  143. if(field != 0xff && pk!=ix)
  144. continue;
  145. if(field == 0xff)
  146. {
  147. if(pk==ix)
  148. value.Printf(_T("%d="),ix);
  149. else
  150. value.Printf(_T("%d"),ix);
  151. t_labpk[ix]->SetLabel(value);
  152. }
  153. if(field & 1)
  154. {
  155. value.Printf(_T("%4d"),sf->peaks[ix].pkfreq);
  156. t_pkfreq[ix]->SetValue(value);
  157. }
  158. if(field & 2)
  159. {
  160. value.Printf(_T("%3d"),sf->peaks[ix].pkheight >> 6);
  161. t_pkheight[ix]->SetValue(value);
  162. }
  163. if(field & 4)
  164. {
  165. value.Printf(_T("%3d"),sf->peaks[ix].pkwidth);
  166. t_pkwidth[ix]->SetValue(value);
  167. #ifdef WIDTH2
  168. value.Printf(_T("%3d"),sf->peaks[ix].pkright);
  169. t_pkright[ix]->SetValue(value);
  170. #endif
  171. }
  172. }
  173. // find the time until the next keyframe
  174. SetSpinCtrl(t_timeframe,int(spectseq->GetFrameLength(frame,1,&original_mS)+0.5)); // round to nearest integer
  175. value.Printf(_T("%d mS"),original_mS);
  176. t_orig_frame->SetLabel(value);
  177. value.Printf(_T("%3d"),sf->amp_adjust);
  178. t_ampframe->SetValue(value);
  179. } // end of FormantDlg::ShowFrame
  180. BEGIN_EVENT_TABLE(ByteGraph,wxScrolledWindow)
  181. EVT_LEFT_DOWN(ByteGraph::OnMouse)
  182. END_EVENT_TABLE()
  183. static wxBrush BRUSH_FORMANT(wxColour(255,100,50),wxSOLID);
  184. static wxPen PEN_KEYFRAME(wxColour(255,220,100),2,wxSOLID);
  185. static wxPen PEN_KEYFORMANT(wxColour(0,0,0),2,wxSOLID);
  186. ByteGraph::ByteGraph(wxWindow *parent, const wxPoint& pos, const wxSize &size):
  187. wxScrolledWindow(parent,-1,pos,size,wxSUNKEN_BORDER)
  188. {//============================================================
  189. npoints = 0;
  190. show_spectrum = 0;
  191. } // end of ByteGraph::ByteGraph
  192. void ByteGraph::SetData(int nx, unsigned char *data)
  193. {//=================================================
  194. npoints = nx;
  195. graph = data;
  196. Refresh();
  197. } // end of ByteGraph::SetData
  198. void ByteGraph::ShowSpectrum(int yes)
  199. {//==================================
  200. show_spectrum = yes;
  201. }
  202. void ByteGraph::DrawSpectrum(wxDC& dc)
  203. {//===================================
  204. SpectSeq *seq;
  205. SpectFrame *sf;
  206. int ix;
  207. int x, y;
  208. int pk;
  209. double length;
  210. int numframes;
  211. int width, height;
  212. double xscale, yscale;
  213. spectrum_scale = 0;
  214. if(currentcanvas==NULL) return;
  215. if((seq = currentcanvas->spectseq) == NULL) return;
  216. numframes = seq->numframes;
  217. if(numframes == 0) return;
  218. GetClientSize(&width,&height);
  219. if(((length = seq->frames[numframes-1]->time)==0) && (numframes>1))
  220. length = seq->frames[numframes-2]->time;
  221. length = length - seq->frames[0]->time;
  222. yscale = height/3000.0;
  223. spectrum_scale = xscale = width/length;
  224. dc.SetBrush(BRUSH_FORMANT);
  225. dc.SetPen(*wxTRANSPARENT_PEN);
  226. for(ix=0; ix<numframes; ix++)
  227. {
  228. sf = seq->frames[ix];
  229. x = int(sf->time*xscale);
  230. if(sf->keyframe)
  231. {
  232. dc.SetPen(PEN_KEYFRAME);
  233. dc.DrawLine(x,0,x,height);
  234. }
  235. dc.SetPen(*wxTRANSPARENT_PEN);
  236. dc.DrawCircle(x,int(height-(sf->formants[1].freq-200)*yscale),3);
  237. dc.DrawCircle(x,int(height-(sf->formants[2].freq-200)*yscale),3);
  238. dc.DrawCircle(x,int(height-(sf->formants[3].freq-200)*yscale),3);
  239. if(sf->keyframe)
  240. {
  241. dc.SetPen(PEN_KEYFORMANT);
  242. for(pk=1; pk<6; pk++)
  243. {
  244. y = height-int((sf->peaks[pk].pkfreq-200)*yscale);
  245. dc.DrawLine(x-3,y,x+3,y);
  246. }
  247. }
  248. }
  249. }
  250. void ByteGraph::OnDraw(wxDC &dc)
  251. {//============================
  252. int x, y, x2, y2, ix;
  253. int width, height;
  254. double xscale, yscale;
  255. if(show_spectrum) DrawSpectrum(dc);
  256. if(npoints==0) return;
  257. GetClientSize(&width,&height);
  258. xscale = double(width) / npoints;
  259. yscale = double(height) / 256.0;
  260. if(npoints > 8)
  261. {
  262. // draw pitch profile
  263. dc.SetPen(*wxMEDIUM_GREY_PEN);
  264. x = 0;
  265. y = height - int(graph[0]*yscale);
  266. for(ix=1; ix<npoints; ix++)
  267. {
  268. dc.DrawLine(x,y,x2=int(ix*xscale),y2=height - int(graph[ix]*yscale));
  269. x = x2;
  270. y = y2;
  271. }
  272. }
  273. } // end of ByteGraph::OnDraw
  274. void ByteGraph::OnMouse(wxMouseEvent& event)
  275. {//========================================
  276. int ix;
  277. SpectSeq *seq;
  278. double time;
  279. double t0, t1;
  280. double t2=0;
  281. if(currentcanvas==NULL) return;
  282. if((seq = currentcanvas->spectseq) == NULL) return;
  283. wxClientDC dc(this);
  284. PrepareDC(dc);
  285. wxPoint pt(event.GetLogicalPosition(dc));
  286. t0 = t2 = seq->frames[0]->time;
  287. time = (double(pt.x) / spectrum_scale) + t0;
  288. for(ix=1; ix<seq->numframes; ix++)
  289. {
  290. t1=t2;
  291. t2 = seq->frames[ix]->time;
  292. if(time <= t1 + (t2-t1)/2)
  293. {
  294. currentcanvas->SelectFrame(ix-1);
  295. break;
  296. }
  297. }
  298. } // end of ByteGraph::OnMouse