00001 #ifndef GAMMA_NOISE_H_INC
00002 #define GAMMA_NOISE_H_INC
00003
00004
00005
00006
00007 #include "Gamma/rnd.h"
00008 #include "Gamma/scl.h"
00009
00010 namespace gam{
00011
00012
00014
00018 template <class RNG = RNGLinCon>
00019 class NoiseBrown{
00020 public:
00021
00027 NoiseBrown(float val=0, float step=0.04, float min=-1, float max=1, uint32_t seed=0)
00028 : val(val), step(step), min(min), max(max)
00029 { if(seed) rng = seed; }
00030
00032 float operator()(){
00033 val = scl::clip(val + rnd::uniS_float(rng) * step, max, min);
00034 return val;
00035 }
00036
00038 void seed(uint32_t v){ rng = v; }
00039
00040 RNG rng;
00041 float val, step, min, max;
00042 };
00043
00044
00046
00049 template <class RNG = RNGLinCon>
00050 class NoisePink{
00051 public:
00052 NoisePink();
00053
00055 NoisePink(uint32_t seed);
00056
00058 float operator()();
00059
00061 void seed(uint32_t v){ rng = v; }
00062
00063 RNG rng;
00064
00065 private:
00066 float mOctave[11];
00067 uint32_t mPhase;
00068 float mRunningSum;
00069 void init();
00070 };
00071
00072
00074
00077 template <class RNG = RNGLinCon>
00078 class NoiseWhite{
00079 public:
00080 NoiseWhite(): rng(){}
00081
00083 NoiseWhite(uint32_t seed) : rng(seed){}
00084
00086 float operator()() const { return rnd::uniS_float(rng); }
00087 float operator[](uint32_t i) const { return (*this)(); }
00088
00090 void seed(uint32_t v){ rng = v; }
00091
00092 mutable RNG rng;
00093 };
00094
00095
00096
00097
00098
00099
00100 #define TEM template<class T>
00101
00102 TEM NoisePink<T>::NoisePink(): rng(){ init(); }
00103 TEM NoisePink<T>::NoisePink(uint32_t seed): rng(seed){ init(); }
00104
00105 TEM void NoisePink<T>::init(){
00106 mRunningSum = 0.f;
00107 for(uint32_t i=0; i<11; ++i){
00108 float r = rnd::uniS_float(rng);
00109 mOctave[i] = r;
00110 mRunningSum += r;
00111 }
00112 mPhase = 0;
00113 }
00114
00115 TEM inline float NoisePink<T>::operator()(){
00116
00117
00118 ++mPhase;
00119 if(mPhase != 2048){
00120 uint32_t i = scl::trailingZeroes(mPhase);
00121 float r = rnd::uniS_float(rng);
00122 mRunningSum += r - mOctave[i];
00123 mOctave[i] = r;
00124 }
00125 else{
00126 mPhase = 0;
00127 }
00128
00129
00130 return (mRunningSum + rnd::uniS_float(rng)) * 0.083333333f;
00131 }
00132
00133
00134
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185 }
00186
00187 #undef TEM
00188
00189 #endif