From 1ccdfb873a480c0306ac0553cf9298036f79d2d7 Mon Sep 17 00:00:00 2001 From: Eric Vidal Date: Sun, 14 Jul 2019 19:41:20 +0800 Subject: [PATCH] Suggestion for very-low-latency mode on portaudio - Needed to keep audio in sync when playing rhythm game machines such as konami system 573. This is because the lowest audio_latency currently supported (audio_latency 1) introduces at least 10 ms of variable latency. I am unsure how to go about this without breaking backward compatibility so I set it to work only when audio_latency is 0 (which was previously ignored by mame itself and was treated equivalent to audio_latency 1). I am aware that setting audio_latency to 0 is not supported by many mame frontends, but this change seems natural. Otherwise, would it be better to add a new mame.ini option, or to automatically enable this low-latency mode when pa_latency is set lower than, say, 0.01 (10ms), which would break people's configurations if they already rely on the old behavior? --- src/osd/modules/sound/pa_sound.cpp | 35 +++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/osd/modules/sound/pa_sound.cpp b/src/osd/modules/sound/pa_sound.cpp index ffa6d6bf6f5..41645318672 100644 --- a/src/osd/modules/sound/pa_sound.cpp +++ b/src/osd/modules/sound/pa_sound.cpp @@ -132,7 +132,7 @@ private: enum { - LATENCY_MIN = 1, + LATENCY_MIN = 0, LATENCY_MAX = 5, }; @@ -261,8 +261,16 @@ int sound_pa::init(osd_options const &options) // clamp to a probable figure callback_interval = std::min(callback_interval, 20.0); - // set the best guess callback interval to allowed count, each audio_latency step > 1 adds 20 ms - m_skip_threshold = ((std::max(callback_interval, 10.0) + (m_audio_latency - 1) * 20.0) / 1000.0) * m_sample_rate * 2 + 0.5f; + if (m_audio_latency == 0) + { + // very-low-latency mode (set audio_latency to 0); pa_latency controls allowable audio jitter + m_skip_threshold = (options.pa_latency() ? options.pa_latency() : device_info->defaultLowOutputLatency) * m_sample_rate * 2 + 0.5f; + } + else + { + // set the best guess callback interval to allowed count, each audio_latency step > 1 adds 20 ms + m_skip_threshold = ((std::max(callback_interval, 10.0) + (m_audio_latency - 1) * 20.0) / 1000.0) * m_sample_rate * 2 + 0.5f; + } osd_printf_verbose("PortAudio: Using device \"%s\" on API \"%s\"\n", device_info->name, api_info->name); osd_printf_verbose("PortAudio: Sample rate is %0.0f Hz, device output latency is %0.2f ms\n", @@ -343,13 +351,24 @@ int sound_pa::callback(s16* output_buffer, size_t number_of_samples) // if we have been above the set threshold for ~1 second, skip forward if (m_osd_ticks - m_skip_threshold_ticks > m_osd_tps) { - int adjust = m_buffer_min_ct - m_skip_threshold / 2; - - // if adjustment is less than two milliseconds, don't bother - if (adjust / 2 > sample_rate() / 500) { - m_ab->increment_playpos(adjust); + if (m_audio_latency == 0) + { + // in very-low-latency mode, always skip forward the whole way + // to prevent input from appearing delayed (due to sound cues getting delayed) + m_ab->increment_playpos(m_buffer_min_ct); + //osd_printf_verbose("PortAudio: skip ahead %d samples\n", m_buffer_min_ct); m_has_overflowed = true; } + else + { + int adjust = m_buffer_min_ct - m_skip_threshold / 2; + + // if adjustment is less than two milliseconds, don't bother + if (adjust / 2 > sample_rate() / 500) { + m_ab->increment_playpos(adjust); + m_has_overflowed = true; + } + } m_skip_threshold_ticks = m_osd_ticks; m_buffer_min_ct = INT_MAX;