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

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