eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

spectdisplay.cpp 23KB

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