From edac3c99eb2cdf8ed41cfca56d97d7e55d983f06 Mon Sep 17 00:00:00 2001 From: philburk Date: Thu, 21 Jul 2011 18:44:58 +0000 Subject: [PATCH] Fix improper calculation of big buffer sizes. See bug #187 This fixes a click heard with big buffers. --- src/common/pa_process.c | 2 +- src/hostapi/coreaudio/pa_mac_core.c | 1 - src/hostapi/coreaudio/pa_mac_core_utilities.c | 113 +++++++----------- 3 files changed, 47 insertions(+), 69 deletions(-) diff --git a/src/common/pa_process.c b/src/common/pa_process.c index 1f3984d..5b1e519 100644 --- a/src/common/pa_process.c +++ b/src/common/pa_process.c @@ -223,7 +223,7 @@ PaError PaUtil_InitializeBufferProcessor( PaUtilBufferProcessor* bp, goto error; } - /* Under the assumption that no ADC in existence delivers better than 24bits resoultion, + /* Under the assumption that no ADC in existence delivers better than 24bits resolution, we disable dithering when host input format is paInt32 and user format is paInt24, since the host samples will just be padded with zeros anyway. */ diff --git a/src/hostapi/coreaudio/pa_mac_core.c b/src/hostapi/coreaudio/pa_mac_core.c index d21c83b..12e435c 100644 --- a/src/hostapi/coreaudio/pa_mac_core.c +++ b/src/hostapi/coreaudio/pa_mac_core.c @@ -1838,7 +1838,6 @@ static PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi, if( outputParameters ) { - outputLatencyFrames += PaUtil_GetBufferProcessorOutputLatencyFrames(&stream->bufferProcessor); stream->streamRepresentation.streamInfo.outputLatency = outputLatencyFrames / sampleRate; } diff --git a/src/hostapi/coreaudio/pa_mac_core_utilities.c b/src/hostapi/coreaudio/pa_mac_core_utilities.c index bbd169f..c42f902 100644 --- a/src/hostapi/coreaudio/pa_mac_core_utilities.c +++ b/src/hostapi/coreaudio/pa_mac_core_utilities.c @@ -524,84 +524,63 @@ PaError setBestFramesPerBuffer( const AudioDeviceID device, UInt32 requestedFramesPerBuffer, UInt32 *actualFramesPerBuffer ) { - UInt32 afpb; - const bool isInput = !isOutput; - UInt32 propsize = sizeof(UInt32); - OSErr err; - Float64 min = -1; /*the min blocksize*/ - Float64 best = -1; /*the best blocksize*/ - int i=0; - AudioValueRange *ranges; - - if( actualFramesPerBuffer == NULL ) - actualFramesPerBuffer = &afpb; - + UInt32 afpb; + const bool isInput = !isOutput; + UInt32 propsize = sizeof(UInt32); + OSErr err; + Float64 min = -1; /*the min blocksize*/ + Float64 best = -1; /*the best blocksize*/ + int i=0; + AudioValueRange range; + + if( actualFramesPerBuffer == NULL ) + { + actualFramesPerBuffer = &afpb; + } - /* -- try and set exact FPB -- */ - err = AudioDeviceSetProperty( device, NULL, 0, isInput, + /* -- try and set exact FPB -- */ + err = AudioDeviceSetProperty( device, NULL, 0, isInput, kAudioDevicePropertyBufferFrameSize, propsize, &requestedFramesPerBuffer); - err = AudioDeviceGetProperty( device, 0, isInput, + err = AudioDeviceGetProperty( device, 0, isInput, kAudioDevicePropertyBufferFrameSize, &propsize, actualFramesPerBuffer); - if( err ) - return ERR( err ); - if( *actualFramesPerBuffer == requestedFramesPerBuffer ) - return paNoError; /* we are done */ - - /* -- fetch available block sizes -- */ - err = AudioDeviceGetPropertyInfo( device, 0, isInput, - kAudioDevicePropertyBufferSizeRange, - &propsize, NULL ); - if( err ) - return ERR( err ); - ranges = (AudioValueRange *)calloc( 1, propsize ); - if( !ranges ) - return paInsufficientMemory; - err = AudioDeviceGetProperty( device, 0, isInput, - kAudioDevicePropertyBufferSizeRange, - &propsize, ranges ); - if( err ) - { - free( ranges ); + if( err ) + { + return ERR( err ); + } + // Did we get the size we asked for? + if( *actualFramesPerBuffer == requestedFramesPerBuffer ) + { + return paNoError; /* we are done */ + } + + // Clip requested value against legal range for the device. + propsize = sizeof(AudioValueRange); + err = AudioDeviceGetProperty( device, 0, isInput, + kAudioDevicePropertyBufferFrameSizeRange, + &propsize, &range ); + if( err ) + { return ERR( err ); - } - VDBUG(("Requested block size of %lu was not available.\n", - requestedFramesPerBuffer )); - VDBUG(("%lu Available block sizes are:\n",propsize/sizeof(AudioValueRange))); -#ifdef MAC_CORE_VERBOSE_DEBUG - for( i=0; i best ) - best = ranges[i].mMaximum; - } - } - if( best == -1 ) - best = min; - VDBUG( ("Minimum FPB %g. best is %g.\n", min, best ) ); - free( ranges ); - + } + if( requestedFramesPerBuffer < range.mMinimum ) + { + requestedFramesPerBuffer = range.mMinimum; + } + else if( requestedFramesPerBuffer > range.mMaximum ) + { + requestedFramesPerBuffer = range.mMaximum; + } + /* --- set the buffer size (ignore errors) -- */ - requestedFramesPerBuffer = (UInt32) best ; - propsize = sizeof( UInt32 ); + propsize = sizeof( UInt32 ); err = AudioDeviceSetProperty( device, NULL, 0, isInput, - kAudioDevicePropertyBufferSize, + kAudioDevicePropertyBufferFrameSize, propsize, &requestedFramesPerBuffer ); /* --- read the property to check that it was set -- */ err = AudioDeviceGetProperty( device, 0, isInput, - kAudioDevicePropertyBufferSize, + kAudioDevicePropertyBufferFrameSize, &propsize, actualFramesPerBuffer ); if( err ) -- 2.43.0