| 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 |
#include <stdio.h> |
| 38 |
#include <math.h> |
| 39 |
#include "portaudio.h" |
| 40 |
|
| 41 |
#define MAX_SINES (500) |
| 42 |
#define MAX_LOAD (1.2) |
| 43 |
#define SAMPLE_RATE (44100) |
| 44 |
#define FRAMES_PER_BUFFER (512) |
| 45 |
#ifndef M_PI |
| 46 |
#define M_PI (3.14159265) |
| 47 |
#endif |
| 48 |
#define TWOPI (M_PI * 2.0) |
| 49 |
|
| 50 |
typedef struct paTestData |
| 51 |
{ |
| 52 |
int numSines; |
| 53 |
double phases[MAX_SINES]; |
| 54 |
} |
| 55 |
paTestData; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
static int patestCallback( const void *inputBuffer, void *outputBuffer, |
| 62 |
unsigned long framesPerBuffer, |
| 63 |
const PaStreamCallbackTimeInfo* timeInfo, |
| 64 |
PaStreamCallbackFlags statusFlags, |
| 65 |
void *userData ) |
| 66 |
{ |
| 67 |
paTestData *data = (paTestData*)userData; |
| 68 |
float *out = (float*)outputBuffer; |
| 69 |
unsigned long i; |
| 70 |
int j; |
| 71 |
int finished = 0; |
| 72 |
(void) inputBuffer; |
| 73 |
|
| 74 |
for( i=0; i<framesPerBuffer; i++ ) |
| 75 |
{ |
| 76 |
float output = 0.0; |
| 77 |
double phaseInc = 0.02; |
| 78 |
double phase; |
| 79 |
for( j=0; j<data->numSines; j++ ) |
| 80 |
{ |
| 81 |
|
| 82 |
phase = data->phases[j]; |
| 83 |
phase += phaseInc; |
| 84 |
if( phase > TWOPI ) phase -= TWOPI; |
| 85 |
|
| 86 |
phaseInc *= 1.02; |
| 87 |
if( phaseInc > 0.5 ) phaseInc *= 0.5; |
| 88 |
|
| 89 |
|
| 90 |
output += (float) sin( phase ); |
| 91 |
data->phases[j] = phase; |
| 92 |
} |
| 93 |
*out++ = (float) (output / data->numSines); |
| 94 |
} |
| 95 |
return finished; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
int main(void); |
| 100 |
int main(void) |
| 101 |
{ |
| 102 |
PaStreamParameters outputParameters; |
| 103 |
PaStream *stream; |
| 104 |
PaError err; |
| 105 |
int numStress; |
| 106 |
paTestData data = {0}; |
| 107 |
double load; |
| 108 |
|
| 109 |
printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d. MAX_LOAD = %f\n", |
| 110 |
SAMPLE_RATE, FRAMES_PER_BUFFER, MAX_LOAD ); |
| 111 |
|
| 112 |
err = Pa_Initialize(); |
| 113 |
if( err != paNoError ) goto error; |
| 114 |
|
| 115 |
outputParameters.device = Pa_GetDefaultOutputDevice(); |
| 116 |
outputParameters.channelCount = 1; |
| 117 |
outputParameters.sampleFormat = paFloat32; |
| 118 |
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; |
| 119 |
outputParameters.hostApiSpecificStreamInfo = NULL; |
| 120 |
|
| 121 |
err = Pa_OpenStream( |
| 122 |
&stream, |
| 123 |
NULL, |
| 124 |
&outputParameters, |
| 125 |
SAMPLE_RATE, |
| 126 |
FRAMES_PER_BUFFER, |
| 127 |
paClipOff, |
| 128 |
patestCallback, |
| 129 |
&data ); |
| 130 |
if( err != paNoError ) goto error; |
| 131 |
err = Pa_StartStream( stream ); |
| 132 |
if( err != paNoError ) goto error; |
| 133 |
|
| 134 |
|
| 135 |
do |
| 136 |
{ |
| 137 |
data.numSines++; |
| 138 |
Pa_Sleep( 100 ); |
| 139 |
|
| 140 |
load = Pa_GetStreamCpuLoad( stream ); |
| 141 |
printf("numSines = %d, CPU load = %f\n", data.numSines, load ); |
| 142 |
} |
| 143 |
while( load < 0.5 ); |
| 144 |
|
| 145 |
|
| 146 |
numStress = (int) (2.0 * data.numSines * MAX_LOAD ); |
| 147 |
for( ; data.numSines < numStress; data.numSines++ ) |
| 148 |
{ |
| 149 |
Pa_Sleep( 200 ); |
| 150 |
load = Pa_GetStreamCpuLoad( stream ); |
| 151 |
printf("STRESSING: numSines = %d, CPU load = %f\n", data.numSines, load ); |
| 152 |
} |
| 153 |
|
| 154 |
printf("Suffer for 5 seconds.\n"); |
| 155 |
Pa_Sleep( 5000 ); |
| 156 |
|
| 157 |
printf("Stop stream.\n"); |
| 158 |
err = Pa_StopStream( stream ); |
| 159 |
if( err != paNoError ) goto error; |
| 160 |
|
| 161 |
err = Pa_CloseStream( stream ); |
| 162 |
if( err != paNoError ) goto error; |
| 163 |
|
| 164 |
Pa_Terminate(); |
| 165 |
printf("Test finished.\n"); |
| 166 |
return err; |
| 167 |
error: |
| 168 |
Pa_Terminate(); |
| •169 |
fprintf( stderr, "An error occurred while using the portaudio stream\n" ); |
| 170 |
fprintf( stderr, "Error number: %d\n", err ); |
| 171 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 172 |
return err; |
| 173 |
} |