eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.

spectseq.cpp 24KB

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