Audacity  2.2.2
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 #include "../Audacity.h"
17 #include "Noise.h"
18 
19 #include <math.h>
20 
21 #include <wx/choice.h>
22 #include <wx/intl.h>
23 #include <wx/textctrl.h>
24 #include <wx/valgen.h>
25 
26 #include "../Prefs.h"
27 #include "../ShuttleGui.h"
28 #include "../widgets/valnum.h"
29 
30 enum kTypes
31 {
36 };
37 
39 {
40  // These are acceptable dual purpose internal/visible names
41  { XO("White") },
42  { XO("Pink") },
43  { XO("Brownian") }
44 };
45 
46 // Define keys, defaults, minimums, and maximums for the effect parameters
47 //
48 // Name Type Key Def Min Max Scale
49 Param( Type, int, wxT("Type"), kWhite, 0, nTypes - 1, 1 );
50 Param( Amp, double, wxT("Amplitude"), 0.8, 0.0, 1.0, 1 );
51 
52 //
53 // EffectNoise
54 //
55 
57 {
58  mType = DEF_Type;
59  mAmp = DEF_Amp;
60 
61  SetLinearEffectFlag(true);
62 
63  y = z = buf0 = buf1 = buf2 = buf3 = buf4 = buf5 = buf6 = 0;
64 }
65 
67 {
68 }
69 
70 // IdentInterface implementation
71 
73 {
74  return NOISE_PLUGIN_SYMBOL;
75 }
76 
78 {
79  return _("Generates one of three different types of noise");
80 }
81 
83 {
84  return wxT("Noise");
85 }
86 
87 // EffectDefinitionInterface implementation
88 
90 {
91  return EffectTypeGenerate;
92 }
93 
94 // EffectClientInterface implementation
95 
97 {
98  return 1;
99 }
100 
101 size_t EffectNoise::ProcessBlock(float **WXUNUSED(inbuf), float **outbuf, size_t size)
102 {
103  float *buffer = outbuf[0];
104 
105  float white;
106  float amplitude;
107  float div = ((float) RAND_MAX) / 2.0f;
108 
109  switch (mType)
110  {
111  default:
112  case kWhite: // white
113  for (decltype(size) i = 0; i < size; i++)
114  {
115  buffer[i] = mAmp * ((rand() / div) - 1.0f);
116  }
117  break;
118 
119  case kPink: // pink
120  // based on Paul Kellet's "instrumentation grade" algorithm.
121 
122  // 0.129f is an experimental normalization factor.
123  amplitude = mAmp * 0.129f;
124  for (decltype(size) i = 0; i < size; i++)
125  {
126  white = (rand() / div) - 1.0f;
127  buf0 = 0.99886f * buf0 + 0.0555179f * white;
128  buf1 = 0.99332f * buf1 + 0.0750759f * white;
129  buf2 = 0.96900f * buf2 + 0.1538520f * white;
130  buf3 = 0.86650f * buf3 + 0.3104856f * white;
131  buf4 = 0.55000f * buf4 + 0.5329522f * white;
132  buf5 = -0.7616f * buf5 - 0.0168980f * white;
133  buffer[i] = amplitude *
134  (buf0 + buf1 + buf2 + buf3 + buf4 + buf5 + buf6 + white * 0.5362);
135  buf6 = white * 0.115926;
136  }
137  break;
138 
139  case kBrownian: // Brownian
140  //float leakage=0.997; // experimental value at 44.1kHz
141  //double scaling = 0.05; // experimental value at 44.1kHz
142  // min and max protect against instability at extreme sample rates.
143  float leakage = ((mSampleRate - 144.0) / mSampleRate < 0.9999)
144  ? (mSampleRate - 144.0) / mSampleRate
145  : 0.9999f;
146 
147  float scaling = (9.0 / sqrt(mSampleRate) > 0.01)
148  ? 9.0 / sqrt(mSampleRate)
149  : 0.01f;
150 
151  for (decltype(size) i = 0; i < size; i++)
152  {
153  white = (rand() / div) - 1.0f;
154  z = leakage * y + white * scaling;
155  y = fabs(z) > 1.0
156  ? leakage * y - white * scaling
157  : z;
158  buffer[i] = mAmp * y;
159  }
160  break;
161  }
162 
163  return size;
164 }
166  S.SHUTTLE_ENUM_PARAM( mType, Type, kTypeStrings, nTypes );
167  S.SHUTTLE_PARAM( mAmp, Amp );
168  return true;
169 }
170 
172 {
173  parms.Write(KEY_Type, kTypeStrings[mType].Internal());
174  parms.Write(KEY_Amp, mAmp);
175 
176  return true;
177 }
178 
180 {
181  ReadAndVerifyEnum(Type, kTypeStrings, nTypes);
182  ReadAndVerifyDouble(Amp);
183 
184  mType = Type;
185  mAmp = Amp;
186 
187  return true;
188 }
189 
190 // Effect implementation
191 
193 {
194  wxString base = wxT("/Effects/Noise/");
195 
196  // Migrate settings from 2.1.0 or before
197 
198  // Already migrated, so bail
199  if (gPrefs->Exists(base + wxT("Migrated")))
200  {
201  return true;
202  }
203 
204  // Load the old "current" settings
205  if (gPrefs->Exists(base))
206  {
207  gPrefs->Read(base + wxT("Type"), &mType, 0L);
208  gPrefs->Read(base + wxT("Amplitude"), &mAmp, 0.8f);
209 
211 
212  // Do not migrate again
213  gPrefs->Write(base + wxT("Migrated"), true);
214  gPrefs->Flush();
215  }
216 
217  return true;
218 }
219 
221 {
222  wxASSERT(nTypes == WXSIZEOF(kTypeStrings));
223 
224  S.StartMultiColumn(2, wxCENTER);
225  {
226  auto typeChoices = LocalizedStrings(kTypeStrings, nTypes);
227  S.AddChoice(_("Noise type:"), wxT(""), &typeChoices)->SetValidator(wxGenericValidator(&mType));
228 
229  FloatingPointValidator<double> vldAmp(6, &mAmp, NumValidatorStyle::NO_TRAILING_ZEROES);
230  vldAmp.SetRange(MIN_Amp, MAX_Amp);
231  S.AddTextBox(_("Amplitude (0-1):"), wxT(""), 12)->SetValidator(vldAmp);
232 
233  S.AddPrompt(_("Duration:"));
235  NumericTextCtrl(S.GetParent(), wxID_ANY,
238  GetDuration(),
239  mProjectRate,
241  .AutoPos(true));
242  mNoiseDurationT->SetName(_("Duration"));
243  S.AddWindow(mNoiseDurationT, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL | wxALL);
244  }
245  S.EndMultiColumn();
246 }
247 
249 {
250  if (!mUIParent->TransferDataToWindow())
251  {
252  return false;
253  }
254 
256 
257  return true;
258 }
259 
261 {
262  if (!mUIParent->Validate() || !mUIParent->TransferDataFromWindow())
263  {
264  return false;
265  }
266 
268 
269  return true;
270 }
AudacityPrefs * gPrefs
Definition: Prefs.cpp:73
NumericTextCtrl * mNoiseDurationT
Definition: Noise.h:66
bool Startup() override
Definition: Noise.cpp:192
static const IdentInterfaceSymbol kTypeStrings[nTypes]
Definition: Noise.cpp:38
bool SaveUserPreset(const wxString &name) override
Definition: Effect.cpp:600
float buf1
Definition: Noise.h:64
Derived from ShuttleGuiBase, an Audacity specific class for shuttling data to and from GUI...
Definition: ShuttleGui.h:409
double GetDuration() override
Definition: Effect.cpp:722
Definition: Noise.cpp:32
Definition: Noise.cpp:35
wxString GetCurrentSettingsGroup() override
Definition: Effect.cpp:801
bool DefineParams(ShuttleParams &S) override
Definition: Noise.cpp:165
wxWindow * AddWindow(wxWindow *pWindow, int Flags=wxALIGN_CENTRE|wxALL)
Definition: ShuttleGui.cpp:288
void SetLinearEffectFlag(bool linearEffectFlag)
Definition: Effect.cpp:1961
EffectNoise()
Definition: Noise.cpp:56
float y
Definition: Noise.h:64
wxArrayString LocalizedStrings(const IdentInterfaceSymbol strings[], size_t nStrings)
Definition: Internat.cpp:303
#define NOISE_PLUGIN_SYMBOL
Definition: Noise.h:24
bool TransferDataToWindow() override
Definition: Noise.cpp:248
void EndMultiColumn()
#define XO(s)
Definition: Internat.h:33
NumericFormatId GetDurationFormat() override
Definition: Effect.cpp:732
float buf3
Definition: Noise.h:64
Shuttle that deals with parameters. This is a base class with lots of virtual functions that do nothi...
Definition: Shuttle.h:60
kTypes
Definition: Noise.cpp:30
int mType
Definition: Noise.h:61
double mSampleRate
Definition: Effect.h:455
#define safenew
Definition: Audacity.h:230
bool GetAutomationParameters(CommandParameters &parms) override
Definition: Noise.cpp:171
float buf2
Definition: Noise.h:64
void AddPrompt(const wxString &Prompt)
Right aligned text string.
Definition: ShuttleGui.cpp:239
wxTextCtrl * AddTextBox(const wxString &Caption, const wxString &Value, const int nChars)
Definition: ShuttleGui.cpp:540
#define ReadAndVerifyEnum(name, list, listSize)
Definition: Effect.h:786
double mAmp
Definition: Noise.h:62
size_t ProcessBlock(float **inBlock, float **outBlock, size_t blockLen) override
Definition: Noise.cpp:101
EffectType GetType() override
Definition: Noise.cpp:89
wxWindow * GetParent()
Definition: ShuttleGui.h:294
void StartMultiColumn(int nCols, int PositionFlags=wxALIGN_LEFT)
IdentInterfaceSymbol GetSymbol() override
Definition: Noise.cpp:72
wxChoice * AddChoice(const wxString &Prompt, const wxString &Selected, const wxArrayString *pChoices)
Definition: ShuttleGui.cpp:371
Definition: Noise.cpp:33
CommandParameters, derived from wxFileConfig, is essentially doing the same things as the Shuttle cla...
#define ReadAndVerifyDouble(name)
Definition: Effect.h:798
unsigned GetAudioOutCount() override
Definition: Noise.cpp:96
wxString ManualPage() override
Definition: Noise.cpp:82
void SetDuration(double duration) override
Definition: Effect.cpp:742
float buf5
Definition: Noise.h:64
IdentInterfaceSymbol pairs a persistent string identifier used internally with an optional...
void SetValue(double newValue)
wxWindow * mUIParent
Definition: Effect.h:472
double mProjectRate
Definition: Effect.h:453
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom")).Raw()), OnMoveTrack)#define SET_TRACK_NAME_PLUGIN_SYMBOLclass SetTrackNameCommand:public AudacityCommand
Param(Type, int, wxT("Type"), kWhite, 0, nTypes-1, 1)
void PopulateOrExchange(ShuttleGui &S) override
Definition: Noise.cpp:220
bool TransferDataFromWindow() override
Definition: Noise.cpp:260
EffectType
float z
Definition: Noise.h:64
float buf4
Definition: Noise.h:64
virtual ~EffectNoise()
Definition: Noise.cpp:66
wxString GetDescription() override
Definition: Noise.cpp:77
Options & AutoPos(bool value)
float buf6
Definition: Noise.h:64
float buf0
Definition: Noise.h:64
bool SetAutomationParameters(CommandParameters &parms) override
Definition: Noise.cpp:179