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

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