annotate src/portaudio_20161030/examples/paex_pink.c @ 151:fe80428a60a5

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