Skip to content

Commit

Permalink
dsound: clamp input buffer size at 62.5 ms
Browse files Browse the repository at this point in the history
This works around a DirectSound limitation where input host buffer sizes
smaller than 31.25 ms are basically unworkable and make PortAudio hang.
The workaround is to impose a minimal buffer size of 2*31.25 ms on
input-only and full-duplex streams. This is enough for the read cursor
to advance twice around the buffer, basically resulting in de facto
double buffering.

This change was tested with `paloopback` under a wide variety of
half/full-duplex, framesPerBuffer, and suggested latency parameters.
(Note the testing was done on top of PortAudio#772 as otherwise paloopback is not
usable.)

Fixes PortAudio#775
  • Loading branch information
dechamps committed Mar 7, 2023
1 parent 04031ed commit 0ed250c
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/hostapi/dsound/pa_win_ds.c
Original file line number Diff line number Diff line change
Expand Up @@ -1741,7 +1741,7 @@ static HRESULT InitOutputBuffer( PaWinDsStream *stream, PaWinDsDeviceInfo *devic

static void CalculateBufferSettings( unsigned long *hostBufferSizeFrames,
unsigned long *pollingPeriodFrames,
int isFullDuplex,
int hasInput, int hasOutput,
unsigned long suggestedInputLatencyFrames,
unsigned long suggestedOutputLatencyFrames,
double sampleRate, unsigned long userFramesPerBuffer )
Expand All @@ -1751,7 +1751,7 @@ static void CalculateBufferSettings( unsigned long *hostBufferSizeFrames,
unsigned long pollingJitterFrames = (unsigned long)(sampleRate * PA_DS_POLLING_JITTER_SECONDS);

unsigned long adjustedSuggestedOutputLatencyFrames = suggestedOutputLatencyFrames;
if( userFramesPerBuffer != paFramesPerBufferUnspecified && isFullDuplex )
if( userFramesPerBuffer != paFramesPerBufferUnspecified && hasInput && hasOutput )
{
/* In full duplex streams we know that the buffer adapter adds userFramesPerBuffer
extra fixed latency. so we subtract it here as a fixed latency before computing
Expand All @@ -1773,6 +1773,17 @@ static void CalculateBufferSettings( unsigned long *hostBufferSizeFrames,
userFramesPerBuffer;
*hostBufferSizeFrames = intendedUserFramesPerBuffer
+ max( intendedUserFramesPerBuffer + pollingJitterFrames, targetBufferingLatencyFrames );

/* In some (most?) systems, DirectSound has an odd limitation where it always uses
a fixed 31.25 ms granularity for the read cursor, regardless of parameters.
This in turn means that if we allocate an input buffer that is less than 31.25 ms,
the read cursor will stay stuck at zero. See https://github.com/PortAudio/portaudio/issues/775
To work around this problem, ensure that the input host buffer is large enough
for at least two 31.25 ms buffer "halves".
*/
if ( hasInput ) {
*hostBufferSizeFrames = max( *hostBufferSizeFrames, 2 * 0.03125 * sampleRate );
}
}


Expand Down Expand Up @@ -2108,7 +2119,8 @@ static PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi,
else
{
CalculateBufferSettings( (unsigned long*)&stream->hostBufferSizeFrames, &pollingPeriodFrames,
/* isFullDuplex = */ (inputParameters && outputParameters),
/* hasInput = */ !!inputParameters,
/* hasOutput = */ !!outputParameters,
suggestedInputLatencyFrames,
suggestedOutputLatencyFrames,
sampleRate, framesPerBuffer );
Expand Down

0 comments on commit 0ed250c

Please sign in to comment.