annotate src/portaudio_20140130/test/patest_sync.c @ 81:7029a4916348

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