| 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 |
#include <stdio.h> |
| 37 |
#include <math.h> |
| 38 |
#include "portaudio.h" |
| 39 |
|
| 40 |
#define NUM_SECONDS (5) |
| 41 |
#define NUM_LOOPS (4) |
| 42 |
#define SAMPLE_RATE (44100) |
| 43 |
#define FRAMES_PER_BUFFER (67) |
| 44 |
|
| 45 |
#ifndef M_PI |
| 46 |
#define M_PI (3.14159265) |
| 47 |
#endif |
| 48 |
|
| 49 |
#define TABLE_SIZE (200) |
| 50 |
typedef struct |
| 51 |
{ |
| 52 |
float sine[TABLE_SIZE]; |
| 53 |
int phase; |
| 54 |
unsigned long generatedFramesCount; |
| 55 |
volatile int callbackReturnedPaComplete; |
| 56 |
volatile int callbackInvokedAfterReturningPaComplete; |
| 57 |
} |
| 58 |
TestData; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
static int TestCallback( const void *input, void *output, |
| 66 |
unsigned long frameCount, |
| 67 |
const PaStreamCallbackTimeInfo* timeInfo, |
| 68 |
PaStreamCallbackFlags statusFlags, |
| 69 |
void *userData ) |
| 70 |
{ |
| 71 |
TestData *data = (TestData*)userData; |
| 72 |
float *out = (float*)output; |
| 73 |
unsigned long i; |
| 74 |
float x; |
| 75 |
|
| 76 |
(void) input; |
| 77 |
(void) timeInfo; |
| 78 |
(void) statusFlags; |
| 79 |
|
| 80 |
|
| 81 |
if( data->callbackReturnedPaComplete ) |
| 82 |
data->callbackInvokedAfterReturningPaComplete = 1; |
| 83 |
|
| 84 |
for( i=0; i<frameCount; i++ ) |
| 85 |
{ |
| 86 |
|
| 87 |
|
| 88 |
x = data->sine[ data->phase++ ]; |
| 89 |
if( data->phase >= TABLE_SIZE ) |
| 90 |
data->phase -= TABLE_SIZE; |
| 91 |
|
| 92 |
*out++ = x; |
| 93 |
*out++ = x; |
| 94 |
} |
| 95 |
|
| 96 |
data->generatedFramesCount += frameCount; |
| 97 |
if( data->generatedFramesCount >= (NUM_SECONDS * SAMPLE_RATE) ) |
| 98 |
{ |
| 99 |
data->callbackReturnedPaComplete = 1; |
| 100 |
return paComplete; |
| 101 |
} |
| 102 |
else |
| 103 |
{ |
| 104 |
return paContinue; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
int main(void); |
| 110 |
int main(void) |
| 111 |
{ |
| 112 |
PaStreamParameters outputParameters; |
| 113 |
PaStream *stream; |
| 114 |
PaError err; |
| 115 |
TestData data; |
| 116 |
int i, j; |
| 117 |
|
| 118 |
|
| 119 |
printf( "PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", |
| 120 |
SAMPLE_RATE, FRAMES_PER_BUFFER ); |
| 121 |
|
| 122 |
|
| 123 |
for( i=0; i<TABLE_SIZE; i++ ) |
| 124 |
{ |
| 125 |
data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ); |
| 126 |
} |
| 127 |
|
| 128 |
err = Pa_Initialize(); |
| 129 |
if( err != paNoError ) goto error; |
| 130 |
|
| 131 |
outputParameters.device = Pa_GetDefaultOutputDevice(); |
| 132 |
outputParameters.channelCount = 2; |
| 133 |
outputParameters.sampleFormat = paFloat32; |
| 134 |
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; |
| 135 |
outputParameters.hostApiSpecificStreamInfo = NULL; |
| 136 |
|
| 137 |
err = Pa_OpenStream( |
| 138 |
&stream, |
| 139 |
NULL, |
| 140 |
&outputParameters, |
| 141 |
SAMPLE_RATE, |
| 142 |
FRAMES_PER_BUFFER, |
| 143 |
paClipOff, |
| 144 |
TestCallback, |
| 145 |
&data ); |
| 146 |
if( err != paNoError ) goto error; |
| 147 |
|
| 148 |
printf("Repeating test %d times.\n", NUM_LOOPS ); |
| 149 |
|
| 150 |
for( i=0; i < NUM_LOOPS; ++i ) |
| 151 |
{ |
| 152 |
data.phase = 0; |
| 153 |
data.generatedFramesCount = 0; |
| 154 |
data.callbackReturnedPaComplete = 0; |
| 155 |
data.callbackInvokedAfterReturningPaComplete = 0; |
| 156 |
|
| 157 |
err = Pa_StartStream( stream ); |
| 158 |
if( err != paNoError ) goto error; |
| 159 |
|
| 160 |
printf("Play for %d seconds.\n", NUM_SECONDS ); |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
do |
| 165 |
{ |
| 166 |
Pa_Sleep( 500 ); |
| 167 |
} |
| 168 |
while( !data.callbackReturnedPaComplete ); |
| 169 |
|
| 170 |
printf( "Callback returned paComplete.\n" ); |
| 171 |
printf( "Waiting for buffers to finish playing...\n" ); |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
j = 0; |
| 178 |
while( (err = Pa_IsStreamActive( stream )) == 1 && j < NUM_SECONDS * 2 ) |
| 179 |
{ |
| 180 |
printf(".\n" ); |
| 181 |
Pa_Sleep( 500 ); |
| 182 |
++j; |
| 183 |
} |
| 184 |
|
| 185 |
if( err < 0 ) |
| 186 |
{ |
| 187 |
goto error; |
| 188 |
} |
| 189 |
else if( err == 1 ) |
| 190 |
{ |
| 191 |
printf( "TEST FAILED: Timed out waiting for buffers to finish playing.\n" ); |
| 192 |
} |
| 193 |
else |
| 194 |
{ |
| 195 |
printf("Buffers finished.\n" ); |
| 196 |
} |
| 197 |
|
| 198 |
if( data.callbackInvokedAfterReturningPaComplete ) |
| 199 |
{ |
| 200 |
printf( "TEST FAILED: Callback was invoked after returning paComplete.\n" ); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
err = Pa_StopStream( stream ); |
| 205 |
if( err != paNoError ) goto error; |
| 206 |
} |
| 207 |
|
| 208 |
err = Pa_CloseStream( stream ); |
| 209 |
if( err != paNoError ) goto error; |
| 210 |
|
| 211 |
Pa_Terminate(); |
| 212 |
printf("Test finished.\n"); |
| 213 |
|
| 214 |
return err; |
| 215 |
error: |
| 216 |
Pa_Terminate(); |
| •217 |
fprintf( stderr, "An error occurred while using the portaudio stream\n" ); |
| 218 |
fprintf( stderr, "Error number: %d\n", err ); |
| 219 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 220 |
return err; |
| 221 |
} |