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 23KB

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