Debian Codegraph @

3rdparty/iaxclient/lib/portaudio/test/patest_sync.c

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