Audacity  3.0.3
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 "../Shuttle.h"
29 #include "../ShuttleGui.h"
30 #include "../widgets/valnum.h"
31 #include "../widgets/NumericTextCtrl.h"
32 
33 enum kTypes
34 {
38  nTypes
39 };
40 
42 {
43  // These are acceptable dual purpose internal/visible names
44  /* i18n-hint: not a color, but "white noise" having a uniform spectrum */
45  { XC("White", "noise") },
46  /* i18n-hint: not a color, but "pink noise" having a spectrum with more power
47  in low frequencies */
48  { XC("Pink", "noise") },
49  /* i18n-hint: a kind of noise spectrum also known as "red" or "brown" */
50  { XC("Brownian", "noise") }
51 };
52 
53 // Define keys, defaults, minimums, and maximums for the effect parameters
54 //
55 // Name Type Key Def Min Max Scale
56 Param( Type, int, wxT("Type"), kWhite, 0, nTypes - 1, 1 );
57 Param( Amp, double, wxT("Amplitude"), 0.8, 0.0, 1.0, 1 );
58 
59 //
60 // EffectNoise
61 //
62 
64 { XO("Noise") };
65 
67 
69 {
70  mType = DEF_Type;
71  mAmp = DEF_Amp;
72 
73  SetLinearEffectFlag(true);
74 
75  y = z = buf0 = buf1 = buf2 = buf3 = buf4 = buf5 = buf6 = 0;
76 }
77 
79 {
80 }
81 
82 // ComponentInterface implementation
83 
85 {
86  return Symbol;
87 }
88 
90 {
91  return XO("Generates one of three different types of noise");
92 }
93 
95 {
96  return wxT("Noise");
97 }
98 
99 // EffectDefinitionInterface implementation
100 
102 {
103  return EffectTypeGenerate;
104 }
105 
106 // EffectClientInterface implementation
107 
109 {
110  return 1;
111 }
112 
113 size_t EffectNoise::ProcessBlock(float **WXUNUSED(inbuf), float **outbuf, size_t size)
114 {
115  float *buffer = outbuf[0];
116 
117  float white;
118  float amplitude;
119  float div = ((float) RAND_MAX) / 2.0f;
120 
121  switch (mType)
122  {
123  default:
124  case kWhite: // white
125  for (decltype(size) i = 0; i < size; i++)
126  {
127  buffer[i] = mAmp * ((rand() / div) - 1.0f);
128  }
129  break;
130 
131  case kPink: // pink
132  // based on Paul Kellet's "instrumentation grade" algorithm.
133 
134  // 0.129f is an experimental normalization factor.
135  amplitude = mAmp * 0.129f;
136  for (decltype(size) i = 0; i < size; i++)
137  {
138  white = (rand() / div) - 1.0f;
139  buf0 = 0.99886f * buf0 + 0.0555179f * white;
140  buf1 = 0.99332f * buf1 + 0.0750759f * white;
141  buf2 = 0.96900f * buf2 + 0.1538520f * white;
142  buf3 = 0.86650f * buf3 + 0.3104856f * white;
143  buf4 = 0.55000f * buf4 + 0.5329522f * white;
144  buf5 = -0.7616f * buf5 - 0.0168980f * white;
145  buffer[i] = amplitude *
146  (buf0 + buf1 + buf2 + buf3 + buf4 + buf5 + buf6 + white * 0.5362);
147  buf6 = white * 0.115926;
148  }
149  break;
150 
151  case kBrownian: // Brownian
152  //float leakage=0.997; // experimental value at 44.1kHz
153  //double scaling = 0.05; // experimental value at 44.1kHz
154  // min and max protect against instability at extreme sample rates.
155  float leakage = ((mSampleRate - 144.0) / mSampleRate < 0.9999)
156  ? (mSampleRate - 144.0) / mSampleRate
157  : 0.9999f;
158 
159  float scaling = (9.0 / sqrt(mSampleRate) > 0.01)
160  ? 9.0 / sqrt(mSampleRate)
161  : 0.01f;
162 
163  for (decltype(size) i = 0; i < size; i++)
164  {
165  white = (rand() / div) - 1.0f;
166  z = leakage * y + white * scaling;
167  y = fabs(z) > 1.0
168  ? leakage * y - white * scaling
169  : z;
170  buffer[i] = mAmp * y;
171  }
172  break;
173  }
174 
175  return size;
176 }
178  S.SHUTTLE_ENUM_PARAM( mType, Type, kTypeStrings, nTypes );
179  S.SHUTTLE_PARAM( mAmp, Amp );
180  return true;
181 }
182 
184 {
185  parms.Write(KEY_Type, kTypeStrings[mType].Internal());
186  parms.Write(KEY_Amp, mAmp);
187 
188  return true;
189 }
190 
192 {
194  ReadAndVerifyDouble(Amp);
195 
196  mType = Type;
197  mAmp = Amp;
198 
199  return true;
200 }
201 
202 // Effect implementation
203 
205 {
206  wxString base = wxT("/Effects/Noise/");
207 
208  // Migrate settings from 2.1.0 or before
209 
210  // Already migrated, so bail
211  if (gPrefs->Exists(base + wxT("Migrated")))
212  {
213  return true;
214  }
215 
216  // Load the old "current" settings
217  if (gPrefs->Exists(base))
218  {
219  gPrefs->Read(base + wxT("Type"), &mType, 0L);
220  gPrefs->Read(base + wxT("Amplitude"), &mAmp, 0.8f);
221 
223 
224  // Do not migrate again
225  gPrefs->Write(base + wxT("Migrated"), true);
226  gPrefs->Flush();
227  }
228 
229  return true;
230 }
231 
233 {
234  wxASSERT(nTypes == WXSIZEOF(kTypeStrings));
235 
236  S.StartMultiColumn(2, wxCENTER);
237  {
238  S.Validator<wxGenericValidator>(&mType)
239  .AddChoice(XXO("&Noise type:"), Msgids(kTypeStrings, nTypes));
240 
241  S.Validator<FloatingPointValidator<double>>(
242  6, &mAmp, NumValidatorStyle::NO_TRAILING_ZEROES, MIN_Amp, MAX_Amp
243  )
244  .AddTextBox(XXO("&Amplitude (0-1):"), wxT(""), 12);
245 
246  S.AddPrompt(XXO("&Duration:"));
248  NumericTextCtrl(S.GetParent(), wxID_ANY,
251  GetDuration(),
252  mProjectRate,
254  .AutoPos(true));
255  S.Name(XO("Duration"))
256  .Position(wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL | wxALL)
258  }
259  S.EndMultiColumn();
260 }
261 
263 {
264  if (!mUIParent->TransferDataToWindow())
265  {
266  return false;
267  }
268 
270 
271  return true;
272 }
273 
275 {
276  if (!mUIParent->Validate() || !mUIParent->TransferDataFromWindow())
277  {
278  return false;
279  }
280 
282 
283  return true;
284 }
TranslatableString
Definition: Types.h:290
CommandParameters
CommandParameters, derived from wxFileConfig, is essentially doing the same things as the Shuttle cla...
Definition: EffectAutomationParameters.h:67
EffectNoise::PopulateOrExchange
void PopulateOrExchange(ShuttleGui &S) override
Definition: Noise.cpp:232
Effect::GetDuration
double GetDuration() override
Definition: Effect.cpp:815
EffectNoise::TransferDataFromWindow
bool TransferDataFromWindow() override
Definition: Noise.cpp:274
EffectNoise::buf2
float buf2
Definition: Noise.h:61
Effect::SetLinearEffectFlag
void SetLinearEffectFlag(bool linearEffectFlag)
Definition: Effect.cpp:2000
EffectNoise::buf0
float buf0
Definition: Noise.h:61
EffectNoise::TransferDataToWindow
bool TransferDataToWindow() override
Definition: Noise.cpp:262
NumericTextCtrl::Options::AutoPos
Options & AutoPos(bool enable)
Definition: NumericTextCtrl.h:189
gPrefs
FileConfig * gPrefs
Definition: Prefs.cpp:67
EffectTypeGenerate
@ EffectTypeGenerate
Definition: EffectInterface.h:58
EffectNoise::GetType
EffectType GetType() override
Definition: Noise.cpp:101
kPink
@ kPink
Definition: Noise.cpp:36
XO
#define XO(s)
Definition: Internat.h:32
XC
#define XC(s, c)
Definition: Internat.h:38
Param
Param(Type, int, wxT("Type"), kWhite, 0, nTypes - 1, 1)
ShuttleParams
Shuttle that deals with parameters. This is a base class with lots of virtual functions that do nothi...
Definition: Shuttle.h:61
ShuttleGuiBase::EndMultiColumn
void EndMultiColumn()
Definition: ShuttleGui.cpp:1212
EffectNoise::DefineParams
bool DefineParams(ShuttleParams &S) override
Definition: Noise.cpp:177
EffectNoise::mType
int mType
Definition: Noise.h:58
kBrownian
@ kBrownian
Definition: Noise.cpp:37
EffectNoise::EffectNoise
EffectNoise()
Definition: Noise.cpp:68
EffectNoise::y
float y
Definition: Noise.h:61
Effect::SaveUserPreset
bool SaveUserPreset(const RegistryPath &name) override
Definition: Effect.cpp:569
NumericTextCtrl
Definition: NumericTextCtrl.h:171
NumericTextCtrl::SetValue
void SetValue(double newValue)
Definition: NumericTextCtrl.cpp:1472
Noise.h
ComponentInterfaceSymbol
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
Definition: ComponentInterface.h:60
NumericTextCtrl::Options
Definition: NumericTextCtrl.h:177
EffectNoise::mNoiseDurationT
NumericTextCtrl * mNoiseDurationT
Definition: Noise.h:63
EffectNoise::Startup
bool Startup() override
Definition: Noise.cpp:204
EffectNoise::buf1
float buf1
Definition: Noise.h:61
Effect::mProjectRate
double mProjectRate
Definition: Effect.h:457
ReadAndVerifyEnum
#define ReadAndVerifyEnum(name, list, listSize)
Definition: Effect.h:620
XXO
#define XXO(s)
Definition: Internat.h:45
EffectNoise::buf3
float buf3
Definition: Noise.h:61
EffectNoise::ProcessBlock
size_t ProcessBlock(float **inBlock, float **outBlock, size_t blockLen) override
Definition: Noise.cpp:113
EffectNoise::GetAutomationParameters
bool GetAutomationParameters(CommandParameters &parms) override
Definition: Noise.cpp:183
EffectNoise::ManualPage
wxString ManualPage() override
Definition: Noise.cpp:94
ShuttleGuiBase::StartMultiColumn
void StartMultiColumn(int nCols, int PositionFlags=wxALIGN_LEFT)
Definition: ShuttleGui.cpp:1203
anonymous_namespace{Noise.cpp}::reg
BuiltinEffectsModule::Registration< EffectNoise > reg
Definition: Noise.cpp:66
Msgids
TranslatableStrings Msgids(const EnumValueSymbol strings[], size_t nStrings)
Definition: Internat.cpp:267
NumericConverter::TIME
@ TIME
Definition: NumericTextCtrl.h:51
ShuttleGuiBase::GetParent
wxWindow * GetParent()
Definition: ShuttleGui.h:503
ShuttleGui::Validator
ShuttleGui & Validator(const Factory &f)
Definition: ShuttleGui.h:685
kWhite
@ kWhite
Definition: Noise.cpp:35
EffectNoise::mAmp
double mAmp
Definition: Noise.h:59
ReadAndVerifyDouble
#define ReadAndVerifyDouble(name)
Definition: Effect.h:632
EffectNoise::Symbol
static const ComponentInterfaceSymbol Symbol
Definition: Noise.h:24
Effect::GetCurrentSettingsGroup
RegistryPath GetCurrentSettingsGroup() override
Definition: Effect.cpp:865
EffectNoise::buf4
float buf4
Definition: Noise.h:61
LoadEffects.h
Effect::SetDuration
void SetDuration(double duration) override
Definition: Effect.cpp:837
nTypes
@ nTypes
Definition: Noise.cpp:38
ShuttleGui::Name
ShuttleGui & Name(const TranslatableString &name)
Definition: ShuttleGui.h:670
Effect::mSampleRate
double mSampleRate
Definition: Effect.h:459
ShuttleGuiBase::AddWindow
wxWindow * AddWindow(wxWindow *pWindow)
Definition: ShuttleGui.cpp:292
FileConfig::Flush
virtual bool Flush(bool bCurrentOnly=false) wxOVERRIDE
Definition: FileConfig.cpp:151
EffectNoise::GetDescription
TranslatableString GetDescription() override
Definition: Noise.cpp:89
BuiltinEffectsModule::Registration
Definition: LoadEffects.h:40
ShuttleGui::Position
ShuttleGui & Position(int flags)
Definition: ShuttleGui.h:719
Effect::mUIParent
wxWindow * mUIParent
Definition: Effect.h:477
EffectNoise::GetAudioOutCount
unsigned GetAudioOutCount() override
Definition: Noise.cpp:108
EffectNoise::buf5
float buf5
Definition: Noise.h:61
kTypeStrings
static const EnumValueSymbol kTypeStrings[nTypes]
Definition: Noise.cpp:41
EffectNoise::SetAutomationParameters
bool SetAutomationParameters(CommandParameters &parms) override
Definition: Noise.cpp:191
Effect::GetDurationFormat
NumericFormatSymbol GetDurationFormat() override
Definition: Effect.cpp:825
ShuttleGuiBase::AddPrompt
void AddPrompt(const TranslatableString &Prompt, int wrapWidth=0)
Right aligned text string.
Definition: ShuttleGui.cpp:231
kTypes
kTypes
Definition: Noise.cpp:34
EffectNoise::z
float z
Definition: Noise.h:61
EffectType
EffectType
Definition: EffectInterface.h:55
safenew
#define safenew
Definition: MemoryX.h:8
NumericConverter::GetValue
double GetValue()
Definition: NumericTextCtrl.cpp:1172
EffectNoise::~EffectNoise
virtual ~EffectNoise()
Definition: Noise.cpp:78
ShuttleGui
Derived from ShuttleGuiBase, an Audacity specific class for shuttling data to and from GUI.
Definition: ShuttleGui.h:638
EffectNoise::buf6
float buf6
Definition: Noise.h:61
EffectNoise::GetSymbol
ComponentInterfaceSymbol GetSymbol() override
Definition: Noise.cpp:84