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
|
/**********************************************************************
Audacity: A Digital Audio Editor
Amplify.cpp
Dominic Mazzoni
This rewritten class supports a smart Amplify effect - it calculates
the maximum amount of gain that can be applied to all tracks without
causing clipping and selects this as the default parameter.
**********************************************************************/
#include <math.h>
#include "Amplify.h"
#include "../WaveTrack.h"
//
// EffectAmplify
//
EffectAmplify::EffectAmplify()
{
ratio = 1.0;
}
bool EffectAmplify::Init()
{
peak = 0.0;
TrackListIterator iter(mWaveTracks);
VTrack *t = iter.First();
int count = 0;
while(t) {
sampleCount start, len;
sampleType min, max;
GetSamples((WaveTrack *)t, &start, &len);
((WaveTrack *)t)->GetMinMax(start, len, &min, &max);
float newpeak = (abs(min) > abs(max) ? abs(min) : abs(max)) / 32768.0;
if (newpeak > peak)
peak = newpeak;
t = iter.Next();
count++;
}
return true;
}
bool EffectAmplify::PromptUser()
{
AmplifyDialog dlog(mParent, -1, "Amplify");
dlog.peak = peak;
dlog.ratio = 1.0 / peak;
dlog.TransferDataToWindow();
dlog.CentreOnParent();
dlog.ShowModal();
if (!dlog.GetReturnCode())
return false;
ratio = dlog.ratio;
return true;
}
bool EffectAmplify::Process()
{
TrackListIterator iter(mWaveTracks);
VTrack *t = iter.First();
int count = 0;
while(t) {
sampleCount start, len;
GetSamples((WaveTrack *)t, &start, &len);
bool success = ProcessOne(count, (WaveTrack *)t, start, len);
if (!success)
return false;
t = iter.Next();
count++;
}
return true;
}
bool EffectAmplify::ProcessOne(int count, WaveTrack *t,
sampleCount start, sampleCount len)
{
sampleCount s = start;
sampleCount originalLen = len;
sampleCount blockSize = t->GetMaxBlockSize();
sampleType *buffer = new sampleType[blockSize];
while (len) {
unsigned int block = t->GetBestBlockSize(s);
if (block > len)
block = len;
t->Get(buffer, s, block);
for (unsigned int i = 0; i < block; i++) {
buffer[i] = (sampleType) (buffer[i] * ratio);
}
t->Set(buffer, s, block);
len -= block;
s += block;
if (TrackProgress(count, (s-start)/(double)originalLen))
break;
}
delete[] buffer;
return true;
}
//----------------------------------------------------------------------------
// AmplifyDialog
//----------------------------------------------------------------------------
#define AMP_MIN -240
#define AMP_MAX 240
// WDR: event table for AmplifyDialog
BEGIN_EVENT_TABLE(AmplifyDialog, wxDialog)
EVT_BUTTON(wxID_OK, AmplifyDialog::OnOk)
EVT_BUTTON(wxID_CANCEL, AmplifyDialog::OnCancel)
EVT_SLIDER(ID_AMP_SLIDER, AmplifyDialog::OnAmpSlider)
EVT_TEXT(ID_AMP_TEXT, AmplifyDialog::OnAmpText)
EVT_TEXT(ID_PEAK_TEXT, AmplifyDialog::OnPeakText)
EVT_CHECKBOX(ID_CLIP_CHECKBOX, AmplifyDialog::OnClipCheckBox)
END_EVENT_TABLE()
AmplifyDialog::AmplifyDialog(wxWindow * parent, wxWindowID id, const wxString & title, const wxPoint & position, const wxSize & size, long style):
wxDialog(parent, id, title, position, size, style)
{
ratio = 1.0;
peak = 0.0;
mLoopDetect = false;
MakeAmplifyDialog(this, TRUE, TRUE);
}
bool AmplifyDialog::Validate()
{
return TRUE;
}
bool AmplifyDialog::TransferDataToWindow()
{
wxSlider *slider;
slider = GetAmpSlider();
if (slider)
slider->SetValue((int)(100*log10(ratio)+0.5));
mLoopDetect = true;
wxTextCtrl *text = GetAmpText();
if (text) {
wxString str;
str.Printf("%.1f", 10*log10(ratio));
text->SetValue(str);
}
text = GetPeakText();
if (text) {
wxString str;
str.Printf("%.1f", 10*log10(ratio*peak));
text->SetValue(str);
}
mLoopDetect = false;
return TRUE;
}
bool AmplifyDialog::TransferDataFromWindow()
{
wxTextCtrl *c = GetAmpText();
if (c) {
double r;
wxString val = c->GetValue();
val.ToDouble(&r);
ratio = pow(10,TrapDouble(r*10, AMP_MIN, AMP_MAX)/100.0);
}
return TRUE;
}
// WDR: handler implementations for AmplifyDialog
void AmplifyDialog::OnAmpText(wxCommandEvent & event)
{
if (mLoopDetect)
return;
wxTextCtrl *c = GetAmpText();
if (c) {
double r;
mLoopDetect = true;
wxString val = c->GetValue();
val.ToDouble(&r);
ratio = pow(10,TrapDouble(r*10, AMP_MIN, AMP_MAX)/100.0);
wxSlider *slider = GetAmpSlider();
if (slider)
slider->SetValue((int)(100*log10(ratio)+0.5));
wxString str;
str.Printf("%.1f", 10*log10(ratio*peak));
GetPeakText()->SetValue(str);
mLoopDetect = false;
}
CheckClip();
}
void AmplifyDialog::OnPeakText(wxCommandEvent & event)
{
if (mLoopDetect)
return;
wxTextCtrl *c = GetPeakText();
if (c) {
double r;
mLoopDetect = true;
wxString val = c->GetValue();
val.ToDouble(&r);
ratio = pow(10, r/10.0) / peak;
double dB = TrapDouble(100*log10(ratio), AMP_MIN, AMP_MAX)/10.0;
ratio = pow(10, dB/10.0);
wxSlider *slider = GetAmpSlider();
if (slider)
slider->SetValue((int)(10*dB+0.5));
wxString str;
str.Printf("%.1f", dB);
GetAmpText()->SetValue(str);
mLoopDetect = false;
}
CheckClip();
}
void AmplifyDialog::OnAmpSlider(wxCommandEvent & event)
{
if (mLoopDetect)
return;
mLoopDetect = true;
wxString str;
double dB = GetAmpSlider()->GetValue() / 10.0;
ratio = pow(10,TrapDouble(dB, AMP_MIN, AMP_MAX)/10.0);
double dB2 = (GetAmpSlider()->GetValue()-1) / 10.0;
double ratio2 = pow(10,TrapDouble(dB2, AMP_MIN, AMP_MAX)/10.0);
if (GetClipCheckBox()->GetValue() &&
ratio * peak > 1.0 &&
ratio2 * peak < 1.0)
ratio = 1.0 / peak;
str.Printf("%.1f", 10*log10(ratio));
GetAmpText()->SetValue(str);
str.Printf("%.1f", 10*log10(ratio*peak));
GetPeakText()->SetValue(str);
mLoopDetect = false;
CheckClip();
}
void AmplifyDialog::OnClipCheckBox(wxCommandEvent & event)
{
CheckClip();
}
void AmplifyDialog::CheckClip()
{
if (GetClipCheckBox()->GetValue() == true) {
GetOK()->Enable(ratio * peak <= 1.0);
}
else {
GetOK()->Enable(true);
}
}
void AmplifyDialog::OnOk(wxCommandEvent & event)
{
TransferDataFromWindow();
if (GetClipCheckBox()->GetValue() == true) {
if (ratio * peak > 1.0)
ratio = 1.0 / peak;
}
if (Validate())
EndModal(true);
else {
event.Skip();
}
}
void AmplifyDialog::OnCancel(wxCommandEvent & event)
{
EndModal(false);
}
wxSizer *MakeAmplifyDialog(wxWindow * parent, bool call_fit,
bool set_sizer)
{
wxBoxSizer *item0 = new wxBoxSizer(wxVERTICAL);
wxStaticText *item1 =
new wxStaticText(parent, ID_TEXT,
"Amplify by Dominic Mazzoni",
wxDefaultPosition, wxDefaultSize, 0);
item0->Add(item1, 0, wxALIGN_CENTRE | wxALL, 5);
wxBoxSizer *item2 = new wxBoxSizer(wxHORIZONTAL);
item2->Add( 10, 10, 1, wxGROW | wxALL, 5 );
wxStaticText *item3 =
new wxStaticText(parent, ID_TEXT,
"Amplification (dB):",
wxDefaultPosition, wxDefaultSize, 0);
item2->Add(item3, 0, wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT | wxALL, 5);
wxTextCtrl *item4 =
new wxTextCtrl(parent, ID_AMP_TEXT, "", wxDefaultPosition,
wxSize(60, -1), 0);
item2->Add(item4, 0, wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT | wxALL, 5);
item0->Add(item2, 0, wxGROW | wxALIGN_CENTRE | wxALL, 5);
wxSlider *item5 =
new wxSlider(parent, ID_AMP_SLIDER, 0, AMP_MIN, AMP_MAX,
wxDefaultPosition, wxSize(100, -1), wxSL_HORIZONTAL);
item0->Add(item5, 1, wxGROW | wxALIGN_CENTRE | wxLEFT | wxRIGHT, 5);
wxBoxSizer *item6 = new wxBoxSizer(wxHORIZONTAL);
item6->Add( 10, 10, 1, wxGROW | wxALL, 5 );
wxStaticText *item7 =
new wxStaticText(parent, ID_TEXT,
"New Peak Amplitude (dB):",
wxDefaultPosition, wxDefaultSize, 0);
item6->Add(item7, 0, wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT | wxALL, 5);
wxTextCtrl *item8 =
new wxTextCtrl(parent, ID_PEAK_TEXT, "", wxDefaultPosition,
wxSize(60, -1), 0);
item6->Add(item8, 0, wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT | wxALL, 5);
item0->Add(item6, 0, wxGROW | wxALIGN_CENTRE | wxALL, 5);
wxCheckBox *item8b = new wxCheckBox(parent, ID_CLIP_CHECKBOX,
"Don't allow clipping",
wxDefaultPosition, wxDefaultSize, 0);
item8b->SetValue(true);
item0->Add(item8b, 0, wxALIGN_LEFT | wxALL, 5);
wxBoxSizer *item9 = new wxBoxSizer(wxHORIZONTAL);
wxButton *item10 =
new wxButton(parent, wxID_OK, "OK", wxDefaultPosition,
wxDefaultSize, 0);
#ifndef TARGET_CARBON
item10->SetDefault();
item10->SetFocus();
#endif
item9->Add(item10, 0, wxALIGN_CENTRE | wxALL, 5);
wxButton *item11 =
new wxButton(parent, wxID_CANCEL, "Cancel", wxDefaultPosition,
wxDefaultSize, 0);
item9->Add(item11, 0, wxALIGN_CENTRE | wxALL, 5);
item0->Add(item9, 0, wxALIGN_CENTRE | wxALL, 5);
if (set_sizer) {
parent->SetAutoLayout(TRUE);
parent->SetSizer(item0);
if (call_fit) {
item0->Fit(parent);
item0->SetSizeHints(parent);
}
}
return item0;
}
|