annotate src/portaudio_20161030/test/patest_sync.c @ 140:59a8758c56b1

Add source for PortAudio stable v190600_20161030
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 03 Jan 2017 13:44:07 +0000
parents
children
rev   line source
cannam@140 1 /** @file patest_sync.c
cannam@140 2 @ingroup test_src
cannam@140 3 @brief Test time stamping and synchronization of audio and video.
cannam@140 4
cannam@140 5 A high latency is used so we can hear the difference in time.
cannam@140 6 Random durations are used so we know we are hearing the right beep
cannam@140 7 and not the one before or after.
cannam@140 8
cannam@140 9 Sequence of events:
cannam@140 10 -# Foreground requests a beep.
cannam@140 11 -# Background randomly schedules a beep.
cannam@140 12 -# Foreground waits for the beep to be heard based on PaUtil_GetTime().
cannam@140 13 -# Foreground outputs video (printf) in sync with audio.
cannam@140 14 -# Repeat.
cannam@140 15
cannam@140 16 @author Phil Burk http://www.softsynth.com
cannam@140 17 */
cannam@140 18 /*
cannam@140 19 * $Id$
cannam@140 20 *
cannam@140 21 * This program uses the PortAudio Portable Audio Library.
cannam@140 22 * For more information see: http://www.portaudio.com
cannam@140 23 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
cannam@140 24 *
cannam@140 25 * Permission is hereby granted, free of charge, to any person obtaining
cannam@140 26 * a copy of this software and associated documentation files
cannam@140 27 * (the "Software"), to deal in the Software without restriction,
cannam@140 28 * including without limitation the rights to use, copy, modify, merge,
cannam@140 29 * publish, distribute, sublicense, and/or sell copies of the Software,
cannam@140 30 * and to permit persons to whom the Software is furnished to do so,
cannam@140 31 * subject to the following conditions:
cannam@140 32 *
cannam@140 33 * The above copyright notice and this permission notice shall be
cannam@140 34 * included in all copies or substantial portions of the Software.
cannam@140 35 *
cannam@140 36 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@140 37 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@140 38 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
cannam@140 39 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
cannam@140 40 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@140 41 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@140 42 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@140 43 */
cannam@140 44
cannam@140 45 /*
cannam@140 46 * The text above constitutes the entire PortAudio license; however,
cannam@140 47 * the PortAudio community also makes the following non-binding requests:
cannam@140 48 *
cannam@140 49 * Any person wishing to distribute modifications to the Software is
cannam@140 50 * requested to send the modifications to the original developer so that
cannam@140 51 * they can be incorporated into the canonical version. It is also
cannam@140 52 * requested that these non-binding requests be included along with the
cannam@140 53 * license above.
cannam@140 54 */
cannam@140 55
cannam@140 56 #include <stdio.h>
cannam@140 57 #include <math.h>
cannam@140 58 #include "portaudio.h"
cannam@140 59 #include "pa_util.h"
cannam@140 60 #define NUM_BEEPS (6)
cannam@140 61 #define SAMPLE_RATE (44100)
cannam@140 62 #define SAMPLE_PERIOD (1.0/44100.0)
cannam@140 63 #define FRAMES_PER_BUFFER (256)
cannam@140 64 #define BEEP_DURATION (400)
cannam@140 65 #define LATENCY_MSEC (2000)
cannam@140 66 #define SLEEP_MSEC (10)
cannam@140 67 #define TIMEOUT_MSEC (15000)
cannam@140 68
cannam@140 69 #define STATE_BKG_IDLE (0)
cannam@140 70 #define STATE_BKG_PENDING (1)
cannam@140 71 #define STATE_BKG_BEEPING (2)
cannam@140 72 typedef struct
cannam@140 73 {
cannam@140 74 float left_phase;
cannam@140 75 float right_phase;
cannam@140 76 int state;
cannam@140 77 volatile int requestBeep; /* Set by foreground, cleared by background. */
cannam@140 78 PaTime beepTime;
cannam@140 79 int beepCount;
cannam@140 80 double latency; /* For debugging. */
cannam@140 81 }
cannam@140 82 paTestData;
cannam@140 83
cannam@140 84 static unsigned long GenerateRandomNumber( void );
cannam@140 85 /************************************************************/
cannam@140 86 /* Calculate pseudo-random 32 bit number based on linear congruential method. */
cannam@140 87 static unsigned long GenerateRandomNumber( void )
cannam@140 88 {
cannam@140 89 static unsigned long randSeed = 99887766; /* Change this for different random sequences. */
cannam@140 90 randSeed = (randSeed * 196314165) + 907633515;
cannam@140 91 return randSeed;
cannam@140 92 }
cannam@140 93
cannam@140 94 /* This routine will be called by the PortAudio engine when audio is needed.
cannam@140 95 ** It may called at interrupt level on some machines so don't do anything
cannam@140 96 ** that could mess up the system like calling malloc() or free().
cannam@140 97 */
cannam@140 98 static int patestCallback( const void *inputBuffer, void *outputBuffer,
cannam@140 99 unsigned long framesPerBuffer,
cannam@140 100 const PaStreamCallbackTimeInfo *timeInfo,
cannam@140 101 PaStreamCallbackFlags statusFlags, void *userData )
cannam@140 102 {
cannam@140 103 /* Cast data passed through stream to our structure. */
cannam@140 104 paTestData *data = (paTestData*)userData;
cannam@140 105 float *out = (float*)outputBuffer;
cannam@140 106 unsigned int i;
cannam@140 107 (void) inputBuffer;
cannam@140 108
cannam@140 109 data->latency = timeInfo->outputBufferDacTime - timeInfo->currentTime;
cannam@140 110
cannam@140 111 for( i=0; i<framesPerBuffer; i++ )
cannam@140 112 {
cannam@140 113 switch( data->state )
cannam@140 114 {
cannam@140 115 case STATE_BKG_IDLE:
cannam@140 116 /* Schedule beep at some random time in the future. */
cannam@140 117 if( data->requestBeep )
cannam@140 118 {
cannam@140 119 int random = GenerateRandomNumber() >> 14;
cannam@140 120 data->beepTime = timeInfo->outputBufferDacTime + (( (double)(random + SAMPLE_RATE)) * SAMPLE_PERIOD );
cannam@140 121 data->state = STATE_BKG_PENDING;
cannam@140 122 }
cannam@140 123 *out++ = 0.0; /* left */
cannam@140 124 *out++ = 0.0; /* right */
cannam@140 125 break;
cannam@140 126
cannam@140 127 case STATE_BKG_PENDING:
cannam@140 128 if( (timeInfo->outputBufferDacTime + (i*SAMPLE_PERIOD)) >= data->beepTime )
cannam@140 129 {
cannam@140 130 data->state = STATE_BKG_BEEPING;
cannam@140 131 data->beepCount = BEEP_DURATION;
cannam@140 132 data->left_phase = data->right_phase = 0.0;
cannam@140 133 }
cannam@140 134 *out++ = 0.0; /* left */
cannam@140 135 *out++ = 0.0; /* right */
cannam@140 136 break;
cannam@140 137
cannam@140 138 case STATE_BKG_BEEPING:
cannam@140 139 if( data->beepCount <= 0 )
cannam@140 140 {
cannam@140 141 data->state = STATE_BKG_IDLE;
cannam@140 142 data->requestBeep = 0;
cannam@140 143 *out++ = 0.0; /* left */
cannam@140 144 *out++ = 0.0; /* right */
cannam@140 145 }
cannam@140 146 else
cannam@140 147 {
cannam@140 148 /* Play sawtooth wave. */
cannam@140 149 *out++ = data->left_phase; /* left */
cannam@140 150 *out++ = data->right_phase; /* right */
cannam@140 151 /* Generate simple sawtooth phaser that ranges between -1.0 and 1.0. */
cannam@140 152 data->left_phase += 0.01f;
cannam@140 153 /* When signal reaches top, drop back down. */
cannam@140 154 if( data->left_phase >= 1.0f ) data->left_phase -= 2.0f;
cannam@140 155 /* higher pitch so we can distinguish left and right. */
cannam@140 156 data->right_phase += 0.03f;
cannam@140 157 if( data->right_phase >= 1.0f ) data->right_phase -= 2.0f;
cannam@140 158 }
cannam@140 159 data->beepCount -= 1;
cannam@140 160 break;
cannam@140 161
cannam@140 162 default:
cannam@140 163 data->state = STATE_BKG_IDLE;
cannam@140 164 break;
cannam@140 165 }
cannam@140 166 }
cannam@140 167 return 0;
cannam@140 168 }
cannam@140 169 /*******************************************************************/
cannam@140 170 int main(void);
cannam@140 171 int main(void)
cannam@140 172 {
cannam@140 173 PaStream *stream;
cannam@140 174 PaError err;
cannam@140 175 paTestData DATA;
cannam@140 176 int i, timeout;
cannam@140 177 PaTime previousTime;
cannam@140 178 PaStreamParameters outputParameters;
cannam@140 179 printf("PortAudio Test: you should see BEEP at the same time you hear it.\n");
cannam@140 180 printf("Wait for a few seconds random delay between BEEPs.\n");
cannam@140 181 printf("BEEP %d times.\n", NUM_BEEPS );
cannam@140 182 /* Initialize our DATA for use by callback. */
cannam@140 183 DATA.left_phase = DATA.right_phase = 0.0;
cannam@140 184 DATA.state = STATE_BKG_IDLE;
cannam@140 185 DATA.requestBeep = 0;
cannam@140 186 /* Initialize library before making any other calls. */
cannam@140 187 err = Pa_Initialize();
cannam@140 188 if( err != paNoError ) goto error;
cannam@140 189
cannam@140 190 outputParameters.device = Pa_GetDefaultOutputDevice();
cannam@140 191 if (outputParameters.device == paNoDevice) {
cannam@140 192 fprintf(stderr,"Error: No default output device.\n");
cannam@140 193 goto error;
cannam@140 194 }
cannam@140 195 outputParameters.channelCount = 2;
cannam@140 196 outputParameters.hostApiSpecificStreamInfo = NULL;
cannam@140 197 outputParameters.sampleFormat = paFloat32;
cannam@140 198 outputParameters.suggestedLatency = (double)LATENCY_MSEC / 1000;
cannam@140 199
cannam@140 200 /* Open an audio I/O stream. */
cannam@140 201 err = Pa_OpenStream(
cannam@140 202 &stream,
cannam@140 203 NULL, /* no input */
cannam@140 204 &outputParameters,
cannam@140 205 SAMPLE_RATE,
cannam@140 206 FRAMES_PER_BUFFER, /* frames per buffer */
cannam@140 207 paClipOff, /* we won't output out of range samples so don't bother clipping them */
cannam@140 208 patestCallback,
cannam@140 209 &DATA );
cannam@140 210 if( err != paNoError ) goto error;
cannam@140 211
cannam@140 212 err = Pa_StartStream( stream );
cannam@140 213 if( err != paNoError ) goto error;
cannam@140 214
cannam@140 215 printf("started\n");
cannam@140 216 fflush(stdout);
cannam@140 217
cannam@140 218 previousTime = Pa_GetStreamTime( stream );
cannam@140 219 for( i=0; i<NUM_BEEPS; i++ )
cannam@140 220 {
cannam@140 221 /* Request a beep from background. */
cannam@140 222 DATA.requestBeep = 1;
cannam@140 223
cannam@140 224 /* Wait for background to acknowledge request. */
cannam@140 225 timeout = TIMEOUT_MSEC;
cannam@140 226 while( (DATA.requestBeep == 1) && (timeout-- > 0 ) ) Pa_Sleep(SLEEP_MSEC);
cannam@140 227 if( timeout <= 0 )
cannam@140 228 {
cannam@140 229 fprintf( stderr, "Timed out waiting for background to acknowledge request.\n" );
cannam@140 230 goto error;
cannam@140 231 }
cannam@140 232 printf("calc beep for %9.3f, latency = %6.3f\n", DATA.beepTime, DATA.latency );
cannam@140 233 fflush(stdout);
cannam@140 234
cannam@140 235 /* Wait for scheduled beep time. */
cannam@140 236 timeout = TIMEOUT_MSEC + (10000/SLEEP_MSEC);
cannam@140 237 while( (Pa_GetStreamTime( stream ) < DATA.beepTime) && (timeout-- > 0 ) )
cannam@140 238 {
cannam@140 239 Pa_Sleep(SLEEP_MSEC);
cannam@140 240 }
cannam@140 241 if( timeout <= 0 )
cannam@140 242 {
cannam@140 243 fprintf( stderr, "Timed out waiting for time. Now = %9.3f, Beep for %9.3f.\n",
cannam@140 244 PaUtil_GetTime(), DATA.beepTime );
cannam@140 245 goto error;
cannam@140 246 }
cannam@140 247
cannam@140 248 /* Beep should be sounding now so print synchronized BEEP. */
cannam@140 249 printf("hear \"BEEP\" at %9.3f, delta = %9.3f\n",
cannam@140 250 Pa_GetStreamTime( stream ), (DATA.beepTime - previousTime) );
cannam@140 251 fflush(stdout);
cannam@140 252
cannam@140 253 previousTime = DATA.beepTime;
cannam@140 254 }
cannam@140 255
cannam@140 256 err = Pa_StopStream( stream );
cannam@140 257 if( err != paNoError ) goto error;
cannam@140 258
cannam@140 259 err = Pa_CloseStream( stream );
cannam@140 260 if( err != paNoError ) goto error;
cannam@140 261
cannam@140 262 Pa_Terminate();
cannam@140 263 printf("Test finished.\n");
cannam@140 264 return err;
cannam@140 265 error:
cannam@140 266 Pa_Terminate();
cannam@140 267 fprintf( stderr, "An error occured while using the portaudio stream\n" );
cannam@140 268 fprintf( stderr, "Error number: %d\n", err );
cannam@140 269 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
cannam@140 270 return err;
cannam@140 271 }