Improve the main loop for DirectSound by making better use of periods.

This commit is contained in:
David Reid
2016-10-21 08:08:30 +10:00
parent 3180cadb4a
commit d984e39e1b
+6 -1
View File
@@ -1789,6 +1789,12 @@ static mal_uint32 mal_device__wait_for_frames__dsound(mal_device* pDevice)
{ {
mal_assert(pDevice != NULL); mal_assert(pDevice != NULL);
// The timeout to use for putting the thread to sleep is based on the size of the buffer and the period count.
DWORD timeoutInMilliseconds = (pDevice->bufferSizeInFrames / (pDevice->sampleRate/1000)) / pDevice->periods;
if (timeoutInMilliseconds < 1) {
timeoutInMilliseconds = 1;
}
while (!pDevice->dsound.breakFromMainLoop) { while (!pDevice->dsound.breakFromMainLoop) {
mal_uint32 framesAvailable = mal_device__get_available_frames__dsound(pDevice); mal_uint32 framesAvailable = mal_device__get_available_frames__dsound(pDevice);
if (framesAvailable > 0) { if (framesAvailable > 0) {
@@ -1796,7 +1802,6 @@ static mal_uint32 mal_device__wait_for_frames__dsound(mal_device* pDevice)
} }
// If we get here it means we weren't able to find any frames. We'll just wait here for a bit. // If we get here it means we weren't able to find any frames. We'll just wait here for a bit.
const DWORD timeoutInMilliseconds = 4; // <-- This affects latency. Should this be a property? Tie this to the period count?
DWORD rc = WaitForMultipleObjects(1, &pDevice->dsound.hStopEvent, FALSE, timeoutInMilliseconds); DWORD rc = WaitForMultipleObjects(1, &pDevice->dsound.hStopEvent, FALSE, timeoutInMilliseconds);
if (rc == WAIT_OBJECT_0) { if (rc == WAIT_OBJECT_0) {
return 0; return 0;