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.

spectdisplay.cpp 23KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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, write see: *
  17. * <http://www.gnu.org/licenses/>. *
  18. ***************************************************************************/
  19. #include "wx/wx.h"
  20. #include "wx/wfstream.h"
  21. #include "wx/filename.h"
  22. #include "wx/datstrm.h"
  23. #include "wx/listimpl.cpp"
  24. #include "wx/numdlg.h"
  25. #include "wx/utils.h"
  26. #include <ctype.h>
  27. #include "speak_lib.h"
  28. #include "speech.h"
  29. #include "main.h"
  30. #include "phoneme.h"
  31. #include "synthesize.h"
  32. #include "voice.h"
  33. #include "translate.h"
  34. #include "spect.h"
  35. #include "options.h"
  36. extern MyFrame *frame ;
  37. #define SCROLLUNITS 20
  38. #define N_CANVASLIST 50
  39. static int canvaslistix=0;
  40. static SpectDisplay *canvaslist[N_CANVASLIST];
  41. SpectSeq *clipboard_spect=NULL;
  42. wxMenu *menu_spectdisplay=NULL;
  43. BEGIN_EVENT_TABLE(SpectDisplay, wxScrolledWindow)
  44. EVT_LEFT_DOWN(SpectDisplay::OnMouse)
  45. EVT_RIGHT_DOWN(SpectDisplay::OnMouse)
  46. EVT_KEY_DOWN(SpectDisplay::OnKey)
  47. EVT_MENU(-1, SpectDisplay::OnMenu)
  48. END_EVENT_TABLE()
  49. // Define a constructor for my canvas
  50. SpectDisplay::SpectDisplay(wxWindow *parent, const wxPoint& pos, const wxSize& size, SpectSeq *spect)
  51. : wxScrolledWindow(parent, -1, pos, size,
  52. wxSUNKEN_BORDER | wxNO_FULL_REPAINT_ON_RESIZE)
  53. {
  54. wxString filename;
  55. // SetCursor(wxCursor(wxCURSOR_PENCIL));
  56. SetBackgroundColour(* wxWHITE);
  57. zoom = 2;
  58. zoomx = 0.5;
  59. zoomy = 0.5;
  60. savepath = _T("");
  61. spectseq = spect;
  62. sframe = 0;
  63. pk_num = 1;
  64. SetExtent();
  65. if(canvaslistix < N_CANVASLIST-1)
  66. canvaslist[canvaslistix++] = this;
  67. // WavegenInitPkData(0);
  68. } // end of SpectDisplay::SpectDisplay
  69. SpectDisplay::~SpectDisplay()
  70. {//==========================
  71. // delete this from the canvaslist
  72. int ix;
  73. for(ix=0; ix<canvaslistix; ix++)
  74. {
  75. if(canvaslist[ix] == this)
  76. {
  77. for(ix=ix+1; ix<canvaslistix; ix++)
  78. canvaslist[ix-1] = canvaslist[ix];
  79. canvaslistix--;
  80. break;
  81. }
  82. }
  83. if(currentcanvas==this)
  84. {
  85. if(canvaslistix > 0)
  86. currentcanvas = canvaslist[0];
  87. else
  88. currentcanvas = NULL;
  89. }
  90. }
  91. void SpectDisplay::SetExtent()
  92. {//===========================
  93. SetScrollbars(SCROLLUNITS,SCROLLUNITS,
  94. (int)(FRAME_WIDTH*zoomx/SCROLLUNITS)+1,
  95. (int)(spectseq->numframes*FRAME_HEIGHT*zoomy/SCROLLUNITS)+1);
  96. Refresh();
  97. } // end of SpectDisplay::SetExtent
  98. void SpectDisplay::OnDraw(wxDC& dc)
  99. {//================================
  100. wxRegion region;
  101. dc.SetUserScale(zoomx,zoomy);
  102. int f1 = 0x7fffffff, f2 = -1;
  103. int x1,x2,y1,y2;
  104. int vX,vY,vW,vH; // Dimensions of client area in pixels
  105. wxRegionIterator upd(GetUpdateRegion()); // get the update rect list
  106. while (upd)
  107. {
  108. vX = upd.GetX();
  109. vY = upd.GetY();
  110. vW = upd.GetW();
  111. vH = upd.GetH();
  112. CalcUnscrolledPosition(vX,vY,&x1,&y1);
  113. CalcUnscrolledPosition(vX+vW,vY+vH,&x2,&y2);
  114. // Repaint this rectangle
  115. if(y1 < f1) f1 = int(y1/zoomy);
  116. if(y2 > f2) f2 = int(y2/zoomy);
  117. upd ++ ;
  118. }
  119. if(spectseq != NULL)
  120. spectseq->Draw(dc,f1,f2);
  121. }
  122. void SpectDisplay::OnActivate(int active)
  123. {//======================================
  124. if(active)
  125. {
  126. currentcanvas = this;
  127. SetFocus(); // this doesn't seem to work
  128. if(sframe >= 0)
  129. formantdlg->ShowFrame(spectseq,sframe,pk_num,0xff);
  130. WriteDialogValues();
  131. if(wxGetKeyState(WXK_CONTROL))
  132. {
  133. ReadDialogValues();
  134. spectseq->MakeWave(0,spectseq->numframes-1,voicedlg->pitchenv);
  135. }
  136. }
  137. else
  138. {
  139. formantdlg->GetValues(spectseq, sframe);
  140. ReadDialogValues();
  141. }
  142. }
  143. void SetNumeric(wxTextCtrl *t, int value)
  144. //{=====================================
  145. {
  146. wxString string;
  147. string.Printf(_T("%d"),value);
  148. t->SetValue(string);
  149. }
  150. void SetSpinCtrl(wxSpinCtrl *t, int value)
  151. //{=======================================
  152. {
  153. wxString string;
  154. string.Printf(_T("%d"),value);
  155. t->SetValue(string);
  156. }
  157. int GetNumeric(wxTextCtrl *t)
  158. //{==========================
  159. {
  160. return(atoi(t->GetValue().mb_str(wxConvLocal)));
  161. }
  162. void SpectDisplay::WriteDialogLength()
  163. {//===================================
  164. wxString string;
  165. string.Printf(_T("%d mS"),int(spectseq->GetKeyedLength()+0.5));
  166. formantdlg->t_orig_seq->SetLabel(string);
  167. }
  168. void SpectDisplay::WriteDialogValues()
  169. //{===============================
  170. {
  171. wxString string;
  172. SetSpinCtrl(formantdlg->t_amplitude,spectseq->amplitude);
  173. string.Printf(_T("%d - %d Hz %d mS"),
  174. spectseq->pitch1,spectseq->pitch2,spectseq->duration);
  175. formantdlg->t_pitch->SetLabel(string);
  176. pitchgraph->SetData(128,spectseq->pitchenv.env);
  177. voicedlg->SetFromSpect(spectseq);
  178. WriteDialogLength();
  179. }
  180. void SpectDisplay::ReadDialogValues()
  181. //{==================================
  182. {
  183. spectseq->amplitude = formantdlg->t_amplitude->GetValue();
  184. spectseq->duration = GetNumeric(voicedlg->vd_duration);
  185. voicedlg->ReadParams();
  186. }
  187. void SpectDisplay::RefreshDialogValues(int type)
  188. //{=============================================
  189. {
  190. int ix;
  191. long int value;
  192. SpectFrame *sf;
  193. if(spectseq == NULL)
  194. return;
  195. sf = spectseq->frames[sframe];
  196. spectseq->amplitude = formantdlg->t_amplitude->GetValue();
  197. sf->amp_adjust = formantdlg->t_ampframe->GetValue();
  198. if(use_spin_controls)
  199. value = formantdlg->t_timeframe->GetValue();
  200. else
  201. formantdlg->tt_timeframe->GetValue().ToLong(&value);
  202. sf->length_adjust = value - spectseq->GetFrameLength(sframe,0,NULL);
  203. for(ix=0; ix < N_KLATTP; ix++)
  204. {
  205. sf->klatt_param[ix] = formantdlg->s_klatt[ix]->GetValue();
  206. }
  207. if(type==0)
  208. {
  209. WriteDialogLength();
  210. Refresh();
  211. }
  212. else
  213. RefreshFrame(sframe); // frame amplitude
  214. }
  215. void SpectDisplay::RefreshFrame(int frame)
  216. {//=======================================
  217. int frame_x, frame_y;
  218. CalcScrolledPosition(0,int(frame*FRAME_HEIGHT*zoomy),
  219. &frame_x,&frame_y);
  220. RefreshRect(wxRect(0,frame_y,int(1000*zoomx),int(FRAME_HEIGHT*zoomy)));
  221. }
  222. int SpectDisplay::ScrollToFrame(int frame, int centre)
  223. {//===================================================
  224. int scrollx, scrolly;
  225. int x, y;
  226. int scrolled = 0;
  227. GetClientSize(&x,&y);
  228. GetViewStart(&scrollx,&scrolly);
  229. scrolly *= SCROLLUNITS;
  230. if(centre)
  231. {
  232. Scroll(-1, int(((frame+0.5)*FRAME_HEIGHT*zoomy - y/2) /SCROLLUNITS));
  233. return(1);
  234. }
  235. if(scrolly > frame * FRAME_HEIGHT * zoomy)
  236. {
  237. Scroll(-1, int((frame * FRAME_HEIGHT * zoomy) / SCROLLUNITS));
  238. scrolled = 1;
  239. }
  240. else
  241. {
  242. if(((frame+1) * FRAME_HEIGHT * zoomy) > (scrolly + y))
  243. {
  244. Scroll(-1, int(((frame+1)*FRAME_HEIGHT*zoomy - y) /SCROLLUNITS)+1);
  245. scrolled = 1;
  246. }
  247. }
  248. return(scrolled);
  249. } // end of SpectDisplay::ScrollToFrame
  250. void SpectDisplay::SelectFrame(int frame)
  251. {//======================================
  252. sframe = frame;
  253. spectseq->SelectAll(0);
  254. spectseq->frames[frame]->selected = 1;
  255. formantdlg->ShowFrame(spectseq,frame,1,0xff);
  256. ScrollToFrame(frame,1);
  257. Refresh();
  258. SetFocus();
  259. } // end of SpectDisplay::SelectFrame
  260. void SpectDisplay::OnZoom(int command)
  261. {//===============================
  262. static float zoomtab[] = {0.25,0.3536,0.5,0.7071,1,1.14142};
  263. int f1, f2, frame;
  264. int x,y;
  265. int height,width;
  266. GetClientSize(&width,&height);
  267. CalcScrolledPosition(0,0,&x,&y);
  268. // centre on a visible selected frame, or if none keep the centred frame centred
  269. f1 = int(-y/(FRAME_HEIGHT*zoomy));
  270. f2 = int((-y+height)/(FRAME_HEIGHT*zoomy));
  271. for(frame=f1; frame<=f2 && frame<spectseq->numframes; frame++)
  272. {
  273. if(spectseq->frames[frame]->selected) break;
  274. }
  275. if(frame > f2)
  276. frame = int((-y+height/2)/(FRAME_HEIGHT*zoomy)); // keep centred frame
  277. switch(command)
  278. {
  279. case T_ZOOMIN:
  280. if(zoom < 5) zoom++;
  281. break;
  282. case T_ZOOMOUT:
  283. if(zoom > 0) zoom--;
  284. break;
  285. }
  286. zoomx = zoomy = zoomtab[zoom];
  287. SetExtent();
  288. ScrollToFrame(frame,1);
  289. Refresh();
  290. } // end of OnZoom
  291. void SpectDisplay::Save(const wxString &path, int selection)
  292. {//=========================================================
  293. wxString filename;
  294. wxFrame *parent;
  295. wxString msg;
  296. wxString fname = _T("");
  297. wxFileName f;
  298. if(selection)
  299. msg = _T("Save selected frames");
  300. else
  301. {
  302. msg = _T("Save spectrum sequence");
  303. fname = spectseq->name;
  304. }
  305. if(path.empty())
  306. {
  307. filename = wxFileSelector(msg,wxFileName(savepath).GetPath(),fname,_T(""),_T("*"),wxSAVE);
  308. }
  309. else
  310. filename = path;
  311. if(filename.IsEmpty())
  312. return;
  313. wxFileOutputStream stream(filename);
  314. if(stream.Ok() == FALSE)
  315. {
  316. wxLogError(_T("Failed to write '%s'"),filename.c_str());
  317. return;
  318. }
  319. spectseq->Save(stream,selection);
  320. savepath = filename;
  321. if((parent = (wxFrame *)GetParent()) != NULL)
  322. {
  323. f = wxFileName(filename);
  324. parent->SetTitle(f.GetName());
  325. spectseq->name = f.GetName();
  326. }
  327. Refresh(); // Shouldn't need this, but redraw problem
  328. if(path.empty())
  329. {
  330. wxFileName p(filename);
  331. }
  332. } // end of Save
  333. void SpectDisplay::SavePitchenv(PitchEnvelope &pitch)
  334. {//==================================================
  335. // save the pitch envelope
  336. int ix;
  337. FILE *f_env;
  338. wxString filename;
  339. char fname[200];
  340. filename = wxFileSelector(_T("(Save pitch envelope"),path_pitches,_T(""),_T(""),_T("*"),wxSAVE);
  341. if(filename.IsEmpty())
  342. return;
  343. strcpy(fname, filename.mb_str(wxConvLocal));
  344. f_env = fopen(fname,"w");
  345. if(f_env == NULL)
  346. {
  347. wxLogError(_T("Failed to write ")+filename);
  348. return;
  349. }
  350. for(ix=0; ix<128; ix++)
  351. {
  352. fprintf(f_env," 0x%.2x,",pitch.env[ix]);
  353. if((ix & 0xf) == 0xf)
  354. fprintf(f_env,"\n");
  355. }
  356. fclose(f_env);
  357. } // end of SpectDisplay::SavePitchenv
  358. // WX_DECLARE_LIST(MyChild,ChildList);
  359. // WX_DEFINE_LIST(ChildList);
  360. void SpectDisplay::PlayChild(int number, PitchEnvelope pitchenv)
  361. {//=========================================================
  362. SpectSeq *seq;
  363. if(number >= canvaslistix) return;
  364. if((seq = canvaslist[number]->spectseq) == NULL)
  365. return;
  366. ReadDialogValues();
  367. seq->MakeWave(0,seq->numframes-1,pitchenv);
  368. } // end of PlayChild
  369. void SpectDisplay::SetKeyframe(SpectFrame *sf, int yes)
  370. {//====================================================
  371. if(sf->keyframe == yes) return; // already done
  372. sf->keyframe = yes;
  373. pitchgraph->Refresh();
  374. WriteDialogLength();
  375. }
  376. void SpectDisplay::OnMenu(wxCommandEvent& event)
  377. {//=============================================
  378. int id;
  379. int code;
  380. wxKeyEvent keyevent;
  381. SpectFrame *sf;
  382. static int key[] = {0x1044,0x3044,0x104e,0x1047,0x105a,0x1051,0x3051,WXK_F1,WXK_F2,0x1049,WXK_F10,0x104b};
  383. id = event.GetId();
  384. if(id >= 300)
  385. {
  386. sf = spectseq->frames[sframe];
  387. sf->ToggleMarker(id-300);
  388. Refresh();
  389. return;
  390. }
  391. // simulate a KeyEvent that corresponds to this menu entry
  392. code = key[id - 201];
  393. keyevent.m_keyCode = code & 0xfff;
  394. if(code & 0x1000)
  395. keyevent.m_controlDown = TRUE;
  396. else
  397. keyevent.m_controlDown = FALSE;
  398. if(code & 0x2000)
  399. keyevent.m_shiftDown = TRUE;
  400. else
  401. keyevent.m_shiftDown = FALSE;
  402. OnKey(keyevent);
  403. }
  404. void SpectDisplay::OnKey(wxKeyEvent& event)
  405. {//====================================
  406. int key;
  407. int display=0; // 1=change one peak 2=one frame, 3=all
  408. int direction=0;
  409. int x_inc;
  410. int y_inc;
  411. int i;
  412. int field=0xff;
  413. int keyfr;
  414. int control;
  415. int number;
  416. double value;
  417. SpectFrame *sf;
  418. peak_t *pk;
  419. // static short pk_inc1[N_PEAKS] = {2,2,2,5,5,5,5,5,5};
  420. static short pk_inc1[N_PEAKS] = {4,4,4,8,8,8,8,8,8};
  421. static short pk_inc2[N_PEAKS] = {8,8,20,20,20,20,25,25,25};
  422. sf = spectseq->frames[sframe];
  423. pk = &sf->peaks[pk_num];
  424. key = event.GetKeyCode();
  425. control = event.ControlDown();
  426. if(control && isalnum(key)) key |= 0x1000;
  427. if(event.ShiftDown())
  428. {
  429. x_inc = pk_inc1[pk_num];
  430. y_inc = 0x40;
  431. }
  432. else
  433. {
  434. x_inc = pk_inc2[pk_num];
  435. y_inc = 0x100;
  436. }
  437. switch(key)
  438. {
  439. case WXK_NEXT:
  440. if(sframe < spectseq->numframes-1)
  441. {
  442. formantdlg->GetValues(spectseq, sframe);
  443. spectseq->SelectAll(0);
  444. sframe++;
  445. spectseq->frames[sframe]->selected = 1;
  446. display = 3;
  447. }
  448. break;
  449. case WXK_PRIOR:
  450. if(sframe > 0)
  451. {
  452. formantdlg->GetValues(spectseq, sframe);
  453. spectseq->SelectAll(0);
  454. sframe--;
  455. spectseq->frames[sframe]->selected = 1;
  456. display = 3;
  457. }
  458. break;
  459. case 'Z':
  460. if(pk_num > 0)
  461. {
  462. pk_num--;
  463. display = 2;
  464. }
  465. break;
  466. case 'X':
  467. if(pk_num < N_PEAKS-1)
  468. {
  469. pk_num++;
  470. display = 2;
  471. }
  472. break;
  473. case WXK_RIGHT:
  474. pk->pkfreq = pk->pkfreq - (pk->pkfreq % x_inc) + x_inc;
  475. if(pk->pkfreq >= MAX_DISPLAY_FREQ)
  476. pk->pkfreq = MAX_DISPLAY_FREQ;
  477. display = 1;
  478. direction = 1;
  479. field = 1;
  480. break;
  481. case WXK_LEFT:
  482. pk->pkfreq = pk->pkfreq - (pk->pkfreq % x_inc) - x_inc;
  483. if(pk->pkfreq < 50)
  484. pk->pkfreq = 50;
  485. display = 1;
  486. direction = -1;
  487. field = 1;
  488. break;
  489. case WXK_UP:
  490. pk->pkheight = pk->pkheight - (pk->pkheight % y_inc) + y_inc;
  491. if(pk->pkheight >= 0x7fff)
  492. pk->pkheight = 0x7fff;
  493. display = 1;
  494. field = 2;
  495. SetKeyframe(sf,1);
  496. break;
  497. case WXK_DOWN:
  498. pk->pkheight = pk->pkheight - (pk->pkheight % y_inc) - y_inc;
  499. if(pk->pkheight <= 0)
  500. {
  501. pk->pkheight = 0;
  502. keyfr = 0; // but only if all the other peaks are zero
  503. for(i=0; i<N_PEAKS; i++)
  504. {
  505. if(sf->peaks[i].pkheight > 0)
  506. keyfr = 1;
  507. }
  508. SetKeyframe(sf,keyfr);
  509. }
  510. display = 1;
  511. field = 2;
  512. break;
  513. case ',': // width--
  514. if(event.ControlDown())
  515. {
  516. // CTRL, rotate, make right slope steeper
  517. if(pk_num < 3)
  518. {
  519. pk->pkright-= 5;
  520. pk->pkwidth += 5;
  521. }
  522. }
  523. else
  524. {
  525. pk->pkright -= 10;
  526. pk->pkwidth -= 10;
  527. if(pk->pkright < 0)
  528. pk->pkright = 0;
  529. if(pk->pkwidth < 0)
  530. pk->pkwidth = 0;
  531. }
  532. field = 4;
  533. display = 1;
  534. break;
  535. case '.': // width++
  536. if(event.ControlDown())
  537. {
  538. // CTRL: rotate, make left slope steeper
  539. if(pk_num < 3)
  540. {
  541. pk->pkright += 5;
  542. pk->pkwidth -= 5;
  543. }
  544. }
  545. else
  546. {
  547. pk->pkright += 10;
  548. pk->pkwidth += 10;
  549. }
  550. field = 4;
  551. display = 1;
  552. break;
  553. case '<': // width--
  554. pk->pkright -= 2;
  555. pk->pkwidth -= 2;
  556. if(pk->pkwidth < 0)
  557. pk->pkwidth = 0;
  558. display = 1;
  559. field = 4;
  560. break;
  561. case '>': // width++
  562. pk->pkright += 2;
  563. pk->pkwidth += 2;
  564. display = 1;
  565. field = 4;
  566. break;
  567. case '[': // width--
  568. pk->pkright -= 1;
  569. if(pk->pkright < 0)
  570. pk->pkright = 0;
  571. display = 1;
  572. field = 4;
  573. break;
  574. case ']': // width++
  575. pk->pkright += 1;
  576. display = 1;
  577. field = 4;
  578. break;
  579. case '/': // make left=right
  580. i = pk->pkwidth + pk->pkright;
  581. pk->pkwidth = pk->pkright = i/2;
  582. display = 1;
  583. field = 4;
  584. break;
  585. case 0x1041: // CTRL-A
  586. spectseq->SelectAll(1);
  587. Refresh();
  588. break;
  589. case 0x1042: // CTRL-B
  590. // toggle bass reduction
  591. spectseq->bass_reduction ^= 1;
  592. Refresh();
  593. break;
  594. case 0x1043: // CTRL-C
  595. spectseq->ClipboardCopy();
  596. break;
  597. case 0x1044: // CTRL-D
  598. // copy peaks down from previous/next keyframe
  599. if(event.ShiftDown())
  600. spectseq->CopyDown(sframe,1);
  601. else
  602. spectseq->CopyDown(sframe,-1);
  603. SetKeyframe(sf,1);
  604. display = 2;
  605. break;
  606. case 0x1047: // CTRL-G toggle grid
  607. spectseq->grid ^= 1;
  608. Refresh();
  609. break;
  610. case 0x1049: // CTRL-I interpolate between two frames
  611. spectseq->InterpolateAdjacent();
  612. display = 2;
  613. break;
  614. case 0x104b: // CTRL-K
  615. // spectseq->SetKlattDefaults();
  616. // display = 3;
  617. break;
  618. case 0x104d: // CTRL-M
  619. sf->ToggleMarker(1);
  620. display = 2;
  621. break;
  622. case 0x104e: // CTRL-N
  623. number = wxGetNumberFromUser(_T("Toggle Marker"),_T("Marker"),_T(""),1,0,7);
  624. sf->ToggleMarker(number);
  625. display = 2;
  626. break;
  627. case 0x104f: // CTRL-O
  628. spectseq->ConstructVowel();
  629. break;
  630. case 0x1051: // CTRL-Q
  631. if(event.ShiftDown())
  632. spectseq->InterpolatePeaks(0); // remove interpolation
  633. else
  634. spectseq->InterpolatePeaks(1); // show interpolation
  635. display = 3;
  636. break;
  637. case 0x1052: // CTRL-R
  638. value = sf->GetRms(spectseq->amplitude);
  639. break;
  640. case 0x1053: // CTRL-S
  641. Save(savepath);
  642. break;
  643. case 0x1056: // CTRL-V
  644. if(event.ShiftDown())
  645. {
  646. // insert frames from the clipboard
  647. sframe = spectseq->ClipboardInsert(sframe);
  648. SetExtent();
  649. display = 3;
  650. pitchgraph->Refresh();
  651. break;
  652. }
  653. // overwrite just the peaks
  654. if(clipboard_spect == NULL)
  655. break;
  656. i = 0;
  657. while((i < clipboard_spect->numframes) && ((sframe+i) < spectseq->numframes))
  658. {
  659. spectseq->frames[sframe+i]->CopyPeaks(clipboard_spect->frames[i]);
  660. i++;
  661. }
  662. display = 3;
  663. break;
  664. case 0x1058: // CTRL-X
  665. spectseq->ClipboardCopy();
  666. spectseq->DeleteSelected();
  667. SetExtent();
  668. sframe = 0;
  669. display = 3;
  670. pitchgraph->Refresh();
  671. break;
  672. case 0x105a: // CTRK-Z
  673. sf->ZeroPeaks();
  674. SetKeyframe(sf,0);
  675. display = 2;
  676. break;
  677. case WXK_F10:
  678. sf->ApplyVoiceMods();
  679. display = 2;
  680. break;
  681. case WXK_F2:
  682. // make and play sequence
  683. ReadDialogValues();
  684. spectseq->MakeWave(0,spectseq->numframes-1,voicedlg->pitchenv);
  685. break;
  686. case WXK_F1:
  687. // make and play selected
  688. ReadDialogValues();
  689. sf->MakeWaveF(0,voicedlg->pitchenv,spectseq->amplitude,spectseq->duration);
  690. break;
  691. case WXK_F3:
  692. // make and play selected
  693. ReadDialogValues();
  694. sf->MakeWaveF(1,voicedlg->pitchenv,spectseq->amplitude,spectseq->duration);
  695. break;
  696. default:
  697. if(key>='0' && key<='9')
  698. {
  699. i = key-'0';
  700. if(event.ControlDown())
  701. {
  702. if(i==0) i=10;
  703. PlayChild(i-1,voicedlg->pitchenv);
  704. }
  705. else
  706. {
  707. // select peak number
  708. if((pk_num = i) >= N_PEAKS) pk_num = N_PEAKS-1;
  709. }
  710. display=2;
  711. }
  712. event.Skip();
  713. break;
  714. }
  715. if(display)
  716. {
  717. pk_select = pk_num;
  718. sf = spectseq->frames[sframe];
  719. pk = &sf->peaks[pk_num];
  720. if(pk->pkwidth < 50) pk->pkwidth = 50; // min. width
  721. if(pk->pkright < 50) pk->pkright = 50;
  722. // ensure minimum separation between peaks & prevent crossover
  723. if(direction > 0)
  724. {
  725. for(i=pk_num+1; i<N_PEAKS; i++)
  726. {
  727. if(sf->peaks[i].pkfreq < sf->peaks[i-1].pkfreq + 100)
  728. sf->peaks[i].pkfreq = sf->peaks[i-1].pkfreq + 100;
  729. }
  730. }
  731. else
  732. if(direction < 0)
  733. {
  734. for(i=pk_num-1; i>=0; i--)
  735. {
  736. if(sf->peaks[i].pkfreq > sf->peaks[i+1].pkfreq - 100)
  737. sf->peaks[i].pkfreq = sf->peaks[i+1].pkfreq - 100;
  738. }
  739. }
  740. if(display==3)
  741. {
  742. formantdlg->ShowFrame(spectseq,sframe,pk_num,0xff);
  743. Refresh();
  744. }
  745. else
  746. {
  747. // only one frame needs to be redrawn
  748. formantdlg->ShowFrame(spectseq,sframe,pk_num,field);
  749. RefreshFrame(sframe);
  750. }
  751. if(sframe >= 0)
  752. {
  753. if(ScrollToFrame(sframe,0))
  754. Refresh(); // shouldn't need this, but we have redraw problems
  755. }
  756. }
  757. } // end of SpectDisplay::OnKey
  758. void SpectDisplay::OnMouse(wxMouseEvent& event)
  759. {//========================================
  760. int frame;
  761. int ix;
  762. if(event.RightDown())
  763. {
  764. PopupMenu(menu_spectdisplay);
  765. return;
  766. }
  767. wxClientDC dc(this);
  768. PrepareDC(dc);
  769. wxPoint pt(event.GetLogicalPosition(dc));
  770. if(spectseq->numframes==0) return;
  771. frame = (int)(pt.y/(FRAME_HEIGHT*zoomy));
  772. if(!event.ControlDown())
  773. spectseq->SelectAll(0);
  774. if(event.ShiftDown())
  775. {
  776. if(sframe >= 0)
  777. {
  778. if(frame < sframe)
  779. for(ix=frame; ix<=sframe; ix++)
  780. spectseq->frames[ix]->selected =1;
  781. else
  782. for(ix=sframe; ix<=frame && ix<spectseq->numframes; ix++)
  783. spectseq->frames[ix]->selected =1;
  784. Refresh();
  785. }
  786. }
  787. else
  788. {
  789. if(frame < spectseq->numframes)
  790. spectseq->frames[frame]->selected ^= 1;
  791. Refresh();
  792. }
  793. if(frame < spectseq->numframes)
  794. {
  795. formantdlg->GetValues(spectseq,sframe);
  796. if(sframe != frame)
  797. formantdlg->ShowFrame(spectseq,frame,pk_num,0xff);
  798. sframe = frame;
  799. }
  800. } // end of SpectDisplay::OnMouse
  801. void MyFrame::OnNewWindow(wxCommandEvent& event)
  802. {//=============================================
  803. SpectSeq *spectseq;
  804. wxString leaf;
  805. wxString pathload;
  806. int width, height;
  807. if(event.GetId() == MENU_SPECTRUM)
  808. pathload = path_spectload;
  809. else
  810. pathload = path_spectload2;
  811. wxString filename = wxFileSelector(_T("Read spectrum or praat data"),pathload,
  812. _T(""),_T(""),_T("*"),wxOPEN);
  813. if(filename.IsEmpty())
  814. {
  815. return;
  816. }
  817. // create SpectSeq and import data
  818. spectseq = new SpectSeq;
  819. if(spectseq == NULL)
  820. {
  821. wxLogError(_T("Failed to create SpectSeq"));
  822. return;
  823. }
  824. wxFileInputStream stream(filename);
  825. if(stream.Ok() == FALSE)
  826. {
  827. wxLogError(_T("Failed to open '%s'"),filename.c_str());
  828. return;
  829. }
  830. wxFileName path = wxFileName(filename);
  831. leaf = path.GetName();
  832. setlocale(LC_NUMERIC,"C"); // read numbers in the form 1.23456
  833. spectseq->Load(stream);
  834. spectseq->name = leaf;
  835. spectseq->MakePitchenv(spectseq->pitchenv,0,spectseq->numframes-1);
  836. if(event.GetId() == MENU_SPECTRUM)
  837. path_spectload = path.GetPath();
  838. else
  839. path_spectload2 = path.GetPath();
  840. // Make another frame, containing a canvas
  841. GetClientSize(&width, &height);
  842. MyChild *subframe = new MyChild(myframe, _T("Spectrum"),
  843. wxPoint(10, 0), wxSize(500, height),
  844. wxDEFAULT_FRAME_STYLE |
  845. wxNO_FULL_REPAINT_ON_RESIZE);
  846. subframe->SetTitle(leaf);
  847. // Give it a status line
  848. subframe->CreateStatusBar();
  849. subframe->GetClientSize(&width, &height);
  850. SpectDisplay *canvas = new SpectDisplay(subframe, wxPoint(0, 0), wxSize(width, height), spectseq);
  851. canvas->savepath = filename;
  852. currentcanvas = canvas;
  853. // Associate the menu bar with the frame
  854. subframe->SetMenuBar(MakeMenu(1,translator->dictionary_name));
  855. subframe->canvas = canvas;
  856. subframe->Show(TRUE);
  857. }
  858. void InitSpectrumDisplay()
  859. {//=======================
  860. wxMenu *menu_markers = new wxMenu;
  861. menu_spectdisplay = new wxMenu;
  862. menu_markers->Append(301,_T("1 Vowel break point"));
  863. menu_markers->Append(302,_T("2 Reduce length changes"));
  864. menu_markers->Append(303,_T("3 Break F0 to F2"));
  865. menu_markers->Append(304,_T("4 Break. All formants"));
  866. menu_markers->Append(305,_T("5 Allow greater rate of formant change"));
  867. menu_markers->Append(306,_T("6 Trill"));
  868. menu_markers->Append(307,_T("7 Defer wav mixing"));
  869. menu_spectdisplay->Append(201,_T("Copy Peaks Down CTRL+D"));
  870. menu_spectdisplay->Append(202,_T("Copy Peaks Up SHIFT+CTRL+D"));
  871. menu_spectdisplay->Append(204,_T("Grid (toggle) CTRL+G"));
  872. menu_spectdisplay->Append(210,_T("Interpolate (percentage) CTRL+I"));
  873. menu_spectdisplay->Append(212,_T("Convert sequence to Klatt format CTRL+K"));
  874. menu_spectdisplay->Append(203,_T("Marker (toggle) CTRL+N"));
  875. menu_spectdisplay->Append(206,_T("Show Interpolation CTRL+Q"));
  876. menu_spectdisplay->Append(207,_T("Hide Interpolation CTRL+SHIFT+Q"));
  877. menu_spectdisplay->Append(205,_T("Zero Peaks CTRL+Z"));
  878. menu_spectdisplay->Append(210,_T("Toggle Marker"),menu_markers);
  879. menu_spectdisplay->Append(211,_T("Apply the formant adjustments from a voice file"));
  880. menu_spectdisplay->AppendSeparator();
  881. menu_spectdisplay->Append(208,_T("Play Selected Frame F1"));
  882. menu_spectdisplay->Append(209,_T("Play All F2"));
  883. }