Audacity 3.2.0
Noise.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 Noise.cpp
6
7 Dominic Mazzoni
8
9*******************************************************************//*******************************************************************/
15#include "Noise.h"
16#include "EffectEditor.h"
17#include "LoadEffects.h"
18
19#include <math.h>
20
21#include <wx/choice.h>
22#include <wx/textctrl.h>
23#include <wx/valgen.h>
24
25#include "Prefs.h"
26#include "ShuttleGui.h"
27#include "../widgets/valnum.h"
28#include "../widgets/NumericTextCtrl.h"
29
31{
32 // These are acceptable dual purpose internal/visible names
33 /* i18n-hint: not a color, but "white noise" having a uniform spectrum */
34 { XC("White", "noise") },
35 /* i18n-hint: not a color, but "pink noise" having a spectrum with more power
36 in low frequencies */
37 { XC("Pink", "noise") },
38 /* i18n-hint: a kind of noise spectrum also known as "red" or "brown" */
39 { XC("Brownian", "noise") }
40};
41
43{
45 Type, Amp
46 > parameters;
47 return parameters;
48}
49
50//
51// EffectNoise
52//
53
55{ XO("Noise") };
56
58
59
61{
62 Parameters().Reset(*this);
63
65
66 y = z = buf0 = buf1 = buf2 = buf3 = buf4 = buf5 = buf6 = 0;
67}
68
70{
71}
72
73// ComponentInterface implementation
74
76{
77 return Symbol;
78}
79
81{
82 return XO("Generates one of three different types of noise");
83}
84
86{
87 return L"Noise";
88}
89
90// EffectDefinitionInterface implementation
91
93{
94 return EffectTypeGenerate;
95}
96
98{
99 return 1;
100}
101
103 double sampleRate, ChannelNames)
104{
106 return true;
107}
108
110 const float *const *, float *const *outbuf, size_t size)
111{
112 float *buffer = outbuf[0];
113
114 float white;
115 float amplitude;
116 float div = ((float) RAND_MAX) / 2.0f;
117
118 switch (mType)
119 {
120 default:
121 case kWhite: // white
122 for (decltype(size) i = 0; i < size; i++)
123 {
124 buffer[i] = mAmp * ((rand() / div) - 1.0f);
125 }
126 break;
127
128 case kPink: // pink
129 // based on Paul Kellet's "instrumentation grade" algorithm.
130
131 // 0.129f is an experimental normalization factor.
132 amplitude = mAmp * 0.129f;
133 for (decltype(size) i = 0; i < size; i++)
134 {
135 white = (rand() / div) - 1.0f;
136 buf0 = 0.99886f * buf0 + 0.0555179f * white;
137 buf1 = 0.99332f * buf1 + 0.0750759f * white;
138 buf2 = 0.96900f * buf2 + 0.1538520f * white;
139 buf3 = 0.86650f * buf3 + 0.3104856f * white;
140 buf4 = 0.55000f * buf4 + 0.5329522f * white;
141 buf5 = -0.7616f * buf5 - 0.0168980f * white;
142 buffer[i] = amplitude *
143 (buf0 + buf1 + buf2 + buf3 + buf4 + buf5 + buf6 + white * 0.5362);
144 buf6 = white * 0.115926;
145 }
146 break;
147
148 case kBrownian: // Brownian
149 //float leakage=0.997; // experimental value at 44.1kHz
150 //double scaling = 0.05; // experimental value at 44.1kHz
151 // min and max protect against instability at extreme sample rates.
152 float leakage = ((mSampleRate - 144.0) / mSampleRate < 0.9999)
153 ? (mSampleRate - 144.0) / mSampleRate
154 : 0.9999f;
155
156 float scaling = (9.0 / sqrt(mSampleRate) > 0.01)
157 ? 9.0 / sqrt(mSampleRate)
158 : 0.01f;
159
160 for (decltype(size) i = 0; i < size; i++)
161 {
162 white = (rand() / div) - 1.0f;
163 z = leakage * y + white * scaling;
164 y = fabs(z) > 1.0
165 ? leakage * y - white * scaling
166 : z;
167 buffer[i] = mAmp * y;
168 }
169 break;
170 }
171
172 return size;
173}
174
175// Effect implementation
176
177std::unique_ptr<EffectEditor> EffectNoise::PopulateOrExchange(
179 const EffectOutputs *)
180{
181 mUIParent = S.GetParent();
182
183 wxASSERT(nTypes == WXSIZEOF(kTypeStrings));
184
185 S.StartMultiColumn(2, wxCENTER);
186 {
187 S.Validator<wxGenericValidator>(&mType)
188 .AddChoice(XXO("&Noise type:"), Msgids(kTypeStrings, nTypes));
189
190 S
191 .Validator<FloatingPointValidator<double>>(
192 6, &mAmp, NumValidatorStyle::NO_TRAILING_ZEROES, Amp.min, Amp.max )
193 .AddTextBox(XXO("&Amplitude (0-1):"), L"", 12);
194
195 S.AddPrompt(XXO("&Duration:"));
196 auto &extra = access.Get().extra;
199 S.GetParent(), wxID_ANY,
201 extra.GetDurationFormat(),
202 extra.GetDuration(),
204 .AutoPos(true));
205 S.Name(XO("Duration"))
206 .Position(wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL | wxALL)
207 .AddWindow(mNoiseDurationT);
208 }
209 S.EndMultiColumn();
210 return nullptr;
211}
212
214{
215 if (!mUIParent->TransferDataToWindow())
216 {
217 return false;
218 }
219
220 mNoiseDurationT->SetValue(settings.extra.GetDuration());
221 return true;
222}
223
225{
226 if (!mUIParent->Validate() || !mUIParent->TransferDataFromWindow())
227 {
228 return false;
229 }
230
231 settings.extra.SetDuration(mNoiseDurationT->GetValue());
232 return true;
233}
EffectType
@ EffectTypeGenerate
ChannelName
XO("Cut/Copy/Paste")
XXO("&Cut/Copy/Paste Toolbar")
@ nTypes
#define XC(s, c)
Definition: Internat.h:37
#define safenew
Definition: MemoryX.h:10
const NumericConverterType & NumericConverterType_TIME()
TranslatableStrings Msgids(const EnumValueSymbol strings[], size_t nStrings)
Convenience function often useful when adding choice controls.
#define S(N)
Definition: ToChars.cpp:64
static Settings & settings()
Definition: TrackInfo.cpp:83
Generates EffectParameterMethods overrides from variadic template arguments.
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
void SetLinearEffectFlag(bool linearEffectFlag)
Definition: EffectBase.cpp:214
double mProjectRate
Definition: EffectBase.h:112
Performs effect computation.
TranslatableString GetDescription() const override
Definition: Noise.cpp:80
static constexpr EffectParameter Amp
Definition: Noise.h:84
float buf3
Definition: Noise.h:67
@ nTypes
Definition: Noise.h:78
@ kPink
Definition: Noise.h:76
@ kBrownian
Definition: Noise.h:77
@ kWhite
Definition: Noise.h:75
size_t ProcessBlock(EffectSettings &settings, const float *const *inBlock, float *const *outBlock, size_t blockLen) override
Called for destructive effect computation.
Definition: Noise.cpp:109
bool TransferDataFromWindow(EffectSettings &settings) override
Definition: Noise.cpp:224
ManualPageID ManualPage() const override
Name of a page in the Audacity alpha manual, default is empty.
Definition: Noise.cpp:85
static const EnumValueSymbol kTypeStrings[nTypes]
Definition: Noise.h:80
static constexpr EnumParameter Type
Definition: Noise.h:82
float y
Definition: Noise.h:67
float buf0
Definition: Noise.h:67
double mSampleRate
Definition: Noise.h:63
const EffectParameterMethods & Parameters() const override
Definition: Noise.cpp:42
std::unique_ptr< EffectEditor > PopulateOrExchange(ShuttleGui &S, EffectInstance &instance, EffectSettingsAccess &access, const EffectOutputs *pOutputs) override
Add controls to effect panel; always succeeds.
Definition: Noise.cpp:177
float buf4
Definition: Noise.h:67
double mAmp
Definition: Noise.h:65
float buf2
Definition: Noise.h:67
NumericTextCtrl * mNoiseDurationT
Definition: Noise.h:69
float buf6
Definition: Noise.h:67
float buf5
Definition: Noise.h:67
unsigned GetAudioOutCount() const override
How many output buffers to allocate at once.
Definition: Noise.cpp:97
ComponentInterfaceSymbol GetSymbol() const override
Definition: Noise.cpp:75
wxWeakRef< wxWindow > mUIParent
Definition: Noise.h:61
static const ComponentInterfaceSymbol Symbol
Definition: Noise.h:28
bool TransferDataToWindow(const EffectSettings &settings) override
Definition: Noise.cpp:213
float z
Definition: Noise.h:67
float buf1
Definition: Noise.h:67
int mType
Definition: Noise.h:64
EffectType GetType() const override
Type determines how it behaves.
Definition: Noise.cpp:92
EffectNoise()
Definition: Noise.cpp:60
bool ProcessInitialize(EffectSettings &settings, double sampleRate, ChannelNames chanMap) override
Definition: Noise.cpp:102
virtual ~EffectNoise()
Definition: Noise.cpp:69
Hold values to send to effect output meters.
Interface for manipulations of an Effect's settings.
virtual void Reset(Effect &effect) const =0
virtual const EffectSettings & Get()=0
static FormatterContext SampleRateContext(double sampleRate)
void SetValue(double newValue)
Derived from ShuttleGuiBase, an Audacity specific class for shuttling data to and from GUI.
Definition: ShuttleGui.h:630
Holds a msgid for the translation catalog; may also bind format arguments.
BuiltinEffectsModule::Registration< EffectNoise > reg
Definition: Noise.cpp:57
__finl float_x4 __vecc sqrt(const float_x4 &a)
const Type min
Minimum value.
const Type max
Maximum value.
Externalized state of a plug-in.
EffectSettingsExtra extra
Options & AutoPos(bool enable)