annotate src/portaudio_20140130/test/patest_suggested_vs_streaminfo_latency.c @ 81:7029a4916348

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