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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. new wxStaticText(this,-1,_T(" Ht Width (Klatt)"),wxPoint(78,5),wxSize(300,20));
  86. new wxStaticText(this,-1,_T("Bw Ap Bp"),wxPoint(188,29),wxSize(300,20));
  87. for(ix=0; ix < 8; ix++)
  88. {
  89. string.Printf(_T("%d"),ix);
  90. t_labpk[ix] = new wxStaticText(this,id++,string,
  91. wxPoint(xplace-22,y+1+24*ix));
  92. if(ix < 7)
  93. {
  94. t_pkfreq[ix] = new wxTextCtrl(this,id++,_T(""),
  95. wxPoint(xplace,y+24*ix),wxSize(44,20),
  96. wxTE_CENTRE);
  97. }
  98. t_pkheight[ix] = new wxTextCtrl(this,id++,_T(""),
  99. wxPoint(xplace+46,y+24*ix),wxSize(36,20),
  100. wxTE_CENTRE);
  101. if(ix < 6)
  102. {
  103. t_pkwidth[ix] = new wxTextCtrl(this,id++,_T(""),
  104. wxPoint(xplace+84,y+24*ix),wxSize(60,20),
  105. wxTE_CENTRE);
  106. }
  107. if((ix == 0) || (ix > 6)) continue;
  108. if(ix < 4)
  109. {
  110. t_klt_bw[ix] = new wxTextCtrl(this,id++,_T(""),
  111. wxPoint(xplace+150,y+24*ix),wxSize(40,20),
  112. wxTE_CENTRE);
  113. }
  114. t_klt_ap[ix] = new wxTextCtrl(this,id++,_T(""),
  115. wxPoint(xplace+192,y+24*ix),wxSize(36,20),
  116. wxTE_CENTRE);
  117. t_klt_bp[ix] = new wxTextCtrl(this,id++,_T(""),
  118. wxPoint(xplace+230,y+24*ix),wxSize(40,20),
  119. wxTE_CENTRE);
  120. }
  121. y=224;
  122. t_timeframe = new wxSpinCtrl(this,T_TIMEFRAME,_T(""), wxPoint(6,y+0), wxSize(52,24), wxTE_CENTRE,0,500);
  123. t_orig_frame = new wxStaticText(this,-1,_T("mS"),wxPoint(61,y+8));
  124. t_ampframe = new wxSpinCtrl(this,T_AMPFRAME,_T(""), wxPoint(104,y+0), wxSize(52,24), wxTE_CENTRE,0,500);
  125. t_lab[3] = new wxStaticText(this,-1,_T("% amp - Frame"),wxPoint(159,y+8));
  126. y += 40;
  127. s_klatt[KLATT_AV] = new wxSpinCtrl(this,T_AV,_T(""), wxPoint(6,y), wxSize(52,24), wxTE_CENTRE,0,500);
  128. t_klatt[KLATT_AV] = new wxStaticText(this,-1,_T("AV"),wxPoint(61,y+4));
  129. s_klatt[KLATT_FNZ] = new wxSpinCtrl(this,T_FNZ,_T(""), wxPoint(104,y), wxSize(52,24), wxTE_CENTRE,0,500);
  130. t_klatt[KLATT_FNZ] = new wxStaticText(this,-1,_T("FNZ"),wxPoint(159,y+4));
  131. y += 28;
  132. s_klatt[KLATT_Tilt] = new wxSpinCtrl(this,T_TILT,_T(""), wxPoint(6,y), wxSize(52,24), wxTE_CENTRE,0,500);
  133. t_klatt[KLATT_Tilt] = new wxStaticText(this,-1,_T("Tilt"),wxPoint(61,y+4));
  134. s_klatt[KLATT_Aspr] = new wxSpinCtrl(this,T_ASPR,_T(""), wxPoint(104,y), wxSize(52,24), wxTE_CENTRE,0,500);
  135. t_klatt[KLATT_Aspr] = new wxStaticText(this,-1,_T("Aspr"),wxPoint(159,y+4));
  136. s_klatt[KLATT_Skew] = new wxSpinCtrl(this,T_SKEW,_T(""), wxPoint(202,y), wxSize(52,24), wxTE_CENTRE,0,500);
  137. t_klatt[KLATT_Skew] = new wxStaticText(this,-1,_T("Skew"),wxPoint(257,y+4));
  138. y += 28;
  139. s_klatt[KLATT_AVp] = new wxSpinCtrl(this,T_AVP,_T(""), wxPoint(6,y), wxSize(52,24), wxTE_CENTRE,0,500);
  140. t_klatt[KLATT_AVp] = new wxStaticText(this,-1,_T("AVp"),wxPoint(61,y+4));
  141. s_klatt[KLATT_Fric] = new wxSpinCtrl(this,T_FRIC,_T(""), wxPoint(104,y), wxSize(52,24), wxTE_CENTRE,0,500);
  142. t_klatt[KLATT_Fric] = new wxStaticText(this,-1,_T("Fric"),wxPoint(159,y+4));
  143. s_klatt[KLATT_FricBP] = new wxSpinCtrl(this,T_FRICBP,_T(""), wxPoint(202,y), wxSize(52,24), wxTE_CENTRE,0,500);
  144. t_klatt[KLATT_FricBP] = new wxStaticText(this,-1,_T("FricBP"),wxPoint(257,y+4));
  145. y += 28;
  146. s_klatt[KLATT_Kopen] = new wxSpinCtrl(this,T_KOPEN,_T(""), wxPoint(6,y), wxSize(52,24), wxTE_CENTRE,0,500);
  147. t_klatt[KLATT_Kopen] = new wxStaticText(this,-1,_T("kopen"),wxPoint(61,y+4));
  148. s_klatt[KLATT_Turb] = new wxSpinCtrl(this,T_TURB,_T(""), wxPoint(104,y), wxSize(52,24), wxTE_CENTRE,0,500);
  149. t_klatt[KLATT_Turb] = new wxStaticText(this,-1,_T("Turb"),wxPoint(159,y+4));
  150. t_zoomout = new wxButton(this,T_ZOOMOUT,_T("Zoom-"),wxPoint(16,420));
  151. t_zoomin = new wxButton(this,T_ZOOMIN,_T("Zoom+"),wxPoint(106,420));
  152. y=468;
  153. t_amplitude = new wxSpinCtrl(this,T_AMPLITUDE,_T(""),
  154. wxPoint(6,y),wxSize(52,24),wxTE_CENTRE,0,y+130);
  155. t_lab[2] = new wxStaticText(this,-1,_T("% amp - Sequence"),wxPoint(61,y+4));
  156. // t_timeseq = new wxSpinCtrl(this,T_TIMESEQ,_T(""),
  157. // wxPoint(6,400),wxSize(52,24),wxTE_CENTRE,0,500);
  158. t_orig_seq = new wxStaticText(this,-1,_T("mS"),wxPoint(61,y+30));
  159. t_pitch = new wxStaticText(this,-1,_T(""),wxPoint(4,520),wxSize(192,24));
  160. pitchgraph = new ByteGraph(this,wxPoint(0,538),wxSize(200,140));
  161. pitchgraph->SetData(128,env_fall);
  162. pitchgraph->ShowSpectrum(1);
  163. pitchgraph->Show();
  164. }
  165. void FormantDlg::GetValues(SpectSeq *spectseq, int frame)
  166. {//======================================================
  167. int ix;
  168. wxString value;
  169. long num;
  170. SpectFrame *sf;
  171. if(spectseq->frames == NULL)
  172. return;
  173. sf = spectseq->frames[frame];
  174. for(ix=0; ix < 8; ix++)
  175. {
  176. if(ix < 7)
  177. {
  178. num = 0;
  179. value = t_pkfreq[ix]->GetValue();
  180. value.ToLong(&num);
  181. sf->peaks[ix].pkfreq = num;
  182. }
  183. num = 0;
  184. value = t_pkheight[ix]->GetValue();
  185. value.ToLong(&num);
  186. sf->peaks[ix].pkheight = num << 6;
  187. if(ix < 6)
  188. {
  189. num = 0;
  190. value = t_pkwidth[ix]->GetValue();
  191. value.ToLong(&num);
  192. sf->peaks[ix].pkwidth = sf->peaks[ix].pkright = num*2;
  193. if((ix < 3) && ((value = value.AfterFirst('/')) != wxEmptyString))
  194. {
  195. num = 0;
  196. value.ToLong(&num);
  197. sf->peaks[ix].pkright = num*2;
  198. }
  199. }
  200. }
  201. for(ix=1; ix < 6; ix++)
  202. {
  203. if(ix < 3)
  204. {
  205. num = 0;
  206. value = t_klt_bw[ix]->GetValue();
  207. value.ToLong(&num);
  208. sf->peaks[ix].klt_bw = num;
  209. }
  210. num = 0;
  211. value = t_klt_ap[ix]->GetValue();
  212. value.ToLong(&num);
  213. sf->peaks[ix].klt_ap = num;
  214. num = 0;
  215. value = t_klt_bp[ix]->GetValue();
  216. value.ToLong(&num);
  217. sf->peaks[ix].klt_bp = num;
  218. }
  219. } // end of FormantDlg::GetValues
  220. void FormantDlg::ShowFrame(SpectSeq *spectseq, int frame, int pk, int field)
  221. {//==============================================================
  222. int ix;
  223. SpectFrame *sf;
  224. wxString value;
  225. int original_mS;
  226. peak_t *peak;
  227. if(spectseq->frames == NULL)
  228. return;
  229. sf = spectseq->frames[frame];
  230. if(field == 0xff)
  231. {
  232. // indicate the selected peak
  233. // t_select_peak[pk]->SetValue(TRUE);
  234. }
  235. for(ix=0; ix < 8; ix++)
  236. {
  237. if(field != 0xff && pk!=ix)
  238. continue;
  239. if(field == 0xff)
  240. {
  241. if(pk==ix)
  242. value.Printf(_T("%d="),ix);
  243. else
  244. value.Printf(_T("%d"),ix);
  245. t_labpk[ix]->SetLabel(value);
  246. }
  247. peak = &(sf->peaks[ix]);
  248. if((field & 1) && (ix < 7))
  249. {
  250. value.Printf(_T("%4d"),sf->peaks[ix].pkfreq);
  251. t_pkfreq[ix]->SetValue(value);
  252. }
  253. if(field & 2)
  254. {
  255. value.Printf(_T("%3d"),sf->peaks[ix].pkheight >> 6);
  256. t_pkheight[ix]->SetValue(value);
  257. }
  258. if((field & 4) && (ix < 6))
  259. {
  260. if(sf->peaks[ix].pkwidth == sf->peaks[ix].pkright)
  261. value.Printf(_T("%3d"),sf->peaks[ix].pkwidth/2);
  262. else
  263. value.Printf(_T("%3d/%3d"),sf->peaks[ix].pkwidth/2, sf->peaks[ix].pkright/2);
  264. t_pkwidth[ix]->SetValue(value);
  265. }
  266. if((ix > 0) && (ix < 7))
  267. {
  268. if((field & 8) && (ix < 4))
  269. {
  270. value.Printf(_T("%3d"),peak->klt_bw);
  271. t_klt_bw[ix]->SetValue(value);
  272. }
  273. if(field & 16)
  274. {
  275. value.Printf(_T("%3d"),peak->klt_ap);
  276. t_klt_ap[ix]->SetValue(value);
  277. }
  278. if(field & 32)
  279. {
  280. value.Printf(_T("%3d"),peak->klt_bp);
  281. t_klt_bp[ix]->SetValue(value);
  282. }
  283. }
  284. }
  285. // find the time until the next keyframe
  286. SetSpinCtrl(t_timeframe,int(spectseq->GetFrameLength(frame,1,&original_mS)+0.5)); // round to nearest integer
  287. value.Printf(_T("%d mS"),original_mS);
  288. t_orig_frame->SetLabel(value);
  289. value.Printf(_T("%3d"),sf->amp_adjust);
  290. t_ampframe->SetValue(value);
  291. for(ix=0; ix<N_KLATTP; ix++)
  292. {
  293. SetSpinCtrl(s_klatt[ix], sf->klatt_param[ix]);
  294. }
  295. } // end of FormantDlg::ShowFrame
  296. BEGIN_EVENT_TABLE(ByteGraph,wxScrolledWindow)
  297. EVT_LEFT_DOWN(ByteGraph::OnMouse)
  298. END_EVENT_TABLE()
  299. static wxBrush BRUSH_FORMANT(wxColour(255,100,50),wxSOLID);
  300. static wxPen PEN_KEYFRAME(wxColour(255,220,100),2,wxSOLID);
  301. static wxPen PEN_KEYFORMANT(wxColour(0,0,0),2,wxSOLID);
  302. ByteGraph::ByteGraph(wxWindow *parent, const wxPoint& pos, const wxSize &size):
  303. wxScrolledWindow(parent,-1,pos,size,wxSUNKEN_BORDER)
  304. {//============================================================
  305. npoints = 0;
  306. show_spectrum = 0;
  307. } // end of ByteGraph::ByteGraph
  308. void ByteGraph::SetData(int nx, unsigned char *data)
  309. {//=================================================
  310. npoints = nx;
  311. graph = data;
  312. Refresh();
  313. } // end of ByteGraph::SetData
  314. void ByteGraph::ShowSpectrum(int yes)
  315. {//==================================
  316. show_spectrum = yes;
  317. }
  318. void ByteGraph::DrawSpectrum(wxDC& dc)
  319. {//===================================
  320. SpectSeq *seq;
  321. SpectFrame *sf;
  322. int ix;
  323. int x, y;
  324. int pk;
  325. double length;
  326. int numframes;
  327. int width, height;
  328. double xscale, yscale;
  329. spectrum_scale = 0;
  330. if(currentcanvas==NULL) return;
  331. if((seq = currentcanvas->spectseq) == NULL) return;
  332. numframes = seq->numframes;
  333. if(numframes == 0) return;
  334. GetClientSize(&width,&height);
  335. if(((length = seq->frames[numframes-1]->time)==0) && (numframes>1))
  336. length = seq->frames[numframes-2]->time;
  337. length = length - seq->frames[0]->time;
  338. yscale = height/3000.0;
  339. spectrum_scale = xscale = width/length;
  340. dc.SetBrush(BRUSH_FORMANT);
  341. dc.SetPen(*wxTRANSPARENT_PEN);
  342. for(ix=0; ix<numframes; ix++)
  343. {
  344. sf = seq->frames[ix];
  345. x = int(sf->time*xscale);
  346. if(sf->keyframe)
  347. {
  348. dc.SetPen(PEN_KEYFRAME);
  349. dc.DrawLine(x,0,x,height);
  350. }
  351. dc.SetPen(*wxTRANSPARENT_PEN);
  352. dc.DrawCircle(x,int(height-(sf->formants[1].freq-200)*yscale),3);
  353. dc.DrawCircle(x,int(height-(sf->formants[2].freq-200)*yscale),3);
  354. dc.DrawCircle(x,int(height-(sf->formants[3].freq-200)*yscale),3);
  355. if(sf->keyframe)
  356. {
  357. dc.SetPen(PEN_KEYFORMANT);
  358. for(pk=1; pk<6; pk++)
  359. {
  360. y = height-int((sf->peaks[pk].pkfreq-200)*yscale);
  361. dc.DrawLine(x-3,y,x+3,y);
  362. }
  363. }
  364. }
  365. }
  366. void ByteGraph::OnDraw(wxDC &dc)
  367. {//============================
  368. int x, y, x2, y2, ix;
  369. int width, height;
  370. double xscale, yscale;
  371. if(show_spectrum) DrawSpectrum(dc);
  372. if(npoints==0) return;
  373. GetClientSize(&width,&height);
  374. xscale = double(width) / npoints;
  375. yscale = double(height) / 256.0;
  376. if(npoints > 8)
  377. {
  378. // draw pitch profile
  379. dc.SetPen(*wxMEDIUM_GREY_PEN);
  380. x = 0;
  381. y = height - int(graph[0]*yscale);
  382. for(ix=1; ix<npoints; ix++)
  383. {
  384. dc.DrawLine(x,y,x2=int(ix*xscale),y2=height - int(graph[ix]*yscale));
  385. x = x2;
  386. y = y2;
  387. }
  388. }
  389. } // end of ByteGraph::OnDraw
  390. void ByteGraph::OnMouse(wxMouseEvent& event)
  391. {//========================================
  392. int ix;
  393. SpectSeq *seq;
  394. double time;
  395. double t0, t1;
  396. double t2=0;
  397. if(currentcanvas==NULL) return;
  398. if((seq = currentcanvas->spectseq) == NULL) return;
  399. wxClientDC dc(this);
  400. PrepareDC(dc);
  401. wxPoint pt(event.GetLogicalPosition(dc));
  402. t0 = t2 = seq->frames[0]->time;
  403. time = (double(pt.x) / spectrum_scale) + t0;
  404. for(ix=1; ix<seq->numframes; ix++)
  405. {
  406. t1=t2;
  407. t2 = seq->frames[ix]->time;
  408. if(time <= t1 + (t2-t1)/2)
  409. {
  410. currentcanvas->SelectFrame(ix-1);
  411. break;
  412. }
  413. }
  414. } // end of ByteGraph::OnMouse