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 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2013 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. #include "translate.h"
  28. extern MyFrame *myframe;
  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. static wxPen PEN_PHSTRESSED(wxColour(80,80,196),3,wxSOLID);
  41. static wxPen PEN_PHSTRESSED2(wxColour(160,160,255),2,wxSOLID);
  42. typedef struct {
  43. unsigned int value;
  44. char *name;
  45. } NAMETAB;
  46. NAMETAB *manifest = NULL;
  47. int n_manifest;
  48. const char *LookupManifest(unsigned int addr)
  49. {//=============================================
  50. int ix;
  51. if(manifest != NULL)
  52. {
  53. for(ix=0; ix < n_manifest; ix++)
  54. {
  55. if(manifest[ix].value == addr)
  56. return(manifest[ix].name);
  57. if(manifest[ix].value > addr)
  58. break;
  59. }
  60. }
  61. return("");
  62. }
  63. void ReadPhondataManifest()
  64. {//=========================
  65. // Read the phondata-manifest file
  66. FILE *f;
  67. int n_lines=0;
  68. int ix;
  69. char *p;
  70. unsigned int value;
  71. char buf[sizeof(path_home)+40];
  72. char name[120];
  73. sprintf(buf,"%s%c%s",path_home,PATHSEP,"phondata-manifest");
  74. if((f = fopen(buf, "r")) == NULL)
  75. return;
  76. while(fgets(buf, sizeof(buf), f) != NULL)
  77. n_lines++;
  78. rewind(f);
  79. if(manifest != NULL)
  80. {
  81. for(ix=0; ix < n_manifest; ix++)
  82. free(manifest[ix].name);
  83. }
  84. if((manifest = (NAMETAB *)realloc(manifest, n_lines * sizeof(NAMETAB))) == NULL)
  85. return;
  86. n_manifest = 0;
  87. while(fgets(buf, sizeof(buf), f) != NULL)
  88. {
  89. if(!isalpha(buf[0]))
  90. continue;
  91. if(sscanf(&buf[2], "%x %s", &value, name) == 2)
  92. {
  93. if((p = (char *)malloc(strlen(name)+1)) != NULL)
  94. {
  95. strcpy(p, name);
  96. manifest[n_manifest].value = value;
  97. manifest[n_manifest].name = p;
  98. n_manifest++;
  99. }
  100. }
  101. }
  102. fclose(f);
  103. }
  104. ProsodyDisplay::ProsodyDisplay(wxWindow *parent, const wxPoint& pos, const wxSize& size)
  105. : wxScrolledWindow(parent, -1, pos, size,
  106. wxSUNKEN_BORDER | wxNO_FULL_REPAINT_ON_RESIZE)
  107. {//=====================================================================
  108. linewidth = size.GetWidth();
  109. scalex = 0.5;
  110. scaley = double(LINESEP*6)/150.0;
  111. selected_ph = -1;
  112. SetBackgroundColour(wxColour(245,245,245));
  113. } // end of ProsodyDisplay::ProsodyDisplay
  114. ProsodyDisplay::~ProsodyDisplay()
  115. {//==========================
  116. prosodycanvas = NULL;
  117. }
  118. extern MNEM_TAB envelope_names[];
  119. void InitProsodyDisplay()
  120. {//======================
  121. wxMenu *menu_envelopes;
  122. int ix;
  123. wxString string;
  124. ReadPhondataManifest();
  125. menu_envelopes = new wxMenu;
  126. // entries match those in envelope_data[] in intonation.cpp
  127. for(ix=0; envelope_names[ix].mnem != NULL; ix++)
  128. {
  129. string = wxString(envelope_names[ix].mnem, wxConvLocal);
  130. menu_envelopes->Append(0x100+envelope_names[ix].value, string);
  131. }
  132. #ifdef deleted
  133. menu_envelopes->Append(0x100,_T("fall"));
  134. menu_envelopes->Append(0x102,_T("rise"));
  135. menu_envelopes->Append(0x104,_T("fall-rise"));
  136. // menu_envelopes->Append(0x105,_T("fall-rise (R)"));
  137. menu_envelopes->Append(0x106,_T("fall-rise 2"));
  138. // menu_envelopes->Append(0x107,_T("fall-rise 2(R)"));
  139. menu_envelopes->Append(0x108,_T("rise-fall"));
  140. menu_envelopes->Append(0x10a,_T("fall-rise 3"));
  141. menu_envelopes->Append(0x10c,_T("fall-rise 4"));
  142. menu_envelopes->Append(0x10e,_T("fall 2"));
  143. menu_envelopes->Append(0x110,_T("rise 2"));
  144. menu_envelopes->Append(0x112,_T("rise-fall-rise"));
  145. #endif
  146. menu_prosody = new wxMenu;
  147. menu_prosody->Append(1,_T("Pitch envelope"),menu_envelopes);
  148. menu_prosody->Append(2,_T("Amplitude"));
  149. menu_prosody->Append(3,_T("Length"));
  150. menu_prosody->Append(4,_T("Play F2"));
  151. }
  152. void ProsodyDisplay::RefreshLine(int line)
  153. {//=====================================
  154. int x,y;
  155. CalcScrolledPosition(0,line*FRAMEHEIGHT,&x,&y);
  156. RefreshRect(wxRect(0,y,linewidth,FRAMEHEIGHT));
  157. }
  158. int ProsodyDisplay::GetWidth(PHONEME_LIST *p)
  159. {//========================================
  160. int w;
  161. if(p->ph == NULL)
  162. return(0);
  163. w = (p->ph->std_length * 2);
  164. if(w == 0) w = 60;
  165. if(p->length != 0)
  166. w = (w * p->length) / 256;
  167. return(int((w + p->prepause)* scalex) + 1);
  168. }
  169. void ProsodyDisplay::SelectPh(int index)
  170. {//=====================================
  171. // A phoneme has been selected
  172. PHONEME_LIST *p;
  173. const char *emphasized;
  174. int y1, y2;
  175. int ix;
  176. const char *name = "?";
  177. char buf[120];
  178. char len_string[20];
  179. char param_string[20];
  180. if(index < 0) return;
  181. p = &phlist[index];
  182. if((p == NULL) || (p->ph == NULL)) return;
  183. emphasized = "";
  184. if(p->stresslevel & 8)
  185. emphasized = "*";
  186. for(ix=0; envelope_names[ix].mnem != NULL; ix++)
  187. {
  188. if(envelope_names[ix].value == (p->env & 0xfe))
  189. {
  190. name = envelope_names[ix].mnem;
  191. break;
  192. }
  193. }
  194. y1 = p->pitch1;
  195. y2 = p->pitch2;
  196. len_string[0] = 0;
  197. param_string[0] = 0;
  198. if(p->std_length > 0)
  199. sprintf(len_string," Length %d", p->std_length*2);
  200. if(p->sound_param != 0)
  201. sprintf(param_string,", %d", p->sound_param);
  202. sprintf(buf,"Stress %s%d Amp %2d LengthMod %2d Pitch %3d %3d %s PhFlags %.2x (%s%s)%s",
  203. emphasized,p->stresslevel&0x7,p->amp, p->length,y1,y2,name,p->ph->phflags, LookupManifest(p->phontab_addr), param_string, len_string);
  204. wxLogStatus(wxString(buf,wxConvLocal));
  205. }
  206. void ProsodyDisplay::ChangePh(int pitch1, int pitch2)
  207. {//================================================
  208. PHONEME_LIST *p;
  209. int sign1;
  210. int sign2;
  211. if(selected_ph < 0)
  212. return;
  213. p = &phlist[selected_ph];
  214. sign1 = p->pitch1 - p->pitch2;
  215. p->pitch1 += pitch1;
  216. p->pitch2 += (pitch1 + pitch2);
  217. sign2 = p->pitch1 - p->pitch2;
  218. if((sign1 != 0) && ((sign1 * sign2) <= 0))
  219. {
  220. // change of sign, change rise to fall
  221. if(p->env == 1)
  222. p->env = 0;
  223. else
  224. if(p->env == 0)
  225. p->env = 1;
  226. }
  227. }
  228. void ProsodyDisplay::OnMenu(wxCommandEvent& event)
  229. {//===============================================
  230. int id;
  231. int value;
  232. PHONEME_LIST *p;
  233. id = event.GetId();
  234. p = &phlist[selected_ph];
  235. if((id & 0xf00) == 0x100)
  236. {
  237. // selected a pitch envelope
  238. p->env = id - 0x100;
  239. }
  240. switch(id)
  241. {
  242. case 2:
  243. value = wxGetNumberFromUser(_T(""),_T("Amplitude"),_T(""),p->amp,0,40);
  244. if(value >= 0)
  245. p->amp = value;
  246. break;
  247. case 3:
  248. value = wxGetNumberFromUser(_T(""),_T("Length"),_T(""),p->length,1,500);
  249. if(value >= 0)
  250. p->length = value;
  251. break;
  252. case 4:
  253. MakeWave2(phlist,numph);
  254. break;
  255. }
  256. SelectPh(selected_ph);
  257. Refresh();
  258. }
  259. void ProsodyDisplay::OnMouse(wxMouseEvent& event)
  260. {//============================================
  261. int line;
  262. int ix;
  263. int xpos=0;
  264. wxClientDC dc(this);
  265. PrepareDC(dc);
  266. wxPoint pt(event.GetLogicalPosition(dc));
  267. if(selected_ph >= 0)
  268. {
  269. // find line for previously selected phoneme
  270. for(line=0; line<num_lines; line++)
  271. if(linetab[line+1] > selected_ph) break;
  272. RefreshLine(line);
  273. selected_ph = -1;
  274. }
  275. line = pt.y / FRAMEHEIGHT;
  276. // find which phoneme is selected on this line
  277. for(ix=linetab[line]; (ix<linetab[line+1]) && (ix<numph); ix++)
  278. {
  279. xpos += GetWidth(&phlist[ix]);
  280. if(xpos > pt.x)
  281. {
  282. selected_ph = ix;
  283. SelectPh(selected_ph);
  284. break;
  285. }
  286. }
  287. RefreshLine(line);
  288. if(event.RightDown())
  289. {
  290. PopupMenu(menu_prosody);
  291. }
  292. } // end of ProsodyDisplay::OnMouse
  293. void ProsodyDisplay::OnKey(wxKeyEvent& event)
  294. {//========================================
  295. PHONEME_LIST *p;
  296. int display=1;
  297. if(selected_ph < 0)
  298. selected_ph = 0;
  299. p = &phlist[selected_ph];
  300. switch(event.GetKeyCode())
  301. {
  302. case WXK_F2:
  303. // make and play from this clause
  304. MakeWave2(phlist,numph);
  305. break;
  306. case WXK_LEFT:
  307. if(selected_ph > 1)
  308. selected_ph--;
  309. break;
  310. case WXK_RIGHT:
  311. if(selected_ph < (numph-2))
  312. selected_ph++;
  313. break;
  314. case WXK_UP:
  315. if(event.ControlDown())
  316. ChangePh(-1,2);
  317. else
  318. ChangePh(1,0);
  319. display = 1;
  320. break;
  321. case WXK_DOWN:
  322. if(event.ControlDown())
  323. ChangePh(1,-2);
  324. else
  325. ChangePh(-1,0);
  326. break;
  327. case ',':
  328. case '<':
  329. if(p->length > 0)
  330. p->length--;
  331. break;
  332. case '.':
  333. case '>':
  334. p->length++;
  335. break;
  336. case WXK_TAB:
  337. display = 0;
  338. event.Skip();
  339. transldlg->SetFocus();
  340. break;
  341. default:
  342. display = 0;
  343. event.Skip();
  344. break;
  345. }
  346. if(display)
  347. {
  348. Refresh();
  349. SelectPh(selected_ph);
  350. }
  351. } // end of ProsodyDisplay::OnKey
  352. void ProsodyDisplay::DrawEnv(wxDC& dc, int x1, int y1, int width, PHONEME_LIST *ph)
  353. {//==============================================================================
  354. int pitchr;
  355. int pitch;
  356. int p1;
  357. int ix;
  358. int x,y;
  359. int y2=0;
  360. unsigned char *env;
  361. PHONEME_DATA phdata_tone;
  362. if(width <= 0) return;
  363. if((pitchr = ph->pitch2 - ph->pitch1) < 0)
  364. {
  365. pitchr = -pitchr;
  366. p1 = ph->pitch2;
  367. }
  368. else
  369. {
  370. p1 = ph->pitch1;
  371. }
  372. if(p1 == 255) return;
  373. dc.SetPen(PEN_PITCHENV);
  374. env = envelope_data[ph->env];
  375. if((ph->type == phVOWEL) && (ph->tone_ph != 0))
  376. {
  377. // the envelope is given by a Tone phoneme acting on this vowel
  378. InterpretPhoneme2(ph->tone_ph, &phdata_tone);
  379. env = GetEnvelope(phdata_tone.pitch_env);
  380. }
  381. if(env == NULL)
  382. return;
  383. for(ix=0; ix<=width; ix+=4)
  384. {
  385. x = int((ix * 127.9)/width);
  386. pitch = p1 + (pitchr * env[x])/256;
  387. y = y1-int(pitch * scaley);
  388. if(ix > 0)
  389. dc.DrawLine(x1+ix-4,y2,x1+ix,y);
  390. y2 = y;
  391. }
  392. } // end of DrawEnv
  393. static void GetPhonemeName(PHONEME_TAB *ph, wxString& string)
  394. {//==========================================================
  395. string = wxString(WordToString(ph->mnemonic),wxConvLocal);
  396. }
  397. void ProsodyDisplay::DrawPitchline(wxDC& dc, int line, int x1, int x2)
  398. {//=================================================================
  399. int ix;
  400. int endix;
  401. int y;
  402. int offy;
  403. int xpos;
  404. int width; // total width, including pre-pause
  405. int width2; // width without pre-pause
  406. int width_env;
  407. int textwidth, textheight;
  408. wxString string;
  409. PHONEME_LIST *p;
  410. if(linetab[line] >= numph) return;
  411. offy = (line+1) * FRAMEHEIGHT - 1;
  412. y = LINEBASE+LINESEP;
  413. dc.SetPen(*wxLIGHT_GREY_PEN);
  414. // dc.SetPen(*wxCYAN_PEN);
  415. for(ix=0; ix<5; ix++)
  416. {
  417. dc.DrawLine(0,offy-y,linewidth,offy-y);
  418. y += LINESEP;
  419. }
  420. endix = linetab[line+1];
  421. xpos = 0;
  422. for(ix=linetab[line]; ix<endix; ix++)
  423. {
  424. if(ix < 0 || ix > numph-2) break;
  425. if(xpos > x2) break; // past the redraw region
  426. p = &phlist[ix];
  427. width = GetWidth(p);
  428. width2 = width - int(p->prepause * scalex) - 1;
  429. if(xpos+width < x1)
  430. {
  431. xpos += width;
  432. continue; // before the redraw region
  433. }
  434. // is this a stressed vowel ?
  435. if((p->type == phVOWEL) && (p->stresslevel >= 2))
  436. {
  437. if(p->stresslevel >= 4)
  438. dc.SetPen(PEN_PHSTRESSED);
  439. else
  440. dc.SetPen(PEN_PHSTRESSED2);
  441. dc.DrawLine(xpos,offy-LINEBASE-1,xpos+width,offy-LINEBASE-1);
  442. }
  443. // is this phoneme selected ?
  444. if(ix == selected_ph)
  445. {
  446. dc.SetPen(PEN_PHSELECTED);
  447. dc.DrawLine(xpos,offy-LINEBASE,xpos+width,offy-LINEBASE);
  448. }
  449. // draw separator bar
  450. if(p->newword)
  451. dc.SetPen(*wxBLACK_PEN); // word boundary
  452. else
  453. dc.SetPen(*wxLIGHT_GREY_PEN);
  454. dc.DrawLine(xpos,offy-LINEBASE,xpos,offy-LINEBASE-LINESEP*6);
  455. // draw pitch envelope
  456. if(((p->ph->phflags & phWAVE) == 0) && (p->ph->type != phPAUSE))
  457. {
  458. if(!(p->synthflags & SFLAG_SEQCONTINUE))
  459. {
  460. width_env = width2;
  461. if(phlist[ix+1].synthflags & SFLAG_SEQCONTINUE)
  462. width_env += GetWidth(&phlist[ix+1]);
  463. DrawEnv(dc,xpos+1+(width-width2),offy-LINEBASE,width_env,p);
  464. }
  465. }
  466. else
  467. if(p->type != phPAUSE)
  468. {
  469. // sampled sound, draw a flat line
  470. dc.SetPen(PEN_SAMPLED);
  471. dc.DrawLine(xpos+1+(width-width2),offy-LINEBASE-LINESEP,
  472. xpos+1+width,offy-LINEBASE-LINESEP);
  473. }
  474. // show phoneme name from the PHONEME_TAB
  475. GetPhonemeName(p->ph,string);
  476. dc.GetTextExtent(string,&textwidth,&textheight);
  477. dc.DrawText(string,xpos+(width-textwidth/2)/2, offy-LINEBASE);
  478. xpos += width;
  479. }
  480. // draw the separator bar at the end of the line
  481. if(ix==endix && ix<numph && phlist[ix].newword)
  482. dc.SetPen(*wxLIGHT_GREY_PEN);
  483. else
  484. dc.SetPen(*wxBLACK_PEN); // word boundary or end of list
  485. dc.DrawLine(xpos,offy-LINEBASE,xpos,offy-LINEBASE-LINESEP*6);
  486. } // end of ProsodyDisplay::DrawPitchline
  487. void ProsodyDisplay::OnDraw(wxDC& dc)
  488. {//================================
  489. int x1,y1;
  490. int vX,vY,vW,vH; // Dimensions of client area in pixels
  491. int line, start, end;
  492. GetClientSize(&x1, &y1);
  493. if(x1 != linewidth)
  494. {
  495. LayoutData(NULL, 0);
  496. }
  497. wxRegionIterator upd(GetUpdateRegion()); // get the update rect list
  498. while (upd)
  499. {
  500. vX = upd.GetX();
  501. vY = upd.GetY();
  502. vW = upd.GetW();
  503. vH = upd.GetH();
  504. CalcUnscrolledPosition(vX,vY,&x1,&y1);
  505. // Repaint this rectangle, find which lines to redraw
  506. start = y1/FRAMEHEIGHT;
  507. end = (y1+vH)/FRAMEHEIGHT;
  508. for(line=start; line<=end && line<num_lines; line++)
  509. DrawPitchline(dc,line,x1,x1+vW);
  510. upd ++ ;
  511. }
  512. } // end of ProsodyDisplay::OnDraw
  513. void ProsodyDisplay::LayoutData(PHONEME_LIST *ph_list, int n_ph)
  514. {//===========================================================
  515. // divide the phoneme list into lines for display
  516. int xpos;
  517. int w;
  518. int ix;
  519. int height;
  520. PHONEME_LIST *p;
  521. if(ph_list != NULL)
  522. {
  523. numph = n_ph;
  524. phlist = ph_list;
  525. }
  526. num_lines = 0;
  527. linetab[0] = 1;
  528. GetClientSize(&linewidth, &height);
  529. xpos = linewidth;
  530. // could improve this to do 'wordwrap' - only split on word boundary
  531. // or unvoiced phonemes
  532. for(ix=1; ix<numph-2; ix++)
  533. {
  534. p = &phlist[ix];
  535. w = GetWidth(p);
  536. if(w + xpos >= linewidth)
  537. {
  538. linetab[num_lines++] = ix;
  539. xpos = 0;
  540. }
  541. xpos += w;
  542. }
  543. linetab[num_lines]=numph-2;
  544. SetScrollbars(SCROLLUNITS,SCROLLUNITS,linewidth/SCROLLUNITS,
  545. (num_lines*FRAMEHEIGHT)/SCROLLUNITS+1);
  546. Refresh();
  547. } // end of ProsodyDisplay::LayoutData
  548. extern int adding_page;
  549. void MyFrame::OnProsody(wxCommandEvent& WXUNUSED(event))
  550. {//=====================================================
  551. // Open the Prosody display window
  552. int width, height;
  553. int ix, npages;
  554. if(prosodycanvas != NULL)
  555. {
  556. // The Prosody window is already open
  557. // ?? select the prosody page ??
  558. npages = screenpages->GetPageCount();
  559. for(ix=0; ix<npages; ix++)
  560. {
  561. if(screenpages->GetPage(ix) == (wxWindow*)prosodycanvas)
  562. {
  563. screenpages->ChangeSelection(ix);
  564. break;
  565. }
  566. }
  567. return;
  568. }
  569. screenpages->GetClientSize(&width, &height);
  570. prosodycanvas = new ProsodyDisplay(screenpages, wxPoint(0, 50), wxSize(width-10, height));
  571. adding_page = 2; // work around for wxNotebook bug (version 2.8.7)
  572. screenpages->AddPage(prosodycanvas, _T("Prosody"), true);
  573. }