CSL  6.0
Noise.h
Go to the documentation of this file.
1 ///
2 /// Noise.h -- Noise Unit Generators
3 /// Comprising Noise superclass, and WhiteNoise, PinkNoise subclasses
4 ///
5 
6 #ifndef INCLUDE_Noise_H
7 #define INCLUDE_Noise_H
8 
9 #include "CSL_Core.h" // include the main CSL header; this includes CSL_Types.h and CGestalt.h
10 
11 #define PINK_MAX_RANDOM_ROWS (30)
12 #define PINK_RANDOM_BITS (24)
13 #define PINK_RANDOM_SHIFT ((sizeof(long)*8)-PINK_RANDOM_BITS)
14 
15 namespace csl {
16 
17 ///
18 /// Abstract Noise class - inherits from UnitGenerator & Scalable, and provides constructors and basic pseudo-raondom methods
19 ///
20 
21 class Noise : public UnitGenerator, public Scalable {
22 
23 public:
24  inline int generateRandomNumber(); ///< returns the next pseudo-random number
25  inline float generateNormalizedRandomNumber(); ///< returns next pseudo-random normalised to +/- 1.0
26 
27  void setSeed(int seed) { mSeed = seed; } ///< set the seed integer for the pseudo-random number generators
28 
29  void dump(); ///< Tell me more about what is happening
30 
31  Noise(); ///< Constructors
32  Noise(double ampl, double offset = 0.0);
33  Noise(int seed, double ampl = 1.0, double offset = 0.0);
34  ~Noise() { }; ///< Destructor
35 
36 protected:
37  int mSeed; ///< seed integer for the pseudo-random number generators
38  float mDivisor; ///< factor to scale ints to +/- 1.0
39 };
40 
41 ///
42 /// White noise -- equal power per frequency
43 ///
44 
45 class WhiteNoise : public Noise {
46 
47 public: /// Constructors
48  WhiteNoise() : Noise() { };
49  WhiteNoise(double ampl, double offset = 0.0) : Noise(ampl, offset) { };
50  WhiteNoise(int seed, double ampl = 1.0, double offset = 0.0) : Noise(seed, ampl, offset) { };
51  ~WhiteNoise() { }; ///< Destructor
52 
53 /********* THIS FUNCTION WAS PROTECTED, BUT IT'S NEEDED TO BE PUBLIC BECAUSE
54  OTHER UGENS MIGHT CALL IT DURING A NEXT BUFFER CALL . . . *********/
55  /// the noise generator DSP function
56  void nextBuffer(Buffer& outputBuffer, unsigned outBufNum) throw (CException);
57 
58 protected:
59 
60 };
61 
62 ///
63 /// Pink noise -- equal power per octave
64 ///
65 
66 class PinkNoise : public Noise {
67 
68 public:
69  PinkNoise(); ///< Constructors
70  PinkNoise(double ampl, double offset = 0.f);
71  PinkNoise(int seed, double ampl = 1.f, double offset = 0.f);
72  ~PinkNoise() { }; ///< Destructor
73 
74  /// the monoNextBuffer method is where the DSP takes place
75  void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
76 
77  sample nextPink(); ///< returns the next pink noise sample
78 
79 protected:
80  int mPinkRows[PINK_MAX_RANDOM_ROWS];///< Pink noise generator rows
81  int mPinkRunningSum; ///< Used to optimize summing of generators.
82  int mPinkIndex; ///< Incremented each sample.
83  int mPinkIndexMask; ///< Index wrapped by ANDing with this mask.
84  float mPinkScalar; ///< Used to scale within range of -1.0 to +1.0
85 
86  void initialize(int numRows); ///< set up PinkNoise for N rows of generators
87 
88 };
89 
90 // inline functions have to be declared in the header file to avoid linker problems
91 
93  // Calculate pseudo-random 32 bit number based on linear congruential method
94  mSeed = (mSeed * 196314165) + 907633515;
95  return mSeed;
96 
97 }
98 
100  // Calculate pseudo-random 32 bit number based on linear congruential method
101  mSeed = (mSeed * 196314165) + 907633515;
102  return (float) mSeed / (float) 0x7fffffff; // dividing by INT_MAX
103 }
104 
105 
106 } // namespace csl
107 
108 #endif
109 
sample nextPink()
returns the next pink noise sample
Definition: Noise.cpp:108
float generateNormalizedRandomNumber()
returns next pseudo-random normalised to +/- 1.0
Definition: Noise.h:99
int mPinkIndex
Incremented each sample.
Definition: Noise.h:82
AdditiveInstrument.h – Sum-of-sines synthesis instrument class.
Definition: Accessor.h:17
int generateRandomNumber()
returns the next pseudo-random number
Definition: Noise.h:92
void initialize(int numRows)
set up PinkNoise for N rows of generators
Definition: Noise.cpp:94
Abstract Noise class - inherits from UnitGenerator & Scalable, and provides constructors and basic ps...
Definition: Noise.h:21
PinkNoise()
Constructors.
Definition: Noise.cpp:78
WhiteNoise(double ampl, double offset=0.0)
Definition: Noise.h:49
float mDivisor
factor to scale ints to +/- 1.0
Definition: Noise.h:38
#define PINK_MAX_RANDOM_ROWS
Noise.h – Noise Unit Generators Comprising Noise superclass, and WhiteNoise, PinkNoise subclasses...
Definition: Noise.h:11
~PinkNoise()
Definition: Noise.h:72
White noise – equal power per frequency.
Definition: Noise.h:45
WhiteNoise()
Constructors.
Definition: Noise.h:48
int mPinkRunningSum
Used to optimize summing of generators.
Definition: Noise.h:81
Noise()
Constructors.
Definition: Noise.cpp:19
Scalable – mix-in class with scale and offset control inputs (may be constants or generators)...
Definition: CSL_Core.h:403
int mPinkIndexMask
Index wrapped by ANDing with this mask.
Definition: Noise.h:83
~Noise()
Definition: Noise.h:34
int mPinkRows[PINK_MAX_RANDOM_ROWS]
Pink noise generator rows.
Definition: Noise.h:80
float mPinkScalar
Used to scale within range of -1.0 to +1.0.
Definition: Noise.h:84
float sample
(could be changed to int, or double)
Definition: CSL_Types.h:191
WhiteNoise(int seed, double ampl=1.0, double offset=0.0)
Definition: Noise.h:50
Buffer – the multi-channel sample buffer class (passed around between generators and IO guys)...
Definition: CSL_Core.h:106
void setSeed(int seed)
set the seed integer for the pseudo-random number generators
Definition: Noise.h:27
forward declaration
Definition: CSL_Core.h:241
void dump()
Tell me more about what is happening.
Definition: Noise.cpp:46
void nextBuffer(Buffer &outputBuffer, unsigned outBufNum)
Destructor.
Definition: Noise.cpp:56
void nextBuffer(Buffer &outputBuffer, unsigned outBufNum)
Destructor.
Definition: Noise.cpp:147
int mSeed
Destructor.
Definition: Noise.h:34
Pink noise – equal power per octave.
Definition: Noise.h:66
Base class of CSL exceptions (written upper-case). Has a string message.