annotate src/portaudio_20140130/test/patest_callbackstop.c @ 167:bd3cc4d1df30

Add FFTW 3.3.8 source, and a Linux build
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 19 Nov 2019 14:52:55 +0000
parents e3d5853d5918
children
rev   line source
cannam@124 1 /** @file patest_callbackstop.c
cannam@124 2 @ingroup test_src
cannam@124 3 @brief Test the paComplete callback result code.
cannam@124 4 @author Ross Bencina <rossb@audiomulch.com>
cannam@124 5 */
cannam@124 6 /*
cannam@124 7 * $Id: patest_callbackstop.c 1368 2008-03-01 00:38:27Z rossb $
cannam@124 8 *
cannam@124 9 * This program uses the PortAudio Portable Audio Library.
cannam@124 10 * For more information see: http://www.portaudio.com/
cannam@124 11 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
cannam@124 12 *
cannam@124 13 * Permission is hereby granted, free of charge, to any person obtaining
cannam@124 14 * a copy of this software and associated documentation files
cannam@124 15 * (the "Software"), to deal in the Software without restriction,
cannam@124 16 * including without limitation the rights to use, copy, modify, merge,
cannam@124 17 * publish, distribute, sublicense, and/or sell copies of the Software,
cannam@124 18 * and to permit persons to whom the Software is furnished to do so,
cannam@124 19 * subject to the following conditions:
cannam@124 20 *
cannam@124 21 * The above copyright notice and this permission notice shall be
cannam@124 22 * included in all copies or substantial portions of the Software.
cannam@124 23 *
cannam@124 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@124 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@124 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
cannam@124 27 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
cannam@124 28 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@124 29 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@124 30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@124 31 */
cannam@124 32
cannam@124 33 /*
cannam@124 34 * The text above constitutes the entire PortAudio license; however,
cannam@124 35 * the PortAudio community also makes the following non-binding requests:
cannam@124 36 *
cannam@124 37 * Any person wishing to distribute modifications to the Software is
cannam@124 38 * requested to send the modifications to the original developer so that
cannam@124 39 * they can be incorporated into the canonical version. It is also
cannam@124 40 * requested that these non-binding requests be included along with the
cannam@124 41 * license above.
cannam@124 42 */
cannam@124 43
cannam@124 44 #include <stdio.h>
cannam@124 45 #include <math.h>
cannam@124 46 #include "portaudio.h"
cannam@124 47
cannam@124 48 #define NUM_SECONDS (5)
cannam@124 49 #define NUM_LOOPS (4)
cannam@124 50 #define SAMPLE_RATE (44100)
cannam@124 51 #define FRAMES_PER_BUFFER (67)
cannam@124 52
cannam@124 53 #ifndef M_PI
cannam@124 54 #define M_PI (3.14159265)
cannam@124 55 #endif
cannam@124 56
cannam@124 57 #define TABLE_SIZE (200)
cannam@124 58 typedef struct
cannam@124 59 {
cannam@124 60 float sine[TABLE_SIZE];
cannam@124 61 int phase;
cannam@124 62 unsigned long generatedFramesCount;
cannam@124 63 volatile int callbackReturnedPaComplete;
cannam@124 64 volatile int callbackInvokedAfterReturningPaComplete;
cannam@124 65 char message[100];
cannam@124 66 }
cannam@124 67 TestData;
cannam@124 68
cannam@124 69 /*
cannam@124 70 This routine will be called by the PortAudio stream when audio is needed.
cannam@124 71 It may be called at interrupt level on some machines so don't do anything
cannam@124 72 that could mess up the system like calling malloc() or free().
cannam@124 73 */
cannam@124 74 static int TestCallback( const void *input, void *output,
cannam@124 75 unsigned long frameCount,
cannam@124 76 const PaStreamCallbackTimeInfo* timeInfo,
cannam@124 77 PaStreamCallbackFlags statusFlags,
cannam@124 78 void *userData )
cannam@124 79 {
cannam@124 80 TestData *data = (TestData*)userData;
cannam@124 81 float *out = (float*)output;
cannam@124 82 unsigned long i;
cannam@124 83 float x;
cannam@124 84
cannam@124 85 (void) input; /* Prevent unused variable warnings. */
cannam@124 86 (void) timeInfo;
cannam@124 87 (void) statusFlags;
cannam@124 88
cannam@124 89
cannam@124 90 if( data->callbackReturnedPaComplete )
cannam@124 91 data->callbackInvokedAfterReturningPaComplete = 1;
cannam@124 92
cannam@124 93 for( i=0; i<frameCount; i++ )
cannam@124 94 {
cannam@124 95 /* generate tone */
cannam@124 96
cannam@124 97 x = data->sine[ data->phase++ ];
cannam@124 98 if( data->phase >= TABLE_SIZE )
cannam@124 99 data->phase -= TABLE_SIZE;
cannam@124 100
cannam@124 101 *out++ = x; /* left */
cannam@124 102 *out++ = x; /* right */
cannam@124 103 }
cannam@124 104
cannam@124 105 data->generatedFramesCount += frameCount;
cannam@124 106 if( data->generatedFramesCount >= (NUM_SECONDS * SAMPLE_RATE) )
cannam@124 107 {
cannam@124 108 data->callbackReturnedPaComplete = 1;
cannam@124 109 return paComplete;
cannam@124 110 }
cannam@124 111 else
cannam@124 112 {
cannam@124 113 return paContinue;
cannam@124 114 }
cannam@124 115 }
cannam@124 116
cannam@124 117 /*
cannam@124 118 * This routine is called by portaudio when playback is done.
cannam@124 119 */
cannam@124 120 static void StreamFinished( void* userData )
cannam@124 121 {
cannam@124 122 TestData *data = (TestData *) userData;
cannam@124 123 printf( "Stream Completed: %s\n", data->message );
cannam@124 124 }
cannam@124 125
cannam@124 126
cannam@124 127 /*----------------------------------------------------------------------------*/
cannam@124 128 int main(void);
cannam@124 129 int main(void)
cannam@124 130 {
cannam@124 131 PaStreamParameters outputParameters;
cannam@124 132 PaStream *stream;
cannam@124 133 PaError err;
cannam@124 134 TestData data;
cannam@124 135 int i, j;
cannam@124 136
cannam@124 137
cannam@124 138 printf( "PortAudio Test: output sine wave. SR = %d, BufSize = %d\n",
cannam@124 139 SAMPLE_RATE, FRAMES_PER_BUFFER );
cannam@124 140
cannam@124 141 /* initialise sinusoidal wavetable */
cannam@124 142 for( i=0; i<TABLE_SIZE; i++ )
cannam@124 143 {
cannam@124 144 data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
cannam@124 145 }
cannam@124 146
cannam@124 147 err = Pa_Initialize();
cannam@124 148 if( err != paNoError ) goto error;
cannam@124 149
cannam@124 150 outputParameters.device = Pa_GetDefaultOutputDevice();
cannam@124 151 if (outputParameters.device == paNoDevice) {
cannam@124 152 fprintf(stderr,"Error: No default output device.\n");
cannam@124 153 goto error;
cannam@124 154 }
cannam@124 155 outputParameters.channelCount = 2; /* stereo output */
cannam@124 156 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
cannam@124 157 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
cannam@124 158 outputParameters.hostApiSpecificStreamInfo = NULL;
cannam@124 159
cannam@124 160 err = Pa_OpenStream(
cannam@124 161 &stream,
cannam@124 162 NULL, /* no input */
cannam@124 163 &outputParameters,
cannam@124 164 SAMPLE_RATE,
cannam@124 165 FRAMES_PER_BUFFER,
cannam@124 166 paClipOff, /* output will be in-range, so no need to clip */
cannam@124 167 TestCallback,
cannam@124 168 &data );
cannam@124 169 if( err != paNoError ) goto error;
cannam@124 170
cannam@124 171 sprintf( data.message, "Loop: XX" );
cannam@124 172 err = Pa_SetStreamFinishedCallback( stream, &StreamFinished );
cannam@124 173 if( err != paNoError ) goto error;
cannam@124 174
cannam@124 175 printf("Repeating test %d times.\n", NUM_LOOPS );
cannam@124 176
cannam@124 177 for( i=0; i < NUM_LOOPS; ++i )
cannam@124 178 {
cannam@124 179 data.phase = 0;
cannam@124 180 data.generatedFramesCount = 0;
cannam@124 181 data.callbackReturnedPaComplete = 0;
cannam@124 182 data.callbackInvokedAfterReturningPaComplete = 0;
cannam@124 183 sprintf( data.message, "Loop: %d", i );
cannam@124 184
cannam@124 185 err = Pa_StartStream( stream );
cannam@124 186 if( err != paNoError ) goto error;
cannam@124 187
cannam@124 188 printf("Play for %d seconds.\n", NUM_SECONDS );
cannam@124 189
cannam@124 190 /* wait for the callback to complete generating NUM_SECONDS of tone */
cannam@124 191
cannam@124 192 do
cannam@124 193 {
cannam@124 194 Pa_Sleep( 500 );
cannam@124 195 }
cannam@124 196 while( !data.callbackReturnedPaComplete );
cannam@124 197
cannam@124 198 printf( "Callback returned paComplete.\n" );
cannam@124 199 printf( "Waiting for buffers to finish playing...\n" );
cannam@124 200
cannam@124 201 /* wait for stream to become inactive,
cannam@124 202 or for a timeout of approximately NUM_SECONDS
cannam@124 203 */
cannam@124 204
cannam@124 205 j = 0;
cannam@124 206 while( (err = Pa_IsStreamActive( stream )) == 1 && j < NUM_SECONDS * 2 )
cannam@124 207 {
cannam@124 208 printf(".\n" );
cannam@124 209 Pa_Sleep( 500 );
cannam@124 210 ++j;
cannam@124 211 }
cannam@124 212
cannam@124 213 if( err < 0 )
cannam@124 214 {
cannam@124 215 goto error;
cannam@124 216 }
cannam@124 217 else if( err == 1 )
cannam@124 218 {
cannam@124 219 printf( "TEST FAILED: Timed out waiting for buffers to finish playing.\n" );
cannam@124 220 }
cannam@124 221 else
cannam@124 222 {
cannam@124 223 printf("Buffers finished.\n" );
cannam@124 224 }
cannam@124 225
cannam@124 226 if( data.callbackInvokedAfterReturningPaComplete )
cannam@124 227 {
cannam@124 228 printf( "TEST FAILED: Callback was invoked after returning paComplete.\n" );
cannam@124 229 }
cannam@124 230
cannam@124 231
cannam@124 232 err = Pa_StopStream( stream );
cannam@124 233 if( err != paNoError ) goto error;
cannam@124 234
cannam@124 235 printf( "sleeping for 1 second...\n" );
cannam@124 236 Pa_Sleep( 1000 );
cannam@124 237 }
cannam@124 238
cannam@124 239 err = Pa_CloseStream( stream );
cannam@124 240 if( err != paNoError ) goto error;
cannam@124 241
cannam@124 242 Pa_Terminate();
cannam@124 243 printf("Test finished.\n");
cannam@124 244
cannam@124 245 return err;
cannam@124 246 error:
cannam@124 247 Pa_Terminate();
cannam@124 248 fprintf( stderr, "An error occured while using the portaudio stream\n" );
cannam@124 249 fprintf( stderr, "Error number: %d\n", err );
cannam@124 250 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
cannam@124 251 return err;
cannam@124 252 }