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