annotate src/portaudio_20161030_catalina_patch/test/patest_sync.c @ 83:ae30d91d2ffe

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