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