robert@464: #ifndef _PinkNoise_H robert@464: #define _PinkNoise_H robert@464: robert@464: // Technique by Larry "RidgeRat" Trammell 3/2006 robert@464: // http://home.earthlink.net/~ltrammell/tech/pinkalg.htm robert@464: // implementation and optimization by David Lowenfels robert@464: robert@464: #include robert@464: #include robert@464: #include robert@464: robert@464: #define PINK_NOISE_NUM_STAGES 3 robert@464: robert@464: class PinkNoise { robert@464: public: robert@464: PinkNoise() { robert@464: srand ( time(NULL) ); // initialize random generator robert@464: clear(); robert@464: } robert@464: robert@464: void clear() { robert@464: for( size_t i=0; i< PINK_NOISE_NUM_STAGES; i++ ) robert@464: state[ i ] = 0.0; robert@464: } robert@464: robert@464: float tick() { robert@464: static const float RMI2 = 2.0 / float(RAND_MAX); // + 1.0; // change for range [0,1) robert@464: static const float offset = A[0] + A[1] + A[2]; robert@464: robert@464: // unrolled loop robert@464: float temp = float( rand() ); robert@464: state[0] = P[0] * (state[0] - temp) + temp; robert@464: temp = float( rand() ); robert@464: state[1] = P[1] * (state[1] - temp) + temp; robert@464: temp = float( rand() ); robert@464: state[2] = P[2] * (state[2] - temp) + temp; robert@464: return ( A[0]*state[0] + A[1]*state[1] + A[2]*state[2] )*RMI2 - offset; robert@464: } robert@464: robert@464: protected: robert@464: float state[ PINK_NOISE_NUM_STAGES ]; robert@464: static const float A[ PINK_NOISE_NUM_STAGES ]; robert@464: static const float P[ PINK_NOISE_NUM_STAGES ]; robert@464: }; robert@464: robert@464: //const float PinkNoise::A[] = { 0.02109238, 0.07113478, 0.68873558 }; // rescaled by (1+P)/(1-P) robert@464: //const float PinkNoise::P[] = { 0.3190, 0.7756, 0.9613 }; robert@464: robert@464: #endif