Jamoma API  0.6.0.a19
TTNoise.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup dspGeneratorLib
4  *
5  * @brief GeneratorLib: Noise Generator
6  *
7  * @details
8  *
9  * @authors Tim Place, Nils Peters, Trond Lossius
10  *
11  * @copyright Copyright © 2003 by Timothy Place @n
12  * This code is licensed under the terms of the "New BSD License" @n
13  * http://creativecommons.org/licenses/BSD/
14  */
15 
16 
17 #include "TTNoise.h"
18 
19 #define thisTTClass TTNoise
20 #define thisTTClassName "noise"
21 #define thisTTClassTags "dspGeneratorLib, audio, generator, noise"
22 
23 
24 TT_AUDIO_CONSTRUCTOR,
25 #ifndef USE_MERSENNE_TWISTER_ALGORITHM
26 accum(0),
27 #endif
28 mNumChannels(0)
29 {
32  setAttributeValue(TT("mode"), TT("white"));
33  setAttributeValue(TT("gain"), 0.0); // 0 dB
34 #ifdef USE_MERSENNE_TWISTER_ALGORITHM
35  addAttribute(Mean, kTypeFloat64);
37  addAttributeProperty(Std, range, TTValue(0.0, 1.0));
38  addAttributeProperty(Std, rangeChecking, TT("low"));
39  setAttributeValue(TT("mean"), 0.0);
40  setAttributeValue(TT("std"), 1.0);
41 #endif
42 }
43 
44 
45 TTNoise::~TTNoise()
46 {
47  ;
48 }
49 
50 
52 {
53  mNumChannels = newNumChannels;
54  mb0.resize(mNumChannels);
55  mb1.resize(mNumChannels);
56  mb2.resize(mNumChannels);
57  mb3.resize(mNumChannels);
58  mb4.resize(mNumChannels);
59  mb5.resize(mNumChannels);
60  mb6.resize(mNumChannels);
61  Clear();
62  return kTTErrNone;
63 }
64 
65 
67 {
68  mb0.assign(mNumChannels, 0.0);
69  mb1.assign(mNumChannels, 0.0);
70  mb2.assign(mNumChannels, 0.0);
71  mb3.assign(mNumChannels, 0.0);
72  mb4.assign(mNumChannels, 0.0);
73  mb5.assign(mNumChannels, 0.0);
74  mb6.assign(mNumChannels, 0.0);
75  return kTTErrNone;
76 }
77 
78 
80 {
81  mMode = newMode;
82  if (mMode == TT("pink"))
84  if (mMode == TT("brown"))
86  if (mMode == TT("blue"))
88  if (mMode == TT("gauss"))
90  else
92 }
93 
94 
95 TTErr TTNoise::setGain(const TTValue& newValue)
96 {
97  mGain = TTDecibelsToLinearGain(newValue);
98  return kTTErrNone;
99 }
100 
101 
103 {
104  returnedValue = TTLinearGainToDecibels(mGain);
105  return kTTErrNone;
106 }
107 
108 
110 {
111  TTAudioSignal& out = outputs->getSignal(0);
112  TTSampleValue tempSample;
113  TTSampleValuePtr outSample;
114  TTChannelCount numChannels = out.getNumChannelsAsInt();
115  TTChannelCount channel;
116  TTUInt16 vs = out.getVectorSizeAsInt();
117 
118  if (numChannels != mNumChannels)
119  setNumChannels(numChannels);
120 
121  for (channel=0; channel<numChannels; channel++) {
122  outSample = out.mSampleVectors[channel];
123  for (TTUInt16 n=0; n<vs; n++) {
124 #ifdef USE_MERSENNE_TWISTER_ALGORITHM
125  tempSample = mTwister.rand(2.0);
126  tempSample -= 1.0;
127 #else
128  accum = (accum * 3877 + 29573) % 139968; // Random number generator
129  tempSample = (1.0 - (2.0 * float(accum) / 139968)); // Scale to audio range
130 #endif
131  *outSample++ = tempSample * mGain;
132  }
133  }
134  return kTTErrNone;
135 }
136 
137 
139 {
140  TTAudioSignal& out = outputs->getSignal(0);
141  TTSampleValue tempSample;
142  TTSampleValuePtr outSample;
143  TTChannelCount numChannels = out.getNumChannelsAsInt();
144  TTChannelCount channel;
145  TTUInt16 vs = out.getVectorSizeAsInt();
146 
147  if (numChannels != mNumChannels)
148  setNumChannels(numChannels);
149 
150  for (channel=0; channel<numChannels; channel++) {
151  outSample = out.mSampleVectors[channel];
152  for (TTUInt16 n=0; n<vs; n++) {
153  // Generate White Noise
154 #ifdef USE_MERSENNE_TWISTER_ALGORITHM
155  tempSample = mTwister.rand(2.0);
156  tempSample -= 1.0;
157 #else
158  accum = (accum * 3877 + 29573) % 139968; // Random number generator
159  tempSample = 1.0 - (2.0 * float(accum) / 139968); // Scale to audio range
160 #endif
161  // Apply a Pinking Filter
162  mb0[channel] = 0.99886 * mb0[channel] + tempSample * 0.0555179;
163  mb1[channel] = 0.99332 * mb1[channel] + tempSample * 0.0750759;
164  mb2[channel] = 0.96900 * mb2[channel] + tempSample * 0.1538520;
165  mb3[channel] = 0.86650 * mb3[channel] + tempSample * 0.3104856;
166  mb4[channel] = 0.55000 * mb4[channel] + tempSample * 0.5329522;
167  mb5[channel] = -0.7616 * mb5[channel] - tempSample * 0.0168980;
168  mb6[channel] = tempSample * 0.115926;
169  tempSample = (mb0[channel] + mb1[channel] + mb2[channel] + mb3[channel] + mb4[channel] + mb5[channel] + mb6[channel] + tempSample * 0.5362) * mGain;
170 
171  // Copy the Output to All Channels
172  *outSample++ = tempSample;
173  }
174  }
175  return kTTErrNone;
176 }
177 
178 
180 {
181  TTAudioSignal& out = outputs->getSignal(0);
182  TTSampleValue tempSample;
183  TTSampleValuePtr outSample;
184  TTChannelCount numChannels = out.getNumChannelsAsInt();
185  TTChannelCount channel;
186  TTUInt16 vs = out.getVectorSizeAsInt();
187 
188  if (numChannels != mNumChannels)
189  setNumChannels(numChannels);
190 
191  for (channel=0; channel<numChannels; channel++) {
192  outSample = out.mSampleVectors[channel];
193  for (TTUInt16 n=0; n<vs; n++) {
194  // Generate White Noise
195 #ifdef USE_MERSENNE_TWISTER_ALGORITHM
196  tempSample = mTwister.rand(2.0);
197  tempSample -= 1.0;
198 #else
199  accum = (accum * 3877 + 29573) % 139968; // Random number generator
200  tempSample = 1.0 - (2.0 * float(accum) / 139968); // Scale to audio range
201 #endif
202  // Apply a "Browning" Filter
203  tempSample *= 0.1; // scale the white noise
204  tempSample = mb0[channel] + tempSample; // 6dB per octave lowpass
205  TTLimit(tempSample, -1.0, 1.0);
206  mb0[channel] = tempSample; // store Feedback Sample
207  tempSample *= 0.25 * mGain; // output
208 
209  // Copy the Output to All Channels
210  *outSample++ = tempSample;
211  }
212  }
213  return kTTErrNone;
214 }
215 
216 
218 {
219  TTAudioSignal& out = outputs->getSignal(0);
220  TTSampleValue tempSample;
221  TTSampleValuePtr outSample;
222  TTChannelCount numChannels = out.getNumChannelsAsInt();
223  TTChannelCount channel;
224  TTUInt16 vs = out.getVectorSizeAsInt();
225 
226  if (numChannels != mNumChannels)
227  setNumChannels(numChannels);
228 
229  for (channel=0; channel<numChannels; channel++) {
230  outSample = out.mSampleVectors[channel];
231  for (TTUInt16 n=0; n<vs; n++) {
232  // Generate White Noise
233 #ifdef USE_MERSENNE_TWISTER_ALGORITHM
234  tempSample = mTwister.rand(2.0);
235  tempSample -= 1.0;
236 #else
237  accum = (accum * 3877 + 29573) % 139968; // Random number generator
238  tempSample = 1.0 - (2.0 * float(accum) / 139968); // Scale to audio range
239 #endif
240  // Apply a "Blue-ing" Filter
241  tempSample -= mb0[channel]; // 6dB per octave highpass (real blue noise = 3dB/oct)
242  TTLimit(tempSample, -1.0, 1.0); // clip
243  mb0[channel] = tempSample; // store feedback sample
244  tempSample *= mGain;
245 
246  // Copy the Output to All Channels
247  *outSample++ = tempSample;
248  }
249  }
250  return kTTErrNone;
251 }
252 
253 #ifdef USE_MERSENNE_TWISTER_ALGORITHM
255 {
256  TTAudioSignal& out = outputs->getSignal(0);
257  TTSampleValuePtr outSample;
258  TTChannelCount numChannels = out.getNumChannelsAsInt();
259  TTChannelCount channel;
260  TTUInt16 vs = out.getVectorSizeAsInt();
261 
262  if (numChannels != mNumChannels)
263  setNumChannels(numChannels);
264 
265  for (channel=0; channel<numChannels; channel++) {
266  outSample = out.mSampleVectors[channel];
267  for (TTUInt16 n=0; n<vs; n++) {
268  *outSample++ = mTwister.randNorm(mMean,mStd);
269  }
270  }
271  return kTTErrNone;
272 }
273 #endif
274 
TTFloat64 TTDecibelsToLinearGain(TTFloat64 value)
Convert decibels into linear ampliude.
TTErr setGain(const TTValue &newValue)
Set the overall gain of the generated noise signal.
Definition: TTNoise.cpp:95
std::uint16_t TTUInt16
16 bit unsigned integer
Definition: TTBase.h:176
TTSymbol mMode
Attribute: what color is the noise?
Definition: TTNoise.h:43
TTSampleVector mb0
Internal coefficient used to keep track of past values. Used for some noise algorithms.
Definition: TTNoise.h:47
TTFloat64 TTLinearGainToDecibels(const TTFloat64 value)
Convert linear amplitude into deciBels.
TTSampleVector mb5
Internal coefficient used to keep track of past values. Used for some noise algorithms.
Definition: TTNoise.h:52
#define addAttribute(name, type)
A convenience macro to be used by subclasses for registering attributes with a custom getter...
Definition: TTAttribute.h:29
TTErr processPinkNoise(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
Audio processing method generating pink noise.
Definition: TTNoise.cpp:138
TTFloat64 mStd
Standard deviation value for gauss distribution process method.
Definition: TTNoise.h:40
TTErr processGauss(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
Audio processing method using the mersenne twister algorithm for gaussian distribution noise...
TTErr setMode(const TTValue &newMode)
Set the mode, that is: Set what kind of noise to generate.
Definition: TTNoise.cpp:79
#define setProcessMethod(methodName)
A convenience macro to be used by subclasses for setting the process method.
TTSampleVector mb6
Internal coefficient used to keep track of past values. Used for some noise algorithms.
Definition: TTNoise.h:53
TTChannelCount mNumChannels
Sets the number of noise channels.
Definition: TTNoise.h:45
Symbol type.
Definition: TTBase.h:282
MTRand mTwister
Class implementing Mersenne Twister algorithm.
Definition: TTNoise.h:38
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
TTSampleVector mb4
Internal coefficient used to keep track of past values. Used for some noise algorithms.
Definition: TTNoise.h:51
TTErr processWhiteNoise(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
Audio processing method generating white noise.
Definition: TTNoise.cpp:109
TTErr Clear()
Clear past values used internally by various noise generator algorithms.
Definition: TTNoise.cpp:66
64-bit floating point
Definition: TTBase.h:272
#define addAttributeProperty(attributeName, propertyName, initialValue)
A convenience macro to be used for registering properties of attributes.
Definition: TTAttribute.h:68
TTFloat64 mMean
Mean value for gauss distribution process method.
Definition: TTNoise.h:39
TTFloat64 mGain
Gain stage.
Definition: TTNoise.h:44
The TTAudioSignal class represents N vectors of audio samples for M channels.
Definition: TTAudioSignal.h:57
TTSampleValue ** mSampleVectors
An array of pointers to the first sample in each vector. Declared Public for fast access...
Definition: TTAudioSignal.h:74
TTUInt16 TTChannelCount
Data type used when counting the number of channels in multi-channel audio signals and processes...
Definition: TTAudioSignal.h:31
TTErr processBlueNoise(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
Audio processing method generating blue noise.
Definition: TTNoise.cpp:217
A simple container for an array of TTAudioSignal pointers.
TTErr getGain(TTValue &returnedValue)
Get the current value used for scaling overall gain level of the generated noise signal.
Definition: TTNoise.cpp:102
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
TTErr setNumChannels(const TTChannelCount newNumChannels)
Set the number of channels to process.
Definition: TTNoise.cpp:51
TTSampleVector mb3
Internal coefficient used to keep track of past values. Used for some noise algorithms.
Definition: TTNoise.h:50
TTSampleVector mb2
Internal coefficient used to keep track of past values. Used for some noise algorithms.
Definition: TTNoise.h:49
#define addAttributeWithSetter(name, type)
A convenience macro to be used by subclasses for registering attributes with a custom setter...
Definition: TTAttribute.h:47
GeneratorLib: Noise Generator.
TTSampleVector mb1
Internal coefficient used to keep track of past values. Used for some noise algorithms.
Definition: TTNoise.h:48
No Error.
Definition: TTBase.h:343
TTErr processBrownNoise(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
Audio processing method generating brown noise.
Definition: TTNoise.cpp:179
TTFloat64 TTSampleValue
A value representing a single audio sample.
Definition: TTBase.h:230
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34
#define addAttributeWithGetterAndSetter(name, type)
A convenience macro to be used by subclasses for registering attributes with a custom getter and sett...
Definition: TTAttribute.h:57
TTUInt32 accum
Accumulator for the noise generation.
Definition: TTNoise.h:46