CoreAudio: if user specifies framesPerBuffer make sure host buffer is an integer multiple of user buffer

This commit is contained in:
rossb 2011-10-20 09:40:12 +00:00
commit f519653f7e

View file

@ -1579,7 +1579,7 @@ static UInt32 CalculateOptimalBufferSize( PaMacAUHAL *auhalHostApi,
double sampleRate, double sampleRate,
UInt32 requestedFramesPerBuffer ) UInt32 requestedFramesPerBuffer )
{ {
UInt32 suggested = 0; UInt32 resultBufferSizeFrames = 0;
// Use maximum of suggested input and output latencies. // Use maximum of suggested input and output latencies.
if( inputParameters ) if( inputParameters )
{ {
@ -1588,28 +1588,25 @@ static UInt32 CalculateOptimalBufferSize( PaMacAUHAL *auhalHostApi,
SInt32 variableLatencyFrames = suggestedLatencyFrames - fixedInputLatency; SInt32 variableLatencyFrames = suggestedLatencyFrames - fixedInputLatency;
// Prevent negative latency. // Prevent negative latency.
variableLatencyFrames = MAX( variableLatencyFrames, 0 ); variableLatencyFrames = MAX( variableLatencyFrames, 0 );
suggested = MAX( suggested, (UInt32) variableLatencyFrames ); resultBufferSizeFrames = MAX( resultBufferSizeFrames, (UInt32) variableLatencyFrames );
} }
if( outputParameters ) if( outputParameters )
{ {
UInt32 suggestedLatencyFrames = outputParameters->suggestedLatency * sampleRate; UInt32 suggestedLatencyFrames = outputParameters->suggestedLatency * sampleRate;
SInt32 variableLatencyFrames = suggestedLatencyFrames - fixedOutputLatency; SInt32 variableLatencyFrames = suggestedLatencyFrames - fixedOutputLatency;
variableLatencyFrames = MAX( variableLatencyFrames, 0 ); variableLatencyFrames = MAX( variableLatencyFrames, 0 );
suggested = MAX( suggested, (UInt32) variableLatencyFrames ); resultBufferSizeFrames = MAX( resultBufferSizeFrames, (UInt32) variableLatencyFrames );
} }
VDBUG( ("Block Size unspecified. Based on Latency, the user wants a Block Size near: %ld.\n",
suggested ) );
if( requestedFramesPerBuffer != paFramesPerBufferUnspecified ) if( requestedFramesPerBuffer != paFramesPerBufferUnspecified )
{ {
if( suggested > (requestedFramesPerBuffer + 1) ) // make host buffer the next highest integer multiple of user frames per buffer
{ UInt32 n = (resultBufferSizeFrames + requestedFramesPerBuffer - 1) / requestedFramesPerBuffer;
// If the user asks for higher latency than the requested buffer size would provide resultBufferSizeFrames = n * requestedFramesPerBuffer;
// then put multiple user buffers in one host buffer.
UInt32 userBuffersPerHostBuffer = (suggested + (requestedFramesPerBuffer - 1)) / requestedFramesPerBuffer; }else{
suggested = userBuffersPerHostBuffer * requestedFramesPerBuffer; VDBUG( ("Block Size unspecified. Based on Latency, the user wants a Block Size near: %ld.\n",
} resultBufferSizeFrames ) );
} }
// Clip to the capabilities of the device. // Clip to the capabilities of the device.
@ -1617,16 +1614,16 @@ static UInt32 CalculateOptimalBufferSize( PaMacAUHAL *auhalHostApi,
{ {
ClipToDeviceBufferSize( auhalHostApi->devIds[inputParameters->device], ClipToDeviceBufferSize( auhalHostApi->devIds[inputParameters->device],
true, // In the old code isInput was false! true, // In the old code isInput was false!
suggested, &suggested ); resultBufferSizeFrames, &resultBufferSizeFrames );
} }
if( outputParameters ) if( outputParameters )
{ {
ClipToDeviceBufferSize( auhalHostApi->devIds[outputParameters->device], ClipToDeviceBufferSize( auhalHostApi->devIds[outputParameters->device],
false, suggested, &suggested ); false, resultBufferSizeFrames, &resultBufferSizeFrames );
} }
VDBUG(("After querying hardware, setting block size to %ld.\n", suggested)); VDBUG(("After querying hardware, setting block size to %ld.\n", resultBufferSizeFrames));
return suggested; return resultBufferSizeFrames;
} }
/* =================================================================================================== */ /* =================================================================================================== */