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