| 1 |
/** @file patest_callbackstop.c |
| 2 |
@brief Test the paComplete callback result code. |
| 3 |
@author Ross Bencina <[email protected]> |
| 4 |
*/ |
| 5 |
/* |
| 6 |
* $Id: patest_callbackstop.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $ |
| 7 |
* |
| 8 |
* This program uses the PortAudio Portable Audio Library. |
| 9 |
* For more information see: http://www.portaudio.com/ |
| 10 |
* Copyright (c) 1999-2000 Ross Bencina and Phil Burk |
| 11 |
* |
| 12 |
* Permission is hereby granted, free of charge, to any person obtaining |
| 13 |
* a copy of this software and associated documentation files |
| 14 |
* (the "Software"), to deal in the Software without restriction, |
| 15 |
* including without limitation the rights to use, copy, modify, merge, |
| 16 |
* publish, distribute, sublicense, and/or sell copies of the Software, |
| 17 |
* and to permit persons to whom the Software is furnished to do so, |
| 18 |
* subject to the following conditions: |
| 19 |
* |
| 20 |
* The above copyright notice and this permission notice shall be |
| 21 |
* included in all copies or substantial portions of the Software. |
| 22 |
* |
| 23 |
* Any person wishing to distribute modifications to the Software is |
| 24 |
* requested to send the modifications to the original developer so that |
| 25 |
* they can be incorporated into the canonical version. |
| 26 |
* |
| 27 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 28 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 29 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 30 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 31 |
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 32 |
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 33 |
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 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 |
This routine will be called by the PortAudio stream when audio is needed. |
| 62 |
It may be called at interrupt level on some machines so don't do anything |
| 63 |
that could mess up the system like calling malloc() or free(). |
| 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; /* Prevent unused variable warnings. */ |
| 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 |
/* generate tone */ |
| 87 |
|
| 88 |
x = data->sine[ data->phase++ ]; |
| 89 |
if( data->phase >= TABLE_SIZE ) |
| 90 |
data->phase -= TABLE_SIZE; |
| 91 |
|
| 92 |
*out++ = x; /* left */ |
| 93 |
*out++ = x; /* right */ |
| 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 |
/* initialise sinusoidal wavetable */ |
| 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; /* stereo output */ |
| 133 |
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */ |
| 134 |
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; |
| 135 |
outputParameters.hostApiSpecificStreamInfo = NULL; |
| 136 |
|
| 137 |
err = Pa_OpenStream( |
| 138 |
&stream, |
| 139 |
NULL, /* no input */ |
| 140 |
&outputParameters, |
| 141 |
SAMPLE_RATE, |
| 142 |
FRAMES_PER_BUFFER, |
| 143 |
paClipOff, /* output will be in-range, so no need to clip */ |
| 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 |
/* wait for the callback to complete generating NUM_SECONDS of tone */ |
| 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 |
/* wait for stream to become inactive, |
| 174 |
or for a timeout of approximately NUM_SECONDS |
| 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 occured 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 |
} |