66 #include "pa_ringbuffer.h"
69 #define PINK_MAX_RANDOM_ROWS (30)
70 #define PINK_RANDOM_BITS (24)
71 #define PINK_RANDOM_SHIFT ((sizeof(long)*8)-PINK_RANDOM_BITS)
75 long pink_Rows[PINK_MAX_RANDOM_ROWS];
106 float wave_bq_left[2];
107 float wave_bq_right[2];
109 EnvState wave_envelope_state;
110 float wave_envelope_level;
111 float wave_envelope_max_level;
113 float wave_pan_right;
114 float wave_attack_incr;
115 float wave_decay_incr;
120 static unsigned long GenerateRandomNumber(
void );
121 void InitializePinkNoise(
PinkNoise *pink,
int numRows );
122 float GeneratePinkNoise(
PinkNoise *pink );
123 unsigned GenerateWave(
OceanWave* wave,
float* output,
unsigned noOfFrames);
127 static unsigned long GenerateRandomNumber(
void )
130 static unsigned long randSeed = 22222;
131 randSeed = (randSeed * 196314165) + 907633515;
137 void InitializePinkNoise(
PinkNoise *pink,
int numRows )
141 pink->pink_Index = 0;
142 pink->pink_IndexMask = (1<<numRows) - 1;
144 pmax = (numRows + 1) * (1<<(PINK_RANDOM_BITS-1));
145 pink->pink_Scalar = 1.0f / pmax;
147 for( i=0; i<numRows; i++ ) pink->pink_Rows[i] = 0;
148 pink->pink_RunningSum = 0;
152 float GeneratePinkNoise(
PinkNoise *pink )
158 pink->pink_Index = (pink->pink_Index + 1) & pink->pink_IndexMask;
160 if( pink->pink_Index != 0 )
165 int n = pink->pink_Index;
166 while( (n & 1) == 0 )
175 pink->pink_RunningSum -= pink->pink_Rows[numZeros];
176 newRandom = ((long)GenerateRandomNumber()) >> PINK_RANDOM_SHIFT;
177 pink->pink_RunningSum += newRandom;
178 pink->pink_Rows[numZeros] = newRandom;
182 newRandom = ((long)GenerateRandomNumber()) >> PINK_RANDOM_SHIFT;
183 sum = pink->pink_RunningSum + newRandom;
185 output = pink->pink_Scalar * sum;
189 float ProcessBiquad(
const BiQuad* coeffs,
float* memory,
float input)
191 float w = input - coeffs->bq_a1 * memory[0] - coeffs->bq_a2 * memory[1];
192 float out = coeffs->bq_b1 * memory[0] + coeffs->bq_b2 * memory[1] + coeffs->bq_b0 * w;
193 memory[1] = memory[0];
198 static const float one_over_2Q_LP = 0.3f;
199 static const float one_over_2Q_HP = 1.0f;
201 unsigned GenerateWave(
OceanWave* wave,
float* output,
unsigned noOfFrames )
204 float targetLevel, levelIncr, currentLevel;
205 switch (wave->wave_envelope_state)
208 targetLevel = noOfFrames * wave->wave_attack_incr + wave->wave_envelope_level;
209 if (targetLevel >= wave->wave_envelope_max_level)
212 wave->wave_envelope_state = State_kPreDecay;
213 targetLevel = wave->wave_envelope_max_level;
229 const float w0 = 3.141592654f * targetLevel / wave->wave_envelope_max_level;
230 const float alpha = sinf(w0) * one_over_2Q_LP;
231 const float cosw0 = cosf(w0);
232 const float a0_fact = 1.0f / (1.0f + alpha);
233 wave->wave_bq_coeffs.bq_b1 = (1.0f - cosw0) * a0_fact;
234 wave->wave_bq_coeffs.bq_b0 = wave->wave_bq_coeffs.bq_b1 * 0.5f;
235 wave->wave_bq_coeffs.bq_b2 = wave->wave_bq_coeffs.bq_b0;
236 wave->wave_bq_coeffs.bq_a2 = (1.0f - alpha) * a0_fact;
237 wave->wave_bq_coeffs.bq_a1 = -2.0f * cosw0 * a0_fact;
241 case State_kPreDecay:
243 memset(wave->wave_bq_left, 0, 2 *
sizeof(
float));
244 memset(wave->wave_bq_right, 0, 2 *
sizeof(
float));
245 wave->wave_envelope_state = State_kDecay;
250 targetLevel = noOfFrames * wave->wave_decay_incr + wave->wave_envelope_level;
251 if (targetLevel < 0.001f)
254 wave->wave_envelope_state = 3;
271 const float v = targetLevel / wave->wave_envelope_max_level;
272 const float w0 = 1.5707963f * (1.0f - (v*v));
273 const float alpha = sinf(w0) * one_over_2Q_HP;
274 const float cosw0 = cosf(w0);
275 const float a0_fact = 1.0f / (1.0f + alpha);
276 wave->wave_bq_coeffs.bq_b1 = (float)(- (1 + cosw0) * a0_fact);
277 wave->wave_bq_coeffs.bq_b0 = -wave->wave_bq_coeffs.bq_b1 * 0.5f;
278 wave->wave_bq_coeffs.bq_b2 = wave->wave_bq_coeffs.bq_b0;
279 wave->wave_bq_coeffs.bq_a2 = (float)((1.0 - alpha) * a0_fact);
280 wave->wave_bq_coeffs.bq_a1 = (float)(-2.0 * cosw0 * a0_fact);
288 currentLevel = wave->wave_envelope_level;
289 wave->wave_envelope_level = targetLevel;
290 levelIncr = (targetLevel - currentLevel) / noOfFrames;
292 for (i = 0; i < noOfFrames; ++i, currentLevel += levelIncr)
294 (*output++) += ProcessBiquad(&wave->wave_bq_coeffs, wave->wave_bq_left, (GeneratePinkNoise(&wave->wave_left))) * currentLevel * wave->wave_pan_left;
295 (*output++) += ProcessBiquad(&wave->wave_bq_coeffs, wave->wave_bq_right, (GeneratePinkNoise(&wave->wave_right))) * currentLevel * wave->wave_pan_right;
308 unsigned noOfActiveWaves;
311 PaUtilRingBuffer rBufToRT;
315 PaUtilRingBuffer rBufFromRT;
316 void* rBufFromRTData;
324 static int patestCallback(
const void* inputBuffer,
326 unsigned long framesPerBuffer,
333 float *out = (
float*)outputBuffer;
337 memset(out, 0, framesPerBuffer * 2 *
sizeof(
float));
339 for (i = 0; i < 16; ++i)
342 if (data->waves[i] == 0 && PaUtil_GetRingBufferReadAvailable(&data->rBufToRT))
345 PaUtil_ReadRingBuffer(&data->rBufToRT, &ptr, 1);
346 data->waves[i] = ptr;
349 if (data->waves[i] != 0)
351 if (GenerateWave(data->waves[i], out, framesPerBuffer))
354 PaUtil_WriteRingBuffer(&data->rBufFromRT, &data->waves[i], 1);
362 #define NEW_ROW_SIZE (12 + (8*rand())/RAND_MAX)
364 OceanWave* InitializeWave(
double SR,
float attackInSeconds,
float maxLevel,
float positionLeftRight)
367 static unsigned lastNoOfRows = 12;
368 unsigned newNoOfRows;
373 InitializePinkNoise(&wave->wave_left, lastNoOfRows);
374 while ((newNoOfRows = NEW_ROW_SIZE) == lastNoOfRows);
375 InitializePinkNoise(&wave->wave_right, newNoOfRows);
376 lastNoOfRows = newNoOfRows;
378 wave->wave_envelope_state = State_kAttack;
379 wave->wave_envelope_level = 0.f;
380 wave->wave_envelope_max_level = maxLevel;
381 wave->wave_attack_incr = wave->wave_envelope_max_level / (attackInSeconds * (float)SR);
382 wave->wave_decay_incr = - wave->wave_envelope_max_level / (attackInSeconds * 4 * (float)SR);
384 wave->wave_pan_left = sqrtf(1.0f - positionLeftRight);
385 wave->wave_pan_right = sqrtf(positionLeftRight);
390 static float GenerateFloatRandom(
float minValue,
float maxValue)
392 return minValue + ((maxValue - minValue) * rand()) / RAND_MAX;
406 static const double SR = 44100.0;
407 static const int FPB = 128;
410 data.rBufToRTData = PaUtil_AllocateZeroInitializedMemory(
sizeof(
OceanWave*) * 256);
411 if (data.rBufToRTData == NULL)
415 PaUtil_InitializeRingBuffer(&data.rBufToRT,
sizeof(
OceanWave*), 256, data.rBufToRTData);
417 data.rBufFromRTData = PaUtil_AllocateZeroInitializedMemory(
sizeof(
OceanWave*) * 256);
418 if (data.rBufFromRTData == NULL)
422 PaUtil_InitializeRingBuffer(&data.rBufFromRT,
sizeof(
OceanWave*), 256, data.rBufFromRTData);
425 if( err != paNoError )
goto error;
430 fprintf(stderr,
"Error: No default output device.\n");
445 if( err != paNoError )
goto error;
448 if( err != paNoError )
goto error;
450 printf(
"Stereo \"ocean waves\" for one minute...\n");
452 tstart = PaUtil_GetTime();
454 srand( (
unsigned)time(NULL) );
458 const double tcurrent = PaUtil_GetTime();
461 while (PaUtil_GetRingBufferReadAvailable(&data.rBufFromRT) > 0)
464 PaUtil_ReadRingBuffer(&data.rBufFromRT, &ptr, 1);
467 printf(
"Wave is deleted...\n");
468 PaUtil_FreeMemory(ptr);
469 --data.noOfActiveWaves;
473 if (tcurrent - tstart < 60.0)
475 if (tcurrent >= tstamp)
477 double tdelta = GenerateFloatRandom(1.0f, 4.0f);
480 if (data.noOfActiveWaves<16)
482 const float attackTime = GenerateFloatRandom(2.0f, 6.0f);
483 const float level = GenerateFloatRandom(0.1f, 1.0f);
484 const float pos = GenerateFloatRandom(0.0f, 1.0f);
485 OceanWave* p = InitializeWave(SR, attackTime, level, pos);
489 PaUtil_WriteRingBuffer(&data.rBufToRT, &p, 1);
490 ++data.noOfActiveWaves;
492 printf(
"Starting wave at level = %.2f, attack = %.2lf, pos = %.2lf\n", level, attackTime, pos);
499 if (data.noOfActiveWaves == 0)
501 printf(
"All waves finished!\n");
508 if( err < 0 )
goto error;
511 if( err != paNoError )
goto error;
513 if (data.rBufToRTData)
515 PaUtil_FreeMemory(data.rBufToRTData);
517 if (data.rBufFromRTData)
519 PaUtil_FreeMemory(data.rBufFromRTData);
529 fprintf( stderr,
"An error occurred while using the portaudio stream\n" );
530 fprintf( stderr,
"Error number: %d\n", err );
PaError Pa_Initialize(void)
PaError Pa_OpenStream(PaStream **stream, const PaStreamParameters *inputParameters, const PaStreamParameters *outputParameters, double sampleRate, unsigned long framesPerBuffer, PaStreamFlags streamFlags, PaStreamCallback *streamCallback, void *userData)
PaError Pa_StartStream(PaStream *stream)
void * hostApiSpecificStreamInfo
The portable PortAudio API.
PaSampleFormat sampleFormat
PaError Pa_IsStreamActive(PaStream *stream)
unsigned long PaStreamCallbackFlags
const PaDeviceInfo * Pa_GetDeviceInfo(PaDeviceIndex device)
PaDeviceIndex Pa_GetDefaultOutputDevice(void)
const char * Pa_GetErrorText(PaError errorCode)
PaError Pa_CloseStream(PaStream *stream)
PaError Pa_Terminate(void)