Gamma  0.9.5
Generic Synthesis Library
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
/Users/ljp/code/gamma/trunk/Gamma/Noise.h
00001 #ifndef GAMMA_NOISE_H_INC
00002 #define GAMMA_NOISE_H_INC
00003 
00004 /*  Gamma - Generic processing library
00005     See COPYRIGHT file for authors and license information */
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 // Implementation_______________________________________________________________
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){   /* init octaves with uniform randoms */
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     // phasor range:    [1, 2048]
00117     //                  [0, 10]     trailing zeroes
00118     ++mPhase;
00119     if(mPhase != 2048){
00120         uint32_t i = scl::trailingZeroes(mPhase);   // # trailing zeroes is octave
00121         float r = rnd::uniS_float(rng);         // uniform random
00122         mRunningSum += r - mOctave[i];          // add new & subtract stored
00123         mOctave[i] = r;                         // store new
00124     }
00125     else{
00126         mPhase = 0;
00127     }
00128     
00129     // add white noise every sample
00130     return (mRunningSum + rnd::uniS_float(rng)) * 0.083333333f;
00131 }
00132 
00133 
00134 /*
00136 Pink Noise
00137     Description:
00138     Infinite sum of octaves of white noise.  Since we cannot compute an infinite
00139     sum, we'll use 12 octaves. This gives a range of 10.77 - 44,100 Hz.
00140 
00141     12345678901234567890123456789012    octave  length
00142     ********************************    0       0
00143     * * * * * * * * * * * * * * * *     1       2
00144      *   *   *   *   *   *   *   *      2       4
00145        *       *       *       *        3       8
00146            *               *            4       16
00147                    *                    5       32
00148     12131214121312151213121412131210
00149 
00150    0                0       00000   
00151     1       000     1       00001
00152      2      001     2       00010
00153     1       000     3       00011
00154       3     010     4       00100
00155     1       000     5       00101
00156      2      001     6       00110
00157     1       000     7       00111
00158         4   011     8       01000
00159     1       000     9       01001
00160      2      001     10      01010
00161     1       000     11      01011
00162        3    010     12      01100
00163     1       000     13      01101
00164      2      001     14      01110
00165     1       000     15      01111
00166          5  100     16      10000
00167     1       000     17      10001
00168      2      001     18      10010
00169     1       000     19      10011
00170       3     010     20      10100
00171     1       000     21      10101
00172      2      001     22      10110
00173     1       000     23      10111
00174         4   011     24      11000
00175     1       000     25      11001
00176      2      001     26      11010
00177     1       000     27      11011
00178        3    010     28      11100
00179     1       000     29      11101
00180      2      001     30      11110
00181     1       000     31      11111
00182     
00183 */
00184 
00185 } // gam::
00186 
00187 #undef TEM
00188 
00189 #endif