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