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

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