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.

spectseq.cpp 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  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 <math.h>
  21. #include "speak_lib.h"
  22. #include "speech.h"
  23. #include "phoneme.h"
  24. #include "synthesize.h"
  25. #include "voice.h"
  26. #include "spect.h"
  27. #include "main.h"
  28. #include "wx/numdlg.h"
  29. #include "wx/txtstrm.h"
  30. #include "wx/datstrm.h"
  31. #define MAX_HARMONIC 400 // 400 * 50Hz = 20 kHz, more than enough
  32. extern void SetSynth_Klatt(int length, int modn, frame_t *fr1, frame_t *fr2, voice_t *v, int control);
  33. extern int Wavegen_Klatt(int resume);
  34. extern void SetSynth(int length, int modn, frame_t *fr1, frame_t *fr2, voice_t *v);
  35. extern int Wavegen();
  36. extern void CloseWaveFile2();
  37. extern void KlattReset(int control);
  38. extern FILE *f_wave;
  39. static int frame_width;
  40. int pk_select;
  41. wxBrush CREAM_BRUSH(wxColour(255,253,245),wxSOLID);
  42. wxPen BORDER_PEN(wxColour(255,240,0),4,wxSOLID);
  43. wxPen VLIGHT_GREY_PEN(wxColour(230,230,230),1,wxSOLID);
  44. float polint(float xa[],float ya[],int n,float x)
  45. {//==============================================
  46. // General polinomial interpolation routine, xa[1...n] ya[1...n]
  47. int i,m,ns=1;
  48. float den,dif,dift,ho,hp,w;
  49. float y; // result
  50. float c[9],d[9];
  51. dif=fabs(x-xa[1]);
  52. for(i=1;i<=n;i++){
  53. if((dift=fabs(x-xa[i])) < dif) {
  54. ns=i;
  55. dif=dift;
  56. }
  57. c[i]=ya[i];
  58. d[i]=ya[i];
  59. }
  60. y=ya[ns--];
  61. for(m=1;m<n;m++) {
  62. for(i=1;i<=n-m;i++) {
  63. ho=xa[i]-x;
  64. hp=xa[i+m]-x;
  65. w=c[i+1]-d[i];
  66. if((den=ho-hp) == 0.0)
  67. {
  68. // fprintf(stderr,"Error in routine 'polint'");
  69. return(ya[2]); // two input xa are identical
  70. }
  71. den=w/den;
  72. d[i]=hp*den;
  73. c[i]=ho*den;
  74. }
  75. y += ((2*ns < (n-m) ? c[ns+1] : d[ns--]));
  76. }
  77. return(y);
  78. } // end of polint
  79. static void PeaksZero(peak_t *sp, peak_t *zero)
  80. {//=====================================
  81. int pk;
  82. memcpy(zero,sp,sizeof(peak_t)*N_PEAKS);
  83. for(pk=0; pk<N_PEAKS; pk++)
  84. zero[pk].pkheight = 0;
  85. } // end of PeaksZero
  86. void MakeWaveFile(int synthesis_method)
  87. {//====================================
  88. int result=1;
  89. int resume=0;
  90. unsigned char wav_outbuf[1024];
  91. while(result != 0)
  92. {
  93. out_ptr = out_start = wav_outbuf;
  94. out_end = &wav_outbuf[sizeof(wav_outbuf)];
  95. if(synthesis_method == 1)
  96. result = Wavegen_Klatt(resume);
  97. else
  98. result = Wavegen();
  99. if(f_wave != NULL)
  100. fwrite(wav_outbuf, 1, out_ptr - wav_outbuf, f_wave);
  101. resume=1;
  102. }
  103. } // end of MakeWaveFile
  104. SpectSeq::SpectSeq(int n)
  105. {//======================
  106. numframes = n;
  107. if(n > 0)
  108. frames = new SpectFrame* [n];
  109. else
  110. frames = NULL;
  111. pk_select = 1;
  112. grid = 1;
  113. duration = 0;
  114. pitch1 = 0;
  115. pitch2 = 0;
  116. bass_reduction = 0;
  117. max_x = 3000;
  118. max_y = 1;
  119. file_format = 0;
  120. }
  121. SpectSeq::~SpectSeq()
  122. {//==================
  123. int ix;
  124. if(frames != NULL)
  125. {
  126. for(ix=0; ix<numframes; ix++)
  127. {
  128. if(frames[ix] != NULL)
  129. delete frames[ix];
  130. }
  131. delete frames;
  132. }
  133. }
  134. void SpectSeq::SelectAll(int yes)
  135. {//==============================
  136. // select of deselect all frames in the sequence
  137. int ix;
  138. for(ix=0; ix<numframes; ix++)
  139. frames[ix]->selected = yes;
  140. }
  141. int SpectSeq::CountSelected()
  142. {//==========================
  143. int ix;
  144. int count=0;
  145. for(ix=0; ix<numframes; ix++)
  146. {
  147. if(frames[ix]->selected)
  148. count++;
  149. }
  150. return(count);
  151. } // end of SpectSeq::CountSelected
  152. void SpectSeq::DeleteSelected()
  153. {//============================
  154. int ix;
  155. int count=0;
  156. for(ix=0; ix<numframes; ix++)
  157. {
  158. if(frames[ix]->selected)
  159. {
  160. count++;
  161. }
  162. else
  163. if(count > 0)
  164. {
  165. *frames[ix-count] = *frames[ix];
  166. }
  167. }
  168. numframes = numframes - count;
  169. } // end of SpectSeq::DeleteSelected
  170. void SpectSeq::ClipboardCopy()
  171. {//===========================
  172. int ix;
  173. int nframes;
  174. int count=0;
  175. nframes = CountSelected();
  176. if(nframes == 0) return;
  177. if(clipboard_spect != NULL)
  178. delete clipboard_spect;
  179. if((clipboard_spect = new SpectSeq(nframes))==NULL) return;
  180. for(ix=0; ix<numframes; ix++)
  181. {
  182. if(frames[ix]->selected)
  183. {
  184. if((clipboard_spect->frames[count] = new SpectFrame(frames[ix])) == NULL)
  185. break;
  186. count++;
  187. }
  188. }
  189. } // end of SpectSeq::ClipboardCopy
  190. int SpectSeq::ClipboardInsert(int insert_at)
  191. {//=========================================
  192. int ix;
  193. int j;
  194. int total;
  195. int result=insert_at;
  196. float thistime=0;
  197. float timeinc=0;
  198. float timeoffset=0;
  199. SpectFrame **frames2;
  200. if(clipboard_spect == NULL) return(result);
  201. if(clipboard_spect->numframes == 0) return(result);
  202. timeoffset = clipboard_spect->frames[0]->time;
  203. if(CountSelected() == 0)
  204. insert_at = -1;
  205. total = numframes + clipboard_spect->numframes;
  206. frames2 = new SpectFrame* [total];
  207. if(frames2 == NULL) return(result);
  208. total = 0;
  209. for(ix=0; ix<numframes; ix++)
  210. {
  211. thistime = frames[ix]->time;
  212. if(ix == insert_at)
  213. {
  214. result = total;
  215. for(j=0; j<clipboard_spect->numframes; j++)
  216. {
  217. frames2[total] = new SpectFrame(clipboard_spect->frames[j]);
  218. frames2[total]->time += (thistime - timeoffset);
  219. timeinc = frames2[total]->time - thistime + (frames2[total]->length/1000);
  220. total++;
  221. }
  222. }
  223. frames2[total] = new SpectFrame(frames[ix]);
  224. frames2[total++]->time += timeinc;
  225. }
  226. if(insert_at == -1)
  227. {
  228. // insert at the end
  229. result = total;
  230. for(j=0; j<clipboard_spect->numframes; j++)
  231. {
  232. frames2[total] = new SpectFrame(clipboard_spect->frames[j]);
  233. frames2[total++]->time += (thistime - timeoffset);
  234. }
  235. }
  236. delete frames;
  237. frames = frames2;
  238. numframes = total;
  239. return(result);
  240. } // end of SpectSeq::ClipboardInsert
  241. void SpectSeq::SetFrameLengths()
  242. {//=============================
  243. int frame;
  244. for(frame=0; frame<numframes; frame++)
  245. {
  246. if(frames[frame]->keyframe)
  247. frames[frame]->length = GetFrameLength(frame,1,NULL);
  248. else
  249. frames[frame]->length = 0;
  250. }
  251. } // end of SetFrameLengths
  252. float SpectSeq::GetFrameLength(int frame, int plus, int *original)
  253. {//===============================================================
  254. int ix;
  255. float adjust=0;
  256. if(frame >= numframes-1) return(0);
  257. // include the adjustment for this frame ?
  258. if(plus) adjust = frames[frame]->length_adjust;
  259. for(ix=frame+1; ix<numframes-1; ix++)
  260. {
  261. if(frames[ix]->keyframe) break; // reached next keyframe
  262. adjust += frames[ix]->length_adjust;
  263. }
  264. if(original != NULL)
  265. *original = int((frames[ix]->time - frames[frame]->time) * 1000.0 + 0.5);
  266. return ((frames[ix]->time - frames[frame]->time) * 1000.0 + adjust);
  267. }
  268. float SpectSeq::GetKeyedLength()
  269. {//=============================
  270. int ix;
  271. int first;
  272. int last=0;
  273. float adjust=0;
  274. first = -1;
  275. for(ix=0; ix<numframes; ix++)
  276. {
  277. if(frames[ix]->keyframe)
  278. {
  279. last = ix;
  280. if(first == -1) first = ix;
  281. }
  282. }
  283. if(first == -1)
  284. return(0); // no keyframes
  285. for(ix=first; ix<last; ix++)
  286. adjust += frames[ix]->length_adjust;
  287. return((frames[last]->time - frames[first]->time) * 1000 + adjust);
  288. }
  289. void SpectSeq::Load2(wxInputStream& stream, int import, int n)
  290. {//===========================================================
  291. // continuation of load/import
  292. int ix;
  293. wxString string;
  294. float time_offset;
  295. float time_acc=0;
  296. int set_max_y=0;
  297. if(n==0) return;
  298. if(frames != NULL) delete frames;
  299. frames = new SpectFrame* [n];
  300. numframes = 0;
  301. max_x = 3000;
  302. if(max_y == 0)
  303. {
  304. set_max_y = 1;
  305. max_y = 1;
  306. }
  307. for(ix = 0; ix < n; ix++)
  308. {
  309. SpectFrame *frame = new SpectFrame;
  310. if(import==1)
  311. {
  312. if(frame->Import(stream) != 0) break;
  313. }
  314. else
  315. if(import==2)
  316. {
  317. if(frame->ImportSPC2(stream,time_acc) != 0) break;
  318. }
  319. else
  320. {
  321. if(frame->Load(stream, file_format) != 0) break;
  322. }
  323. frames[numframes++] = frame;
  324. if(set_max_y && (frame->max_y > max_y))
  325. max_y = frame->max_y;
  326. if(frame->nx * frame->dx > max_x) max_x = int(frame->nx * frame->dx);
  327. }
  328. max_x = 9000; // disable auto-xscaling
  329. frame_width = int((FRAME_WIDTH*max_x)/MAX_DISPLAY_FREQ);
  330. if(frame_width > FRAME_WIDTH) frame_width = FRAME_WIDTH;
  331. // start times from zero
  332. time_offset = frames[0]->time;
  333. for(ix=0; ix<numframes; ix++)
  334. frames[ix]->time -= time_offset;
  335. pitch1 = pitchenv.pitch1;
  336. pitch2 = pitchenv.pitch2;
  337. duration = int(frames[numframes-1]->time * 1000);
  338. if(max_y < 400)
  339. max_y = 200;
  340. else
  341. max_y = 29000; // disable auto height scaling
  342. } // end of SpectSeq::Load2
  343. int SpectSeq::Import(wxInputStream& stream)
  344. {//========================================
  345. // Import data from Pratt analysis
  346. int n = 0;
  347. wxTextInputStream text_stream(stream);
  348. name = _T("");
  349. text_stream >> n;
  350. amplitude = 100;
  351. max_y = 0;
  352. Load2(stream,1,n);
  353. return(0);
  354. } // end of SpectSeq::Import
  355. int SPC2_size_cycle(CYCLE *cy)
  356. /****************************/
  357. /* Find number of bytes in cycle record */
  358. {
  359. int i;
  360. i = 44 + cy->n_harm;
  361. if(cy->flags & 1)
  362. {
  363. i += 4; /* label */
  364. }
  365. return(i);
  366. } /* end of size_cycle */
  367. int SpectSeq::ImportSPC2(wxInputStream & stream)
  368. {//=============================================
  369. // load an spectrum with an old "SPC2" format
  370. int n_cycles = 0;
  371. int x;
  372. CYCLE cy;
  373. /* count number of cycles */
  374. while(!stream.Eof())
  375. {
  376. stream.TellI();
  377. stream.Read(&cy,44);
  378. stream.TellI();
  379. if(stream.Eof()) break;
  380. n_cycles++;
  381. x = SPC2_size_cycle(&cy) - 44;
  382. stream.SeekI(x,wxFromCurrent);
  383. }
  384. if(n_cycles == 0) return(0);
  385. name = _T("");
  386. amplitude = 100;
  387. max_y = 0;
  388. stream.SeekI(4); // rewind and skip header
  389. Load2(stream,2,n_cycles);
  390. return(0);
  391. }
  392. int SpectSeq::Load(wxInputStream & stream)
  393. {//=======================================
  394. int n;
  395. int ix;
  396. unsigned int id1, id2;
  397. wxDataInputStream s(stream);
  398. id1 = s.Read32();
  399. id2 = s.Read32();
  400. if(id1 == FILEID1_SPC2)
  401. {
  402. stream.SeekI(4);
  403. return(ImportSPC2(stream));
  404. }
  405. else
  406. if((id1 == FILEID1_SPECTSEQ) && (id2 == FILEID2_SPECTSEQ))
  407. {
  408. file_format = 0; // eSpeak formants
  409. }
  410. else
  411. if((id1 == FILEID1_SPECTSEQ) && (id2 == FILEID2_SPECTSEK))
  412. {
  413. file_format = 1; // formants for Klatt synthesizer
  414. }
  415. else
  416. if((id1 == FILEID1_SPECTSEQ) && (id2 == FILEID2_SPECTSQ2))
  417. {
  418. file_format = 2; // formants for Klatt synthesizer
  419. }
  420. else
  421. {
  422. // Praat analysis data
  423. stream.SeekI(0);
  424. return(Import(stream));
  425. }
  426. name = s.ReadString();
  427. n = s.Read16();
  428. amplitude = s.Read16();
  429. max_y = s.Read16();
  430. s.Read16();
  431. Load2(stream,0,n);
  432. for(ix=0; ix<numframes; ix++)
  433. {
  434. if(frames[ix]->keyframe)
  435. frames[ix]->length_adjust = frames[ix]->length - GetFrameLength(ix,0,NULL);
  436. }
  437. return(0);
  438. } // end of SpectSeq::Load
  439. int SpectSeq::Save(wxOutputStream &stream, int selection)
  440. {//======================================================
  441. int ix;
  442. int count=numframes;
  443. if(selection)
  444. {
  445. count = CountSelected();
  446. }
  447. SetFrameLengths();
  448. wxDataOutputStream s(stream);
  449. file_format = 2; // inclue Klatt data in new saves
  450. s.Write32(FILEID1_SPECTSEQ);
  451. if(file_format == 2)
  452. s.Write32(FILEID2_SPECTSQ2);
  453. else
  454. if(file_format == 1)
  455. s.Write32(FILEID2_SPECTSEK);
  456. else
  457. s.Write32(FILEID2_SPECTSEQ);
  458. s.WriteString(name);
  459. s.Write16(count);
  460. s.Write16(amplitude);
  461. s.Write16(selection ? max_y : 0);
  462. s.Write16(0); // spare
  463. for(ix=0; ix<numframes; ix++)
  464. {
  465. if((selection==0) || frames[ix]->selected)
  466. {
  467. if(frames[ix]->Save(stream, file_format) != 0) return(1);
  468. }
  469. }
  470. return(0);
  471. } // end of SpectSeq::Save
  472. void SpectSeq::ConstructVowel(void)
  473. {//================================
  474. // not completed
  475. int ix;
  476. int j=0;
  477. int frames_selected[4];
  478. for(ix=0; ix<numframes; ix++)
  479. {
  480. if(frames[ix]->selected)
  481. {
  482. if(ix >= 4)
  483. break;
  484. frames_selected[j++] = ix;
  485. }
  486. }
  487. if(j==0 || j>= 4)
  488. return;
  489. if(frames_selected[0] == 0)
  490. return;
  491. } // end of ConstructVowel
  492. void SpectSeq::Draw(wxDC& dc, int start_y, int end_y)
  493. {//==================================================
  494. int fm;
  495. int f, f1, f2;
  496. int x;
  497. if(end_y < start_y) return;
  498. if((start_y -= 4) < 0) start_y = 0;
  499. f1 = start_y / FRAME_HEIGHT;
  500. f2 = end_y / FRAME_HEIGHT;
  501. scaley = double(FRAME_HEIGHT) / max_y;
  502. scalex = double(frame_width) / max_x;
  503. // scalex = 0.6;
  504. for(fm=f1; fm <= f2 && fm < numframes; fm++)
  505. {
  506. if(frames[fm]->keyframe)
  507. {
  508. dc.SetBrush(CREAM_BRUSH);
  509. dc.SetPen(BORDER_PEN);
  510. }
  511. else
  512. {
  513. dc.SetBrush(*wxWHITE_BRUSH);
  514. dc.SetPen(*wxTRANSPARENT_PEN);
  515. }
  516. if(frames[fm]->selected)
  517. dc.SetPen(*wxRED_PEN);
  518. dc.DrawRectangle(0,FRAME_HEIGHT*fm+2,frame_width,
  519. FRAME_HEIGHT-2);
  520. }
  521. if(grid==1)
  522. {
  523. for(f=500; f<=MAX_DISPLAY_FREQ; f+=500)
  524. {
  525. x = int(f * scalex);
  526. if(x > max_x) break;
  527. if(f==3000 || f==6000 || f==9000)
  528. dc.SetPen(*wxLIGHT_GREY_PEN);
  529. else
  530. dc.SetPen(VLIGHT_GREY_PEN);
  531. dc.DrawLine(x,start_y,x,numframes*FRAME_HEIGHT);
  532. }
  533. }
  534. for(fm=f1; fm <= f2 && fm < numframes; fm++)
  535. {
  536. frames[fm]->Draw(dc,FRAME_HEIGHT*(fm+1),frame_width,
  537. scalex,scaley);
  538. }
  539. } // end of SpectSeq::Draw
  540. void SpectSeq::InterpolatePeak(int peak)
  541. {//=====================================
  542. int f, f1=0, f2;
  543. peak_t *p, *p1=NULL, *p2;
  544. double t1=0, t2;
  545. double interval;
  546. double ratio;
  547. int first = 1;
  548. for(f2=0; f2 < numframes; f2++)
  549. {
  550. if(frames[f2]->keyframe)
  551. {
  552. t2 = frames[f2]->time;
  553. p2 = &frames[f2]->peaks[peak];
  554. if(first)
  555. first = 0;
  556. else
  557. {
  558. interval = t2 - t1;
  559. for(f=f1+1; f<f2; f++)
  560. {
  561. p = &frames[f]->peaks[peak];
  562. ratio = (frames[f]->time - t1)/interval;
  563. p->pkfreq = p1->pkfreq + int((p2->pkfreq - p1->pkfreq)*ratio);
  564. p->pkheight=p1->pkheight+int((p2->pkheight-p1->pkheight)*ratio);
  565. p->pkwidth = p1->pkwidth + int((p2->pkwidth - p1->pkwidth)*ratio);
  566. p->pkright =p1->pkright + int((p2->pkright - p2->pkright)*ratio);
  567. }
  568. }
  569. f1 = f2;
  570. t1 = t2;
  571. p1 = p2;
  572. }
  573. }
  574. } // end of SpectSeq::InterpolatePeak
  575. void SpectSeq::InterpolateAdjacent(void)
  576. {//=====================================
  577. int ix;
  578. int f1 = -1;
  579. int select = -1;
  580. int f2 = -1;
  581. float ratio;
  582. peak_t *p = NULL;
  583. peak_t *p1 = NULL;
  584. peak_t *p2 = NULL;
  585. for(ix=0; ix<numframes; ix++)
  586. {
  587. if(frames[ix]->selected)
  588. select = ix;
  589. else
  590. if(frames[ix]->keyframe)
  591. {
  592. if(select >= 0)
  593. {
  594. f2 = ix;
  595. break;
  596. }
  597. else
  598. f1 = ix;
  599. }
  600. }
  601. if(f1 < 0)
  602. {
  603. wxLogError(_T("No previous keyframe"));
  604. return;
  605. }
  606. if(select < 0)
  607. {
  608. wxLogError(_T("No selected frame"));
  609. return;
  610. }
  611. if(f2 < 0)
  612. {
  613. wxLogError(_T("No subsequent keyframe"));
  614. return;
  615. }
  616. // get ratio
  617. ix = wxGetNumberFromUser(_T("Interpolate between adjacent frames"),_T("percent"),_T(""),50);
  618. ratio = (float)ix/100.0;
  619. for(ix=0; ix<N_PEAKS; ix++)
  620. {
  621. p = &frames[select]->peaks[ix];
  622. p1 = &frames[f1]->peaks[ix];
  623. p2 = &frames[f2]->peaks[ix];
  624. p->pkfreq = p1->pkfreq + int((p2->pkfreq - p1->pkfreq)*ratio);
  625. p->pkheight=p1->pkheight+int((p2->pkheight-p1->pkheight)*ratio);
  626. p->pkwidth = p1->pkwidth + int((p2->pkwidth - p1->pkwidth)*ratio);
  627. p->pkright =p1->pkright + int((p2->pkright - p2->pkright)*ratio);
  628. }
  629. frames[select]->keyframe = 1;
  630. formantdlg->ShowFrame(this,select,1,0xff);
  631. }
  632. void SpectSeq::InterpolatePeaks(int control)
  633. {//=========================================
  634. // 0=turn off 1=turn on
  635. int f, peak;
  636. if(control==1)
  637. {
  638. for(peak=0; peak<N_PEAKS; peak++)
  639. {
  640. InterpolatePeak(peak);
  641. }
  642. }
  643. else
  644. {
  645. for(f=0; f<numframes; f++)
  646. {
  647. if(frames[f]->keyframe == 0)
  648. frames[f]->ZeroPeaks();
  649. }
  650. }
  651. } // end of SpectSeq::InterpolatePeaks
  652. void SpectSeq::CopyDown(int frame, int direction)
  653. {//==============================================
  654. // Copy peaks down from next earlier/later keyframe
  655. int f1;
  656. for(f1=frame+direction; f1>=0 && f1<numframes; f1 += direction)
  657. {
  658. if(frames[f1]->keyframe)
  659. {
  660. memcpy(frames[frame]->peaks, frames[f1]->peaks, sizeof(frames[frame]->peaks));
  661. memcpy(frames[frame]->klatt_param, frames[f1]->klatt_param, sizeof(frames[frame]->klatt_param));
  662. break;
  663. }
  664. }
  665. } // end of CopyDown
  666. void SpectSeq::MakePitchenv(PitchEnvelope &pitchenv, int start_frame, int end_frame)
  667. {//=================================================================================
  668. double f;
  669. double min=8000;
  670. double max=0;
  671. double diff;
  672. double t_start = -1;
  673. double t_end=0, t_diff;
  674. double yy;
  675. int ix;
  676. int x, y;
  677. int xx;
  678. int nx=0;
  679. float *ax, *ay;
  680. memset(pitchenv.env,127,128);
  681. for(ix=start_frame; ix<=end_frame; ix++)
  682. {
  683. if((f = frames[ix]->pitch) == 0) continue;
  684. nx++;
  685. t_end = frames[ix]->time;
  686. if(t_start < 0) t_start = t_end;
  687. if(f < min) min = f;
  688. if(f > max) max = f;
  689. }
  690. diff = max-min;
  691. t_diff = t_end - t_start;
  692. if(nx<2 || diff<=0 || t_diff<=0)
  693. {
  694. // no pitch info, use defaults
  695. pitchenv.pitch1=80;
  696. pitchenv.pitch2=120;
  697. return;
  698. }
  699. pitchenv.pitch1 = int(min);
  700. pitchenv.pitch2 = int(max);
  701. ax = new float [nx+1];
  702. ay = new float[nx+1];
  703. nx = 0;
  704. for(ix=start_frame; ix<=end_frame; ix++)
  705. {
  706. if((f = frames[ix]->pitch) == 0) continue;
  707. ax[++nx] = (frames[ix]->time - t_start) * 128 / t_diff;
  708. ay[nx] = (frames[ix]->pitch - min) * 255 / diff;
  709. }
  710. pitchenv.env[0] = int(ay[1]);
  711. pitchenv.env[127] = int(ay[nx]);
  712. // create pitch envelope by interpolating the time/pitch
  713. // values from the spectrum sequence
  714. xx = 1;
  715. for(x=1; x<127; x++)
  716. {
  717. while((ax[xx] < x) && (xx < nx)) xx++;
  718. if(xx < 3)
  719. yy = polint(&ax[xx-1],&ay[xx-1],3,(float)x);
  720. else if(xx > nx-1)
  721. yy = polint(&ax[xx-2],&ay[xx-2],3,(float)x);
  722. else
  723. yy = polint(&ax[xx-2],&ay[xx-2],4,(float)x);
  724. y = int(yy);
  725. if(y < 0) y = 0;
  726. if(y > 255) y = 255;
  727. pitchenv.env[x] = y;
  728. }
  729. delete ax;
  730. delete ay;
  731. } // end of SpectSeq::MakePitchenv
  732. void SpectSeq::ApplyAmp_adjust(SpectFrame *sp, peak_t *peaks)
  733. {//=============================================================
  734. int ix;
  735. int y;
  736. memcpy(peaks,sp->peaks,sizeof(*peaks)*N_PEAKS);
  737. for(ix=0; ix<N_PEAKS; ix++)
  738. {
  739. y = peaks[ix].pkheight * sp->amp_adjust * amplitude;
  740. peaks[ix].pkheight = y / 10000;
  741. }
  742. } // end of ApplyAmp_adjust
  743. void PeaksToFrame(SpectFrame *sp1, peak_t *pks, frame_t *fr)
  744. {//=========================================================
  745. int ix;
  746. int x;
  747. fr->frflags = FRFLAG_KLATT;
  748. for(ix=0; ix < 8; ix++)
  749. {
  750. if(ix < 7)
  751. {
  752. fr->ffreq[ix] = pks[ix].pkfreq;
  753. fr->klatt_ap[ix] = pks[ix].klt_ap;
  754. fr->klatt_bp[ix] = pks[ix].klt_bp/2;
  755. }
  756. if(ix < 4)
  757. fr->bw[ix] = pks[ix].klt_bw/2;
  758. fr->fheight[ix] = pks[ix].pkheight >> 6;
  759. if(ix < 6)
  760. {
  761. if((x = (pks[ix].pkwidth >> 2)) > 255)
  762. x = 255;
  763. fr->fwidth[ix] = x;
  764. if(ix < 3)
  765. {
  766. if((x = (pks[ix].pkright >> 2)) > 255)
  767. x = 255;
  768. fr->fright[ix] = x;
  769. }
  770. }
  771. }
  772. for(ix=0; ix<N_KLATTP; ix++)
  773. {
  774. fr->klattp[ix] = sp1->klatt_param[ix];
  775. }
  776. fr->klattp[KLATT_FNZ] = sp1->klatt_param[KLATT_FNZ]/2;
  777. if(fr->fheight[1] == 0)
  778. {
  779. fr->klattp[KLATT_AV] -= 10; // fade in/out
  780. }
  781. }
  782. static void SetSynth_mS(int length_mS, SpectFrame *sp1, SpectFrame *sp2, peak_t *pks1, peak_t *pks2, int control)
  783. {//==============================================================================================================
  784. static frame_t fr1, fr2;
  785. PeaksToFrame(sp1,pks1,&fr1);
  786. PeaksToFrame(sp2,pks2,&fr2);
  787. if(voice->klattv[0])
  788. {
  789. SetSynth_Klatt((length_mS * samplerate) / 1000, 0, &fr1, &fr2, voice, control); // convert mS to samples
  790. }
  791. else
  792. {
  793. SetSynth((length_mS * samplerate) / 1000, 0, &fr1, &fr2, voice); // convert mS to samples
  794. }
  795. };
  796. void SpectSeq::MakeWave(int start, int end, PitchEnvelope &pitch)
  797. {//==============================================================
  798. int ix;
  799. int length;
  800. int len_samples;
  801. int total_length;
  802. float sum_length=0;
  803. float prev_length=0;
  804. int first;
  805. char *fname_speech;
  806. SpectFrame *sp1 = NULL;
  807. SpectFrame *sp2;
  808. double lfactor;
  809. peak_t peaks0[N_PEAKS];
  810. peak_t peaks1[N_PEAKS];
  811. peak_t peaks2[N_PEAKS];
  812. int synthesizer_type = 0;
  813. if(voice->klattv[0])
  814. {
  815. synthesizer_type = 1;
  816. KlattReset(2);
  817. }
  818. SpeakNextClause(NULL,NULL,2); // stop speaking file
  819. if(numframes==0) return;
  820. SetFrameLengths();
  821. // find overall length of sequence
  822. for(ix=0; ix<numframes; ix++)
  823. {
  824. if(frames[ix]->keyframe)
  825. {
  826. sum_length += prev_length;
  827. prev_length = frames[ix]->length;
  828. sp2 = frames[ix];
  829. if(sp1 == NULL)
  830. sp1 = sp2;
  831. }
  832. }
  833. if(sp1 == NULL)
  834. {
  835. wxLogError(_T("(No frames have peaks set"));
  836. return;
  837. }
  838. total_length = int(sum_length);
  839. if((start==end) || (total_length == 0))
  840. {
  841. sp1->MakeWaveF(0,voicedlg->pitchenv,amplitude,duration);
  842. return;
  843. }
  844. if((duration > 0) && (duration < 40000))
  845. lfactor = double(duration)/double(total_length);
  846. else
  847. {
  848. duration = total_length;
  849. lfactor = 1;
  850. }
  851. len_samples = int(((total_length * lfactor + 50) * samplerate) / 1000);
  852. SetPitch(len_samples,pitch.env,9,44);
  853. fname_speech = WavFileName();
  854. OpenWaveFile2(fname_speech);
  855. first = 0;
  856. if(start > 0)
  857. first=1; // a selection, use fade-in
  858. sp2 = NULL;
  859. for(ix=start; ix<=end; ix++)
  860. {
  861. if(frames[ix]->keyframe)
  862. {
  863. sp1 = sp2;
  864. sp2 = frames[ix];
  865. if(sp1 != NULL)
  866. {
  867. ApplyAmp_adjust(sp1,peaks1);
  868. ApplyAmp_adjust(sp2,peaks2);
  869. if(first)
  870. {
  871. PeaksZero(peaks1,peaks0); // fade in
  872. SetSynth_mS(20,sp1,sp1,peaks0,peaks1,0);
  873. MakeWaveFile(synthesizer_type);
  874. first=0;
  875. }
  876. length = int(sp1->length * lfactor);
  877. SetSynth_mS(length,sp1,sp2,peaks1,peaks2,0);
  878. MakeWaveFile(synthesizer_type);
  879. }
  880. }
  881. }
  882. PeaksZero(peaks2,peaks0); // fade out
  883. SetSynth_mS(30,sp2,sp2,peaks2,peaks0,2);
  884. MakeWaveFile(synthesizer_type);
  885. CloseWaveFile2();
  886. PlayWavFile(fname_speech);
  887. } // end of SpectSeq::MakeWave
  888. void SpectFrame::MakeHtab(int numh, int *htab, int pitch)
  889. {//======================================================
  890. // interpolate the spectrum to give a harmonic table for
  891. // the given pitch (Hz<<12)
  892. } // end of SpectFrame::MakeHtab
  893. void SpectFrame::MakeWaveF(int control, PitchEnvelope &pitche, int amplitude, int duration)
  894. {//======================================================================================
  895. // amplitude: percentage adjustment
  896. int ix;
  897. int length; // mS
  898. int len_samples;
  899. int y;
  900. peak_t peaks0[N_PEAKS];
  901. peak_t peaks1[N_PEAKS];
  902. char *fname_speech;
  903. int synthesizer_type = 0;
  904. if(voice->klattv[0])
  905. {
  906. synthesizer_type = 1;
  907. KlattReset(2);
  908. }
  909. SpeakNextClause(NULL,NULL,2); // stop speaking file
  910. length = duration;
  911. if(length==0)
  912. length = 200; // default length, mS
  913. len_samples = (length * samplerate) / 1000;
  914. SetPitch(len_samples + 50,pitche.env,9,44);
  915. fname_speech = WavFileName();
  916. if(OpenWaveFile2(fname_speech) != 0)
  917. return;
  918. if(control==0)
  919. {
  920. memcpy(peaks1,peaks,sizeof(peaks1));
  921. for(ix=0; ix<N_PEAKS; ix++)
  922. {
  923. y = peaks1[ix].pkheight * amp_adjust * amplitude;
  924. peaks1[ix].pkheight = y/10000;
  925. }
  926. PeaksZero(peaks1,peaks0);
  927. SetSynth_mS(20,this,this,peaks0,peaks1,0);
  928. MakeWaveFile(synthesizer_type);
  929. SetSynth_mS(length,this,this,peaks1,peaks1,0);
  930. MakeWaveFile(synthesizer_type);
  931. SetSynth_mS(30,this,this,peaks1,peaks0,2);
  932. MakeWaveFile(synthesizer_type);
  933. }
  934. else
  935. {
  936. #ifdef SPECT_EDITOR
  937. int maxh;
  938. USHORT htab0[600];
  939. maxh = nx;
  940. if(maxh >= 600)
  941. maxh = 600-1;
  942. for(ix=0; ix<=maxh; ix++)
  943. htab0[ix] = 0;
  944. // SetSynthHtab(20,htab0,maxh,dx,spect,maxh,dx);
  945. SetSynthHtab(20,spect,maxh,dx,spect,maxh,dx);
  946. MakeWaveFile(0);
  947. SetSynthHtab(length,spect,maxh,dx,spect,maxh,dx);
  948. MakeWaveFile(0);
  949. SetSynthHtab(30,spect,maxh,dx,htab0,maxh,dx);
  950. MakeWaveFile(0);
  951. #endif
  952. }
  953. CloseWaveFile2();
  954. PlayWavFile(fname_speech);
  955. } // end of SpectFrame::MakeWaveFrame