annotate src/portaudio_20140130/examples/paex_pink.c @ 81:7029a4916348

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