Mercurial > hg > beaglert
annotate examples/10-Instruments/d-box/PinkNoise.h @ 541:c301cc07ae11 prerelease
updated heavy to new API
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Fri, 24 Jun 2016 02:29:05 +0100 |
parents | 8fcfbfb32aa0 |
children |
rev | line source |
---|---|
robert@464 | 1 #ifndef _PinkNoise_H |
robert@464 | 2 #define _PinkNoise_H |
robert@464 | 3 |
robert@464 | 4 // Technique by Larry "RidgeRat" Trammell 3/2006 |
robert@464 | 5 // http://home.earthlink.net/~ltrammell/tech/pinkalg.htm |
robert@464 | 6 // implementation and optimization by David Lowenfels |
robert@464 | 7 |
robert@464 | 8 #include <cstdlib> |
robert@464 | 9 #include <ctime> |
robert@464 | 10 #include <stdlib.h> |
robert@464 | 11 |
robert@464 | 12 #define PINK_NOISE_NUM_STAGES 3 |
robert@464 | 13 |
robert@464 | 14 class PinkNoise { |
robert@464 | 15 public: |
robert@464 | 16 PinkNoise() { |
robert@464 | 17 srand ( time(NULL) ); // initialize random generator |
robert@464 | 18 clear(); |
robert@464 | 19 } |
robert@464 | 20 |
robert@464 | 21 void clear() { |
robert@464 | 22 for( size_t i=0; i< PINK_NOISE_NUM_STAGES; i++ ) |
robert@464 | 23 state[ i ] = 0.0; |
robert@464 | 24 } |
robert@464 | 25 |
robert@464 | 26 float tick() { |
robert@464 | 27 static const float RMI2 = 2.0 / float(RAND_MAX); // + 1.0; // change for range [0,1) |
robert@464 | 28 static const float offset = A[0] + A[1] + A[2]; |
robert@464 | 29 |
robert@464 | 30 // unrolled loop |
robert@464 | 31 float temp = float( rand() ); |
robert@464 | 32 state[0] = P[0] * (state[0] - temp) + temp; |
robert@464 | 33 temp = float( rand() ); |
robert@464 | 34 state[1] = P[1] * (state[1] - temp) + temp; |
robert@464 | 35 temp = float( rand() ); |
robert@464 | 36 state[2] = P[2] * (state[2] - temp) + temp; |
robert@464 | 37 return ( A[0]*state[0] + A[1]*state[1] + A[2]*state[2] )*RMI2 - offset; |
robert@464 | 38 } |
robert@464 | 39 |
robert@464 | 40 protected: |
robert@464 | 41 float state[ PINK_NOISE_NUM_STAGES ]; |
robert@464 | 42 static const float A[ PINK_NOISE_NUM_STAGES ]; |
robert@464 | 43 static const float P[ PINK_NOISE_NUM_STAGES ]; |
robert@464 | 44 }; |
robert@464 | 45 |
robert@464 | 46 //const float PinkNoise::A[] = { 0.02109238, 0.07113478, 0.68873558 }; // rescaled by (1+P)/(1-P) |
robert@464 | 47 //const float PinkNoise::P[] = { 0.3190, 0.7756, 0.9613 }; |
robert@464 | 48 |
robert@464 | 49 #endif |