Audacity 3.2.0
NoiseBase.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 NoiseBase.cpp
6
7 Dominic Mazzoni
8
9**********************************************************************/
10#include "NoiseBase.h"
11#include <cmath>
12
14 // These are acceptable dual purpose internal/visible names
15 /* i18n-hint: not a color, but "white noise" having a uniform spectrum */
16 { XC("White", "noise") },
17 /* i18n-hint: not a color, but "pink noise" having a spectrum with more power
18 in low frequencies */
19 { XC("Pink", "noise") },
20 /* i18n-hint: a kind of noise spectrum also known as "red" or "brown" */
21 { XC("Brownian", "noise") }
22};
23
25{
27 return parameters;
28}
29
30//
31// NoiseBase
32//
33
35
37{
38 Parameters().Reset(*this);
39
41
42 y = z = buf0 = buf1 = buf2 = buf3 = buf4 = buf5 = buf6 = 0;
43}
44
46{
47}
48
49// ComponentInterface implementation
50
52{
53 return Symbol;
54}
55
57{
58 return XO("Generates one of three different types of noise");
59}
60
62{
63 return L"Noise";
64}
65
66// EffectDefinitionInterface implementation
67
69{
70 return EffectTypeGenerate;
71}
72
74{
75 return 1;
76}
77
80{
82 return true;
83}
84
86 EffectSettings&, const float* const*, float* const* outbuf, size_t size)
87{
88 float* buffer = outbuf[0];
89
90 float white;
91 float amplitude;
92 float div = ((float)RAND_MAX) / 2.0f;
93
94 switch (mType)
95 {
96 default:
97 case kWhite: // white
98 for (decltype(size) i = 0; i < size; i++)
99 {
100 buffer[i] = mAmp * ((rand() / div) - 1.0f);
101 }
102 break;
103
104 case kPink: // pink
105 // based on Paul Kellet's "instrumentation grade" algorithm.
106
107 // 0.129f is an experimental normalization factor.
108 amplitude = mAmp * 0.129f;
109 for (decltype(size) i = 0; i < size; i++)
110 {
111 white = (rand() / div) - 1.0f;
112 buf0 = 0.99886f * buf0 + 0.0555179f * white;
113 buf1 = 0.99332f * buf1 + 0.0750759f * white;
114 buf2 = 0.96900f * buf2 + 0.1538520f * white;
115 buf3 = 0.86650f * buf3 + 0.3104856f * white;
116 buf4 = 0.55000f * buf4 + 0.5329522f * white;
117 buf5 = -0.7616f * buf5 - 0.0168980f * white;
118 buffer[i] = amplitude * (buf0 + buf1 + buf2 + buf3 + buf4 + buf5 +
119 buf6 + white * 0.5362);
120 buf6 = white * 0.115926;
121 }
122 break;
123
124 case kBrownian: // Brownian
125 // float leakage=0.997; // experimental value at 44.1kHz
126 // double scaling = 0.05; // experimental value at 44.1kHz
127 // min and max protect against instability at extreme sample rates.
128 float leakage = ((mSampleRate - 144.0) / mSampleRate < 0.9999) ?
129 (mSampleRate - 144.0) / mSampleRate :
130 0.9999f;
131
132 float scaling =
133 (9.0 / sqrt(mSampleRate) > 0.01) ? 9.0 / sqrt(mSampleRate) : 0.01f;
134
135 for (decltype(size) i = 0; i < size; i++)
136 {
137 white = (rand() / div) - 1.0f;
138 z = leakage * y + white * scaling;
139 y = fabs(z) > 1.0 ? leakage * y - white * scaling : z;
140 buffer[i] = mAmp * y;
141 }
142 break;
143 }
144
145 return size;
146}
147
EffectType
@ EffectTypeGenerate
ChannelName
XO("Cut/Copy/Paste")
@ nTypes
#define XC(s, c)
Definition: Internat.h:37
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:210
Interface for manipulations of an Effect's settings.
virtual void Reset(Effect &effect) const =0
float buf2
Definition: NoiseBase.h:52
const EffectParameterMethods & Parameters() const override
Definition: NoiseBase.cpp:24
double mSampleRate
Definition: NoiseBase.h:48
static const ComponentInterfaceSymbol Symbol
Definition: NoiseBase.h:22
float y
Definition: NoiseBase.h:52
float buf3
Definition: NoiseBase.h:52
virtual ~NoiseBase()
Definition: NoiseBase.cpp:45
float buf4
Definition: NoiseBase.h:52
static const EnumValueSymbol kTypeStrings[nTypes]
Definition: NoiseBase.h:63
bool ProcessInitialize(EffectSettings &settings, double sampleRate, ChannelNames chanMap) override
Definition: NoiseBase.cpp:78
unsigned GetAudioOutCount() const override
How many output buffers to allocate at once.
Definition: NoiseBase.cpp:73
int mType
Definition: NoiseBase.h:49
float buf0
Definition: NoiseBase.h:52
float buf5
Definition: NoiseBase.h:52
float buf6
Definition: NoiseBase.h:52
ComponentInterfaceSymbol GetSymbol() const override
Definition: NoiseBase.cpp:51
TranslatableString GetDescription() const override
Definition: NoiseBase.cpp:56
@ kBrownian
Definition: NoiseBase.h:60
ManualPageID ManualPage() const override
Name of a page in the Audacity alpha manual, default is empty.
Definition: NoiseBase.cpp:61
float buf1
Definition: NoiseBase.h:52
size_t ProcessBlock(EffectSettings &settings, const float *const *inBlock, float *const *outBlock, size_t blockLen) override
Called for destructive effect computation.
Definition: NoiseBase.cpp:85
float z
Definition: NoiseBase.h:52
EffectType GetType() const override
Type determines how it behaves.
Definition: NoiseBase.cpp:68
double mAmp
Definition: NoiseBase.h:50
Holds a msgid for the translation catalog; may also bind format arguments.
__finl float_x4 __vecc sqrt(const float_x4 &a)
Externalized state of a plug-in.