1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
|
/***************************************************************************
* Copyright (C) 2005 to 2007 by Jonathan Duddington *
* email: jonsd@users.sourceforge.net *
* Copyright (C) 2013 by Reece H. Dunn *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write see: *
* <http://www.gnu.org/licenses/>. *
***************************************************************************/
#include "wx/wx.h"
#include "speak_lib.h"
#include "speech.h"
#include "main.h"
#include "phoneme.h"
#include "synthesize.h"
#include "voice.h"
#include "spect.h"
#include "options.h"
#include "wx/txtstrm.h"
#include "wx/brush.h"
#include "wx/datstrm.h"
extern int PeaksToHarmspect(wavegen_peaks_t *peaks, int pitch, int *htab, int control);
extern unsigned char pk_shape1[];
extern int pk_select;
extern char voice_name[];
wxPen BLUE_PEN(wxColour(0,0,255),2,wxSOLID);
wxBrush BRUSH_SELECTED_PEAK(wxColour(255,180,180),wxSOLID);
wxBrush BRUSH_MARKER[N_MARKERS] = {
wxBrush(wxColour(200,0,255),wxSOLID),
wxBrush(wxColour(255,0,0),wxSOLID),
wxBrush(wxColour(255,200,0),wxSOLID),
wxBrush(wxColour(0,255,0),wxSOLID),
wxBrush(wxColour(0,255,255),wxSOLID),
wxBrush(wxColour(200,0,255),wxSOLID),
wxBrush(wxColour(200,0,255),wxSOLID),
wxBrush(wxColour(255,0,200),wxSOLID) };
#define DRAWPEAKWIDTH 2000
#define PEAKSHAPEW 256
#include <math.h>
static int default_freq[N_PEAKS] =
{200,500,1200,3000,3500,4000,6900,7800,9000};
static int default_width[N_PEAKS] =
{750,500,550,550,600,700,700,700,700};
static int default_klt_bw[N_PEAKS] =
{89,90,140,260,260,260,500,500,500};
float SpectTilt(int value, int freq)
{//=================================
float x;
float y;
if((currentcanvas == NULL) || (currentcanvas->spectseq->bass_reduction == 0))
return(float(value));
y = value*value*2;
if(freq < 600)
{
return(sqrt(y/2.5));
}
else
if(freq < 1050)
{
x = 1.0 + ((1050.0-freq)* 1.5)/450.0;
return(sqrt(y/x));
}
else
{
return(sqrt(y));
}
}
SpectFrame::SpectFrame(SpectFrame *copy)
: FONT_SMALL(8,wxSWISS,wxNORMAL,wxNORMAL)
, FONT_MEDIUM(9,wxSWISS,wxNORMAL,wxNORMAL)
{//=====================================
int ix;
FONT_SMALL = wxFont(8, wxSWISS, wxNORMAL, wxNORMAL); // wxWidgets 3, Font creation needs a GTK+ Window
FONT_MEDIUM = wxFont(9, wxSWISS, wxNORMAL, wxNORMAL);
selected = 0;
keyframe = 0;
spect = NULL;
markers = 0;
pitch = 0;
nx = 0;
time = 0;
length = 0;
amp_adjust = 100;
length_adjust = 0;
for(ix=0; ix<N_PEAKS; ix++)
{
formants[ix].freq = 0;
peaks[ix].pkfreq = default_freq[ix];
peaks[ix].pkheight = 0;
peaks[ix].pkwidth = default_width[ix];
peaks[ix].pkright = default_width[ix];
peaks[ix].klt_bw = default_klt_bw[ix];
peaks[ix].klt_ap = 0;
peaks[ix].klt_bp = default_klt_bw[ix];
}
memset(klatt_param, 0, sizeof(klatt_param));
klatt_param[KLATT_AV] = 59;
klatt_param[KLATT_Kopen] = 40;
if(copy != NULL)
{
*this = *copy;
spect = new USHORT[nx];
memcpy(spect,copy->spect,sizeof(USHORT)*nx);
}
}
SpectFrame::~SpectFrame()
{//=======================
if(spect != NULL)
delete spect;
}
int SpectFrame::Import(wxInputStream& stream1)
{//==========================================
// Import Pratt analysis data
int ix;
double x;
unsigned short *spect_data;
wxTextInputStream stream(stream1);
stream >> time;
stream >> pitch;
stream >> nx;
stream >> dx;
if(stream1.Eof())
return(1);
for(ix=0; ix<N_PEAKS; ix++)
{
peaks[ix].pkfreq = default_freq[ix];
peaks[ix].pkheight = 0;
peaks[ix].pkwidth = default_width[ix];
peaks[ix].pkright = default_width[ix];
}
for(ix=1; ix<=5; ix++)
{
stream >> x;
formants[ix].freq = (int)x;
if(x > 0)
peaks[ix].pkfreq = (int)x;
stream >> x;
formants[ix].bandw = (int)x;
}
spect_data = new USHORT[nx];
if(spect_data == NULL)
{
wxLogError(_T("Failed to allocate memory"));
return(1);
}
max_y = 0;
for(ix=0; ix<nx; ix++)
{
stream >> x;
spect_data[ix] = (int)(sqrt(x) * 16386);
if(spect_data[ix] > max_y)
max_y = spect_data[ix];
}
spect = spect_data;
return(0);
} // End of SpectFrame::Import
int SpectFrame::ImportSPC2(wxInputStream& stream, float &time_acc)
{//===============================================================
int ix;
int size;
unsigned short *spect_data;
CYCLE cy;
CYCLE *p;
float len;
static char peak_factor[8] = {4,5,11,20,20,25,32,32};
stream.Read(&cy,44);
size = SPC2_size_cycle(&cy);
p = (CYCLE *)malloc(size);
if(p == NULL)
{
return(1);
}
stream.SeekI(-44,wxFromCurrent);
stream.Read(p,size);
time = time_acc;
len = cy.length / 15625.0;
time_acc += len;
pitch = float(cy.pitch) / 16.0;
nx = cy.n_harm;
dx = pitch;
for(ix=0; ix<7; ix++)
{
peaks[ix].pkfreq = cy.peak_data[ix].freq * peak_factor[ix];
if(peaks[ix].pkfreq == 0)
peaks[ix].pkfreq = default_freq[ix];
peaks[ix].pkheight = cy.peak_data[ix].height * 40;
peaks[ix].pkwidth = cy.peak_data[ix].width_l * 12;
peaks[ix].pkright = cy.peak_data[ix].width_r * 12;
}
for(ix=7; ix<=8; ix++)
{
peaks[ix].pkfreq = default_freq[ix]; // default
peaks[ix].pkheight = 0;
peaks[ix].pkwidth = peaks[ix].pkright = default_width[ix];
}
if(((cy.flags & 0x80)==0) && (peaks[1].pkheight > 0))
keyframe = 1;
if(cy.flags & 0x08)
markers |= 4;
if(cy.flags & 0x10)
markers |= 2;
if(cy.flags & 0x04)
markers |= 8;
spect_data = new USHORT[nx];
if(spect_data == NULL)
{
wxLogError(_T("Failed to allocate memory"));
return(1);
}
max_y = 0;
for(ix=0; ix<nx; ix++)
{
spect_data[ix] = p->data[ix];
if(spect_data[ix] > max_y)
max_y = spect_data[ix];
}
if(nx==0)
{
nx = int(8000/dx);
spect_data = new USHORT[nx];
for(ix=0; ix<nx; ix++)
spect_data[ix] = 1;
max_y = 1;
}
spect = spect_data;
free(p);
return(0);
} // end of ImportSPC2
int SpectFrame::Load(wxInputStream& stream, int file_format_type)
{//==============================================================
int ix;
int x;
unsigned short *spect_data;
wxDataInputStream s(stream);
time = s.ReadDouble();
pitch = s.ReadDouble();
length = s.ReadDouble();
dx = s.ReadDouble();
nx = s.Read16();
markers = s.Read16();
amp_adjust = s.Read16();
if(file_format_type == 2)
{
ix = s.Read16(); // spare
ix = s.Read16(); // spare
}
for(ix=0; ix<N_PEAKS; ix++)
{
formants[ix].freq = s.Read16();
formants[ix].bandw = s.Read16();
peaks[ix].pkfreq = s.Read16();
if((peaks[ix].pkheight = s.Read16()) > 0)
keyframe = 1;
peaks[ix].pkwidth = s.Read16();
peaks[ix].pkright = s.Read16();
if(file_format_type == 2)
{
peaks[ix].klt_bw = s.Read16();
peaks[ix].klt_ap = s.Read16();
peaks[ix].klt_bp = s.Read16();
}
}
if(file_format_type > 0)
{
for(ix=0; ix<N_KLATTP2; ix++)
{
klatt_param[ix] = s.Read16();
}
}
spect_data = new USHORT[nx];
if(spect_data == NULL)
{
wxLogError(_T("Failed to allocate memory"));
return(1);
}
max_y = 0;
for(ix=0; ix<nx; ix++)
{
x = spect_data[ix] = s.Read16();
if(x > max_y) max_y = x;
}
spect = spect_data;
return(0);
} // End of SpectFrame::Load
int SpectFrame::Save(wxOutputStream& stream, int file_format_type)
{//===============================================================
int ix;
wxDataOutputStream s(stream);
s.WriteDouble(time);
s.WriteDouble(pitch);
s.WriteDouble(length);
s.WriteDouble(dx);
s.Write16(nx);
s.Write16(markers);
s.Write16(amp_adjust);
if(file_format_type == 2)
{
s.Write16(0); // spare
s.Write16(0); // spare
}
for(ix=0; ix<N_PEAKS; ix++)
{
s.Write16(formants[ix].freq);
s.Write16(formants[ix].bandw);
s.Write16(peaks[ix].pkfreq);
s.Write16(keyframe ? peaks[ix].pkheight : 0);
s.Write16(peaks[ix].pkwidth);
s.Write16(peaks[ix].pkright);
if(file_format_type == 2)
{
s.Write16(peaks[ix].klt_bw);
s.Write16(peaks[ix].klt_ap);
s.Write16(peaks[ix].klt_bp);
}
}
if(file_format_type > 0)
{
for(ix=0; ix<N_KLATTP2; ix++)
{
s.Write16(klatt_param[ix]);
}
}
for(ix=0; ix<nx; ix++)
{
s.Write16(spect[ix]);
}
return(0);
} // end of SpectFrame::Save
void SpectFrame::ZeroPeaks()
{//=========================
int pk;
for(pk=0; pk<N_PEAKS; pk++)
peaks[pk].pkheight = 0;
}
void SpectFrame::CopyPeaks(SpectFrame *sf)
{//=======================================
memcpy(peaks,sf->peaks,sizeof(peaks));
memcpy(klatt_param, sf->klatt_param, sizeof(klatt_param));
keyframe = sf->keyframe;
}
void SpectFrame::ToggleMarker(int n)
{//=================================
markers ^= 1<<n;
}
void SpectFrame::ApplyVoiceMods()
{//==============================
// apply the modifications to the formants which are defined in the current voice
int pk;
char voice_name1[40];
strcpy(voice_name1, voice_name2); // remember current voice name
if(LoadVoice(path_modifiervoice.mb_str(wxConvLocal),0x13) == NULL)
{
wxLogError(_T("Can't read voice: ")+path_modifiervoice);
OnOptions2(MENU_PATH4);
return;
}
wxLogStatus(_T("Convert using voice: ")+path_modifiervoice);
for(pk=0; pk<N_PEAKS; pk++)
{
peaks[pk].pkfreq = (peaks[pk].pkfreq * voice->freq2[pk])/256;
peaks[pk].pkheight = (peaks[pk].pkheight * voice->height2[pk])/256;
peaks[pk].pkwidth = (peaks[pk].pkwidth * voice->width2[pk])/256;
peaks[pk].pkright = (peaks[pk].pkright * voice->width2[pk])/256;
}
LoadVoice(voice_name1,1);
}
double SpectFrame::GetRms(int seq_amplitude)
{//=========================================
int h;
float total=0;
int maxh;
int height;
int htab[400];
wavegen_peaks_t wpeaks[9];
for(h=0; h<9; h++)
{
height = (peaks[h].pkheight * seq_amplitude * amp_adjust)/10000;
wpeaks[h].height = height << 8;
wpeaks[h].freq = peaks[h].pkfreq << 16;
wpeaks[h].left = peaks[h].pkwidth << 16;
wpeaks[h].right = peaks[h].pkright << 16;
}
maxh = PeaksToHarmspect(wpeaks,90<<16,htab,0);
for(h=1; h<maxh; h++)
{
total += ((htab[h] * htab[h]) >> 10);
}
rms = sqrt(total) / 7.25;
// DrawPeaks(NULL,0,0,amp);
return(rms);
}
void SpectFrame::DrawPeaks(wxDC *dc, int offy, int frame_width, int seq_amplitude, double scale_x)
{//==============================================================================================
// dc==NULL means don't draw, just calculate RMS
int peak;
peak_t *pk;
int x1,x2,x3,width,ix;
int y1, y2;
double yy;
int max_ix;
int height;
int pkright;
int pkwidth;
int buf[DRAWPEAKWIDTH*2];
max_ix = int(9000 * scale_x);
memset(buf,0,sizeof(buf));
for(peak=0; peak<N_PEAKS; peak++)
{
pk = &peaks[peak];
if((pk->pkfreq == 0) || (pk->pkheight==0)) continue;
height = pk->pkheight;
pkright = pk->pkright;
pkwidth = pk->pkwidth;
x1 = (int)(pk->pkfreq*scale_x);
x2 = (int)((pk->pkfreq + pkright)*scale_x);
x3 = (int)((pk->pkfreq - pkwidth)*scale_x);
if(x3 >= DRAWPEAKWIDTH)
continue; // whole peak is off the scale
if((width = x2-x1) <= 0) continue;
for(ix=0; ix<width; ix++)
{
buf[x1+ix] += height * pk_shape1[(ix*PEAKSHAPEW)/width];
}
if((width = x1-x3) <= 0) continue;
for(ix=1; ix<width; ix++)
{
if(x3+ix >= 0)
{
buf[x3+ix] += height * pk_shape1[((width-ix)*PEAKSHAPEW)/width];
}
}
}
rms = buf[0]>>12;
rms = rms*rms*23;
rms = rms*rms;
if(dc != NULL) dc->SetPen(*wxGREEN_PEN);
x1 = 0;
y1 = offy - ((buf[0] * FRAME_HEIGHT) >> 21);
for(ix=1; ix<max_ix; ix++)
{
yy = buf[ix]>>12;
yy = yy*yy*23;
rms += (yy*yy);
x2 = ix;
y2 = offy - ((buf[ix] * FRAME_HEIGHT) >> 21);
if(dc != NULL) dc->DrawLine(x1,y1,x2,y2);
x1 = x2;
y1 = y2;
}
rms = sqrt(rms)/200000.0;
// apply adjustment from spectseq amplitude
rms = rms * seq_amplitude * amp_adjust / 10000.0;
rms = GetRms(seq_amplitude);
} // end of SpectFrame::DrawPeaks
void SpectFrame::Draw(wxDC& dc, int offy, int frame_width, double scalex, double scaley)
{//=====================================================================================
int pt;
int peak;
peak_t *pk;
int ix;
double x0, x1;
int y0, y1;
int x, x2, x3;
double xinc;
double yf;
int font_height;
wxString text;
if(currentcanvas == NULL)
return;
dc.SetFont(*wxSWISS_FONT);
xinc = dx * scalex;
x0 = xinc;
x1 = nx * xinc;
if(selected) // this frame is selected
{
// highlight selected peak by drawing a red triangle
pk = &peaks[pk_select];
x2 = int(pk->pkright * scalex * 0.44);
x3 = int(pk->pkwidth * scalex * 0.44);
x = int((pk->pkfreq) * scalex);
y1 = (pk->pkheight * FRAME_HEIGHT) >> 14;
if(y1 < 5) y1 = 5;
wxPoint triangle[3];
dc.SetBrush(BRUSH_SELECTED_PEAK);
dc.SetPen(*wxTRANSPARENT_PEN);
triangle[0] = wxPoint(0,-y1);
triangle[1] = wxPoint(x2,0);
triangle[2] = wxPoint(-x3,0);
dc.DrawPolygon(3,triangle,x,offy);
}
// draw the measured formants
dc.SetPen(BLUE_PEN);
for(peak=1; peak<=5; peak++)
{
if(formants[peak].freq != 0)
{
// set height from linear interpolation of the adjacent
// points in the spectrum
pt = (int)(formants[peak].freq / dx);
y0 = spect[pt-1];
y1 = spect[pt];
yf = (y1-y0) * (formants[peak].freq - pt*dx)/dx;
y1 = offy - (int)((y0+yf) * scaley);
x1 = formants[peak].freq * scalex;
dc.DrawLine((int)x1,offy,(int)x1,y1);
}
}
// draw the spectrum outline
if(keyframe)
dc.SetPen(*wxBLACK_PEN);
else
dc.SetPen(*wxMEDIUM_GREY_PEN);
if(spect != NULL)
{
y0 = offy - (int)(spect[0] * scaley);
for(pt=1; pt<nx; pt++)
{
x1 = x0 + xinc;
y1 = offy - (int)(SpectTilt(spect[pt],int(pt*dx)) * scaley);
dc.DrawLine((int)x0,y0,(int)x1,y1);
x0 = x1;
y0 = y1;
}
}
if(currentcanvas->zoom < 2)
dc.SetFont(FONT_SMALL);
else
dc.SetFont(FONT_MEDIUM);
// Markers
x = frame_width - 120 - 32;
for(ix=0; ix<N_MARKERS; ix++)
{
if(markers & 1<<ix)
{
dc.SetBrush(BRUSH_MARKER[ix]);
y0 = offy-FRAME_HEIGHT+22;
dc.DrawRectangle(x,y0,22,22);
if(currentcanvas->zoom > 2)
{
text.Printf(_T("%d"),ix);
dc.DrawText(text,x+2,y0);
}
x -= 26;
}
}
DrawPeaks(&dc,offy,frame_width,currentcanvas->spectseq->amplitude,scalex);
font_height = int(15 / currentcanvas->zoomy);
text.Printf(_T("%3dmS %.1fHz"),int(time*1000),pitch);
dc.DrawText(text,frame_width-130,offy-FRAME_HEIGHT+20+font_height);
if(keyframe || rms > 0)
{
text.Printf(_T("%3d"),(int)rms);
dc.DrawText(text,frame_width-130,offy-FRAME_HEIGHT+20+font_height*2);
}
dc.SetPen(*wxBLACK_PEN);
dc.DrawLine(0,offy,frame_width,offy); // base line
} // end of SpectFrame::Draw
void SpectFrame::KlattDefaults()
{//============================
// set default values for Klatt parameters
int pk;
int bw;
int bw3;
klatt_param[KLATT_AV] = 59;
klatt_param[KLATT_AVp] = 0;
klatt_param[KLATT_Fric] = 0;
klatt_param[KLATT_FricBP] = 0;
klatt_param[KLATT_Aspr] = 0;
klatt_param[KLATT_Turb] = 0;
klatt_param[KLATT_Skew] = 0;
klatt_param[KLATT_Tilt] = 0;
klatt_param[KLATT_Kopen] = 40;
klatt_param[KLATT_FNZ] = 280;
bw = 60;
if(peaks[1].pkfreq < 400)
bw = 55;
if(peaks[1].pkfreq > 600)
bw = 70;
if(peaks[1].pkfreq > 650)
bw = 80;
if(peaks[1].pkfreq > 750)
bw = 90;
peaks[1].pkwidth = bw;
bw = 90;
bw3 = 150;
if(peaks[2].pkfreq < 1000)
{
bw = 80;
bw3 = 120;
}
if(peaks[2].pkfreq > 1600)
{
bw = 100;
bw3 = 200;
}
if(peaks[2].pkfreq > 2000)
{
bw = 110;
bw3 = 250;
}
peaks[2].pkwidth = bw;
peaks[3].pkwidth = bw3;
peaks[4].pkwidth = 200;
peaks[5].pkwidth = 200;
peaks[6].pkwidth = 500;
peaks[0].pkfreq = 280; // FNP
peaks[0].pkwidth = 280; // FNZ
peaks[7].pkfreq = 7800;
peaks[7].pkwidth = 0;
peaks[8].pkfreq = 9000;
peaks[8].pkwidth = 0;
for(pk=0; pk<=8; pk++)
{
peaks[pk].pkheight = peaks[pk].pkwidth << 6;
peaks[pk].pkright = 0;
}
}
|