annotate src/portaudio_20140130/test/patest_suggested_vs_streaminfo_latency.c @ 167:bd3cc4d1df30

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