From 6b83b0dbee760766c13b0150384da7342876255d Mon Sep 17 00:00:00 2001 From: Brad Hughes Date: Wed, 17 Aug 2016 23:40:50 -0400 Subject: [PATCH] Minor XAudio2 tweak to not always submit buffers if not needed (nw) --- src/osd/modules/sound/xaudio2_sound.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/osd/modules/sound/xaudio2_sound.cpp b/src/osd/modules/sound/xaudio2_sound.cpp index 3fb02b13d54..f835f690644 100644 --- a/src/osd/modules/sound/xaudio2_sound.cpp +++ b/src/osd/modules/sound/xaudio2_sound.cpp @@ -229,6 +229,7 @@ public: m_overflows(0), m_underflows(0), m_in_underflow(FALSE), + XAudio2Create(nullptr), m_initialized(FALSE) { } @@ -596,12 +597,21 @@ void sound_xaudio2::submit_needed() XAUDIO2_VOICE_STATE state; m_sourceVoice->GetState(&state, XAUDIO2_VOICE_NOSAMPLESPLAYED); - // If we have a too many buffers on the queue, flush to resync - if (state.BuffersQueued > 2) - m_sourceVoice->FlushSourceBuffers(); - std::lock_guard lock(m_buffer_lock); + // If we have buffers queued into XAudio and our current in-memory buffer + // isn't yet full, there's no need to submit it + if (state.BuffersQueued >= 1 && m_queue.empty()) + return; + + // We do however want to achieve some kind of minimal latency, so if the queued buffers + // are greater than 2, flush them to re-sync the audio + if (state.BuffersQueued > 2) + { + m_sourceVoice->FlushSourceBuffers(); + m_overflows++; + } + // Roll the buffer roll_buffer(); @@ -685,7 +695,7 @@ void sound_xaudio2::roll_buffer() m_writepos = 0; // We only want to keep a maximum number of buffers at any given time - // so remove any from queue greater than MAX_QUEUED_BUFFERS + // so remove any from queue greater than our target count if (m_queue.size() > m_buffer_count) { xaudio2_buffer *next_buffer = &m_queue.front();