Skip to content

Commit 060a0a6

Browse files
committed
dsound: simplify CalculateBufferSettings()
This commit rewrites CalculateBufferSettings() in an attempt to make it a bit less headache-inducing. The basic idea is to reduce branching, reduce code duplication, and share code paths between the various cases (fixed/variable user buffer, half/full duplex) as much as possible. Also, min()/max() are easier to read than if statements. This is a pure refactoring - there should be no change in observable behaviour. The math stays the same, it is merely reshuffled around in place.
1 parent 68e963a commit 060a0a6

File tree

1 file changed

+18
-52
lines changed

1 file changed

+18
-52
lines changed

src/hostapi/dsound/pa_win_ds.c

Lines changed: 18 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,63 +1750,29 @@ static void CalculateBufferSettings( unsigned long *hostBufferSizeFrames,
17501750
unsigned long maximumPollingPeriodFrames = (unsigned long)(sampleRate * PA_DS_MAXIMUM_POLLING_PERIOD_SECONDS);
17511751
unsigned long pollingJitterFrames = (unsigned long)(sampleRate * PA_DS_POLLING_JITTER_SECONDS);
17521752

1753-
if( userFramesPerBuffer == paFramesPerBufferUnspecified )
1753+
unsigned long adjustedSuggestedOutputLatencyFrames = suggestedOutputLatencyFrames;
1754+
if( userFramesPerBuffer != paFramesPerBufferUnspecified && isFullDuplex )
17541755
{
1755-
unsigned long targetBufferingLatencyFrames = max( suggestedInputLatencyFrames, suggestedOutputLatencyFrames );
1756-
1757-
*pollingPeriodFrames = targetBufferingLatencyFrames / 4;
1758-
if( *pollingPeriodFrames < minimumPollingPeriodFrames )
1759-
{
1760-
*pollingPeriodFrames = minimumPollingPeriodFrames;
1761-
}
1762-
else if( *pollingPeriodFrames > maximumPollingPeriodFrames )
1763-
{
1764-
*pollingPeriodFrames = maximumPollingPeriodFrames;
1765-
}
1766-
1767-
*hostBufferSizeFrames = *pollingPeriodFrames
1768-
+ max( *pollingPeriodFrames + pollingJitterFrames, targetBufferingLatencyFrames);
1756+
/* In full duplex streams we know that the buffer adapter adds userFramesPerBuffer
1757+
extra fixed latency. so we subtract it here as a fixed latency before computing
1758+
the buffer size. being careful not to produce an unrepresentable negative result.
1759+
*/
1760+
adjustedSuggestedOutputLatencyFrames -= min( userFramesPerBuffer, adjustedSuggestedOutputLatencyFrames );
17691761
}
1770-
else
1771-
{
1772-
unsigned long targetBufferingLatencyFrames = suggestedInputLatencyFrames;
1773-
if( isFullDuplex )
1774-
{
1775-
/* In full duplex streams we know that the buffer adapter adds userFramesPerBuffer
1776-
extra fixed latency. so we subtract it here as a fixed latency before computing
1777-
the buffer size. being careful not to produce an unrepresentable negative result.
17781762

1779-
Note: this only works as expected if output latency is greater than input latency.
1780-
Otherwise we use input latency anyway since we do max(in,out).
1781-
*/
1763+
const unsigned long targetBufferingLatencyFrames = max( suggestedInputLatencyFrames, adjustedSuggestedOutputLatencyFrames );
17821764

1783-
if( userFramesPerBuffer < suggestedOutputLatencyFrames )
1784-
{
1785-
unsigned long adjustedSuggestedOutputLatencyFrames =
1786-
suggestedOutputLatencyFrames - userFramesPerBuffer;
1765+
*pollingPeriodFrames = (userFramesPerBuffer == paFramesPerBufferUnspecified) ?
1766+
targetBufferingLatencyFrames / 4 :
1767+
max( max( 1, userFramesPerBuffer / 4 ), targetBufferingLatencyFrames / 16 );
1768+
*pollingPeriodFrames = min( max( *pollingPeriodFrames, minimumPollingPeriodFrames ), maximumPollingPeriodFrames );
17871769

1788-
/* maximum of input and adjusted output suggested latency */
1789-
if( adjustedSuggestedOutputLatencyFrames > targetBufferingLatencyFrames )
1790-
targetBufferingLatencyFrames = adjustedSuggestedOutputLatencyFrames;
1791-
}
1792-
}
1793-
else
1794-
{
1795-
/* maximum of input and output suggested latency */
1796-
if( suggestedOutputLatencyFrames > suggestedInputLatencyFrames )
1797-
targetBufferingLatencyFrames = suggestedOutputLatencyFrames;
1798-
}
1799-
1800-
*hostBufferSizeFrames = userFramesPerBuffer
1801-
+ max( userFramesPerBuffer + pollingJitterFrames, targetBufferingLatencyFrames);
1802-
1803-
*pollingPeriodFrames = max( max(1, userFramesPerBuffer / 4), targetBufferingLatencyFrames / 16 );
1804-
1805-
if( *pollingPeriodFrames > maximumPollingPeriodFrames )
1806-
{
1807-
*pollingPeriodFrames = maximumPollingPeriodFrames;
1808-
}
1809-
}
1770+
const unsigned long intendedUserFramesPerBuffer =
1771+
(userFramesPerBuffer == paFramesPerBufferUnspecified) ?
1772+
*pollingPeriodFrames :
1773+
userFramesPerBuffer;
1774+
*hostBufferSizeFrames = intendedUserFramesPerBuffer
1775+
+ max( intendedUserFramesPerBuffer + pollingJitterFrames, targetBufferingLatencyFrames );
18101776
}
18111777

18121778

0 commit comments

Comments
 (0)