annotate src/portaudio_20140130/examples/paex_pink.c @ 169:223a55898ab9 tip default

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