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.

prosodydisplay.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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/numdlg.h"
  21. #include "speak_lib.h"
  22. #include "main.h"
  23. #include "speech.h"
  24. #include "phoneme.h"
  25. #include "synthesize.h"
  26. #include "prosodydisplay.h"
  27. extern MyFrame *myframe;
  28. extern ChildFrProsody *prosodyframe;
  29. extern ProsodyDisplay *prosodycanvas;
  30. wxMenu *menu_prosody;
  31. BEGIN_EVENT_TABLE(ProsodyDisplay, wxScrolledWindow)
  32. EVT_LEFT_DOWN(ProsodyDisplay::OnMouse)
  33. EVT_RIGHT_DOWN(ProsodyDisplay::OnMouse)
  34. EVT_CHAR(ProsodyDisplay::OnKey)
  35. EVT_MENU(-1, ProsodyDisplay::OnMenu)
  36. END_EVENT_TABLE()
  37. static wxPen PEN_PITCHENV(wxColour(0,0,255),1,wxSOLID);
  38. static wxPen PEN_SAMPLED(wxColour(0,200,200),2,wxSOLID);
  39. static wxPen PEN_PHSELECTED(wxColour(255,0,0),2,wxSOLID);
  40. ProsodyDisplay::ProsodyDisplay(wxWindow *parent, const wxPoint& pos, const wxSize& size)
  41. : wxScrolledWindow(parent, -1, pos, size,
  42. wxSUNKEN_BORDER | wxNO_FULL_REPAINT_ON_RESIZE)
  43. {//=====================================================================
  44. linewidth = size.GetWidth();
  45. scalex = 0.5;
  46. scaley = double(LINESEP*6)/80.0;
  47. selected_ph = -1;
  48. // SetBackgroundColour(* wxWHITE);
  49. LayoutData(phoneme_list,n_phoneme_list);
  50. } // end of ProsodyDisplay::ProsodyDisplay
  51. ProsodyDisplay::~ProsodyDisplay()
  52. {//==========================
  53. prosodycanvas = NULL;
  54. }
  55. void InitProsodyDisplay()
  56. {//======================
  57. wxMenu *menu_envelopes;
  58. menu_envelopes = new wxMenu;
  59. // entries match those in envelope_data[] in intonation.cpp
  60. menu_envelopes->Append(0x100,_T("Fall"));
  61. menu_envelopes->Append(0x101,_T("Rise"));
  62. menu_envelopes->Append(0x102,_T("Fall-rise"));
  63. menu_envelopes->Append(0x103,_T("Fall-rise (R)"));
  64. menu_envelopes->Append(0x104,_T("Fall-rise 2"));
  65. menu_envelopes->Append(0x105,_T("Fall-rise 2(R)"));
  66. menu_envelopes->Append(0x106,_T("Rise-fall"));
  67. menu_envelopes->Append(0x108,_T("Fall-rise 3"));
  68. menu_envelopes->Append(0x10a,_T("Fall-rise 4"));
  69. menu_envelopes->Append(0x10c,_T("Fall 2"));
  70. menu_envelopes->Append(0x10e,_T("Rise 2"));
  71. menu_envelopes->Append(0x110,_T("Rise-fall-rise"));
  72. menu_prosody = new wxMenu;
  73. menu_prosody->Append(1,_T("Pitch envelope"),menu_envelopes);
  74. menu_prosody->Append(2,_T("Amplitude"));
  75. menu_prosody->Append(3,_T("Length"));
  76. menu_prosody->Append(4,_T("Play F2"));
  77. }
  78. void ProsodyDisplay::RefreshLine(int line)
  79. {//=====================================
  80. int x,y;
  81. CalcScrolledPosition(0,line*FRAMEHEIGHT,&x,&y);
  82. RefreshRect(wxRect(0,y,linewidth,FRAMEHEIGHT));
  83. }
  84. int ProsodyDisplay::GetWidth(PHONEME_LIST *p)
  85. {//========================================
  86. int w;
  87. if(p->ph == NULL)
  88. return(0);
  89. w = p->ph->std_length;
  90. if(w == 0) w = 60;
  91. if(p->length != 0)
  92. w = (w * p->length) / 256;
  93. return(int((w + p->prepause)* scalex) + 1);
  94. }
  95. void ProsodyDisplay::SelectPh(int index)
  96. {//=====================================
  97. // A phoneme has been selected
  98. PHONEME_LIST *p;
  99. const char *emphasized;
  100. char buf[120];
  101. if(index < 0) return;
  102. p = &phlist[index];
  103. if((p == NULL) || (p->ph == NULL)) return;
  104. emphasized = "";
  105. if(p->tone & 8)
  106. emphasized = "*";
  107. sprintf(buf,"Stress %s%d Amp %2d LenMod %2d Pitch %3d %3d [env=%d] Flags %.2x ",
  108. emphasized,p->tone&0x7,p->amp,p->length,p->pitch1,p->pitch2,p->env,p->ph->phflags);
  109. wxLogStatus(wxString(buf,wxConvLocal));
  110. }
  111. void ProsodyDisplay::ChangePh(int pitch1, int pitch2)
  112. {//================================================
  113. PHONEME_LIST *p;
  114. int sign1;
  115. int sign2;
  116. if(selected_ph < 0)
  117. return;
  118. p = &phlist[selected_ph];
  119. sign1 = p->pitch1 - p->pitch2;
  120. p->pitch1 += pitch1;
  121. p->pitch2 += (pitch1 + pitch2);
  122. sign2 = p->pitch1 - p->pitch2;
  123. if((sign1 != 0) && ((sign1 * sign2) <= 0))
  124. {
  125. // change of sign, change rise to fall
  126. if(p->env == 1)
  127. p->env = 0;
  128. else
  129. if(p->env == 0)
  130. p->env = 1;
  131. }
  132. }
  133. void ProsodyDisplay::OnMenu(wxCommandEvent& event)
  134. {//===============================================
  135. int id;
  136. int value;
  137. PHONEME_LIST *p;
  138. id = event.GetId();
  139. p = &phlist[selected_ph];
  140. if((id & 0xf00) == 0x100)
  141. {
  142. // selected a pitch envelope
  143. p->env = id - 0x100;
  144. }
  145. switch(id)
  146. {
  147. case 2:
  148. value = wxGetNumberFromUser(_T(""),_T("Amplitude"),_T(""),p->amp,0,40);
  149. if(value >= 0)
  150. p->amp = value;
  151. break;
  152. case 3:
  153. value = wxGetNumberFromUser(_T(""),_T("Length"),_T(""),p->length,1,500);
  154. if(value >= 0)
  155. p->length = value;
  156. break;
  157. case 4:
  158. MakeWave2(phlist,numph);
  159. break;
  160. }
  161. SelectPh(selected_ph);
  162. Refresh();
  163. }
  164. void ProsodyDisplay::OnMouse(wxMouseEvent& event)
  165. {//============================================
  166. int line;
  167. int ix;
  168. int xpos=0;
  169. wxClientDC dc(this);
  170. PrepareDC(dc);
  171. wxPoint pt(event.GetLogicalPosition(dc));
  172. if(selected_ph >= 0)
  173. {
  174. // find line for previously selected phoneme
  175. for(line=0; line<num_lines; line++)
  176. if(linetab[line+1] > selected_ph) break;
  177. RefreshLine(line);
  178. selected_ph = -1;
  179. }
  180. line = pt.y / FRAMEHEIGHT;
  181. // find which phoneme is selected on this line
  182. for(ix=linetab[line]; (ix<linetab[line+1]) && (ix<numph); ix++)
  183. {
  184. xpos += GetWidth(&phlist[ix]);
  185. if(xpos > pt.x)
  186. {
  187. selected_ph = ix;
  188. SelectPh(selected_ph);
  189. break;
  190. }
  191. }
  192. RefreshLine(line);
  193. if(event.RightDown())
  194. {
  195. PopupMenu(menu_prosody);
  196. }
  197. } // end of ProsodyDisplay::OnMouse
  198. void ProsodyDisplay::OnKey(wxKeyEvent& event)
  199. {//========================================
  200. PHONEME_LIST *p;
  201. int display=1;
  202. if(selected_ph < 0)
  203. selected_ph = 0;
  204. p = &phlist[selected_ph];
  205. switch(event.GetKeyCode())
  206. {
  207. case WXK_F2:
  208. // make and play from this clause
  209. MakeWave2(phlist,numph);
  210. break;
  211. case WXK_LEFT:
  212. if(selected_ph > 1)
  213. selected_ph--;
  214. break;
  215. case WXK_RIGHT:
  216. if(selected_ph < (numph-2))
  217. selected_ph++;
  218. break;
  219. case WXK_UP:
  220. if(event.ControlDown())
  221. ChangePh(-1,2);
  222. else
  223. ChangePh(1,0);
  224. display = 1;
  225. break;
  226. case WXK_DOWN:
  227. if(event.ControlDown())
  228. ChangePh(1,-2);
  229. else
  230. ChangePh(-1,0);
  231. break;
  232. case ',':
  233. case '<':
  234. if(p->length > 0)
  235. p->length--;
  236. break;
  237. case '.':
  238. case '>':
  239. p->length++;
  240. break;
  241. case WXK_TAB:
  242. display = 0;
  243. event.Skip();
  244. transldlg->SetFocus();
  245. break;
  246. default:
  247. display = 0;
  248. event.Skip();
  249. break;
  250. }
  251. if(display)
  252. {
  253. Refresh();
  254. SelectPh(selected_ph);
  255. }
  256. } // end of ProsodyDisplay::OnKey
  257. void ProsodyDisplay::DrawEnv(wxDC& dc, int x1, int y1, int width, PHONEME_LIST *ph)
  258. {//==============================================================================
  259. int pitchr;
  260. int pitch;
  261. int p1;
  262. int ix;
  263. int x,y;
  264. int y2=0;
  265. unsigned char *env;
  266. if(width <= 0) return;
  267. if((pitchr = ph->pitch2 - ph->pitch1) < 0)
  268. {
  269. pitchr = -pitchr;
  270. p1 = ph->pitch2;
  271. }
  272. else
  273. {
  274. p1 = ph->pitch1;
  275. }
  276. dc.SetPen(PEN_PITCHENV);
  277. env = envelope_data[ph->env];
  278. if((ph->type == phVOWEL) && (ph->tone_ph != 0))
  279. {
  280. // the envelope is given by a Tone phoneme acting on this vowel
  281. env = LookupEnvelope(phoneme_tab[ph->tone_ph]->spect);
  282. }
  283. for(ix=0; ix<=width; ix+=4)
  284. {
  285. x = int((ix * 127.9)/width);
  286. pitch = p1 + (pitchr * env[x])/256;
  287. y = y1-int(pitch * scaley);
  288. if(ix > 0)
  289. dc.DrawLine(x1+ix-4,y2,x1+ix,y);
  290. y2 = y;
  291. }
  292. } // end of DrawEnv
  293. static void GetPhonemeName(PHONEME_TAB *ph, wxString& string)
  294. {//==========================================================
  295. string = wxString(WordToString(ph->mnemonic),wxConvLocal);
  296. }
  297. void ProsodyDisplay::DrawPitchline(wxDC& dc, int line, int x1, int x2)
  298. {//=================================================================
  299. int ix;
  300. int endix;
  301. int y;
  302. int offy;
  303. int xpos;
  304. int width; // total width, including pre-pause
  305. int width2; // width without pre-pause
  306. int width_env;
  307. int textwidth, textheight;
  308. wxString string;
  309. PHONEME_LIST *p;
  310. if(linetab[line] >= numph) return;
  311. offy = (line+1) * FRAMEHEIGHT - 1;
  312. y = LINEBASE+LINESEP;
  313. dc.SetPen(*wxLIGHT_GREY_PEN);
  314. for(ix=0; ix<5; ix++)
  315. {
  316. dc.DrawLine(0,offy-y,linewidth,offy-y);
  317. y += LINESEP;
  318. }
  319. endix = linetab[line+1];
  320. xpos = 0;
  321. for(ix=linetab[line]; ix<endix; ix++)
  322. {
  323. if(ix < 0 || ix > numph-2) break;
  324. if(xpos > x2) break; // past the redraw region
  325. p = &phlist[ix];
  326. width = GetWidth(p);
  327. width2 = width - int(p->prepause * scalex) - 1;
  328. if(xpos+width < x1)
  329. {
  330. xpos += width;
  331. continue; // before the redraw region
  332. }
  333. // is this phoneme selected ?
  334. if(ix == selected_ph)
  335. {
  336. dc.SetPen(PEN_PHSELECTED);
  337. dc.DrawLine(xpos,offy-LINEBASE,xpos+width,offy-LINEBASE);
  338. }
  339. // draw separator bar
  340. if(p->newword)
  341. dc.SetPen(*wxBLACK_PEN); // word boundary
  342. else
  343. dc.SetPen(*wxLIGHT_GREY_PEN);
  344. dc.DrawLine(xpos,offy-LINEBASE,xpos,offy-LINEBASE-LINESEP*6);
  345. // draw pitch envelope
  346. if(((p->ph->phflags & phWAVE) == 0) && (p->ph->type != phPAUSE))
  347. {
  348. if(!(p->synthflags & SFLAG_SEQCONTINUE))
  349. {
  350. width_env = width2;
  351. if(phlist[ix+1].synthflags & SFLAG_SEQCONTINUE)
  352. width_env += GetWidth(&phlist[ix+1]);
  353. DrawEnv(dc,xpos+1+(width-width2),offy-LINEBASE,width_env,p);
  354. }
  355. }
  356. else
  357. if(p->type != phPAUSE)
  358. {
  359. // sampled sound, draw a flat line
  360. dc.SetPen(PEN_SAMPLED);
  361. dc.DrawLine(xpos+1+(width-width2),offy-LINEBASE-LINESEP,
  362. xpos+1+width,offy-LINEBASE-LINESEP);
  363. }
  364. // show phoneme name from the PHONEME_TAB
  365. GetPhonemeName(p->ph,string);
  366. dc.GetTextExtent(string,&textwidth,&textheight);
  367. dc.DrawText(string,xpos+(width-textwidth/2)/2, offy-LINEBASE);
  368. xpos += width;
  369. }
  370. // draw the separator bar at the end of the line
  371. if(ix==endix && ix<numph && phlist[ix].newword)
  372. dc.SetPen(*wxLIGHT_GREY_PEN);
  373. else
  374. dc.SetPen(*wxBLACK_PEN); // word boundary or end of list
  375. dc.DrawLine(xpos,offy-LINEBASE,xpos,offy-LINEBASE-LINESEP*6);
  376. } // end of ProsodyDisplay::DrawPitchline
  377. void ProsodyDisplay::OnDraw(wxDC& dc)
  378. {//================================
  379. int x1,y1;
  380. int vX,vY,vW,vH; // Dimensions of client area in pixels
  381. int line, start, end;
  382. wxRegionIterator upd(GetUpdateRegion()); // get the update rect list
  383. while (upd)
  384. {
  385. vX = upd.GetX();
  386. vY = upd.GetY();
  387. vW = upd.GetW();
  388. vH = upd.GetH();
  389. CalcUnscrolledPosition(vX,vY,&x1,&y1);
  390. // Repaint this rectangle, find which lines to redraw
  391. start = y1/FRAMEHEIGHT;
  392. end = (y1+vH)/FRAMEHEIGHT;
  393. for(line=start; line<=end && line<num_lines; line++)
  394. DrawPitchline(dc,line,x1,x1+vW);
  395. upd ++ ;
  396. }
  397. } // end of ProsodyDisplay::OnDraw
  398. void ProsodyDisplay::LayoutData(PHONEME_LIST *ph_list, int n_ph)
  399. {//===========================================================
  400. // divide the phoneme list into lines for display
  401. int xpos;
  402. int w;
  403. int ix;
  404. PHONEME_LIST *p;
  405. numph = n_ph;
  406. phlist = ph_list;
  407. num_lines = 0;
  408. linetab[0] = 1;
  409. xpos = linewidth;
  410. // could improve this to do 'wordwrap' - only split on word boundary
  411. // or unvoiced phonemes
  412. for(ix=1; ix<numph-2; ix++)
  413. {
  414. p = &phlist[ix];
  415. w = GetWidth(p);
  416. if(w + xpos >= linewidth)
  417. {
  418. linetab[num_lines++] = ix;
  419. xpos = 0;
  420. }
  421. xpos += w;
  422. }
  423. linetab[num_lines]=numph-2;
  424. SetScrollbars(SCROLLUNITS,SCROLLUNITS,linewidth/SCROLLUNITS,
  425. (num_lines*FRAMEHEIGHT)/SCROLLUNITS+1);
  426. Refresh();
  427. } // end of ProsodyDisplay::LayoutData
  428. void MyFrame::OnProsody(wxCommandEvent& WXUNUSED(event))
  429. {//=====================================================
  430. // Open the Prosody display window
  431. // Make another frame, containing a canvas
  432. if(prosodyframe != NULL)
  433. {
  434. // The Prosody window is already open
  435. prosodyframe->Activate();
  436. return;
  437. }
  438. prosodyframe = new ChildFrProsody(myframe, _T(""),
  439. wxPoint(10, 200), wxSize(1000, 300),
  440. wxDEFAULT_FRAME_STYLE |
  441. wxNO_FULL_REPAINT_ON_RESIZE);
  442. prosodyframe->SetTitle(_T("Prosody"));
  443. // Give it a status line
  444. prosodyframe->CreateStatusBar();
  445. int width, height;
  446. wxMDIClientWindow *clientwin = this->GetClientWindow();
  447. clientwin->GetClientSize(&width, &height);
  448. #ifdef deleted
  449. wxPanel *panel = new wxPanel(prosodyframe,-1,wxPoint(0,0), wxSize(width,50));
  450. ProsodyDisplay *canvas = new ProsodyDisplay(prosodyframe, wxPoint(0, 50), wxSize(width-2, height-50));
  451. #else
  452. ProsodyDisplay *canvas = new ProsodyDisplay(prosodyframe, wxPoint(0, 50), wxSize(width-10, height));
  453. #endif
  454. prosodycanvas = canvas;
  455. // Associate the menu bar with the frame
  456. prosodyframe->SetMenuBar(MakeMenu(2));
  457. prosodyframe->prosodycanvas = canvas;
  458. prosodyframe->Show(TRUE);
  459. }
  460. BEGIN_EVENT_TABLE(ChildFrProsody, wxMDIChildFrame)
  461. EVT_MENU(SPECTSEQ_CLOSE, ChildFrProsody::OnQuit)
  462. // EVT_ACTIVATE(ChildFrProsody::OnActivate)
  463. END_EVENT_TABLE()
  464. extern wxList my_children;
  465. ChildFrProsody::ChildFrProsody(wxMDIParentFrame *parent, const wxString& title, const wxPoint& pos, const wxSize& size,
  466. const long style):
  467. wxMDIChildFrame(parent, -1, title, pos, size, style)
  468. {
  469. my_children.Append(this);
  470. }
  471. ChildFrProsody::~ChildFrProsody(void)
  472. {
  473. wxWindow *w;
  474. my_children.DeleteObject(this);
  475. prosodycanvas = NULL;
  476. prosodyframe = NULL;
  477. #ifndef PLATFORM_WINDOWS
  478. // bug in wxMDIChildFrame, we need to explicitly remove the ChildFrame from the ClientWindow
  479. w = myframe->GetClientWindow();
  480. w->RemoveChild(this);
  481. #endif
  482. }
  483. void ChildFrProsody::OnQuit(wxCommandEvent& WXUNUSED(event))
  484. {
  485. Close(TRUE);
  486. }