cannam@140: /** @file paex_pink.c cannam@140: @ingroup examples_src cannam@140: @brief Generate Pink Noise using Gardner method. cannam@140: cannam@140: Optimization suggested by James McCartney uses a tree cannam@140: to select which random value to replace. cannam@140:
cannam@140: 	x x x x x x x x x x x x x x x x 
cannam@140: 	x   x   x   x   x   x   x   x   
cannam@140: 	x       x       x       x       
cannam@140: 	 x               x               
cannam@140: 	   x   
cannam@140: 
cannam@140: Tree is generated by counting trailing zeros in an increasing index. cannam@140: When the index is zero, no random number is selected. cannam@140: cannam@140: @author Phil Burk http://www.softsynth.com cannam@140: */ cannam@140: /* cannam@140: * $Id$ cannam@140: * cannam@140: * This program uses the PortAudio Portable Audio Library. cannam@140: * For more information see: http://www.portaudio.com cannam@140: * Copyright (c) 1999-2000 Ross Bencina and Phil Burk cannam@140: * cannam@140: * Permission is hereby granted, free of charge, to any person obtaining cannam@140: * a copy of this software and associated documentation files cannam@140: * (the "Software"), to deal in the Software without restriction, cannam@140: * including without limitation the rights to use, copy, modify, merge, cannam@140: * publish, distribute, sublicense, and/or sell copies of the Software, cannam@140: * and to permit persons to whom the Software is furnished to do so, cannam@140: * subject to the following conditions: cannam@140: * cannam@140: * The above copyright notice and this permission notice shall be cannam@140: * included in all copies or substantial portions of the Software. cannam@140: * cannam@140: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, cannam@140: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF cannam@140: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. cannam@140: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR cannam@140: * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF cannam@140: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION cannam@140: * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. cannam@140: */ cannam@140: cannam@140: /* cannam@140: * The text above constitutes the entire PortAudio license; however, cannam@140: * the PortAudio community also makes the following non-binding requests: cannam@140: * cannam@140: * Any person wishing to distribute modifications to the Software is cannam@140: * requested to send the modifications to the original developer so that cannam@140: * they can be incorporated into the canonical version. It is also cannam@140: * requested that these non-binding requests be included along with the cannam@140: * license above. cannam@140: */ cannam@140: cannam@140: #include cannam@140: #include cannam@140: #include "portaudio.h" cannam@140: cannam@140: #define PINK_MAX_RANDOM_ROWS (30) cannam@140: #define PINK_RANDOM_BITS (24) cannam@140: #define PINK_RANDOM_SHIFT ((sizeof(long)*8)-PINK_RANDOM_BITS) cannam@140: cannam@140: typedef struct cannam@140: { cannam@140: long pink_Rows[PINK_MAX_RANDOM_ROWS]; cannam@140: long pink_RunningSum; /* Used to optimize summing of generators. */ cannam@140: int pink_Index; /* Incremented each sample. */ cannam@140: int pink_IndexMask; /* Index wrapped by ANDing with this mask. */ cannam@140: float pink_Scalar; /* Used to scale within range of -1.0 to +1.0 */ cannam@140: } cannam@140: PinkNoise; cannam@140: cannam@140: /* Prototypes */ cannam@140: static unsigned long GenerateRandomNumber( void ); cannam@140: void InitializePinkNoise( PinkNoise *pink, int numRows ); cannam@140: float GeneratePinkNoise( PinkNoise *pink ); cannam@140: cannam@140: /************************************************************/ cannam@140: /* Calculate pseudo-random 32 bit number based on linear congruential method. */ cannam@140: static unsigned long GenerateRandomNumber( void ) cannam@140: { cannam@140: /* Change this seed for different random sequences. */ cannam@140: static unsigned long randSeed = 22222; cannam@140: randSeed = (randSeed * 196314165) + 907633515; cannam@140: return randSeed; cannam@140: } cannam@140: cannam@140: /************************************************************/ cannam@140: /* Setup PinkNoise structure for N rows of generators. */ cannam@140: void InitializePinkNoise( PinkNoise *pink, int numRows ) cannam@140: { cannam@140: int i; cannam@140: long pmax; cannam@140: pink->pink_Index = 0; cannam@140: pink->pink_IndexMask = (1<pink_Scalar = 1.0f / pmax; cannam@140: /* Initialize rows. */ cannam@140: for( i=0; ipink_Rows[i] = 0; cannam@140: pink->pink_RunningSum = 0; cannam@140: } cannam@140: cannam@140: #define PINK_MEASURE cannam@140: #ifdef PINK_MEASURE cannam@140: float pinkMax = -999.0; cannam@140: float pinkMin = 999.0; cannam@140: #endif cannam@140: cannam@140: /* Generate Pink noise values between -1.0 and +1.0 */ cannam@140: float GeneratePinkNoise( PinkNoise *pink ) cannam@140: { cannam@140: long newRandom; cannam@140: long sum; cannam@140: float output; cannam@140: /* Increment and mask index. */ cannam@140: pink->pink_Index = (pink->pink_Index + 1) & pink->pink_IndexMask; cannam@140: /* If index is zero, don't update any random values. */ cannam@140: if( pink->pink_Index != 0 ) cannam@140: { cannam@140: /* Determine how many trailing zeros in PinkIndex. */ cannam@140: /* This algorithm will hang if n==0 so test first. */ cannam@140: int numZeros = 0; cannam@140: int n = pink->pink_Index; cannam@140: while( (n & 1) == 0 ) cannam@140: { cannam@140: n = n >> 1; cannam@140: numZeros++; cannam@140: } cannam@140: /* Replace the indexed ROWS random value. cannam@140: * Subtract and add back to RunningSum instead of adding all the random cannam@140: * values together. Only one changes each time. cannam@140: */ cannam@140: pink->pink_RunningSum -= pink->pink_Rows[numZeros]; cannam@140: newRandom = ((long)GenerateRandomNumber()) >> PINK_RANDOM_SHIFT; cannam@140: pink->pink_RunningSum += newRandom; cannam@140: pink->pink_Rows[numZeros] = newRandom; cannam@140: } cannam@140: cannam@140: /* Add extra white noise value. */ cannam@140: newRandom = ((long)GenerateRandomNumber()) >> PINK_RANDOM_SHIFT; cannam@140: sum = pink->pink_RunningSum + newRandom; cannam@140: /* Scale to range of -1.0 to 0.9999. */ cannam@140: output = pink->pink_Scalar * sum; cannam@140: #ifdef PINK_MEASURE cannam@140: /* Check Min/Max */ cannam@140: if( output > pinkMax ) pinkMax = output; cannam@140: else if( output < pinkMin ) pinkMin = output; cannam@140: #endif cannam@140: return output; cannam@140: } cannam@140: cannam@140: /*******************************************************************/ cannam@140: #define PINK_TEST cannam@140: #ifdef PINK_TEST cannam@140: cannam@140: /* Context for callback routine. */ cannam@140: typedef struct cannam@140: { cannam@140: PinkNoise leftPink; cannam@140: PinkNoise rightPink; cannam@140: unsigned int sampsToGo; cannam@140: } cannam@140: paTestData; cannam@140: cannam@140: /* This routine will be called by the PortAudio engine when audio is needed. cannam@140: ** It may called at interrupt level on some machines so don't do anything cannam@140: ** that could mess up the system like calling malloc() or free(). cannam@140: */ cannam@140: static int patestCallback(const void* inputBuffer, cannam@140: void* outputBuffer, cannam@140: unsigned long framesPerBuffer, cannam@140: const PaStreamCallbackTimeInfo* timeInfo, cannam@140: PaStreamCallbackFlags statusFlags, cannam@140: void* userData) cannam@140: { cannam@140: int finished; cannam@140: int i; cannam@140: int numFrames; cannam@140: paTestData *data = (paTestData*)userData; cannam@140: float *out = (float*)outputBuffer; cannam@140: (void) inputBuffer; /* Prevent "unused variable" warnings. */ cannam@140: cannam@140: /* Are we almost at end. */ cannam@140: if( data->sampsToGo < framesPerBuffer ) cannam@140: { cannam@140: numFrames = data->sampsToGo; cannam@140: finished = 1; cannam@140: } cannam@140: else cannam@140: { cannam@140: numFrames = framesPerBuffer; cannam@140: finished = 0; cannam@140: } cannam@140: for( i=0; ileftPink ); cannam@140: *out++ = GeneratePinkNoise( &data->rightPink ); cannam@140: } cannam@140: data->sampsToGo -= numFrames; cannam@140: return finished; cannam@140: } cannam@140: cannam@140: /*******************************************************************/ cannam@140: int main(void); cannam@140: int main(void) cannam@140: { cannam@140: PaStream* stream; cannam@140: PaError err; cannam@140: paTestData data; cannam@140: PaStreamParameters outputParameters; cannam@140: int totalSamps; cannam@140: static const double SR = 44100.0; cannam@140: static const int FPB = 2048; /* Frames per buffer: 46 ms buffers. */ cannam@140: cannam@140: /* Initialize two pink noise signals with different numbers of rows. */ cannam@140: InitializePinkNoise( &data.leftPink, 12 ); cannam@140: InitializePinkNoise( &data.rightPink, 16 ); cannam@140: cannam@140: /* Look at a few values. */ cannam@140: { cannam@140: int i; cannam@140: float pink; cannam@140: for( i=0; i<20; i++ ) cannam@140: { cannam@140: pink = GeneratePinkNoise( &data.leftPink ); cannam@140: printf("Pink = %f\n", pink ); cannam@140: } cannam@140: } cannam@140: cannam@140: data.sampsToGo = totalSamps = (int)(60.0 * SR); /* Play a whole minute. */ cannam@140: err = Pa_Initialize(); cannam@140: if( err != paNoError ) goto error; cannam@140: cannam@140: /* Open a stereo PortAudio stream so we can hear the result. */ cannam@140: outputParameters.device = Pa_GetDefaultOutputDevice(); /* Take the default output device. */ cannam@140: if (outputParameters.device == paNoDevice) { cannam@140: fprintf(stderr,"Error: No default output device.\n"); cannam@140: goto error; cannam@140: } cannam@140: outputParameters.channelCount = 2; /* Stereo output, most likely supported. */ cannam@140: outputParameters.hostApiSpecificStreamInfo = NULL; cannam@140: outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output. */ cannam@140: outputParameters.suggestedLatency = cannam@140: Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency; cannam@140: err = Pa_OpenStream(&stream, cannam@140: NULL, /* No input. */ cannam@140: &outputParameters, cannam@140: SR, /* Sample rate. */ cannam@140: FPB, /* Frames per buffer. */ cannam@140: paClipOff, /* we won't output out of range samples so don't bother clipping them */ cannam@140: patestCallback, cannam@140: &data); cannam@140: if( err != paNoError ) goto error; cannam@140: cannam@140: err = Pa_StartStream( stream ); cannam@140: if( err != paNoError ) goto error; cannam@140: cannam@140: printf("Stereo pink noise for one minute...\n"); cannam@140: cannam@140: while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) Pa_Sleep(100); cannam@140: if( err < 0 ) goto error; cannam@140: cannam@140: err = Pa_CloseStream( stream ); cannam@140: if( err != paNoError ) goto error; cannam@140: #ifdef PINK_MEASURE cannam@140: printf("Pink min = %f, max = %f\n", pinkMin, pinkMax ); cannam@140: #endif cannam@140: Pa_Terminate(); cannam@140: return 0; cannam@140: error: cannam@140: Pa_Terminate(); cannam@140: fprintf( stderr, "An error occured while using the portaudio stream\n" ); cannam@140: fprintf( stderr, "Error number: %d\n", err ); cannam@140: fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); cannam@140: return 0; cannam@140: } cannam@140: #endif /* PINK_TEST */