forked from audacity/audacity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToneGen.cpp
More file actions
511 lines (432 loc) · 14.3 KB
/
Copy pathToneGen.cpp
File metadata and controls
511 lines (432 loc) · 14.3 KB
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
/**********************************************************************
Audacity: A Digital Audio Editor
ToneGen.cpp
Steve Jolly
James Crook (Adapted for 'Chirps')
This class implements a tone generator effect.
*******************************************************************//**
\class EffectToneGen
\brief An Effect that can generate a sine, square or sawtooth wave.
An extended mode of EffectToneGen supports 'chirps' where the
frequency changes smoothly during the tone.
*//*******************************************************************/
#include "../Audacity.h"
#include "ToneGen.h"
#include "LoadEffects.h"
#include <math.h>
#include <float.h>
#include <wx/choice.h>
#include <wx/intl.h>
#include <wx/valgen.h>
#include "../Project.h"
#include "../ProjectSettings.h"
#include "../Shuttle.h"
#include "../ShuttleGui.h"
#include "../widgets/valnum.h"
#include "../widgets/NumericTextCtrl.h"
enum kInterpolations
{
kLinear,
kLogarithmic,
nInterpolations
};
static const EnumValueSymbol kInterStrings[nInterpolations] =
{
// These are acceptable dual purpose internal/visible names
{ XO("Linear") },
{ XO("Logarithmic") }
};
enum kWaveforms
{
kSine,
kSquare,
kSawtooth,
kSquareNoAlias,
nWaveforms
};
static const EnumValueSymbol kWaveStrings[nWaveforms] =
{
{ XO("Sine") },
{ XO("Square") },
{ XO("Sawtooth") },
{ XO("Square, no alias") }
};
// Define keys, defaults, minimums, and maximums for the effect parameters
//
// Name Type Key Def Min Max Scale
Param( StartFreq, double, wxT("StartFreq"), 440.0, 1.0, DBL_MAX, 1 );
Param( EndFreq, double, wxT("EndFreq"), 1320.0, 1.0, DBL_MAX, 1 );
Param( StartAmp, double, wxT("StartAmp"), 0.8, 0.0, 1.0, 1 );
Param( EndAmp, double, wxT("EndAmp"), 0.1, 0.0, 1.0, 1 );
Param( Frequency, double, wxT("Frequency"), 440.0, 1.0, DBL_MAX, 1 );
Param( Amplitude, double, wxT("Amplitude"), 0.8, 0.0, 1.0, 1 );
Param( Waveform, int, wxT("Waveform"), 0, 0, nWaveforms - 1, 1 );
Param( Interp, int, wxT("Interpolation"), 0, 0, nInterpolations - 1, 1 );
//
// EffectToneGen
//
const ComponentInterfaceSymbol EffectChirp::Symbol
{ XO("Chirp") };
namespace{ BuiltinEffectsModule::Registration< EffectChirp > reg; }
const ComponentInterfaceSymbol EffectTone::Symbol
{ XO("Tone") };
namespace{ BuiltinEffectsModule::Registration< EffectTone > reg2; }
BEGIN_EVENT_TABLE(EffectToneGen, wxEvtHandler)
EVT_TEXT(wxID_ANY, EffectToneGen::OnControlUpdate)
END_EVENT_TABLE();
EffectToneGen::EffectToneGen(bool isChirp)
{
wxASSERT(nWaveforms == WXSIZEOF(kWaveStrings));
wxASSERT(nInterpolations == WXSIZEOF(kInterStrings));
mChirp = isChirp;
mWaveform = DEF_Waveform;
mFrequency[0] = DEF_StartFreq;
mFrequency[1] = DEF_EndFreq;
mAmplitude[0] = DEF_StartAmp;
mAmplitude[1] = DEF_EndAmp;
mInterpolation = DEF_Interp;
// Chirp varies over time so must use selected duration.
// TODO: When previewing, calculate only the first 'preview length'.
if (isChirp)
SetLinearEffectFlag(false);
else
SetLinearEffectFlag(true);
}
EffectToneGen::~EffectToneGen()
{
}
// ComponentInterface implementation
ComponentInterfaceSymbol EffectToneGen::GetSymbol()
{
return mChirp
? EffectChirp::Symbol
: EffectTone::Symbol;
}
TranslatableString EffectToneGen::GetDescription()
{
return mChirp
? XO("Generates an ascending or descending tone of one of four types")
: XO("Generates a constant frequency tone of one of four types");
}
wxString EffectToneGen::ManualPage()
{
return mChirp
? wxT("Chirp")
: wxT("Tone");
}
// EffectDefinitionInterface implementation
EffectType EffectToneGen::GetType()
{
return EffectTypeGenerate;
}
// EffectClientInterface implementation
unsigned EffectToneGen::GetAudioOutCount()
{
return 1;
}
bool EffectToneGen::ProcessInitialize(sampleCount WXUNUSED(totalLen), ChannelNames WXUNUSED(chanMap))
{
mPositionInCycles = 0.0;
mSample = 0;
return true;
}
size_t EffectToneGen::ProcessBlock(float **WXUNUSED(inBlock), float **outBlock, size_t blockLen)
{
float *buffer = outBlock[0];
double throwaway = 0; //passed to modf but never used
double f = 0.0;
double a, b;
int k;
double frequencyQuantum;
double BlendedFrequency;
double BlendedAmplitude;
double BlendedLogFrequency = 0.0;
// calculate delta, and reposition from where we left
auto doubleSampleCount = mSampleCnt.as_double();
auto doubleSample = mSample.as_double();
double amplitudeQuantum =
(mAmplitude[1] - mAmplitude[0]) / doubleSampleCount;
BlendedAmplitude = mAmplitude[0] +
amplitudeQuantum * doubleSample;
// precalculations:
double pre2PI = 2.0 * M_PI;
double pre4divPI = 4.0 / M_PI;
// initial setup should calculate deltas
if (mInterpolation == kLogarithmic)
{
// this for log interpolation
mLogFrequency[0] = log10(mFrequency[0]);
mLogFrequency[1] = log10(mFrequency[1]);
// calculate delta, and reposition from where we left
frequencyQuantum = (mLogFrequency[1] - mLogFrequency[0]) / doubleSampleCount;
BlendedLogFrequency = mLogFrequency[0] + frequencyQuantum * doubleSample;
BlendedFrequency = pow(10.0, BlendedLogFrequency);
}
else
{
// this for regular case, linear interpolation
frequencyQuantum = (mFrequency[1] - mFrequency[0]) / doubleSampleCount;
BlendedFrequency = mFrequency[0] + frequencyQuantum * doubleSample;
}
// synth loop
for (decltype(blockLen) i = 0; i < blockLen; i++)
{
switch (mWaveform)
{
case kSine:
f = sin(pre2PI * mPositionInCycles / mSampleRate);
break;
case kSquare:
f = (modf(mPositionInCycles / mSampleRate, &throwaway) < 0.5) ? 1.0 : -1.0;
break;
case kSawtooth:
f = (2.0 * modf(mPositionInCycles / mSampleRate + 0.5, &throwaway)) - 1.0;
break;
case kSquareNoAlias: // Good down to 110Hz @ 44100Hz sampling.
//do fundamental (k=1) outside loop
b = (1.0 + cos((pre2PI * BlendedFrequency) / mSampleRate)) / pre4divPI; //scaling
f = pre4divPI * sin(pre2PI * mPositionInCycles / mSampleRate);
for (k = 3; (k < 200) && (k * BlendedFrequency < mSampleRate / 2.0); k += 2)
{
//Hann Window in freq domain
a = 1.0 + cos((pre2PI * k * BlendedFrequency) / mSampleRate);
//calc harmonic, apply window, scale to amplitude of fundamental
f += a * sin(pre2PI * mPositionInCycles / mSampleRate * k) / (b * k);
}
}
// insert value in buffer
buffer[i] = (float) (BlendedAmplitude * f);
// update freq,amplitude
mPositionInCycles += BlendedFrequency;
BlendedAmplitude += amplitudeQuantum;
if (mInterpolation == kLogarithmic)
{
BlendedLogFrequency += frequencyQuantum;
BlendedFrequency = pow(10.0, BlendedLogFrequency);
}
else
{
BlendedFrequency += frequencyQuantum;
}
}
// update external placeholder
mSample += blockLen;
return blockLen;
}
bool EffectToneGen::DefineParams( ShuttleParams & S ){
if( mChirp ){
S.SHUTTLE_PARAM( mFrequency[0], StartFreq );
S.SHUTTLE_PARAM( mFrequency[1], EndFreq );
S.SHUTTLE_PARAM( mAmplitude[0], StartAmp );
S.SHUTTLE_PARAM( mAmplitude[1], EndAmp );
} else {
S.SHUTTLE_PARAM( mFrequency[0], Frequency );
S.SHUTTLE_PARAM( mAmplitude[0], Amplitude );
// Slightly hacky way to set freq and ampl
// since we do this whatever query to params was made.
mFrequency[1] = mFrequency[0];
mAmplitude[1] = mAmplitude[0];
}
S.SHUTTLE_ENUM_PARAM( mWaveform, Waveform, kWaveStrings, nWaveforms );
S.SHUTTLE_ENUM_PARAM( mInterpolation, Interp, kInterStrings, nInterpolations );
// double freqMax = (FindProject() ? FindProject()->GetRate() : 44100.0) / 2.0;
// mFrequency[1] = TrapDouble(mFrequency[1], MIN_EndFreq, freqMax);
return true;
}
bool EffectToneGen::GetAutomationParameters(CommandParameters & parms)
{
if (mChirp)
{
parms.Write(KEY_StartFreq, mFrequency[0]);
parms.Write(KEY_EndFreq, mFrequency[1]);
parms.Write(KEY_StartAmp, mAmplitude[0]);
parms.Write(KEY_EndAmp, mAmplitude[1]);
}
else
{
parms.Write(KEY_Frequency, mFrequency[0]);
parms.Write(KEY_Amplitude, mAmplitude[0]);
}
parms.Write(KEY_Waveform, kWaveStrings[mWaveform].Internal());
parms.Write(KEY_Interp, kInterStrings[mInterpolation].Internal());
return true;
}
bool EffectToneGen::SetAutomationParameters(CommandParameters & parms)
{
ReadAndVerifyEnum(Waveform, kWaveStrings, nWaveforms);
ReadAndVerifyEnum(Interp, kInterStrings, nInterpolations);
if (mChirp)
{
ReadAndVerifyDouble(StartFreq);
ReadAndVerifyDouble(EndFreq);
ReadAndVerifyDouble(StartAmp);
ReadAndVerifyDouble(EndAmp);
mFrequency[0] = StartFreq;
mFrequency[1] = EndFreq;
mAmplitude[0] = StartAmp;
mAmplitude[1] = EndAmp;
}
else
{
ReadAndVerifyDouble(Frequency);
ReadAndVerifyDouble(Amplitude);
mFrequency[0] = Frequency;
mFrequency[1] = Frequency;
mAmplitude[0] = Amplitude;
mAmplitude[1] = Amplitude;
}
mWaveform = Waveform;
mInterpolation = Interp;
double freqMax =
(FindProject()
? ProjectSettings::Get( *FindProject() ).GetRate()
: 44100.0)
/ 2.0;
mFrequency[1] = TrapDouble(mFrequency[1], MIN_EndFreq, freqMax);
return true;
}
// Effect implementation
void EffectToneGen::PopulateOrExchange(ShuttleGui & S)
{
wxTextCtrl *t;
S.StartMultiColumn(2, wxCENTER);
{
S.Validator<wxGenericValidator>(&mWaveform)
.AddChoice(XXO("&Waveform:"),
Msgids( kWaveStrings, nWaveforms ) );
if (mChirp)
{
S.AddFixedText( {} );
S.StartHorizontalLay(wxEXPAND);
{
S.StartHorizontalLay(wxLEFT, 50);
{
S.AddTitle(XO("Start"));
}
S.EndHorizontalLay();
S.StartHorizontalLay(wxLEFT, 50);
{
S.AddTitle(XO("End"));
}
S.EndHorizontalLay();
}
S.EndHorizontalLay();
S.AddPrompt(XXO("&Frequency (Hz):"));
S.StartHorizontalLay(wxEXPAND);
{
S.StartHorizontalLay(wxLEFT, 50);
{
t = S.Name(XO("Frequency Hertz Start"))
.Validator<FloatingPointValidator<double>>(
6, &mFrequency[0],
NumValidatorStyle::NO_TRAILING_ZEROES,
MIN_StartFreq,
mProjectRate / 2.0
)
.AddTextBox( {}, wxT(""), 12);
}
S.EndHorizontalLay();
S.StartHorizontalLay(wxLEFT, 50);
{
t = S.Name(XO("Frequency Hertz End"))
.Validator<FloatingPointValidator<double>>(
6, &mFrequency[1],
NumValidatorStyle::NO_TRAILING_ZEROES,
MIN_EndFreq,
mProjectRate / 2.0
)
.AddTextBox( {}, wxT(""), 12);
}
S.EndHorizontalLay();
}
S.EndHorizontalLay();
S.AddPrompt(XXO("&Amplitude (0-1):"));
S.StartHorizontalLay(wxEXPAND);
{
S.StartHorizontalLay(wxLEFT, 50);
{
t = S.Name(XO("Amplitude Start"))
.Validator<FloatingPointValidator<double>>(
6, &mAmplitude[0], NumValidatorStyle::NO_TRAILING_ZEROES,
MIN_StartAmp, MAX_StartAmp
)
.AddTextBox( {}, wxT(""), 12);
}
S.EndHorizontalLay();
S.StartHorizontalLay(wxLEFT, 50);
{
t = S.Name(XO("Amplitude End"))
.Validator<FloatingPointValidator<double>>(
6, &mAmplitude[1], NumValidatorStyle::NO_TRAILING_ZEROES,
MIN_EndAmp, MAX_EndAmp
)
.AddTextBox( {}, wxT(""), 12);
}
S.EndHorizontalLay();
}
S.EndHorizontalLay();
S.Validator<wxGenericValidator>(&mInterpolation)
.AddChoice(XXO("I&nterpolation:"),
Msgids( kInterStrings, nInterpolations ) );
}
else
{
t = S.Validator<FloatingPointValidator<double>>(
6, &mFrequency[0], NumValidatorStyle::NO_TRAILING_ZEROES,
MIN_Frequency,
mProjectRate / 2.0
)
.AddTextBox(XXO("&Frequency (Hz):"), wxT(""), 12);
t = S.Validator<FloatingPointValidator<double>>(
6, &mAmplitude[0], NumValidatorStyle::NO_TRAILING_ZEROES,
MIN_Amplitude, MAX_Amplitude
)
.AddTextBox(XXO("&Amplitude (0-1):"), wxT(""), 12);
}
S.AddPrompt(XXO("&Duration:"));
mToneDurationT = safenew
NumericTextCtrl(S.GetParent(), wxID_ANY,
NumericConverter::TIME,
GetDurationFormat(),
GetDuration(),
mProjectRate,
NumericTextCtrl::Options{}
.AutoPos(true));
S.Name(XO("Duration"))
.Position(wxALIGN_LEFT | wxALL)
.AddWindow(mToneDurationT);
}
S.EndMultiColumn();
return;
}
bool EffectToneGen::TransferDataToWindow()
{
if (!mUIParent->TransferDataToWindow())
{
return false;
}
mToneDurationT->SetValue(GetDuration());
return true;
}
bool EffectToneGen::TransferDataFromWindow()
{
if (!mUIParent->Validate() || !mUIParent->TransferDataFromWindow())
{
return false;
}
if (!mChirp)
{
mFrequency[1] = mFrequency[0];
mAmplitude[1] = mAmplitude[0];
}
SetDuration(mToneDurationT->GetValue());
return true;
}
// EffectToneGen implementation
void EffectToneGen::OnControlUpdate(wxCommandEvent & WXUNUSED(evt))
{
if (!EnableApply(mUIParent->TransferDataFromWindow()))
{
return;
}
}