1
/** @file patest_callbackstop.c
3
@brief Test the paComplete callback result code.
4
@author Ross Bencina <rossb@audiomulch.com>
7
* $Id: patest_callbackstop.c,v 1.2 2006/09/23 18:42:51 llucius Exp $
9
* This program uses the PortAudio Portable Audio Library.
10
* For more information see: http://www.portaudio.com/
11
* Copyright (c) 1999-2000 Ross Bencina and Phil Burk
13
* Permission is hereby granted, free of charge, to any person obtaining
14
* a copy of this software and associated documentation files
15
* (the "Software"), to deal in the Software without restriction,
16
* including without limitation the rights to use, copy, modify, merge,
17
* publish, distribute, sublicense, and/or sell copies of the Software,
18
* and to permit persons to whom the Software is furnished to do so,
19
* subject to the following conditions:
21
* The above copyright notice and this permission notice shall be
22
* included in all copies or substantial portions of the Software.
24
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
28
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34
* The text above constitutes the entire PortAudio license; however,
35
* the PortAudio community also makes the following non-binding requests:
37
* Any person wishing to distribute modifications to the Software is
38
* requested to send the modifications to the original developer so that
39
* they can be incorporated into the canonical version. It is also
40
* requested that these non-binding requests be included along with the
46
#include "portaudio.h"
48
#define NUM_SECONDS (5)
50
#define SAMPLE_RATE (44100)
51
#define FRAMES_PER_BUFFER (67)
54
#define M_PI (3.14159265)
57
#define TABLE_SIZE (200)
60
float sine[TABLE_SIZE];
62
unsigned long generatedFramesCount;
63
volatile int callbackReturnedPaComplete;
64
volatile int callbackInvokedAfterReturningPaComplete;
69
This routine will be called by the PortAudio stream when audio is needed.
70
It may be called at interrupt level on some machines so don't do anything
71
that could mess up the system like calling malloc() or free().
73
static int TestCallback( const void *input, void *output,
74
unsigned long frameCount,
75
const PaStreamCallbackTimeInfo* timeInfo,
76
PaStreamCallbackFlags statusFlags,
79
TestData *data = (TestData*)userData;
80
float *out = (float*)output;
84
(void) input; /* Prevent unused variable warnings. */
89
if( data->callbackReturnedPaComplete )
90
data->callbackInvokedAfterReturningPaComplete = 1;
92
for( i=0; i<frameCount; i++ )
96
x = data->sine[ data->phase++ ];
97
if( data->phase >= TABLE_SIZE )
98
data->phase -= TABLE_SIZE;
100
*out++ = x; /* left */
101
*out++ = x; /* right */
104
data->generatedFramesCount += frameCount;
105
if( data->generatedFramesCount >= (NUM_SECONDS * SAMPLE_RATE) )
107
data->callbackReturnedPaComplete = 1;
116
/*----------------------------------------------------------------------------*/
120
PaStreamParameters outputParameters;
127
printf( "PortAudio Test: output sine wave. SR = %d, BufSize = %d\n",
128
SAMPLE_RATE, FRAMES_PER_BUFFER );
130
/* initialise sinusoidal wavetable */
131
for( i=0; i<TABLE_SIZE; i++ )
133
data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
136
err = Pa_Initialize();
137
if( err != paNoError ) goto error;
139
outputParameters.device = Pa_GetDefaultOutputDevice();
140
outputParameters.channelCount = 2; /* stereo output */
141
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
142
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
143
outputParameters.hostApiSpecificStreamInfo = NULL;
151
paClipOff, /* output will be in-range, so no need to clip */
154
if( err != paNoError ) goto error;
156
printf("Repeating test %d times.\n", NUM_LOOPS );
158
for( i=0; i < NUM_LOOPS; ++i )
161
data.generatedFramesCount = 0;
162
data.callbackReturnedPaComplete = 0;
163
data.callbackInvokedAfterReturningPaComplete = 0;
165
err = Pa_StartStream( stream );
166
if( err != paNoError ) goto error;
168
printf("Play for %d seconds.\n", NUM_SECONDS );
170
/* wait for the callback to complete generating NUM_SECONDS of tone */
176
while( !data.callbackReturnedPaComplete );
178
printf( "Callback returned paComplete.\n" );
179
printf( "Waiting for buffers to finish playing...\n" );
181
/* wait for stream to become inactive,
182
or for a timeout of approximately NUM_SECONDS
186
while( (err = Pa_IsStreamActive( stream )) == 1 && j < NUM_SECONDS * 2 )
199
printf( "TEST FAILED: Timed out waiting for buffers to finish playing.\n" );
203
printf("Buffers finished.\n" );
206
if( data.callbackInvokedAfterReturningPaComplete )
208
printf( "TEST FAILED: Callback was invoked after returning paComplete.\n" );
212
err = Pa_StopStream( stream );
213
if( err != paNoError ) goto error;
216
err = Pa_CloseStream( stream );
217
if( err != paNoError ) goto error;
220
printf("Test finished.\n");
225
fprintf( stderr, "An error occured while using the portaudio stream\n" );
226
fprintf( stderr, "Error number: %d\n", err );
227
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );