annotate src/portaudio/examples/paex_pink.c @ 89:8a15ff55d9af

Add bzip2, zlib, liblo, portaudio sources
author Chris Cannam <cannam@all-day-breakfast.com>
date Wed, 20 Mar 2013 13:59:52 +0000
parents
children
rev   line source
cannam@89 1 /** @file paex_pink.c
cannam@89 2 @ingroup examples_src
cannam@89 3 @brief Generate Pink Noise using Gardner method.
cannam@89 4
cannam@89 5 Optimization suggested by James McCartney uses a tree
cannam@89 6 to select which random value to replace.
cannam@89 7 <pre>
cannam@89 8 x x x x x x x x x x x x x x x x
cannam@89 9 x x x x x x x x
cannam@89 10 x x x x
cannam@89 11 x x
cannam@89 12 x
cannam@89 13 </pre>
cannam@89 14 Tree is generated by counting trailing zeros in an increasing index.
cannam@89 15 When the index is zero, no random number is selected.
cannam@89 16
cannam@89 17 @author Phil Burk http://www.softsynth.com
cannam@89 18 */
cannam@89 19 /*
cannam@89 20 * $Id: paex_pink.c 1752 2011-09-08 03:21:55Z philburk $
cannam@89 21 *
cannam@89 22 * This program uses the PortAudio Portable Audio Library.
cannam@89 23 * For more information see: http://www.portaudio.com
cannam@89 24 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
cannam@89 25 *
cannam@89 26 * Permission is hereby granted, free of charge, to any person obtaining
cannam@89 27 * a copy of this software and associated documentation files
cannam@89 28 * (the "Software"), to deal in the Software without restriction,
cannam@89 29 * including without limitation the rights to use, copy, modify, merge,
cannam@89 30 * publish, distribute, sublicense, and/or sell copies of the Software,
cannam@89 31 * and to permit persons to whom the Software is furnished to do so,
cannam@89 32 * subject to the following conditions:
cannam@89 33 *
cannam@89 34 * The above copyright notice and this permission notice shall be
cannam@89 35 * included in all copies or substantial portions of the Software.
cannam@89 36 *
cannam@89 37 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@89 38 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@89 39 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
cannam@89 40 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
cannam@89 41 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@89 42 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@89 43 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@89 44 */
cannam@89 45
cannam@89 46 /*
cannam@89 47 * The text above constitutes the entire PortAudio license; however,
cannam@89 48 * the PortAudio community also makes the following non-binding requests:
cannam@89 49 *
cannam@89 50 * Any person wishing to distribute modifications to the Software is
cannam@89 51 * requested to send the modifications to the original developer so that
cannam@89 52 * they can be incorporated into the canonical version. It is also
cannam@89 53 * requested that these non-binding requests be included along with the
cannam@89 54 * license above.
cannam@89 55 */
cannam@89 56
cannam@89 57 #include <stdio.h>
cannam@89 58 #include <math.h>
cannam@89 59 #include "portaudio.h"
cannam@89 60
cannam@89 61 #define PINK_MAX_RANDOM_ROWS (30)
cannam@89 62 #define PINK_RANDOM_BITS (24)
cannam@89 63 #define PINK_RANDOM_SHIFT ((sizeof(long)*8)-PINK_RANDOM_BITS)
cannam@89 64
cannam@89 65 typedef struct
cannam@89 66 {
cannam@89 67 long pink_Rows[PINK_MAX_RANDOM_ROWS];
cannam@89 68 long pink_RunningSum; /* Used to optimize summing of generators. */
cannam@89 69 int pink_Index; /* Incremented each sample. */
cannam@89 70 int pink_IndexMask; /* Index wrapped by ANDing with this mask. */
cannam@89 71 float pink_Scalar; /* Used to scale within range of -1.0 to +1.0 */
cannam@89 72 }
cannam@89 73 PinkNoise;
cannam@89 74
cannam@89 75 /* Prototypes */
cannam@89 76 static unsigned long GenerateRandomNumber( void );
cannam@89 77 void InitializePinkNoise( PinkNoise *pink, int numRows );
cannam@89 78 float GeneratePinkNoise( PinkNoise *pink );
cannam@89 79
cannam@89 80 /************************************************************/
cannam@89 81 /* Calculate pseudo-random 32 bit number based on linear congruential method. */
cannam@89 82 static unsigned long GenerateRandomNumber( void )
cannam@89 83 {
cannam@89 84 /* Change this seed for different random sequences. */
cannam@89 85 static unsigned long randSeed = 22222;
cannam@89 86 randSeed = (randSeed * 196314165) + 907633515;
cannam@89 87 return randSeed;
cannam@89 88 }
cannam@89 89
cannam@89 90 /************************************************************/
cannam@89 91 /* Setup PinkNoise structure for N rows of generators. */
cannam@89 92 void InitializePinkNoise( PinkNoise *pink, int numRows )
cannam@89 93 {
cannam@89 94 int i;
cannam@89 95 long pmax;
cannam@89 96 pink->pink_Index = 0;
cannam@89 97 pink->pink_IndexMask = (1<<numRows) - 1;
cannam@89 98 /* Calculate maximum possible signed random value. Extra 1 for white noise always added. */
cannam@89 99 pmax = (numRows + 1) * (1<<(PINK_RANDOM_BITS-1));
cannam@89 100 pink->pink_Scalar = 1.0f / pmax;
cannam@89 101 /* Initialize rows. */
cannam@89 102 for( i=0; i<numRows; i++ ) pink->pink_Rows[i] = 0;
cannam@89 103 pink->pink_RunningSum = 0;
cannam@89 104 }
cannam@89 105
cannam@89 106 #define PINK_MEASURE
cannam@89 107 #ifdef PINK_MEASURE
cannam@89 108 float pinkMax = -999.0;
cannam@89 109 float pinkMin = 999.0;
cannam@89 110 #endif
cannam@89 111
cannam@89 112 /* Generate Pink noise values between -1.0 and +1.0 */
cannam@89 113 float GeneratePinkNoise( PinkNoise *pink )
cannam@89 114 {
cannam@89 115 long newRandom;
cannam@89 116 long sum;
cannam@89 117 float output;
cannam@89 118 /* Increment and mask index. */
cannam@89 119 pink->pink_Index = (pink->pink_Index + 1) & pink->pink_IndexMask;
cannam@89 120 /* If index is zero, don't update any random values. */
cannam@89 121 if( pink->pink_Index != 0 )
cannam@89 122 {
cannam@89 123 /* Determine how many trailing zeros in PinkIndex. */
cannam@89 124 /* This algorithm will hang if n==0 so test first. */
cannam@89 125 int numZeros = 0;
cannam@89 126 int n = pink->pink_Index;
cannam@89 127 while( (n & 1) == 0 )
cannam@89 128 {
cannam@89 129 n = n >> 1;
cannam@89 130 numZeros++;
cannam@89 131 }
cannam@89 132 /* Replace the indexed ROWS random value.
cannam@89 133 * Subtract and add back to RunningSum instead of adding all the random
cannam@89 134 * values together. Only one changes each time.
cannam@89 135 */
cannam@89 136 pink->pink_RunningSum -= pink->pink_Rows[numZeros];
cannam@89 137 newRandom = ((long)GenerateRandomNumber()) >> PINK_RANDOM_SHIFT;
cannam@89 138 pink->pink_RunningSum += newRandom;
cannam@89 139 pink->pink_Rows[numZeros] = newRandom;
cannam@89 140 }
cannam@89 141
cannam@89 142 /* Add extra white noise value. */
cannam@89 143 newRandom = ((long)GenerateRandomNumber()) >> PINK_RANDOM_SHIFT;
cannam@89 144 sum = pink->pink_RunningSum + newRandom;
cannam@89 145 /* Scale to range of -1.0 to 0.9999. */
cannam@89 146 output = pink->pink_Scalar * sum;
cannam@89 147 #ifdef PINK_MEASURE
cannam@89 148 /* Check Min/Max */
cannam@89 149 if( output > pinkMax ) pinkMax = output;
cannam@89 150 else if( output < pinkMin ) pinkMin = output;
cannam@89 151 #endif
cannam@89 152 return output;
cannam@89 153 }
cannam@89 154
cannam@89 155 /*******************************************************************/
cannam@89 156 #define PINK_TEST
cannam@89 157 #ifdef PINK_TEST
cannam@89 158
cannam@89 159 /* Context for callback routine. */
cannam@89 160 typedef struct
cannam@89 161 {
cannam@89 162 PinkNoise leftPink;
cannam@89 163 PinkNoise rightPink;
cannam@89 164 unsigned int sampsToGo;
cannam@89 165 }
cannam@89 166 paTestData;
cannam@89 167
cannam@89 168 /* This routine will be called by the PortAudio engine when audio is needed.
cannam@89 169 ** It may called at interrupt level on some machines so don't do anything
cannam@89 170 ** that could mess up the system like calling malloc() or free().
cannam@89 171 */
cannam@89 172 static int patestCallback(const void* inputBuffer,
cannam@89 173 void* outputBuffer,
cannam@89 174 unsigned long framesPerBuffer,
cannam@89 175 const PaStreamCallbackTimeInfo* timeInfo,
cannam@89 176 PaStreamCallbackFlags statusFlags,
cannam@89 177 void* userData)
cannam@89 178 {
cannam@89 179 int finished;
cannam@89 180 int i;
cannam@89 181 int numFrames;
cannam@89 182 paTestData *data = (paTestData*)userData;
cannam@89 183 float *out = (float*)outputBuffer;
cannam@89 184 (void) inputBuffer; /* Prevent "unused variable" warnings. */
cannam@89 185
cannam@89 186 /* Are we almost at end. */
cannam@89 187 if( data->sampsToGo < framesPerBuffer )
cannam@89 188 {
cannam@89 189 numFrames = data->sampsToGo;
cannam@89 190 finished = 1;
cannam@89 191 }
cannam@89 192 else
cannam@89 193 {
cannam@89 194 numFrames = framesPerBuffer;
cannam@89 195 finished = 0;
cannam@89 196 }
cannam@89 197 for( i=0; i<numFrames; i++ )
cannam@89 198 {
cannam@89 199 *out++ = GeneratePinkNoise( &data->leftPink );
cannam@89 200 *out++ = GeneratePinkNoise( &data->rightPink );
cannam@89 201 }
cannam@89 202 data->sampsToGo -= numFrames;
cannam@89 203 return finished;
cannam@89 204 }
cannam@89 205
cannam@89 206 /*******************************************************************/
cannam@89 207 int main(void);
cannam@89 208 int main(void)
cannam@89 209 {
cannam@89 210 PaStream* stream;
cannam@89 211 PaError err;
cannam@89 212 paTestData data;
cannam@89 213 PaStreamParameters outputParameters;
cannam@89 214 int totalSamps;
cannam@89 215 static const double SR = 44100.0;
cannam@89 216 static const int FPB = 2048; /* Frames per buffer: 46 ms buffers. */
cannam@89 217
cannam@89 218 /* Initialize two pink noise signals with different numbers of rows. */
cannam@89 219 InitializePinkNoise( &data.leftPink, 12 );
cannam@89 220 InitializePinkNoise( &data.rightPink, 16 );
cannam@89 221
cannam@89 222 /* Look at a few values. */
cannam@89 223 {
cannam@89 224 int i;
cannam@89 225 float pink;
cannam@89 226 for( i=0; i<20; i++ )
cannam@89 227 {
cannam@89 228 pink = GeneratePinkNoise( &data.leftPink );
cannam@89 229 printf("Pink = %f\n", pink );
cannam@89 230 }
cannam@89 231 }
cannam@89 232
cannam@89 233 data.sampsToGo = totalSamps = (int)(60.0 * SR); /* Play a whole minute. */
cannam@89 234 err = Pa_Initialize();
cannam@89 235 if( err != paNoError ) goto error;
cannam@89 236
cannam@89 237 /* Open a stereo PortAudio stream so we can hear the result. */
cannam@89 238 outputParameters.device = Pa_GetDefaultOutputDevice(); /* Take the default output device. */
cannam@89 239 if (outputParameters.device == paNoDevice) {
cannam@89 240 fprintf(stderr,"Error: No default output device.\n");
cannam@89 241 goto error;
cannam@89 242 }
cannam@89 243 outputParameters.channelCount = 2; /* Stereo output, most likely supported. */
cannam@89 244 outputParameters.hostApiSpecificStreamInfo = NULL;
cannam@89 245 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output. */
cannam@89 246 outputParameters.suggestedLatency =
cannam@89 247 Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
cannam@89 248 err = Pa_OpenStream(&stream,
cannam@89 249 NULL, /* No input. */
cannam@89 250 &outputParameters,
cannam@89 251 SR, /* Sample rate. */
cannam@89 252 FPB, /* Frames per buffer. */
cannam@89 253 paClipOff, /* we won't output out of range samples so don't bother clipping them */
cannam@89 254 patestCallback,
cannam@89 255 &data);
cannam@89 256 if( err != paNoError ) goto error;
cannam@89 257
cannam@89 258 err = Pa_StartStream( stream );
cannam@89 259 if( err != paNoError ) goto error;
cannam@89 260
cannam@89 261 printf("Stereo pink noise for one minute...\n");
cannam@89 262
cannam@89 263 while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) Pa_Sleep(100);
cannam@89 264 if( err < 0 ) goto error;
cannam@89 265
cannam@89 266 err = Pa_CloseStream( stream );
cannam@89 267 if( err != paNoError ) goto error;
cannam@89 268 #ifdef PINK_MEASURE
cannam@89 269 printf("Pink min = %f, max = %f\n", pinkMin, pinkMax );
cannam@89 270 #endif
cannam@89 271 Pa_Terminate();
cannam@89 272 return 0;
cannam@89 273 error:
cannam@89 274 Pa_Terminate();
cannam@89 275 fprintf( stderr, "An error occured while using the portaudio stream\n" );
cannam@89 276 fprintf( stderr, "Error number: %d\n", err );
cannam@89 277 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
cannam@89 278 return 0;
cannam@89 279 }
cannam@89 280 #endif /* PINK_TEST */