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

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