annotate src/portaudio/test/patest_suggested_vs_streaminfo_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
rev   line source
cannam@89 1 /** @file patest_suggested_vs_streaminfo_latency.c
cannam@89 2 @ingroup test_src
cannam@89 3 @brief Print suggested vs. PaStreamInfo reported actual latency
cannam@89 4 @author Ross Bencina <rossb@audiomulch.com>
cannam@89 5
cannam@89 6 Opens streams with a sequence of suggested latency values
cannam@89 7 from 0 to 2 seconds in .5ms intervals and gathers the resulting actual
cannam@89 8 latency values. Output a csv file and graph suggested vs. actual. Run
cannam@89 9 with framesPerBuffer unspecified, powers of 2 and multiples of 50 and
cannam@89 10 prime number buffer sizes.
cannam@89 11 */
cannam@89 12 /*
cannam@89 13 * $Id: patest_sine.c 1368 2008-03-01 00:38:27Z rossb $
cannam@89 14 *
cannam@89 15 * This program uses the PortAudio Portable Audio Library.
cannam@89 16 * For more information see: http://www.portaudio.com/
cannam@89 17 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
cannam@89 18 *
cannam@89 19 * Permission is hereby granted, free of charge, to any person obtaining
cannam@89 20 * a copy of this software and associated documentation files
cannam@89 21 * (the "Software"), to deal in the Software without restriction,
cannam@89 22 * including without limitation the rights to use, copy, modify, merge,
cannam@89 23 * publish, distribute, sublicense, and/or sell copies of the Software,
cannam@89 24 * and to permit persons to whom the Software is furnished to do so,
cannam@89 25 * subject to the following conditions:
cannam@89 26 *
cannam@89 27 * The above copyright notice and this permission notice shall be
cannam@89 28 * included in all copies or substantial portions of the Software.
cannam@89 29 *
cannam@89 30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@89 31 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@89 32 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
cannam@89 33 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
cannam@89 34 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@89 35 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@89 36 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@89 37 */
cannam@89 38
cannam@89 39 /*
cannam@89 40 * The text above constitutes the entire PortAudio license; however,
cannam@89 41 * the PortAudio community also makes the following non-binding requests:
cannam@89 42 *
cannam@89 43 * Any person wishing to distribute modifications to the Software is
cannam@89 44 * requested to send the modifications to the original developer so that
cannam@89 45 * they can be incorporated into the canonical version. It is also
cannam@89 46 * requested that these non-binding requests be included along with the
cannam@89 47 * license above.
cannam@89 48 */
cannam@89 49 #include <stdio.h>
cannam@89 50 #include <stdlib.h>
cannam@89 51 #include <string.h>
cannam@89 52 #include <math.h>
cannam@89 53 #include "portaudio.h"
cannam@89 54
cannam@89 55 #define SAMPLE_RATE (44100)
cannam@89 56 #define FRAMES_PER_BUFFER 2//(128)
cannam@89 57 #define NUM_CHANNELS (2)
cannam@89 58
cannam@89 59 #define SUGGESTED_LATENCY_START_SECONDS (0.0)
cannam@89 60 #define SUGGESTED_LATENCY_END_SECONDS (2.0)
cannam@89 61 #define SUGGESTED_LATENCY_INCREMENT_SECONDS (0.0005) /* half a millisecond increments */
cannam@89 62
cannam@89 63
cannam@89 64 /* dummy callback. does nothing. never gets called */
cannam@89 65 static int patestCallback( const void *inputBuffer, void *outputBuffer,
cannam@89 66 unsigned long framesPerBuffer,
cannam@89 67 const PaStreamCallbackTimeInfo* timeInfo,
cannam@89 68 PaStreamCallbackFlags statusFlags,
cannam@89 69 void *userData )
cannam@89 70 {
cannam@89 71 return paContinue;
cannam@89 72 }
cannam@89 73
cannam@89 74 /*******************************************************************/
cannam@89 75 static void usage()
cannam@89 76 {
cannam@89 77 int i;
cannam@89 78 const PaDeviceInfo *deviceInfo;
cannam@89 79 const char *channelString;
cannam@89 80
cannam@89 81 fprintf( stderr, "PortAudio suggested (requested) vs. resulting (reported) stream latency test\n" );
cannam@89 82 fprintf( stderr, "Usage: x.exe input-device-index output-device-index sample-rate frames-per-buffer\n" );
cannam@89 83 fprintf( stderr, "Use -1 for default device index, or use one of these:\n" );
cannam@89 84 for( i=0; i < Pa_GetDeviceCount(); ++i ){
cannam@89 85 deviceInfo = Pa_GetDeviceInfo(i);
cannam@89 86 if( deviceInfo->maxInputChannels > 0 && deviceInfo->maxOutputChannels > 0 )
cannam@89 87 channelString = "full-duplex";
cannam@89 88 else if( deviceInfo->maxInputChannels > 0 )
cannam@89 89 channelString = "input only";
cannam@89 90 else
cannam@89 91 channelString = "output only";
cannam@89 92
cannam@89 93 fprintf( stderr, "%d (%s, %s, %s)\n", i, deviceInfo->name, Pa_GetHostApiInfo(deviceInfo->hostApi)->name, channelString );
cannam@89 94 }
cannam@89 95 Pa_Terminate();
cannam@89 96 exit(-1);
cannam@89 97 }
cannam@89 98
cannam@89 99 int main( int argc, const char* argv[] );
cannam@89 100 int main( int argc, const char* argv[] )
cannam@89 101 {
cannam@89 102 PaStreamParameters inputParameters, outputParameters;
cannam@89 103 PaStream *stream;
cannam@89 104 PaError err;
cannam@89 105 PaTime suggestedLatency;
cannam@89 106 const PaStreamInfo *streamInfo;
cannam@89 107 const PaDeviceInfo *deviceInfo;
cannam@89 108 float sampleRate = SAMPLE_RATE;
cannam@89 109 int framesPerBuffer = FRAMES_PER_BUFFER;
cannam@89 110 err = Pa_Initialize();
cannam@89 111 if( err != paNoError ) goto error;
cannam@89 112
cannam@89 113 if( argc > 1 && strcmp(argv[1],"-h") == 0 )
cannam@89 114 usage();
cannam@89 115
cannam@89 116 if( argc > 3 ){
cannam@89 117 sampleRate = atoi(argv[3]);
cannam@89 118 }
cannam@89 119
cannam@89 120 if( argc > 4 ){
cannam@89 121 framesPerBuffer = atoi(argv[4]);
cannam@89 122 }
cannam@89 123
cannam@89 124 printf("# sample rate=%f, frames per buffer=%d\n", (float)sampleRate, framesPerBuffer );
cannam@89 125
cannam@89 126 inputParameters.device = -1;
cannam@89 127 if( argc > 1 )
cannam@89 128 inputParameters.device = atoi(argv[1]);
cannam@89 129 if( inputParameters.device == -1 ){
cannam@89 130 inputParameters.device = Pa_GetDefaultInputDevice();
cannam@89 131 if (inputParameters.device == paNoDevice) {
cannam@89 132 fprintf(stderr,"Error: No default input device available.\n");
cannam@89 133 goto error;
cannam@89 134 }
cannam@89 135 }else{
cannam@89 136 deviceInfo = Pa_GetDeviceInfo(inputParameters.device);
cannam@89 137 if( !deviceInfo ){
cannam@89 138 fprintf(stderr,"Error: Invalid input device index.\n");
cannam@89 139 usage();
cannam@89 140 }
cannam@89 141 if( deviceInfo->maxInputChannels == 0 ){
cannam@89 142 fprintf(stderr,"Error: Specified input device has no input channels (an output only device?).\n");
cannam@89 143 usage();
cannam@89 144 }
cannam@89 145 }
cannam@89 146
cannam@89 147 inputParameters.channelCount = NUM_CHANNELS;
cannam@89 148 inputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
cannam@89 149 inputParameters.hostApiSpecificStreamInfo = NULL;
cannam@89 150
cannam@89 151 deviceInfo = Pa_GetDeviceInfo(inputParameters.device);
cannam@89 152 printf( "# using input device id %d (%s, %s)\n", inputParameters.device, deviceInfo->name, Pa_GetHostApiInfo(deviceInfo->hostApi)->name );
cannam@89 153
cannam@89 154
cannam@89 155 outputParameters.device = -1;
cannam@89 156 if( argc > 2 )
cannam@89 157 outputParameters.device = atoi(argv[2]);
cannam@89 158 if( outputParameters.device == -1 ){
cannam@89 159 outputParameters.device = Pa_GetDefaultOutputDevice();
cannam@89 160 if (outputParameters.device == paNoDevice) {
cannam@89 161 fprintf(stderr,"Error: No default output device available.\n");
cannam@89 162 goto error;
cannam@89 163 }
cannam@89 164 }else{
cannam@89 165 deviceInfo = Pa_GetDeviceInfo(outputParameters.device);
cannam@89 166 if( !deviceInfo ){
cannam@89 167 fprintf(stderr,"Error: Invalid output device index.\n");
cannam@89 168 usage();
cannam@89 169 }
cannam@89 170 if( deviceInfo->maxOutputChannels == 0 ){
cannam@89 171 fprintf(stderr,"Error: Specified output device has no output channels (an input only device?).\n");
cannam@89 172 usage();
cannam@89 173 }
cannam@89 174 }
cannam@89 175
cannam@89 176 outputParameters.channelCount = NUM_CHANNELS;
cannam@89 177 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
cannam@89 178 outputParameters.hostApiSpecificStreamInfo = NULL;
cannam@89 179
cannam@89 180 deviceInfo = Pa_GetDeviceInfo(outputParameters.device);
cannam@89 181 printf( "# using output device id %d (%s, %s)\n", outputParameters.device, deviceInfo->name, Pa_GetHostApiInfo(deviceInfo->hostApi)->name );
cannam@89 182
cannam@89 183
cannam@89 184 printf( "# suggested latency, half duplex PaStreamInfo::outputLatency, half duplex PaStreamInfo::inputLatency, full duplex PaStreamInfo::outputLatency, full duplex PaStreamInfo::inputLatency\n" );
cannam@89 185 suggestedLatency = SUGGESTED_LATENCY_START_SECONDS;
cannam@89 186 while( suggestedLatency <= SUGGESTED_LATENCY_END_SECONDS ){
cannam@89 187
cannam@89 188 outputParameters.suggestedLatency = suggestedLatency;
cannam@89 189 inputParameters.suggestedLatency = suggestedLatency;
cannam@89 190
cannam@89 191 printf( "%f, ", suggestedLatency );
cannam@89 192
cannam@89 193 /* ------------------------------ output ------------------------------ */
cannam@89 194
cannam@89 195 err = Pa_OpenStream(
cannam@89 196 &stream,
cannam@89 197 NULL, /* no input */
cannam@89 198 &outputParameters,
cannam@89 199 sampleRate,
cannam@89 200 framesPerBuffer,
cannam@89 201 paClipOff, /* we won't output out of range samples so don't bother clipping them */
cannam@89 202 patestCallback,
cannam@89 203 0 );
cannam@89 204 if( err != paNoError ) goto error;
cannam@89 205
cannam@89 206 streamInfo = Pa_GetStreamInfo( stream );
cannam@89 207
cannam@89 208 printf( "%f,", streamInfo->outputLatency );
cannam@89 209
cannam@89 210 err = Pa_CloseStream( stream );
cannam@89 211 if( err != paNoError ) goto error;
cannam@89 212
cannam@89 213 /* ------------------------------ input ------------------------------ */
cannam@89 214
cannam@89 215 err = Pa_OpenStream(
cannam@89 216 &stream,
cannam@89 217 &inputParameters,
cannam@89 218 NULL, /* no output */
cannam@89 219 sampleRate,
cannam@89 220 framesPerBuffer,
cannam@89 221 paClipOff, /* we won't output out of range samples so don't bother clipping them */
cannam@89 222 patestCallback,
cannam@89 223 0 );
cannam@89 224 if( err != paNoError ) goto error;
cannam@89 225
cannam@89 226 streamInfo = Pa_GetStreamInfo( stream );
cannam@89 227
cannam@89 228 printf( "%f,", streamInfo->inputLatency );
cannam@89 229
cannam@89 230 err = Pa_CloseStream( stream );
cannam@89 231 if( err != paNoError ) goto error;
cannam@89 232
cannam@89 233 /* ------------------------------ full duplex ------------------------------ */
cannam@89 234
cannam@89 235 err = Pa_OpenStream(
cannam@89 236 &stream,
cannam@89 237 &inputParameters,
cannam@89 238 &outputParameters,
cannam@89 239 sampleRate,
cannam@89 240 framesPerBuffer,
cannam@89 241 paClipOff, /* we won't output out of range samples so don't bother clipping them */
cannam@89 242 patestCallback,
cannam@89 243 0 );
cannam@89 244 if( err != paNoError ) goto error;
cannam@89 245
cannam@89 246 streamInfo = Pa_GetStreamInfo( stream );
cannam@89 247
cannam@89 248 printf( "%f,%f", streamInfo->outputLatency, streamInfo->inputLatency );
cannam@89 249
cannam@89 250 err = Pa_CloseStream( stream );
cannam@89 251 if( err != paNoError ) goto error;
cannam@89 252
cannam@89 253 /* ------------------------------------------------------------ */
cannam@89 254
cannam@89 255 printf( "\n" );
cannam@89 256 suggestedLatency += SUGGESTED_LATENCY_INCREMENT_SECONDS;
cannam@89 257 }
cannam@89 258
cannam@89 259 Pa_Terminate();
cannam@89 260 printf("# Test finished.\n");
cannam@89 261
cannam@89 262 return err;
cannam@89 263 error:
cannam@89 264 Pa_Terminate();
cannam@89 265 fprintf( stderr, "An error occured while using the portaudio stream\n" );
cannam@89 266 fprintf( stderr, "Error number: %d\n", err );
cannam@89 267 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
cannam@89 268 return err;
cannam@89 269 }