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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2014 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. if(line < num_lines)
  277. {
  278. // find which phoneme is selected on this line
  279. for(ix=linetab[line]; (ix<linetab[line+1]) && (ix<numph); ix++)
  280. {
  281. xpos += GetWidth(&phlist[ix]);
  282. if(xpos > pt.x)
  283. {
  284. selected_ph = ix;
  285. SelectPh(selected_ph);
  286. break;
  287. }
  288. }
  289. RefreshLine(line);
  290. }
  291. if(event.RightDown())
  292. {
  293. PopupMenu(menu_prosody);
  294. }
  295. } // end of ProsodyDisplay::OnMouse
  296. void ProsodyDisplay::OnKey(wxKeyEvent& event)
  297. {//========================================
  298. PHONEME_LIST *p;
  299. int display=1;
  300. if(selected_ph < 0)
  301. selected_ph = 0;
  302. p = &phlist[selected_ph];
  303. switch(event.GetKeyCode())
  304. {
  305. case WXK_F2:
  306. // make and play from this clause
  307. MakeWave2(phlist,numph);
  308. break;
  309. case WXK_LEFT:
  310. if(selected_ph > 1)
  311. selected_ph--;
  312. break;
  313. case WXK_RIGHT:
  314. if(selected_ph < (numph-2))
  315. selected_ph++;
  316. break;
  317. case WXK_UP:
  318. if(event.ControlDown())
  319. ChangePh(-1,2);
  320. else
  321. ChangePh(1,0);
  322. display = 1;
  323. break;
  324. case WXK_DOWN:
  325. if(event.ControlDown())
  326. ChangePh(1,-2);
  327. else
  328. ChangePh(-1,0);
  329. break;
  330. case ',':
  331. case '<':
  332. if(p->length > 0)
  333. p->length--;
  334. break;
  335. case '.':
  336. case '>':
  337. p->length++;
  338. break;
  339. case WXK_TAB:
  340. display = 0;
  341. event.Skip();
  342. transldlg->SetFocus();
  343. break;
  344. default:
  345. display = 0;
  346. event.Skip();
  347. break;
  348. }
  349. if(display)
  350. {
  351. Refresh();
  352. SelectPh(selected_ph);
  353. }
  354. } // end of ProsodyDisplay::OnKey
  355. void ProsodyDisplay::DrawEnv(wxDC& dc, int x1, int y1, int width, PHONEME_LIST *ph)
  356. {//==============================================================================
  357. int pitchr;
  358. int pitch;
  359. int p1;
  360. int ix;
  361. int x,y;
  362. int y2=0;
  363. unsigned char *env;
  364. PHONEME_DATA phdata_tone;
  365. if(width <= 0) return;
  366. if((pitchr = ph->pitch2 - ph->pitch1) < 0)
  367. {
  368. pitchr = -pitchr;
  369. p1 = ph->pitch2;
  370. }
  371. else
  372. {
  373. p1 = ph->pitch1;
  374. }
  375. if(p1 == 255) return;
  376. dc.SetPen(PEN_PITCHENV);
  377. env = envelope_data[ph->env];
  378. if((ph->type == phVOWEL) && (ph->tone_ph != 0))
  379. {
  380. // the envelope is given by a Tone phoneme acting on this vowel
  381. InterpretPhoneme2(ph->tone_ph, &phdata_tone);
  382. env = GetEnvelope(phdata_tone.pitch_env);
  383. }
  384. if(env == NULL)
  385. return;
  386. for(ix=0; ix<=width; ix+=4)
  387. {
  388. x = int((ix * 127.9)/width);
  389. pitch = p1 + (pitchr * env[x])/256;
  390. y = y1-int(pitch * scaley);
  391. if(ix > 0)
  392. dc.DrawLine(x1+ix-4,y2,x1+ix,y);
  393. y2 = y;
  394. }
  395. } // end of DrawEnv
  396. static void GetPhonemeName(PHONEME_TAB *ph, wxString& string)
  397. {//==========================================================
  398. string = wxString(WordToString(ph->mnemonic),wxConvLocal);
  399. }
  400. void ProsodyDisplay::DrawPitchline(wxDC& dc, int line, int x1, int x2)
  401. {//=================================================================
  402. int ix;
  403. int endix;
  404. int y;
  405. int offy;
  406. int xpos;
  407. int width; // total width, including pre-pause
  408. int width2; // width without pre-pause
  409. int width_env;
  410. int textwidth, textheight;
  411. wxString string;
  412. PHONEME_LIST *p;
  413. if(linetab[line] >= numph) return;
  414. offy = (line+1) * FRAMEHEIGHT - 1;
  415. y = LINEBASE+LINESEP;
  416. dc.SetPen(*wxLIGHT_GREY_PEN);
  417. // dc.SetPen(*wxCYAN_PEN);
  418. for(ix=0; ix<5; ix++)
  419. {
  420. dc.DrawLine(0,offy-y,linewidth,offy-y);
  421. y += LINESEP;
  422. }
  423. endix = linetab[line+1];
  424. xpos = 0;
  425. for(ix=linetab[line]; ix<endix; ix++)
  426. {
  427. if(ix < 0 || ix > numph-2) break;
  428. if(xpos > x2) break; // past the redraw region
  429. p = &phlist[ix];
  430. width = GetWidth(p);
  431. width2 = width - int(p->prepause * scalex) - 1;
  432. if(xpos+width < x1)
  433. {
  434. xpos += width;
  435. continue; // before the redraw region
  436. }
  437. // is this a stressed vowel ?
  438. if((p->type == phVOWEL) && (p->stresslevel >= 2))
  439. {
  440. if(p->stresslevel >= 4)
  441. dc.SetPen(PEN_PHSTRESSED);
  442. else
  443. dc.SetPen(PEN_PHSTRESSED2);
  444. dc.DrawLine(xpos,offy-LINEBASE-1,xpos+width,offy-LINEBASE-1);
  445. }
  446. // is this phoneme selected ?
  447. if(ix == selected_ph)
  448. {
  449. dc.SetPen(PEN_PHSELECTED);
  450. dc.DrawLine(xpos,offy-LINEBASE,xpos+width,offy-LINEBASE);
  451. }
  452. // draw separator bar
  453. if(p->newword)
  454. dc.SetPen(*wxBLACK_PEN); // word boundary
  455. else
  456. dc.SetPen(*wxLIGHT_GREY_PEN);
  457. dc.DrawLine(xpos,offy-LINEBASE,xpos,offy-LINEBASE-LINESEP*6);
  458. // draw pitch envelope
  459. if(((p->ph->phflags & phWAVE) == 0) && (p->ph->type != phPAUSE))
  460. {
  461. if(!(p->synthflags & SFLAG_SEQCONTINUE))
  462. {
  463. width_env = width2;
  464. if(phlist[ix+1].synthflags & SFLAG_SEQCONTINUE)
  465. width_env += GetWidth(&phlist[ix+1]);
  466. DrawEnv(dc,xpos+1+(width-width2),offy-LINEBASE,width_env,p);
  467. }
  468. }
  469. else
  470. if(p->type != phPAUSE)
  471. {
  472. // sampled sound, draw a flat line
  473. dc.SetPen(PEN_SAMPLED);
  474. dc.DrawLine(xpos+1+(width-width2),offy-LINEBASE-LINESEP,
  475. xpos+1+width,offy-LINEBASE-LINESEP);
  476. }
  477. // show phoneme name from the PHONEME_TAB
  478. GetPhonemeName(p->ph,string);
  479. dc.GetTextExtent(string,&textwidth,&textheight);
  480. dc.DrawText(string,xpos+(width-textwidth/2)/2, offy-LINEBASE);
  481. xpos += width;
  482. }
  483. // draw the separator bar at the end of the line
  484. if(ix==endix && ix<numph && phlist[ix].newword)
  485. dc.SetPen(*wxLIGHT_GREY_PEN);
  486. else
  487. dc.SetPen(*wxBLACK_PEN); // word boundary or end of list
  488. dc.DrawLine(xpos,offy-LINEBASE,xpos,offy-LINEBASE-LINESEP*6);
  489. } // end of ProsodyDisplay::DrawPitchline
  490. void ProsodyDisplay::OnDraw(wxDC& dc)
  491. {//================================
  492. int x1,y1;
  493. int vX,vY,vW,vH; // Dimensions of client area in pixels
  494. int line, start, end;
  495. GetClientSize(&x1, &y1);
  496. if(x1 != linewidth)
  497. {
  498. LayoutData(NULL, 0);
  499. }
  500. wxRegionIterator upd(GetUpdateRegion()); // get the update rect list
  501. while (upd)
  502. {
  503. vX = upd.GetX();
  504. vY = upd.GetY();
  505. vW = upd.GetW();
  506. vH = upd.GetH();
  507. CalcUnscrolledPosition(vX,vY,&x1,&y1);
  508. // Repaint this rectangle, find which lines to redraw
  509. start = y1/FRAMEHEIGHT;
  510. end = (y1+vH)/FRAMEHEIGHT;
  511. for(line=start; line<=end && line<num_lines; line++)
  512. DrawPitchline(dc,line,x1,x1+vW);
  513. upd ++ ;
  514. }
  515. } // end of ProsodyDisplay::OnDraw
  516. void ProsodyDisplay::LayoutData(PHONEME_LIST *ph_list, int n_ph)
  517. {//===========================================================
  518. // divide the phoneme list into lines for display
  519. int xpos;
  520. int w;
  521. int ix;
  522. int height;
  523. PHONEME_LIST *p;
  524. if(ph_list != NULL)
  525. {
  526. numph = n_ph;
  527. phlist = ph_list;
  528. }
  529. num_lines = 0;
  530. linetab[0] = 1;
  531. GetClientSize(&linewidth, &height);
  532. xpos = linewidth;
  533. // could improve this to do 'wordwrap' - only split on word boundary
  534. // or unvoiced phonemes
  535. for(ix=1; ix<numph-2; ix++)
  536. {
  537. p = &phlist[ix];
  538. w = GetWidth(p);
  539. if(w + xpos >= linewidth)
  540. {
  541. linetab[num_lines++] = ix;
  542. xpos = 0;
  543. }
  544. xpos += w;
  545. }
  546. linetab[num_lines]=numph-2;
  547. SetScrollbars(SCROLLUNITS,SCROLLUNITS,linewidth/SCROLLUNITS,
  548. (num_lines*FRAMEHEIGHT)/SCROLLUNITS+1);
  549. Refresh();
  550. } // end of ProsodyDisplay::LayoutData
  551. extern int adding_page;
  552. void MyFrame::OnProsody(wxCommandEvent& WXUNUSED(event))
  553. {//=====================================================
  554. // Open the Prosody display window
  555. int width, height;
  556. int ix, npages;
  557. if(prosodycanvas != NULL)
  558. {
  559. // The Prosody window is already open
  560. // ?? select the prosody page ??
  561. npages = screenpages->GetPageCount();
  562. for(ix=0; ix<npages; ix++)
  563. {
  564. if(screenpages->GetPage(ix) == (wxWindow*)prosodycanvas)
  565. {
  566. screenpages->ChangeSelection(ix);
  567. break;
  568. }
  569. }
  570. return;
  571. }
  572. screenpages->GetClientSize(&width, &height);
  573. prosodycanvas = new ProsodyDisplay(screenpages, wxPoint(0, 50), wxSize(width-10, height));
  574. adding_page = 2; // work around for wxNotebook bug (version 2.8.7)
  575. screenpages->AddPage(prosodycanvas, _T("Prosody"), true);
  576. }