4 generate Pink Noise using Gardner method.
5 Optimization suggested by James McCartney uses a tree
6 to select which random value to replace.
8 x x x x x x x x x x x x x x x x
14 Tree is generated by counting trailing zeros in an increasing index.
15 When the index is zero, no random number is selected.
17 This program uses the Portable Audio library which is under development.
18 For more information see: http://www.audiomulch.com/portaudio/
20 Author: Phil Burk, http://www.softsynth.com
24 Copyleft 1999 Phil Burk - No rights reserved.
31 /************************************************************/
32 /* Calculate pseudo-random 32 bit number based on linear congruential method. */
33 static unsigned long generate_random_number( void )
35 static unsigned long rand_seed = 22222; /* Change this for different random sequences. */
36 rand_seed = (rand_seed * 196314165) + 907633515;
40 /* Setup PinkNoise structure for N rows of generators. */
41 void initialize_pink_noise( pink_noise_t *pink, int num_rows )
46 pink->pink_index_mask = (1<<num_rows) - 1;
47 /* Calculate maximum possible signed random value. Extra 1 for white noise always added. */
48 pmax = (num_rows + 1) * (1<<(PINK_RANDOM_BITS-1));
49 pink->pink_scalar = 1.0f / pmax;
50 /* Initialize rows. */
51 for( i=0; i<num_rows; i++ ) pink->pink_rows[i] = 0;
52 pink->pink_running_sum = 0;
55 /* generate Pink noise values between -1.0 and +1.0 */
56 float generate_pink_noise_sample( pink_noise_t *pink )
62 /* Increment and mask index. */
63 pink->pink_index = (pink->pink_index + 1) & pink->pink_index_mask;
65 /* If index is zero, don't update any random values. */
66 if( pink->pink_index != 0 )
68 /* Determine how many trailing zeros in PinkIndex. */
69 /* This algorithm will hang if n==0 so test first. */
71 int n = pink->pink_index;
78 /* Replace the indexed ROWS random value.
79 * Subtract and add back to Running_sum instead of adding all the random
80 * values together. Only one changes each time.
82 pink->pink_running_sum -= pink->pink_rows[num_zeros];
83 new_random = ((long)generate_random_number()) >> PINK_RANDOM_SHIFT;
84 pink->pink_running_sum += new_random;
85 pink->pink_rows[num_zeros] = new_random;
88 /* Add extra white noise value. */
89 new_random = ((long)generate_random_number()) >> PINK_RANDOM_SHIFT;
90 sum = pink->pink_running_sum + new_random;
92 /* Scale to range of -1.0 to 0.9999. */
93 output = pink->pink_scalar * sum;