Chris@55: /** @file patest_suggested_vs_streaminfo_latency.c Chris@55: @ingroup test_src Chris@55: @brief Print suggested vs. PaStreamInfo reported actual latency Chris@55: @author Ross Bencina Chris@55: Chris@55: Opens streams with a sequence of suggested latency values Chris@55: from 0 to 2 seconds in .5ms intervals and gathers the resulting actual Chris@55: latency values. Output a csv file and graph suggested vs. actual. Run Chris@55: with framesPerBuffer unspecified, powers of 2 and multiples of 50 and Chris@55: prime number buffer sizes. Chris@55: */ Chris@55: /* Chris@55: * $Id: patest_sine.c 1368 2008-03-01 00:38:27Z rossb $ Chris@55: * Chris@55: * This program uses the PortAudio Portable Audio Library. Chris@55: * For more information see: http://www.portaudio.com/ Chris@55: * Copyright (c) 1999-2000 Ross Bencina and Phil Burk Chris@55: * Chris@55: * Permission is hereby granted, free of charge, to any person obtaining Chris@55: * a copy of this software and associated documentation files Chris@55: * (the "Software"), to deal in the Software without restriction, Chris@55: * including without limitation the rights to use, copy, modify, merge, Chris@55: * publish, distribute, sublicense, and/or sell copies of the Software, Chris@55: * and to permit persons to whom the Software is furnished to do so, Chris@55: * subject to the following conditions: Chris@55: * Chris@55: * The above copyright notice and this permission notice shall be Chris@55: * included in all copies or substantial portions of the Software. Chris@55: * Chris@55: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, Chris@55: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF Chris@55: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. Chris@55: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR Chris@55: * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF Chris@55: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION Chris@55: * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Chris@55: */ Chris@55: Chris@55: /* Chris@55: * The text above constitutes the entire PortAudio license; however, Chris@55: * the PortAudio community also makes the following non-binding requests: Chris@55: * Chris@55: * Any person wishing to distribute modifications to the Software is Chris@55: * requested to send the modifications to the original developer so that Chris@55: * they can be incorporated into the canonical version. It is also Chris@55: * requested that these non-binding requests be included along with the Chris@55: * license above. Chris@55: */ Chris@55: #include Chris@55: #include Chris@55: #include Chris@55: #include Chris@55: #include "portaudio.h" Chris@55: Chris@55: #define SAMPLE_RATE (44100) Chris@55: #define FRAMES_PER_BUFFER 2//(128) Chris@55: #define NUM_CHANNELS (2) Chris@55: Chris@55: #define SUGGESTED_LATENCY_START_SECONDS (0.0) Chris@55: #define SUGGESTED_LATENCY_END_SECONDS (2.0) Chris@55: #define SUGGESTED_LATENCY_INCREMENT_SECONDS (0.0005) /* half a millisecond increments */ Chris@55: Chris@55: Chris@55: /* dummy callback. does nothing. never gets called */ Chris@55: static int patestCallback( const void *inputBuffer, void *outputBuffer, Chris@55: unsigned long framesPerBuffer, Chris@55: const PaStreamCallbackTimeInfo* timeInfo, Chris@55: PaStreamCallbackFlags statusFlags, Chris@55: void *userData ) Chris@55: { Chris@55: return paContinue; Chris@55: } Chris@55: Chris@55: /*******************************************************************/ Chris@55: static void usage() Chris@55: { Chris@55: int i; Chris@55: const PaDeviceInfo *deviceInfo; Chris@55: const char *channelString; Chris@55: Chris@55: fprintf( stderr, "PortAudio suggested (requested) vs. resulting (reported) stream latency test\n" ); Chris@55: fprintf( stderr, "Usage: x.exe input-device-index output-device-index sample-rate frames-per-buffer\n" ); Chris@55: fprintf( stderr, "Use -1 for default device index, or use one of these:\n" ); Chris@55: for( i=0; i < Pa_GetDeviceCount(); ++i ){ Chris@55: deviceInfo = Pa_GetDeviceInfo(i); Chris@55: if( deviceInfo->maxInputChannels > 0 && deviceInfo->maxOutputChannels > 0 ) Chris@55: channelString = "full-duplex"; Chris@55: else if( deviceInfo->maxInputChannels > 0 ) Chris@55: channelString = "input only"; Chris@55: else Chris@55: channelString = "output only"; Chris@55: Chris@55: fprintf( stderr, "%d (%s, %s, %s)\n", i, deviceInfo->name, Pa_GetHostApiInfo(deviceInfo->hostApi)->name, channelString ); Chris@55: } Chris@55: Pa_Terminate(); Chris@55: exit(-1); Chris@55: } Chris@55: Chris@55: int main( int argc, const char* argv[] ); Chris@55: int main( int argc, const char* argv[] ) Chris@55: { Chris@55: PaStreamParameters inputParameters, outputParameters; Chris@55: PaStream *stream; Chris@55: PaError err; Chris@55: PaTime suggestedLatency; Chris@55: const PaStreamInfo *streamInfo; Chris@55: const PaDeviceInfo *deviceInfo; Chris@55: float sampleRate = SAMPLE_RATE; Chris@55: int framesPerBuffer = FRAMES_PER_BUFFER; Chris@55: err = Pa_Initialize(); Chris@55: if( err != paNoError ) goto error; Chris@55: Chris@55: if( argc > 1 && strcmp(argv[1],"-h") == 0 ) Chris@55: usage(); Chris@55: Chris@55: if( argc > 3 ){ Chris@55: sampleRate = atoi(argv[3]); Chris@55: } Chris@55: Chris@55: if( argc > 4 ){ Chris@55: framesPerBuffer = atoi(argv[4]); Chris@55: } Chris@55: Chris@55: printf("# sample rate=%f, frames per buffer=%d\n", (float)sampleRate, framesPerBuffer ); Chris@55: Chris@55: inputParameters.device = -1; Chris@55: if( argc > 1 ) Chris@55: inputParameters.device = atoi(argv[1]); Chris@55: if( inputParameters.device == -1 ){ Chris@55: inputParameters.device = Pa_GetDefaultInputDevice(); Chris@55: if (inputParameters.device == paNoDevice) { Chris@55: fprintf(stderr,"Error: No default input device available.\n"); Chris@55: goto error; Chris@55: } Chris@55: }else{ Chris@55: deviceInfo = Pa_GetDeviceInfo(inputParameters.device); Chris@55: if( !deviceInfo ){ Chris@55: fprintf(stderr,"Error: Invalid input device index.\n"); Chris@55: usage(); Chris@55: } Chris@55: if( deviceInfo->maxInputChannels == 0 ){ Chris@55: fprintf(stderr,"Error: Specified input device has no input channels (an output only device?).\n"); Chris@55: usage(); Chris@55: } Chris@55: } Chris@55: Chris@55: inputParameters.channelCount = NUM_CHANNELS; Chris@55: inputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */ Chris@55: inputParameters.hostApiSpecificStreamInfo = NULL; Chris@55: Chris@55: deviceInfo = Pa_GetDeviceInfo(inputParameters.device); Chris@55: printf( "# using input device id %d (%s, %s)\n", inputParameters.device, deviceInfo->name, Pa_GetHostApiInfo(deviceInfo->hostApi)->name ); Chris@55: Chris@55: Chris@55: outputParameters.device = -1; Chris@55: if( argc > 2 ) Chris@55: outputParameters.device = atoi(argv[2]); Chris@55: if( outputParameters.device == -1 ){ Chris@55: outputParameters.device = Pa_GetDefaultOutputDevice(); Chris@55: if (outputParameters.device == paNoDevice) { Chris@55: fprintf(stderr,"Error: No default output device available.\n"); Chris@55: goto error; Chris@55: } Chris@55: }else{ Chris@55: deviceInfo = Pa_GetDeviceInfo(outputParameters.device); Chris@55: if( !deviceInfo ){ Chris@55: fprintf(stderr,"Error: Invalid output device index.\n"); Chris@55: usage(); Chris@55: } Chris@55: if( deviceInfo->maxOutputChannels == 0 ){ Chris@55: fprintf(stderr,"Error: Specified output device has no output channels (an input only device?).\n"); Chris@55: usage(); Chris@55: } Chris@55: } Chris@55: Chris@55: outputParameters.channelCount = NUM_CHANNELS; Chris@55: outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */ Chris@55: outputParameters.hostApiSpecificStreamInfo = NULL; Chris@55: Chris@55: deviceInfo = Pa_GetDeviceInfo(outputParameters.device); Chris@55: printf( "# using output device id %d (%s, %s)\n", outputParameters.device, deviceInfo->name, Pa_GetHostApiInfo(deviceInfo->hostApi)->name ); Chris@55: Chris@55: Chris@55: printf( "# suggested latency, half duplex PaStreamInfo::outputLatency, half duplex PaStreamInfo::inputLatency, full duplex PaStreamInfo::outputLatency, full duplex PaStreamInfo::inputLatency\n" ); Chris@55: suggestedLatency = SUGGESTED_LATENCY_START_SECONDS; Chris@55: while( suggestedLatency <= SUGGESTED_LATENCY_END_SECONDS ){ Chris@55: Chris@55: outputParameters.suggestedLatency = suggestedLatency; Chris@55: inputParameters.suggestedLatency = suggestedLatency; Chris@55: Chris@55: printf( "%f, ", suggestedLatency ); Chris@55: Chris@55: /* ------------------------------ output ------------------------------ */ Chris@55: Chris@55: err = Pa_OpenStream( Chris@55: &stream, Chris@55: NULL, /* no input */ Chris@55: &outputParameters, Chris@55: sampleRate, Chris@55: framesPerBuffer, Chris@55: paClipOff, /* we won't output out of range samples so don't bother clipping them */ Chris@55: patestCallback, Chris@55: 0 ); Chris@55: if( err != paNoError ) goto error; Chris@55: Chris@55: streamInfo = Pa_GetStreamInfo( stream ); Chris@55: Chris@55: printf( "%f,", streamInfo->outputLatency ); Chris@55: Chris@55: err = Pa_CloseStream( stream ); Chris@55: if( err != paNoError ) goto error; Chris@55: Chris@55: /* ------------------------------ input ------------------------------ */ Chris@55: Chris@55: err = Pa_OpenStream( Chris@55: &stream, Chris@55: &inputParameters, Chris@55: NULL, /* no output */ Chris@55: sampleRate, Chris@55: framesPerBuffer, Chris@55: paClipOff, /* we won't output out of range samples so don't bother clipping them */ Chris@55: patestCallback, Chris@55: 0 ); Chris@55: if( err != paNoError ) goto error; Chris@55: Chris@55: streamInfo = Pa_GetStreamInfo( stream ); Chris@55: Chris@55: printf( "%f,", streamInfo->inputLatency ); Chris@55: Chris@55: err = Pa_CloseStream( stream ); Chris@55: if( err != paNoError ) goto error; Chris@55: Chris@55: /* ------------------------------ full duplex ------------------------------ */ Chris@55: Chris@55: err = Pa_OpenStream( Chris@55: &stream, Chris@55: &inputParameters, Chris@55: &outputParameters, Chris@55: sampleRate, Chris@55: framesPerBuffer, Chris@55: paClipOff, /* we won't output out of range samples so don't bother clipping them */ Chris@55: patestCallback, Chris@55: 0 ); Chris@55: if( err != paNoError ) goto error; Chris@55: Chris@55: streamInfo = Pa_GetStreamInfo( stream ); Chris@55: Chris@55: printf( "%f,%f", streamInfo->outputLatency, streamInfo->inputLatency ); Chris@55: Chris@55: err = Pa_CloseStream( stream ); Chris@55: if( err != paNoError ) goto error; Chris@55: Chris@55: /* ------------------------------------------------------------ */ Chris@55: Chris@55: printf( "\n" ); Chris@55: suggestedLatency += SUGGESTED_LATENCY_INCREMENT_SECONDS; Chris@55: } Chris@55: Chris@55: Pa_Terminate(); Chris@55: printf("# Test finished.\n"); Chris@55: Chris@55: return err; Chris@55: error: Chris@55: Pa_Terminate(); Chris@55: fprintf( stderr, "An error occured while using the portaudio stream\n" ); Chris@55: fprintf( stderr, "Error number: %d\n", err ); Chris@55: fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); Chris@55: return err; Chris@55: }