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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  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. int pos;
  374. /* count number of cycles */
  375. while(!stream.Eof())
  376. {
  377. pos = stream.TellI();
  378. stream.Read(&cy,44);
  379. pos = stream.TellI();
  380. if(stream.Eof()) break;
  381. n_cycles++;
  382. x = SPC2_size_cycle(&cy) - 44;
  383. stream.SeekI(x,wxFromCurrent);
  384. }
  385. if(n_cycles == 0) return(0);
  386. name = _T("");
  387. amplitude = 100;
  388. max_y = 0;
  389. stream.SeekI(4); // rewind and skip header
  390. Load2(stream,2,n_cycles);
  391. return(0);
  392. }
  393. int SpectSeq::Load(wxInputStream & stream)
  394. {//=======================================
  395. int n;
  396. int ix;
  397. unsigned int id1, id2;
  398. wxDataInputStream s(stream);
  399. id1 = s.Read32();
  400. id2 = s.Read32();
  401. if(id1 == FILEID1_SPC2)
  402. {
  403. stream.SeekI(4);
  404. return(ImportSPC2(stream));
  405. }
  406. else
  407. if((id1 == FILEID1_SPECTSEQ) && (id2 == FILEID2_SPECTSEQ))
  408. {
  409. file_format = 0; // eSpeak formants
  410. }
  411. else
  412. if((id1 == FILEID1_SPECTSEQ) && (id2 == FILEID2_SPECTSEK))
  413. {
  414. file_format = 1; // formants for Klatt synthesizer
  415. }
  416. else
  417. if((id1 == FILEID1_SPECTSEQ) && (id2 == FILEID2_SPECTSQ2))
  418. {
  419. file_format = 2; // formants for Klatt synthesizer
  420. }
  421. else
  422. {
  423. // Praat analysis data
  424. stream.SeekI(0);
  425. return(Import(stream));
  426. }
  427. name = s.ReadString();
  428. n = s.Read16();
  429. amplitude = s.Read16();
  430. max_y = s.Read16();
  431. s.Read16();
  432. Load2(stream,0,n);
  433. for(ix=0; ix<numframes; ix++)
  434. {
  435. if(frames[ix]->keyframe)
  436. frames[ix]->length_adjust = frames[ix]->length - GetFrameLength(ix,0,NULL);
  437. }
  438. return(0);
  439. } // end of SpectSeq::Load
  440. int SpectSeq::Save(wxOutputStream &stream, int selection)
  441. {//======================================================
  442. int ix;
  443. int count=numframes;
  444. if(selection)
  445. {
  446. count = CountSelected();
  447. }
  448. SetFrameLengths();
  449. wxDataOutputStream s(stream);
  450. file_format = 2; // inclue Klatt data in new saves
  451. s.Write32(FILEID1_SPECTSEQ);
  452. if(file_format == 2)
  453. s.Write32(FILEID2_SPECTSQ2);
  454. else
  455. if(file_format == 1)
  456. s.Write32(FILEID2_SPECTSEK);
  457. else
  458. s.Write32(FILEID2_SPECTSEQ);
  459. s.WriteString(name);
  460. s.Write16(count);
  461. s.Write16(amplitude);
  462. s.Write16(selection ? max_y : 0);
  463. s.Write16(0); // spare
  464. for(ix=0; ix<numframes; ix++)
  465. {
  466. if((selection==0) || frames[ix]->selected)
  467. {
  468. if(frames[ix]->Save(stream, file_format) != 0) return(1);
  469. }
  470. }
  471. return(0);
  472. } // end of SpectSeq::Save
  473. void SpectSeq::ConstructVowel(void)
  474. {//================================
  475. // not completed
  476. int ix;
  477. int j=0;
  478. int frames_selected[4];
  479. for(ix=0; ix<numframes; ix++)
  480. {
  481. if(frames[ix]->selected)
  482. {
  483. if(ix >= 4)
  484. break;
  485. frames_selected[j++] = ix;
  486. }
  487. }
  488. if(j==0 || j>= 4)
  489. return;
  490. if(frames_selected[0] == 0)
  491. return;
  492. } // end of ConstructVowel
  493. void SpectSeq::Draw(wxDC& dc, int start_y, int end_y)
  494. {//==================================================
  495. int fm;
  496. int f, f1, f2;
  497. int x;
  498. if(end_y < start_y) return;
  499. if((start_y -= 4) < 0) start_y = 0;
  500. f1 = start_y / FRAME_HEIGHT;
  501. f2 = end_y / FRAME_HEIGHT;
  502. scaley = double(FRAME_HEIGHT) / max_y;
  503. scalex = double(frame_width) / max_x;
  504. // scalex = 0.6;
  505. for(fm=f1; fm <= f2 && fm < numframes; fm++)
  506. {
  507. if(frames[fm]->keyframe)
  508. {
  509. dc.SetBrush(CREAM_BRUSH);
  510. dc.SetPen(BORDER_PEN);
  511. }
  512. else
  513. {
  514. dc.SetBrush(*wxWHITE_BRUSH);
  515. dc.SetPen(*wxTRANSPARENT_PEN);
  516. }
  517. if(frames[fm]->selected)
  518. dc.SetPen(*wxRED_PEN);
  519. dc.DrawRectangle(0,FRAME_HEIGHT*fm+2,frame_width,
  520. FRAME_HEIGHT-2);
  521. }
  522. if(grid==1)
  523. {
  524. for(f=500; f<=MAX_DISPLAY_FREQ; f+=500)
  525. {
  526. x = int(f * scalex);
  527. if(x > max_x) break;
  528. if(f==3000 || f==6000 || f==9000)
  529. dc.SetPen(*wxLIGHT_GREY_PEN);
  530. else
  531. dc.SetPen(VLIGHT_GREY_PEN);
  532. dc.DrawLine(x,start_y,x,numframes*FRAME_HEIGHT);
  533. }
  534. }
  535. for(fm=f1; fm <= f2 && fm < numframes; fm++)
  536. {
  537. frames[fm]->Draw(dc,FRAME_HEIGHT*(fm+1),frame_width,
  538. scalex,scaley);
  539. }
  540. } // end of SpectSeq::Draw
  541. void SpectSeq::InterpolatePeak(int peak)
  542. {//=====================================
  543. int f, f1=0, f2;
  544. peak_t *p, *p1=NULL, *p2;
  545. double t1=0, t2;
  546. double interval;
  547. double ratio;
  548. int first = 1;
  549. for(f2=0; f2 < numframes; f2++)
  550. {
  551. if(frames[f2]->keyframe)
  552. {
  553. t2 = frames[f2]->time;
  554. p2 = &frames[f2]->peaks[peak];
  555. if(first)
  556. first = 0;
  557. else
  558. {
  559. interval = t2 - t1;
  560. for(f=f1+1; f<f2; f++)
  561. {
  562. p = &frames[f]->peaks[peak];
  563. ratio = (frames[f]->time - t1)/interval;
  564. p->pkfreq = p1->pkfreq + int((p2->pkfreq - p1->pkfreq)*ratio);
  565. p->pkheight=p1->pkheight+int((p2->pkheight-p1->pkheight)*ratio);
  566. p->pkwidth = p1->pkwidth + int((p2->pkwidth - p1->pkwidth)*ratio);
  567. p->pkright =p1->pkright + int((p2->pkright - p2->pkright)*ratio);
  568. }
  569. }
  570. f1 = f2;
  571. t1 = t2;
  572. p1 = p2;
  573. }
  574. }
  575. } // end of SpectSeq::InterpolatePeak
  576. void SpectSeq::InterpolateAdjacent(void)
  577. {//=====================================
  578. int ix;
  579. int f1 = -1;
  580. int select = -1;
  581. int f2 = -1;
  582. float ratio;
  583. peak_t *p = NULL;
  584. peak_t *p1 = NULL;
  585. peak_t *p2 = NULL;
  586. for(ix=0; ix<numframes; ix++)
  587. {
  588. if(frames[ix]->selected)
  589. select = ix;
  590. else
  591. if(frames[ix]->keyframe)
  592. {
  593. if(select >= 0)
  594. {
  595. f2 = ix;
  596. break;
  597. }
  598. else
  599. f1 = ix;
  600. }
  601. }
  602. if(f1 < 0)
  603. {
  604. wxLogError(_T("No previous keyframe"));
  605. return;
  606. }
  607. if(select < 0)
  608. {
  609. wxLogError(_T("No selected frame"));
  610. return;
  611. }
  612. if(f2 < 0)
  613. {
  614. wxLogError(_T("No subsequent keyframe"));
  615. return;
  616. }
  617. // get ratio
  618. ix = wxGetNumberFromUser(_T("Interpolate between adjacent frames"),_T("percent"),_T(""),50);
  619. ratio = (float)ix/100.0;
  620. for(ix=0; ix<N_PEAKS; ix++)
  621. {
  622. p = &frames[select]->peaks[ix];
  623. p1 = &frames[f1]->peaks[ix];
  624. p2 = &frames[f2]->peaks[ix];
  625. p->pkfreq = p1->pkfreq + int((p2->pkfreq - p1->pkfreq)*ratio);
  626. p->pkheight=p1->pkheight+int((p2->pkheight-p1->pkheight)*ratio);
  627. p->pkwidth = p1->pkwidth + int((p2->pkwidth - p1->pkwidth)*ratio);
  628. p->pkright =p1->pkright + int((p2->pkright - p2->pkright)*ratio);
  629. }
  630. frames[select]->keyframe = 1;
  631. formantdlg->ShowFrame(this,select,1,0xff);
  632. }
  633. void SpectSeq::InterpolatePeaks(int control)
  634. {//=========================================
  635. // 0=turn off 1=turn on
  636. int f, peak;
  637. if(control==1)
  638. {
  639. for(peak=0; peak<N_PEAKS; peak++)
  640. {
  641. InterpolatePeak(peak);
  642. }
  643. }
  644. else
  645. {
  646. for(f=0; f<numframes; f++)
  647. {
  648. if(frames[f]->keyframe == 0)
  649. frames[f]->ZeroPeaks();
  650. }
  651. }
  652. } // end of SpectSeq::InterpolatePeaks
  653. void SpectSeq::CopyDown(int frame, int direction)
  654. {//==============================================
  655. // Copy peaks down from next earlier/later keyframe
  656. int f1;
  657. for(f1=frame+direction; f1>=0 && f1<numframes; f1 += direction)
  658. {
  659. if(frames[f1]->keyframe)
  660. {
  661. memcpy(frames[frame]->peaks, frames[f1]->peaks, sizeof(frames[frame]->peaks));
  662. memcpy(frames[frame]->klatt_param, frames[f1]->klatt_param, sizeof(frames[frame]->klatt_param));
  663. break;
  664. }
  665. }
  666. } // end of CopyDown
  667. void SpectSeq::MakePitchenv(PitchEnvelope &pitchenv, int start_frame, int end_frame)
  668. {//=================================================================================
  669. double f;
  670. double min=8000;
  671. double max=0;
  672. double diff;
  673. double t_start = -1;
  674. double t_end=0, t_diff;
  675. double yy;
  676. int ix;
  677. int x, y;
  678. int xx;
  679. int nx=0;
  680. float *ax, *ay;
  681. memset(pitchenv.env,127,128);
  682. for(ix=start_frame; ix<=end_frame; ix++)
  683. {
  684. if((f = frames[ix]->pitch) == 0) continue;
  685. nx++;
  686. t_end = frames[ix]->time;
  687. if(t_start < 0) t_start = t_end;
  688. if(f < min) min = f;
  689. if(f > max) max = f;
  690. }
  691. diff = max-min;
  692. t_diff = t_end - t_start;
  693. if(nx<2 || diff<=0 || t_diff<=0)
  694. {
  695. // no pitch info, use defaults
  696. pitchenv.pitch1=80;
  697. pitchenv.pitch2=120;
  698. return;
  699. }
  700. pitchenv.pitch1 = int(min);
  701. pitchenv.pitch2 = int(max);
  702. ax = new float [nx+1];
  703. ay = new float[nx+1];
  704. nx = 0;
  705. for(ix=start_frame; ix<=end_frame; ix++)
  706. {
  707. if((f = frames[ix]->pitch) == 0) continue;
  708. ax[++nx] = (frames[ix]->time - t_start) * 128 / t_diff;
  709. ay[nx] = (frames[ix]->pitch - min) * 255 / diff;
  710. }
  711. pitchenv.env[0] = int(ay[1]);
  712. pitchenv.env[127] = int(ay[nx]);
  713. // create pitch envelope by interpolating the time/pitch
  714. // values from the spectrum sequence
  715. xx = 1;
  716. for(x=1; x<127; x++)
  717. {
  718. while((ax[xx] < x) && (xx < nx)) xx++;
  719. if(xx < 3)
  720. yy = polint(&ax[xx-1],&ay[xx-1],3,(float)x);
  721. else if(xx > nx-1)
  722. yy = polint(&ax[xx-2],&ay[xx-2],3,(float)x);
  723. else
  724. yy = polint(&ax[xx-2],&ay[xx-2],4,(float)x);
  725. y = int(yy);
  726. if(y < 0) y = 0;
  727. if(y > 255) y = 255;
  728. pitchenv.env[x] = y;
  729. }
  730. delete ax;
  731. delete ay;
  732. } // end of SpectSeq::MakePitchenv
  733. void SpectSeq::ApplyAmp_adjust(SpectFrame *sp, peak_t *peaks)
  734. {//=============================================================
  735. int ix;
  736. int y;
  737. memcpy(peaks,sp->peaks,sizeof(*peaks)*N_PEAKS);
  738. for(ix=0; ix<N_PEAKS; ix++)
  739. {
  740. y = peaks[ix].pkheight * sp->amp_adjust * amplitude;
  741. peaks[ix].pkheight = y / 10000;
  742. }
  743. } // end of ApplyAmp_adjust
  744. void PeaksToFrame(SpectFrame *sp1, peak_t *pks, frame_t *fr)
  745. {//=========================================================
  746. int ix;
  747. int x;
  748. fr->frflags = FRFLAG_KLATT;
  749. for(ix=0; ix < 8; ix++)
  750. {
  751. if(ix < 7)
  752. {
  753. fr->ffreq[ix] = pks[ix].pkfreq;
  754. fr->klatt_ap[ix] = pks[ix].klt_ap;
  755. fr->klatt_bp[ix] = pks[ix].klt_bp/2;
  756. }
  757. if(ix < 4)
  758. fr->bw[ix] = pks[ix].klt_bw/2;
  759. fr->fheight[ix] = pks[ix].pkheight >> 6;
  760. if(ix < 6)
  761. {
  762. if((x = (pks[ix].pkwidth >> 2)) > 255)
  763. x = 255;
  764. fr->fwidth[ix] = x;
  765. if(ix < 3)
  766. {
  767. if((x = (pks[ix].pkright >> 2)) > 255)
  768. x = 255;
  769. fr->fright[ix] = x;
  770. }
  771. }
  772. }
  773. for(ix=0; ix<N_KLATTP; ix++)
  774. {
  775. fr->klattp[ix] = sp1->klatt_param[ix];
  776. }
  777. fr->klattp[KLATT_FNZ] = sp1->klatt_param[KLATT_FNZ]/2;
  778. if(fr->fheight[1] == 0)
  779. {
  780. fr->klattp[KLATT_AV] -= 10; // fade in/out
  781. }
  782. }
  783. static void SetSynth_mS(int length_mS, SpectFrame *sp1, SpectFrame *sp2, peak_t *pks1, peak_t *pks2, int control)
  784. {//==============================================================================================================
  785. static frame_t fr1, fr2;
  786. PeaksToFrame(sp1,pks1,&fr1);
  787. PeaksToFrame(sp2,pks2,&fr2);
  788. if(voice->klattv[0])
  789. {
  790. SetSynth_Klatt((length_mS * samplerate) / 1000, 0, &fr1, &fr2, voice, control); // convert mS to samples
  791. }
  792. else
  793. {
  794. SetSynth((length_mS * samplerate) / 1000, 0, &fr1, &fr2, voice); // convert mS to samples
  795. }
  796. };
  797. void SpectSeq::MakeWave(int start, int end, PitchEnvelope &pitch)
  798. {//==============================================================
  799. int ix;
  800. int length;
  801. int len_samples;
  802. int total_length;
  803. float sum_length=0;
  804. float prev_length=0;
  805. int first;
  806. int pbase;
  807. char *fname_speech;
  808. SpectFrame *sp1 = NULL;
  809. SpectFrame *sp2;
  810. double lfactor;
  811. peak_t peaks0[N_PEAKS];
  812. peak_t peaks1[N_PEAKS];
  813. peak_t peaks2[N_PEAKS];
  814. int synthesizer_type = 0;
  815. if(voice->klattv[0])
  816. {
  817. synthesizer_type = 1;
  818. KlattReset(2);
  819. }
  820. SpeakNextClause(NULL,NULL,2); // stop speaking file
  821. if(numframes==0) return;
  822. SetFrameLengths();
  823. // find overall length of sequence
  824. for(ix=0; ix<numframes; ix++)
  825. {
  826. if(frames[ix]->keyframe)
  827. {
  828. sum_length += prev_length;
  829. prev_length = frames[ix]->length;
  830. sp2 = frames[ix];
  831. if(sp1 == NULL)
  832. sp1 = sp2;
  833. }
  834. }
  835. if(sp1 == NULL)
  836. {
  837. wxLogError(_T("(No frames have peaks set"));
  838. return;
  839. }
  840. total_length = int(sum_length);
  841. pbase = voice->pitch_base >> 12;
  842. if((start==end) || (total_length == 0))
  843. {
  844. sp1->MakeWaveF(0,voicedlg->pitchenv,amplitude,duration);
  845. return;
  846. }
  847. if((duration > 0) && (duration < 40000))
  848. lfactor = double(duration)/double(total_length);
  849. else
  850. {
  851. duration = total_length;
  852. lfactor = 1;
  853. }
  854. len_samples = int(((total_length * lfactor + 50) * samplerate) / 1000);
  855. SetPitch(len_samples,pitch.env,9,44);
  856. fname_speech = WavFileName();
  857. OpenWaveFile2(fname_speech);
  858. first = 0;
  859. if(start > 0)
  860. first=1; // a selection, use fade-in
  861. sp2 = NULL;
  862. for(ix=start; ix<=end; ix++)
  863. {
  864. if(frames[ix]->keyframe)
  865. {
  866. sp1 = sp2;
  867. sp2 = frames[ix];
  868. if(sp1 != NULL)
  869. {
  870. ApplyAmp_adjust(sp1,peaks1);
  871. ApplyAmp_adjust(sp2,peaks2);
  872. if(first)
  873. {
  874. PeaksZero(peaks1,peaks0); // fade in
  875. SetSynth_mS(20,sp1,sp1,peaks0,peaks1,0);
  876. MakeWaveFile(synthesizer_type);
  877. first=0;
  878. }
  879. length = int(sp1->length * lfactor);
  880. SetSynth_mS(length,sp1,sp2,peaks1,peaks2,0);
  881. MakeWaveFile(synthesizer_type);
  882. }
  883. }
  884. }
  885. PeaksZero(peaks2,peaks0); // fade out
  886. SetSynth_mS(30,sp2,sp2,peaks2,peaks0,2);
  887. MakeWaveFile(synthesizer_type);
  888. CloseWaveFile2();
  889. PlayWavFile(fname_speech);
  890. } // end of SpectSeq::MakeWave
  891. void SpectFrame::MakeHtab(int numh, int *htab, int pitch)
  892. {//======================================================
  893. // interpolate the spectrum to give a harmonic table for
  894. // the given pitch (Hz<<12)
  895. } // end of SpectFrame::MakeHtab
  896. void SpectFrame::MakeWaveF(int control, PitchEnvelope &pitche, int amplitude, int duration)
  897. {//======================================================================================
  898. // amplitude: percentage adjustment
  899. int ix;
  900. int length; // mS
  901. int len_samples;
  902. int y;
  903. peak_t peaks0[N_PEAKS];
  904. peak_t peaks1[N_PEAKS];
  905. int ipitch;
  906. int pbase;
  907. char *fname_speech;
  908. int synthesizer_type = 0;
  909. if(voice->klattv[0])
  910. {
  911. synthesizer_type = 1;
  912. KlattReset(2);
  913. }
  914. SpeakNextClause(NULL,NULL,2); // stop speaking file
  915. length = duration;
  916. ipitch = int(pitch) << 16;
  917. if(length==0)
  918. length = 200; // default length, mS
  919. len_samples = (length * samplerate) / 1000;
  920. pbase = voice->pitch_base >> 12;
  921. SetPitch(len_samples + 50,pitche.env,9,44);
  922. fname_speech = WavFileName();
  923. if(OpenWaveFile2(fname_speech) != 0)
  924. return;
  925. if(control==0)
  926. {
  927. memcpy(peaks1,peaks,sizeof(peaks1));
  928. for(ix=0; ix<N_PEAKS; ix++)
  929. {
  930. y = peaks1[ix].pkheight * amp_adjust * amplitude;
  931. peaks1[ix].pkheight = y/10000;
  932. }
  933. PeaksZero(peaks1,peaks0);
  934. SetSynth_mS(20,this,this,peaks0,peaks1,0);
  935. MakeWaveFile(synthesizer_type);
  936. SetSynth_mS(length,this,this,peaks1,peaks1,0);
  937. MakeWaveFile(synthesizer_type);
  938. SetSynth_mS(30,this,this,peaks1,peaks0,2);
  939. MakeWaveFile(synthesizer_type);
  940. }
  941. else
  942. {
  943. #ifdef SPECT_EDITOR
  944. int maxh;
  945. USHORT htab0[600];
  946. maxh = nx;
  947. if(maxh >= 600)
  948. maxh = 600-1;
  949. for(ix=0; ix<=maxh; ix++)
  950. htab0[ix] = 0;
  951. // SetSynthHtab(20,htab0,maxh,dx,spect,maxh,dx);
  952. SetSynthHtab(20,spect,maxh,dx,spect,maxh,dx);
  953. MakeWaveFile(0);
  954. SetSynthHtab(length,spect,maxh,dx,spect,maxh,dx);
  955. MakeWaveFile(0);
  956. SetSynthHtab(30,spect,maxh,dx,htab0,maxh,dx);
  957. MakeWaveFile(0);
  958. #endif
  959. }
  960. CloseWaveFile2();
  961. PlayWavFile(fname_speech);
  962. } // end of SpectFrame::MakeWaveFrame