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