annotate src/portaudio/test/patest_sync.c @ 89:8a15ff55d9af

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