comparison src/portaudio/qa/paqa_latency.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
comparison
equal deleted inserted replaced
88:fe7c3a0b0259 89:8a15ff55d9af
1 /** @file paqa_latency.c
2 @ingroup qa_src
3 @brief Test latency estimates.
4 @author Ross Bencina <rossb@audiomulch.com>
5 @author Phil Burk <philburk@softsynth.com>
6 */
7 /*
8 * $Id: patest_sine.c 1368 2008-03-01 00:38:27Z rossb $
9 *
10 * This program uses the PortAudio Portable Audio Library.
11 * For more information see: http://www.portaudio.com/
12 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining
15 * a copy of this software and associated documentation files
16 * (the "Software"), to deal in the Software without restriction,
17 * including without limitation the rights to use, copy, modify, merge,
18 * publish, distribute, sublicense, and/or sell copies of the Software,
19 * and to permit persons to whom the Software is furnished to do so,
20 * subject to the following conditions:
21 *
22 * The above copyright notice and this permission notice shall be
23 * included in all copies or substantial portions of the Software.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
28 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
29 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
30 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 */
33
34 /*
35 * The text above constitutes the entire PortAudio license; however,
36 * the PortAudio community also makes the following non-binding requests:
37 *
38 * Any person wishing to distribute modifications to the Software is
39 * requested to send the modifications to the original developer so that
40 * they can be incorporated into the canonical version. It is also
41 * requested that these non-binding requests be included along with the
42 * license above.
43 */
44 #include <stdio.h>
45 #include <math.h>
46 #include "portaudio.h"
47 #include "loopback/src/qa_tools.h"
48
49 #define NUM_SECONDS (5)
50 #define SAMPLE_RATE (44100)
51 #define FRAMES_PER_BUFFER (64)
52
53 #ifndef M_PI
54 #define M_PI (3.14159265)
55 #endif
56
57 #define TABLE_SIZE (200)
58 typedef struct
59 {
60 float sine[TABLE_SIZE];
61 int left_phase;
62 int right_phase;
63 char message[20];
64 int minFramesPerBuffer;
65 int maxFramesPerBuffer;
66 int callbackCount;
67 PaTime minDeltaDacTime;
68 PaTime maxDeltaDacTime;
69 PaStreamCallbackTimeInfo previousTimeInfo;
70 }
71 paTestData;
72
73 /* Used to tally the results of the QA tests. */
74 int g_testsPassed = 0;
75 int g_testsFailed = 0;
76
77 /* This routine will be called by the PortAudio engine when audio is needed.
78 ** It may called at interrupt level on some machines so don't do anything
79 ** that could mess up the system like calling malloc() or free().
80 */
81 static int patestCallback( const void *inputBuffer, void *outputBuffer,
82 unsigned long framesPerBuffer,
83 const PaStreamCallbackTimeInfo* timeInfo,
84 PaStreamCallbackFlags statusFlags,
85 void *userData )
86 {
87 paTestData *data = (paTestData*)userData;
88 float *out = (float*)outputBuffer;
89 unsigned long i;
90
91 (void) timeInfo; /* Prevent unused variable warnings. */
92 (void) statusFlags;
93 (void) inputBuffer;
94
95 if( data->minFramesPerBuffer > framesPerBuffer )
96 {
97 data->minFramesPerBuffer = framesPerBuffer;
98 }
99 if( data->maxFramesPerBuffer < framesPerBuffer )
100 {
101 data->maxFramesPerBuffer = framesPerBuffer;
102 }
103
104 /* Measure min and max output time stamp delta. */
105 if( data->callbackCount > 0 )
106 {
107 PaTime delta = timeInfo->outputBufferDacTime - data->previousTimeInfo.outputBufferDacTime;
108 if( data->minDeltaDacTime > delta )
109 {
110 data->minDeltaDacTime = delta;
111 }
112 if( data->maxDeltaDacTime < delta )
113 {
114 data->maxDeltaDacTime = delta;
115 }
116 }
117 data->previousTimeInfo = *timeInfo;
118
119 for( i=0; i<framesPerBuffer; i++ )
120 {
121 *out++ = data->sine[data->left_phase]; /* left */
122 *out++ = data->sine[data->right_phase]; /* right */
123 data->left_phase += 1;
124 if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
125 data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
126 if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
127 }
128
129 data->callbackCount += 1;
130 return paContinue;
131 }
132
133 PaError paqaCheckLatency( PaStreamParameters *outputParamsPtr,
134 paTestData *dataPtr, double sampleRate, unsigned long framesPerBuffer )
135 {
136 PaError err;
137 PaStream *stream;
138 const PaStreamInfo* streamInfo;
139
140 dataPtr->minFramesPerBuffer = 9999999;
141 dataPtr->maxFramesPerBuffer = 0;
142 dataPtr->minDeltaDacTime = 9999999.0;
143 dataPtr->maxDeltaDacTime = 0.0;
144 dataPtr->callbackCount = 0;
145
146 printf("Stream parameter: suggestedOutputLatency = %g\n", outputParamsPtr->suggestedLatency );
147 if( framesPerBuffer == paFramesPerBufferUnspecified ){
148 printf("Stream parameter: user framesPerBuffer = paFramesPerBufferUnspecified\n" );
149 }else{
150 printf("Stream parameter: user framesPerBuffer = %lu\n", framesPerBuffer );
151 }
152 err = Pa_OpenStream(
153 &stream,
154 NULL, /* no input */
155 outputParamsPtr,
156 sampleRate,
157 framesPerBuffer,
158 paClipOff, /* we won't output out of range samples so don't bother clipping them */
159 patestCallback,
160 dataPtr );
161 if( err != paNoError ) goto error1;
162
163 streamInfo = Pa_GetStreamInfo( stream );
164 printf("Stream info: inputLatency = %g\n", streamInfo->inputLatency );
165 printf("Stream info: outputLatency = %g\n", streamInfo->outputLatency );
166
167 err = Pa_StartStream( stream );
168 if( err != paNoError ) goto error2;
169
170 printf("Play for %d seconds.\n", NUM_SECONDS );
171 Pa_Sleep( NUM_SECONDS * 1000 );
172
173 printf(" minFramesPerBuffer = %4d\n", dataPtr->minFramesPerBuffer );
174 printf(" maxFramesPerBuffer = %4d\n", dataPtr->maxFramesPerBuffer );
175 printf(" minDeltaDacTime = %f\n", dataPtr->minDeltaDacTime );
176 printf(" maxDeltaDacTime = %f\n", dataPtr->maxDeltaDacTime );
177
178 err = Pa_StopStream( stream );
179 if( err != paNoError ) goto error2;
180
181 err = Pa_CloseStream( stream );
182 Pa_Sleep( 1 * 1000 );
183
184
185 printf("-------------------------------------\n");
186 return err;
187 error2:
188 Pa_CloseStream( stream );
189 error1:
190 printf("-------------------------------------\n");
191 return err;
192 }
193
194
195 /*******************************************************************/
196 static int paqaNoopCallback( const void *inputBuffer, void *outputBuffer,
197 unsigned long framesPerBuffer,
198 const PaStreamCallbackTimeInfo* timeInfo,
199 PaStreamCallbackFlags statusFlags,
200 void *userData )
201 {
202 (void)inputBuffer;
203 (void)outputBuffer;
204 (void)framesPerBuffer;
205 (void)timeInfo;
206 (void)statusFlags;
207 (void)userData;
208 return paContinue;
209 }
210
211 /*******************************************************************/
212 static int paqaCheckMultipleSuggested( PaDeviceIndex deviceIndex, int isInput )
213 {
214 int i;
215 int numLoops = 10;
216 PaError err;
217 PaStream *stream;
218 PaStreamParameters streamParameters;
219 const PaStreamInfo* streamInfo;
220 double lowLatency;
221 double highLatency;
222 double finalLatency;
223 double sampleRate = 44100.0;
224 const PaDeviceInfo *pdi = Pa_GetDeviceInfo( deviceIndex );
225 double previousLatency = 0.0;
226 int numChannels = 1;
227
228 printf("------------------------ paqaCheckMultipleSuggested - %s\n",
229 (isInput ? "INPUT" : "OUTPUT") );
230 if( isInput )
231 {
232 lowLatency = pdi->defaultLowInputLatency;
233 highLatency = pdi->defaultHighInputLatency;
234 numChannels = (pdi->maxInputChannels < 2) ? 1 : 2;
235 }
236 else
237 {
238 lowLatency = pdi->defaultLowOutputLatency;
239 highLatency = pdi->defaultHighOutputLatency;
240 numChannels = (pdi->maxOutputChannels < 2) ? 1 : 2;
241 }
242 streamParameters.channelCount = numChannels;
243 streamParameters.device = deviceIndex;
244 streamParameters.hostApiSpecificStreamInfo = NULL;
245 streamParameters.sampleFormat = paFloat32;
246
247 printf(" lowLatency = %g\n", lowLatency );
248 printf(" highLatency = %g\n", highLatency );
249 printf(" numChannels = %d\n", numChannels );
250
251 if( (highLatency - lowLatency) < 0.001 )
252 {
253 numLoops = 1;
254 }
255
256 for( i=0; i<numLoops; i++ )
257 {
258 streamParameters.suggestedLatency = lowLatency + ((highLatency - lowLatency) * i /(numLoops - 1.0));
259
260 printf(" suggestedLatency[%d] = %6.4f\n", i, streamParameters.suggestedLatency );
261
262 err = Pa_OpenStream(
263 &stream,
264 (isInput ? &streamParameters : NULL),
265 (isInput ? NULL : &streamParameters),
266 sampleRate,
267 paFramesPerBufferUnspecified,
268 paClipOff, /* we won't output out of range samples so don't bother clipping them */
269 paqaNoopCallback,
270 NULL );
271 if( err != paNoError ) goto error;
272
273 streamInfo = Pa_GetStreamInfo( stream );
274
275 err = Pa_CloseStream( stream );
276
277 if( isInput )
278 {
279 finalLatency = streamInfo->inputLatency;
280 }
281 else
282 {
283 finalLatency = streamInfo->outputLatency;
284 }
285 printf(" finalLatency = %6.4f\n", finalLatency );
286 QA_ASSERT_CLOSE( "final latency should be close to suggested latency",
287 streamParameters.suggestedLatency, finalLatency, (streamParameters.suggestedLatency * 0.3) );
288
289 QA_ASSERT_TRUE( " final latency should increase with suggested latency", (finalLatency > previousLatency) );
290 previousLatency = finalLatency;
291 }
292
293 return 0;
294 error:
295 return -1;
296 }
297
298 /*******************************************************************/
299 static int paqaVerifySuggestedLatency( void )
300 {
301 PaDeviceIndex id;
302 int result = 0;
303 const PaDeviceInfo *pdi;
304 int numDevices = Pa_GetDeviceCount();
305
306 printf("\n ------------------------ paqaVerifySuggestedLatency\n");
307 for( id=0; id<numDevices; id++ ) /* Iterate through all devices. */
308 {
309 pdi = Pa_GetDeviceInfo( id );
310 printf("Using device #%d: '%s' (%s)\n", id, pdi->name, Pa_GetHostApiInfo(pdi->hostApi)->name);
311 if( pdi->maxOutputChannels > 0 )
312 {
313 if( (result = paqaCheckMultipleSuggested( id, 0 )) != 0 ) goto error;
314 }
315 if( pdi->maxInputChannels > 0 )
316 {
317 if( (result = paqaCheckMultipleSuggested( id, 1 )) != 0 ) goto error;
318 }
319 }
320 return 0;
321 error:
322 return -1;
323 }
324
325 /*******************************************************************/
326 static int paqaVerifyDeviceInfoLatency( void )
327 {
328 PaDeviceIndex id;
329 const PaDeviceInfo *pdi;
330 int numDevices = Pa_GetDeviceCount();
331
332 printf("\n ------------------------ paqaVerifyDeviceInfoLatency\n");
333 for( id=0; id<numDevices; id++ ) /* Iterate through all devices. */
334 {
335 pdi = Pa_GetDeviceInfo( id );
336 printf("Using device #%d: '%s' (%s)\n", id, pdi->name, Pa_GetHostApiInfo(pdi->hostApi)->name);
337 if( pdi->maxOutputChannels > 0 )
338 {
339 printf(" Output defaultLowOutputLatency = %f seconds\n", pdi->defaultLowOutputLatency);
340 printf(" Output info: defaultHighOutputLatency = %f seconds\n", pdi->defaultHighOutputLatency);
341 QA_ASSERT_TRUE( "defaultLowOutputLatency should be > 0", (pdi->defaultLowOutputLatency > 0.0) );
342 QA_ASSERT_TRUE( "defaultHighOutputLatency should be > 0", (pdi->defaultHighOutputLatency > 0.0) );
343 //QA_ASSERT_TRUE( "defaultHighOutputLatency should be > Low", (pdi->defaultHighOutputLatency > pdi->defaultLowOutputLatency) );
344 }
345 if( pdi->maxInputChannels > 0 )
346 {
347 printf(" Input defaultLowOutputLatency = %f seconds\n", pdi->defaultLowInputLatency);
348 printf(" Input defaultHighOutputLatency = %f seconds\n", pdi->defaultHighInputLatency);
349 QA_ASSERT_TRUE( "defaultLowOutputLatency should be > 0", (pdi->defaultLowInputLatency > 0.0) );
350 QA_ASSERT_TRUE( "defaultHighOutputLatency should be > 0", (pdi->defaultHighInputLatency > 0.0) );
351 //QA_ASSERT_TRUE( "defaultHighOutputLatency should be > Low", (pdi->defaultHighInputLatency > pdi->defaultLowInputLatency) );
352 }
353 }
354 return 0;
355 error:
356 return -1;
357 }
358
359
360
361 /*******************************************************************/
362 int main(void);
363 int main(void)
364 {
365 PaStreamParameters outputParameters;
366 PaError err;
367 paTestData data;
368 const PaDeviceInfo *deviceInfo;
369 int i;
370 int framesPerBuffer;
371 double sampleRate = 44100;
372
373 printf("PortAudio QA: investigate output latency. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
374
375 /* initialise sinusoidal wavetable */
376 for( i=0; i<TABLE_SIZE; i++ )
377 {
378 data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
379 }
380 data.left_phase = data.right_phase = 0;
381
382 err = Pa_Initialize();
383 if( err != paNoError ) goto error;
384
385 /* Run self tests. */
386 if( paqaVerifyDeviceInfoLatency() < 0 ) goto error;
387
388 if( paqaVerifySuggestedLatency() < 0 ) goto error;
389
390 outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
391 if (outputParameters.device == paNoDevice) {
392 fprintf(stderr,"Error: No default output device.\n");
393 goto error;
394 }
395
396 outputParameters.channelCount = 2; /* stereo output */
397 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
398 deviceInfo = Pa_GetDeviceInfo( outputParameters.device );
399 printf("Using device #%d: '%s' (%s)\n", outputParameters.device, deviceInfo->name, Pa_GetHostApiInfo(deviceInfo->hostApi)->name);
400 printf("Device info: defaultLowOutputLatency = %f seconds\n", deviceInfo->defaultLowOutputLatency);
401 printf("Device info: defaultHighOutputLatency = %f seconds\n", deviceInfo->defaultHighOutputLatency);
402 outputParameters.hostApiSpecificStreamInfo = NULL;
403
404 // Try to use a small buffer that is smaller than we think the device can handle.
405 // Try to force combining multiple user buffers into a host buffer.
406 printf("------------- Try a very small buffer.\n");
407 framesPerBuffer = 9;
408 outputParameters.suggestedLatency = deviceInfo->defaultLowOutputLatency;
409 err = paqaCheckLatency( &outputParameters, &data, sampleRate, framesPerBuffer );
410 if( err != paNoError ) goto error;
411
412 printf("------------- 64 frame buffer with 1.1 * defaultLow latency.\n");
413 framesPerBuffer = 64;
414 outputParameters.suggestedLatency = deviceInfo->defaultLowOutputLatency * 1.1;
415 err = paqaCheckLatency( &outputParameters, &data, sampleRate, framesPerBuffer );
416 if( err != paNoError ) goto error;
417
418 // Try to create a huge buffer that is bigger than the allowed device maximum.
419 printf("------------- Try a huge buffer.\n");
420 framesPerBuffer = 16*1024;
421 outputParameters.suggestedLatency = ((double)framesPerBuffer) / sampleRate; // approximate
422 err = paqaCheckLatency( &outputParameters, &data, sampleRate, framesPerBuffer );
423 if( err != paNoError ) goto error;
424
425 printf("------------- Try suggestedLatency = 0.0\n");
426 outputParameters.suggestedLatency = 0.0;
427 err = paqaCheckLatency( &outputParameters, &data, sampleRate, paFramesPerBufferUnspecified );
428 if( err != paNoError ) goto error;
429
430 printf("------------- Try suggestedLatency = defaultLowOutputLatency\n");
431 outputParameters.suggestedLatency = deviceInfo->defaultLowOutputLatency;
432 err = paqaCheckLatency( &outputParameters, &data, sampleRate, paFramesPerBufferUnspecified );
433 if( err != paNoError ) goto error;
434
435 printf("------------- Try suggestedLatency = defaultHighOutputLatency\n");
436 outputParameters.suggestedLatency = deviceInfo->defaultHighOutputLatency;
437 err = paqaCheckLatency( &outputParameters, &data, sampleRate, paFramesPerBufferUnspecified );
438 if( err != paNoError ) goto error;
439
440 printf("------------- Try suggestedLatency = defaultHighOutputLatency * 4\n");
441 outputParameters.suggestedLatency = deviceInfo->defaultHighOutputLatency * 4;
442 err = paqaCheckLatency( &outputParameters, &data, sampleRate, paFramesPerBufferUnspecified );
443 if( err != paNoError ) goto error;
444
445
446 Pa_Terminate();
447 printf("Test finished.\n");
448
449 return err;
450 error:
451 Pa_Terminate();
452 fprintf( stderr, "An error occured while using the portaudio stream\n" );
453 fprintf( stderr, "Error number: %d\n", err );
454 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
455 return err;
456 }