CSL  5.2
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