| 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 |
#include "pa_util.h" |
| 42 |
|
| 43 |
#define NUM_BEEPS (3) |
| 44 |
#define SAMPLE_RATE (44100) |
| 45 |
#define SAMPLE_PERIOD (1.0/44100.0) |
| 46 |
#define FRAMES_PER_BUFFER (256) |
| 47 |
#define BEEP_DURATION (400) |
| 48 |
#define IDLE_DURATION (SAMPLE_RATE*2) |
| 49 |
#define SLEEP_MSEC (50) |
| 50 |
|
| 51 |
#define STATE_BKG_IDLE (0) |
| 52 |
#define STATE_BKG_BEEPING (1) |
| 53 |
|
| 54 |
typedef struct |
| 55 |
{ |
| 56 |
float leftPhase; |
| 57 |
float rightPhase; |
| 58 |
int state; |
| 59 |
int beepCountdown; |
| 60 |
int idleCountdown; |
| 61 |
} |
| 62 |
paTestData; |
| 63 |
|
| 64 |
static void InitializeTestData( paTestData *testData ) |
| 65 |
{ |
| 66 |
testData->leftPhase = 0; |
| 67 |
testData->rightPhase = 0; |
| 68 |
testData->state = STATE_BKG_BEEPING; |
| 69 |
testData->beepCountdown = BEEP_DURATION; |
| 70 |
testData->idleCountdown = IDLE_DURATION; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
static int patestCallback( const void *inputBuffer, void *outputBuffer, |
| 78 |
unsigned long framesPerBuffer, |
| 79 |
const PaStreamCallbackTimeInfo *timeInfo, |
| 80 |
PaStreamCallbackFlags statusFlags, void *userData ) |
| 81 |
{ |
| 82 |
|
| 83 |
paTestData *data = (paTestData*)userData; |
| 84 |
float *out = (float*)outputBuffer; |
| 85 |
unsigned int i; |
| 86 |
int result = paContinue; |
| 87 |
|
| 88 |
|
| 89 |
(void) inputBuffer; |
| 90 |
(void) timeInfo; |
| 91 |
(void) statusFlags; |
| 92 |
|
| 93 |
for( i=0; i<framesPerBuffer; i++ ) |
| 94 |
{ |
| 95 |
switch( data->state ) |
| 96 |
{ |
| 97 |
case STATE_BKG_IDLE: |
| 98 |
*out++ = 0.0; |
| 99 |
*out++ = 0.0; |
| 100 |
--data->idleCountdown; |
| 101 |
|
| 102 |
if( data->idleCountdown <= 0 ) result = paComplete; |
| 103 |
break; |
| 104 |
|
| 105 |
case STATE_BKG_BEEPING: |
| 106 |
if( data->beepCountdown <= 0 ) |
| 107 |
{ |
| 108 |
data->state = STATE_BKG_IDLE; |
| 109 |
*out++ = 0.0; |
| 110 |
*out++ = 0.0; |
| 111 |
} |
| 112 |
else |
| 113 |
{ |
| 114 |
|
| 115 |
*out++ = data->leftPhase; |
| 116 |
*out++ = data->rightPhase; |
| 117 |
|
| 118 |
data->leftPhase += 0.01f; |
| 119 |
|
| 120 |
if( data->leftPhase >= 1.0f ) data->leftPhase -= 2.0f; |
| 121 |
|
| 122 |
data->rightPhase += 0.03f; |
| 123 |
if( data->rightPhase >= 1.0f ) data->rightPhase -= 2.0f; |
| 124 |
} |
| 125 |
--data->beepCountdown; |
| 126 |
break; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
return result; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
static PaError DoTest( int flags ) |
| 135 |
{ |
| 136 |
PaStream *stream; |
| 137 |
PaError err; |
| 138 |
paTestData data; |
| 139 |
PaStreamParameters outputParameters; |
| 140 |
|
| 141 |
InitializeTestData( &data ); |
| 142 |
|
| 143 |
outputParameters.device = Pa_GetDefaultOutputDevice(); |
| 144 |
outputParameters.channelCount = 2; |
| 145 |
outputParameters.hostApiSpecificStreamInfo = NULL; |
| 146 |
outputParameters.sampleFormat = paFloat32; |
| 147 |
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency; |
| 148 |
|
| 149 |
|
| 150 |
err = Pa_OpenStream( |
| 151 |
&stream, |
| 152 |
NULL, |
| 153 |
&outputParameters, |
| 154 |
SAMPLE_RATE, |
| 155 |
FRAMES_PER_BUFFER, |
| 156 |
paClipOff | flags, |
| 157 |
patestCallback, |
| 158 |
&data ); |
| 159 |
if( err != paNoError ) goto error; |
| 160 |
|
| 161 |
|
| 162 |
err = Pa_StartStream( stream ); |
| 163 |
if( err != paNoError ) goto error; |
| 164 |
|
| 165 |
printf("hear \"BEEP\"\n" ); |
| 166 |
fflush(stdout); |
| 167 |
|
| 168 |
while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) Pa_Sleep(SLEEP_MSEC); |
| 169 |
if( err < 0 ) goto error; |
| 170 |
|
| 171 |
err = Pa_StopStream( stream ); |
| 172 |
if( err != paNoError ) goto error; |
| 173 |
|
| 174 |
err = Pa_CloseStream( stream ); |
| 175 |
if( err != paNoError ) goto error; |
| 176 |
|
| 177 |
return err; |
| 178 |
error: |
| 179 |
return err; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
int main(void); |
| 184 |
int main(void) |
| 185 |
{ |
| 186 |
PaError err = paNoError; |
| 187 |
int i; |
| 188 |
|
| 189 |
|
| 190 |
err = Pa_Initialize(); |
| 191 |
if( err != paNoError ) goto error; |
| 192 |
|
| 193 |
printf("PortAudio Test: Testing stream playback with no priming.\n"); |
| 194 |
printf("PortAudio Test: you should see BEEP before you hear it.\n"); |
| 195 |
printf("BEEP %d times.\n", NUM_BEEPS ); |
| 196 |
|
| 197 |
for( i=0; i< NUM_BEEPS; ++i ) |
| 198 |
{ |
| 199 |
err = DoTest( 0 ); |
| 200 |
if( err != paNoError ) |
| 201 |
goto error; |
| 202 |
} |
| 203 |
|
| 204 |
printf("PortAudio Test: Testing stream playback with priming.\n"); |
| 205 |
printf("PortAudio Test: you should see BEEP around the same time you hear it.\n"); |
| 206 |
for( i=0; i< NUM_BEEPS; ++i ) |
| 207 |
{ |
| 208 |
err = DoTest( paPrimeOutputBuffersUsingStreamCallback ); |
| 209 |
if( err != paNoError ) |
| 210 |
goto error; |
| 211 |
} |
| 212 |
|
| 213 |
printf("Test finished.\n"); |
| 214 |
|
| 215 |
Pa_Terminate(); |
| 216 |
return err; |
| 217 |
error: |
| 218 |
Pa_Terminate(); |
| •219 |
fprintf( stderr, "An error occurred while using the portaudio stream\n" ); |
| 220 |
fprintf( stderr, "Error number: %d\n", err ); |
| 221 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 222 |
return err; |
| 223 |
} |