| 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 |
#include <stdio.h> |
| 39 |
#include <math.h> |
| 40 |
#include "portaudio.h" |
| 41 |
|
| 42 |
#define MAX_SINES (500) |
| 43 |
#define MAX_LOAD (1.2) |
| 44 |
#define SAMPLE_RATE (44100) |
| 45 |
#define FRAMES_PER_BUFFER (512) |
| 46 |
#ifndef M_PI |
| 47 |
#define M_PI (3.14159265) |
| 48 |
#endif |
| 49 |
#define TWOPI (M_PI * 2.0) |
| 50 |
|
| 51 |
typedef struct paTestData |
| 52 |
{ |
| 53 |
int sineCount; |
| 54 |
double phases[MAX_SINES]; |
| 55 |
int countUnderflows; |
| 56 |
int outputUnderflowCount; |
| 57 |
} |
| 58 |
paTestData; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
static int patestCallback( const void *inputBuffer, void *outputBuffer, |
| 65 |
unsigned long framesPerBuffer, |
| 66 |
const PaStreamCallbackTimeInfo* timeInfo, |
| 67 |
PaStreamCallbackFlags statusFlags, |
| 68 |
void *userData ) |
| 69 |
{ |
| 70 |
paTestData *data = (paTestData*)userData; |
| 71 |
float *out = (float*)outputBuffer; |
| 72 |
unsigned long i; |
| 73 |
int j; |
| 74 |
int finished = paContinue; |
| 75 |
(void) timeInfo; |
| 76 |
(void) inputBuffer; |
| 77 |
|
| 78 |
|
| 79 |
if( data->countUnderflows && (statusFlags & paOutputUnderflow) ) |
| 80 |
data->outputUnderflowCount++; |
| 81 |
|
| 82 |
for( i=0; i<framesPerBuffer; i++ ) |
| 83 |
{ |
| 84 |
float output = 0.0; |
| 85 |
double phaseInc = 0.02; |
| 86 |
double phase; |
| 87 |
|
| 88 |
for( j=0; j<data->sineCount; j++ ) |
| 89 |
{ |
| 90 |
|
| 91 |
phase = data->phases[j]; |
| 92 |
phase += phaseInc; |
| 93 |
if( phase > TWOPI ) phase -= TWOPI; |
| 94 |
|
| 95 |
phaseInc *= 1.02; |
| 96 |
if( phaseInc > 0.5 ) phaseInc *= 0.5; |
| 97 |
|
| 98 |
|
| 99 |
output += (float) sin( phase ); |
| 100 |
data->phases[j] = phase; |
| 101 |
} |
| 102 |
*out++ = (float) (output / data->sineCount); |
| 103 |
} |
| 104 |
|
| 105 |
return finished; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
int main(void); |
| 110 |
int main(void) |
| 111 |
{ |
| 112 |
PaStreamParameters outputParameters; |
| 113 |
PaStream *stream; |
| 114 |
PaError err; |
| 115 |
int safeSineCount, stressedSineCount; |
| 116 |
int safeUnderflowCount, stressedUnderflowCount; |
| 117 |
paTestData data = {0}; |
| 118 |
double load; |
| 119 |
|
| 120 |
|
| 121 |
printf("PortAudio Test: output sine waves, count underflows. SR = %d, BufSize = %d. MAX_LOAD = %f\n", |
| 122 |
SAMPLE_RATE, FRAMES_PER_BUFFER, (float)MAX_LOAD ); |
| 123 |
|
| 124 |
err = Pa_Initialize(); |
| 125 |
if( err != paNoError ) goto error; |
| 126 |
|
| 127 |
outputParameters.device = Pa_GetDefaultOutputDevice(); |
| 128 |
outputParameters.channelCount = 1; |
| 129 |
outputParameters.sampleFormat = paFloat32; |
| 130 |
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; |
| 131 |
outputParameters.hostApiSpecificStreamInfo = NULL; |
| 132 |
|
| 133 |
err = Pa_OpenStream( |
| 134 |
&stream, |
| 135 |
NULL, |
| 136 |
&outputParameters, |
| 137 |
SAMPLE_RATE, |
| 138 |
FRAMES_PER_BUFFER, |
| 139 |
paClipOff, |
| 140 |
patestCallback, |
| 141 |
&data ); |
| 142 |
if( err != paNoError ) goto error; |
| 143 |
err = Pa_StartStream( stream ); |
| 144 |
if( err != paNoError ) goto error; |
| 145 |
|
| 146 |
printf("Establishing load conditions...\n" ); |
| 147 |
|
| 148 |
|
| 149 |
do |
| 150 |
{ |
| 151 |
data.sineCount++; |
| 152 |
Pa_Sleep( 100 ); |
| 153 |
|
| 154 |
load = Pa_GetStreamCpuLoad( stream ); |
| 155 |
printf("sineCount = %d, CPU load = %f\n", data.sineCount, load ); |
| 156 |
} |
| 157 |
while( load < 0.5 && data.sineCount < (MAX_SINES-1)); |
| 158 |
|
| 159 |
safeSineCount = data.sineCount; |
| 160 |
|
| 161 |
|
| 162 |
stressedSineCount = (int) (2.0 * data.sineCount * MAX_LOAD ); |
| 163 |
if( stressedSineCount > MAX_SINES ) |
| 164 |
stressedSineCount = MAX_SINES; |
| 165 |
for( ; data.sineCount < stressedSineCount; data.sineCount++ ) |
| 166 |
{ |
| 167 |
Pa_Sleep( 100 ); |
| 168 |
load = Pa_GetStreamCpuLoad( stream ); |
| 169 |
printf("STRESSING: sineCount = %d, CPU load = %f\n", data.sineCount, load ); |
| 170 |
} |
| 171 |
|
| 172 |
printf("Counting underflows for 5 seconds.\n"); |
| 173 |
data.countUnderflows = 1; |
| 174 |
Pa_Sleep( 5000 ); |
| 175 |
|
| 176 |
stressedUnderflowCount = data.outputUnderflowCount; |
| 177 |
|
| 178 |
data.countUnderflows = 0; |
| 179 |
data.sineCount = safeSineCount; |
| 180 |
|
| 181 |
printf("Resuming safe load...\n"); |
| 182 |
Pa_Sleep( 1500 ); |
| 183 |
data.outputUnderflowCount = 0; |
| 184 |
Pa_Sleep( 1500 ); |
| 185 |
load = Pa_GetStreamCpuLoad( stream ); |
| 186 |
printf("sineCount = %d, CPU load = %f\n", data.sineCount, load ); |
| 187 |
|
| 188 |
printf("Counting underflows for 5 seconds.\n"); |
| 189 |
data.countUnderflows = 1; |
| 190 |
Pa_Sleep( 5000 ); |
| 191 |
|
| 192 |
safeUnderflowCount = data.outputUnderflowCount; |
| 193 |
|
| 194 |
printf("Stop stream.\n"); |
| 195 |
err = Pa_StopStream( stream ); |
| 196 |
if( err != paNoError ) goto error; |
| 197 |
|
| 198 |
err = Pa_CloseStream( stream ); |
| 199 |
if( err != paNoError ) goto error; |
| 200 |
|
| 201 |
Pa_Terminate(); |
| 202 |
|
| 203 |
if( stressedUnderflowCount == 0 ) |
| 204 |
printf("Test failed, no output underflows detected under stress.\n"); |
| 205 |
else if( safeUnderflowCount != 0 ) |
| 206 |
printf("Test failed, %d unexpected underflows detected under safe load.\n", safeUnderflowCount); |
| 207 |
else |
| 208 |
printf("Test passed, %d expected output underflows detected under stress, 0 unexpected underflows detected under safe load.\n", stressedUnderflowCount ); |
| 209 |
|
| 210 |
return err; |
| 211 |
error: |
| 212 |
Pa_Terminate(); |
| •213 |
fprintf( stderr, "An error occurred while using the portaudio stream\n" ); |
| 214 |
fprintf( stderr, "Error number: %d\n", err ); |
| 215 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 216 |
return err; |
| 217 |
} |