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

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