| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
#include <stdio.h> |
| 51 |
#include <math.h> |
| 52 |
#include "portaudio.h" |
| 53 |
|
| 54 |
#define PINK_MAX_RANDOM_ROWS (30) |
| 55 |
#define PINK_RANDOM_BITS (24) |
| 56 |
#define PINK_RANDOM_SHIFT ((sizeof(long)*8)-PINK_RANDOM_BITS) |
| 57 |
|
| 58 |
typedef struct |
| 59 |
{ |
| 60 |
long pink_Rows[PINK_MAX_RANDOM_ROWS]; |
| 61 |
long pink_RunningSum; |
| 62 |
int pink_Index; |
| 63 |
int pink_IndexMask; |
| 64 |
float pink_Scalar; |
| 65 |
} |
| 66 |
PinkNoise; |
| 67 |
|
| 68 |
|
| 69 |
static unsigned long GenerateRandomNumber( void ); |
| 70 |
void InitializePinkNoise( PinkNoise *pink, int numRows ); |
| 71 |
float GeneratePinkNoise( PinkNoise *pink ); |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
static unsigned long GenerateRandomNumber( void ) |
| 76 |
{ |
| 77 |
|
| 78 |
static unsigned long randSeed = 22222; |
| 79 |
randSeed = (randSeed * 196314165) + 907633515; |
| 80 |
return randSeed; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
void InitializePinkNoise( PinkNoise *pink, int numRows ) |
| 86 |
{ |
| 87 |
int i; |
| 88 |
long pmax; |
| 89 |
pink->pink_Index = 0; |
| 90 |
pink->pink_IndexMask = (1<<numRows) - 1; |
| 91 |
|
| 92 |
pmax = (numRows + 1) * (1<<(PINK_RANDOM_BITS-1)); |
| 93 |
pink->pink_Scalar = 1.0f / pmax; |
| 94 |
|
| 95 |
for( i=0; i<numRows; i++ ) pink->pink_Rows[i] = 0; |
| 96 |
pink->pink_RunningSum = 0; |
| 97 |
} |
| 98 |
|
| 99 |
#define PINK_MEASURE |
| 100 |
#ifdef PINK_MEASURE |
| 101 |
float pinkMax = -999.0; |
| 102 |
float pinkMin = 999.0; |
| 103 |
#endif |
| 104 |
|
| 105 |
|
| 106 |
float GeneratePinkNoise( PinkNoise *pink ) |
| 107 |
{ |
| 108 |
long newRandom; |
| 109 |
long sum; |
| 110 |
float output; |
| 111 |
|
| 112 |
pink->pink_Index = (pink->pink_Index + 1) & pink->pink_IndexMask; |
| 113 |
|
| 114 |
if( pink->pink_Index != 0 ) |
| 115 |
{ |
| 116 |
|
| 117 |
|
| 118 |
int numZeros = 0; |
| 119 |
int n = pink->pink_Index; |
| 120 |
while( (n & 1) == 0 ) |
| 121 |
{ |
| 122 |
n = n >> 1; |
| 123 |
numZeros++; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
pink->pink_RunningSum -= pink->pink_Rows[numZeros]; |
| 130 |
newRandom = ((long)GenerateRandomNumber()) >> PINK_RANDOM_SHIFT; |
| 131 |
pink->pink_RunningSum += newRandom; |
| 132 |
pink->pink_Rows[numZeros] = newRandom; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
newRandom = ((long)GenerateRandomNumber()) >> PINK_RANDOM_SHIFT; |
| 137 |
sum = pink->pink_RunningSum + newRandom; |
| 138 |
|
| 139 |
output = pink->pink_Scalar * sum; |
| 140 |
#ifdef PINK_MEASURE |
| 141 |
|
| 142 |
if( output > pinkMax ) pinkMax = output; |
| 143 |
else if( output < pinkMin ) pinkMin = output; |
| 144 |
#endif |
| 145 |
return output; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
#define PINK_TEST |
| 150 |
#ifdef PINK_TEST |
| 151 |
|
| 152 |
|
| 153 |
typedef struct |
| 154 |
{ |
| 155 |
PinkNoise leftPink; |
| 156 |
PinkNoise rightPink; |
| 157 |
unsigned int sampsToGo; |
| 158 |
} |
| 159 |
paTestData; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
static int patestCallback(const void* inputBuffer, |
| 166 |
void* outputBuffer, |
| 167 |
unsigned long framesPerBuffer, |
| 168 |
const PaStreamCallbackTimeInfo* timeInfo, |
| 169 |
PaStreamCallbackFlags statusFlags, |
| 170 |
void* userData) |
| 171 |
{ |
| 172 |
int finished; |
| 173 |
int i; |
| 174 |
int numFrames; |
| 175 |
paTestData *data = (paTestData*)userData; |
| 176 |
float *out = (float*)outputBuffer; |
| 177 |
(void) inputBuffer; |
| 178 |
|
| 179 |
|
| 180 |
if( data->sampsToGo < framesPerBuffer ) |
| 181 |
{ |
| 182 |
numFrames = data->sampsToGo; |
| 183 |
finished = 1; |
| 184 |
} |
| 185 |
else |
| 186 |
{ |
| 187 |
numFrames = framesPerBuffer; |
| 188 |
finished = 0; |
| 189 |
} |
| 190 |
for( i=0; i<numFrames; i++ ) |
| 191 |
{ |
| 192 |
*out++ = GeneratePinkNoise( &data->leftPink ); |
| 193 |
*out++ = GeneratePinkNoise( &data->rightPink ); |
| 194 |
} |
| 195 |
data->sampsToGo -= numFrames; |
| 196 |
return finished; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
int main(void); |
| 201 |
int main(void) |
| 202 |
{ |
| 203 |
PaStream* stream; |
| 204 |
PaError err; |
| 205 |
paTestData data; |
| 206 |
PaStreamParameters outputParameters; |
| 207 |
int totalSamps; |
| 208 |
static const double SR = 44100.0; |
| 209 |
static const int FPB = 2048; |
| 210 |
|
| 211 |
|
| 212 |
InitializePinkNoise( &data.leftPink, 12 ); |
| 213 |
InitializePinkNoise( &data.rightPink, 16 ); |
| 214 |
|
| 215 |
|
| 216 |
{ |
| 217 |
int i; |
| 218 |
float pink; |
| 219 |
for( i=0; i<20; i++ ) |
| 220 |
{ |
| 221 |
pink = GeneratePinkNoise( &data.leftPink ); |
| 222 |
printf("Pink = %f\n", pink ); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
data.sampsToGo = totalSamps = (int)(60.0 * SR); |
| 227 |
err = Pa_Initialize(); |
| 228 |
if( err != paNoError ) goto error; |
| 229 |
|
| 230 |
|
| 231 |
outputParameters.device = Pa_GetDefaultOutputDevice(); |
| 232 |
outputParameters.channelCount = 2; |
| 233 |
outputParameters.hostApiSpecificStreamInfo = NULL; |
| 234 |
outputParameters.sampleFormat = paFloat32; |
| 235 |
outputParameters.suggestedLatency = |
| 236 |
Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency; |
| 237 |
err = Pa_OpenStream(&stream, |
| 238 |
NULL, |
| 239 |
&outputParameters, |
| 240 |
SR, |
| 241 |
FPB, |
| 242 |
paClipOff, |
| 243 |
patestCallback, |
| 244 |
&data); |
| 245 |
if( err != paNoError ) goto error; |
| 246 |
|
| 247 |
err = Pa_StartStream( stream ); |
| 248 |
if( err != paNoError ) goto error; |
| 249 |
|
| 250 |
printf("Stereo pink noise for one minute...\n"); |
| 251 |
|
| 252 |
while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) Pa_Sleep(100); |
| 253 |
if( err < 0 ) goto error; |
| 254 |
|
| 255 |
err = Pa_CloseStream( stream ); |
| 256 |
if( err != paNoError ) goto error; |
| 257 |
#ifdef PINK_MEASURE |
| 258 |
printf("Pink min = %f, max = %f\n", pinkMin, pinkMax ); |
| 259 |
#endif |
| 260 |
Pa_Terminate(); |
| 261 |
return 0; |
| 262 |
error: |
| 263 |
Pa_Terminate(); |
| •264 |
fprintf( stderr, "An error occurred while using the portaudio stream\n" ); |
| 265 |
fprintf( stderr, "Error number: %d\n", err ); |
| 266 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 267 |
return 0; |
| 268 |
} |
| 269 |
#endif |