From b5ffbfe826b01f7e13eff11e67a7424bc74e0662 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Wed, 13 Jan 2021 02:36:11 +1000 Subject: [PATCH] Qt: Fix audio sliders not applying correctly --- .../app/src/cpp/android_host_interface.cpp | 2 +- src/core/system.cpp | 5 +++ src/core/system.h | 1 + src/duckstation-qt/audiosettingswidget.cpp | 6 ++-- src/duckstation-qt/qthostinterface.cpp | 8 ++--- src/duckstation-sdl/sdl_host_interface.cpp | 2 +- src/frontend-common/common_host_interface.cpp | 35 ++++++++++++------- src/frontend-common/common_host_interface.h | 6 ++-- 8 files changed, 39 insertions(+), 26 deletions(-) diff --git a/android/app/src/cpp/android_host_interface.cpp b/android/app/src/cpp/android_host_interface.cpp index 08547ce20..1d6ac4419 100644 --- a/android/app/src/cpp/android_host_interface.cpp +++ b/android/app/src/cpp/android_host_interface.cpp @@ -445,7 +445,7 @@ void AndroidHostInterface::EmulationThreadLoop(JNIEnv* env) { System::UpdatePerformanceCounters(); - if (m_speed_limiter_enabled) + if (m_throttler_enabled) System::Throttle(); } } diff --git a/src/core/system.cpp b/src/core/system.cpp index a699d5364..d287515b8 100644 --- a/src/core/system.cpp +++ b/src/core/system.cpp @@ -1233,6 +1233,11 @@ void RunFrame() g_gpu->ResetGraphicsAPIState(); } +float GetTargetSpeed() +{ + return s_target_speed; +} + void SetTargetSpeed(float speed) { s_target_speed = speed; diff --git a/src/core/system.h b/src/core/system.h index cc5ad41ae..3e4261979 100644 --- a/src/core/system.h +++ b/src/core/system.h @@ -149,6 +149,7 @@ void SingleStepCPU(); void RunFrame(); /// Sets target emulation speed. +float GetTargetSpeed(); void SetTargetSpeed(float speed); /// Adjusts the throttle frequency, i.e. how many times we should sleep per second. diff --git a/src/duckstation-qt/audiosettingswidget.cpp b/src/duckstation-qt/audiosettingswidget.cpp index c377b9cf4..6b450737d 100644 --- a/src/duckstation-qt/audiosettingswidget.cpp +++ b/src/duckstation-qt/audiosettingswidget.cpp @@ -99,8 +99,7 @@ void AudioSettingsWidget::updateVolumeLabel() void AudioSettingsWidget::onOutputVolumeChanged(int new_value) { m_host_interface->SetIntSettingValue("Audio", "OutputVolume", new_value); - if (!m_ui.muted->isChecked() && !QtHostInterface::GetInstance()->IsFastForwardEnabled()) - m_host_interface->setAudioOutputVolume(new_value, m_ui.fastForwardVolume->value()); + m_host_interface->setAudioOutputVolume(new_value, m_ui.fastForwardVolume->value()); updateVolumeLabel(); } @@ -108,8 +107,7 @@ void AudioSettingsWidget::onOutputVolumeChanged(int new_value) void AudioSettingsWidget::onFastForwardVolumeChanged(int new_value) { m_host_interface->SetIntSettingValue("Audio", "FastForwardVolume", new_value); - if (!m_ui.muted->isChecked() && QtHostInterface::GetInstance()->IsFastForwardEnabled()) - m_host_interface->setAudioOutputVolume(m_ui.volume->value(), new_value); + m_host_interface->setAudioOutputVolume(m_ui.volume->value(), new_value); updateVolumeLabel(); } diff --git a/src/duckstation-qt/qthostinterface.cpp b/src/duckstation-qt/qthostinterface.cpp index 5ec81811d..c3487e63b 100644 --- a/src/duckstation-qt/qthostinterface.cpp +++ b/src/duckstation-qt/qthostinterface.cpp @@ -1226,11 +1226,11 @@ void QtHostInterface::setAudioOutputVolume(int volume, int fast_forward_volume) return; } - if (m_audio_stream) - m_audio_stream->SetOutputVolume(m_speed_limiter_enabled ? volume : fast_forward_volume); - g_settings.audio_output_volume = volume; g_settings.audio_fast_forward_volume = fast_forward_volume; + + if (m_audio_stream) + m_audio_stream->SetOutputVolume(GetAudioOutputVolume()); } void QtHostInterface::setAudioOutputMuted(bool muted) @@ -1400,7 +1400,7 @@ void QtHostInterface::threadEntryPoint() System::UpdatePerformanceCounters(); - if (m_speed_limiter_enabled) + if (m_throttler_enabled) System::Throttle(); m_worker_thread_event_loop->processEvents(QEventLoop::AllEvents); diff --git a/src/duckstation-sdl/sdl_host_interface.cpp b/src/duckstation-sdl/sdl_host_interface.cpp index 5de776675..64522151a 100644 --- a/src/duckstation-sdl/sdl_host_interface.cpp +++ b/src/duckstation-sdl/sdl_host_interface.cpp @@ -1877,7 +1877,7 @@ void SDLHostInterface::Run() { System::UpdatePerformanceCounters(); - if (m_speed_limiter_enabled) + if (m_throttler_enabled) System::Throttle(); } } diff --git a/src/frontend-common/common_host_interface.cpp b/src/frontend-common/common_host_interface.cpp index 212a0f4fe..2940566f4 100644 --- a/src/frontend-common/common_host_interface.cpp +++ b/src/frontend-common/common_host_interface.cpp @@ -475,7 +475,7 @@ std::unique_ptr CommonHostInterface::CreateAudioStream(AudioBackend s32 CommonHostInterface::GetAudioOutputVolume() const { - return g_settings.GetAudioOutputVolume(!m_speed_limiter_enabled); + return g_settings.GetAudioOutputVolume(IsRunningAtNonStandardSpeed()); } void CommonHostInterface::UpdateControllerInterface() @@ -602,12 +602,21 @@ bool CommonHostInterface::ResumeSystemFromMostRecentState() return LoadState(path.c_str()); } +bool CommonHostInterface::IsRunningAtNonStandardSpeed() const +{ + if (!System::IsValid()) + return false; + + const float target_speed = System::GetTargetSpeed(); + return (target_speed <= 0.95f || target_speed >= 1.05f); +} + void CommonHostInterface::UpdateSpeedLimiterState() { float target_speed = m_turbo_enabled ? g_settings.turbo_speed : (m_fast_forward_enabled ? g_settings.fast_forward_speed : g_settings.emulation_speed); - m_speed_limiter_enabled = (target_speed != 0.0f); + m_throttler_enabled = (target_speed != 0.0f); bool syncing_to_host = false; if (g_settings.sync_to_host_refresh_rate && g_settings.audio_resampling && target_speed == 1.0f && @@ -627,15 +636,21 @@ void CommonHostInterface::UpdateSpeedLimiterState() const bool is_non_standard_speed = (std::abs(target_speed - 1.0f) > 0.05f); const bool audio_sync_enabled = - !System::IsRunning() || (m_speed_limiter_enabled && g_settings.audio_sync_enabled && !is_non_standard_speed); + !System::IsRunning() || (m_throttler_enabled && g_settings.audio_sync_enabled && !is_non_standard_speed); const bool video_sync_enabled = - !System::IsRunning() || (m_speed_limiter_enabled && g_settings.video_sync_enabled && !is_non_standard_speed); - const float max_display_fps = m_speed_limiter_enabled ? 0.0f : g_settings.display_max_fps; + !System::IsRunning() || (m_throttler_enabled && g_settings.video_sync_enabled && !is_non_standard_speed); + const float max_display_fps = m_throttler_enabled ? 0.0f : g_settings.display_max_fps; Log_InfoPrintf("Target speed: %f%%", target_speed * 100.0f); Log_InfoPrintf("Syncing to %s%s", audio_sync_enabled ? "audio" : "", (audio_sync_enabled && video_sync_enabled) ? " and video" : (video_sync_enabled ? "video" : "")); Log_InfoPrintf("Max display fps: %f", max_display_fps); + if (System::IsValid()) + { + System::SetTargetSpeed(target_speed); + System::ResetPerformanceCounters(); + } + if (m_audio_stream) { const u32 input_sample_rate = (target_speed == 0.0f || !g_settings.audio_resampling) ? @@ -657,16 +672,10 @@ void CommonHostInterface::UpdateSpeedLimiterState() } if (g_settings.increase_timer_resolution) - SetTimerResolutionIncreased(m_speed_limiter_enabled); - - if (System::IsValid()) - { - System::SetTargetSpeed(m_speed_limiter_enabled ? target_speed : 1.0f); - System::ResetPerformanceCounters(); - } + SetTimerResolutionIncreased(m_throttler_enabled); if (syncing_to_host) - m_speed_limiter_enabled = false; + m_throttler_enabled = false; } void CommonHostInterface::RecreateSystem() diff --git a/src/frontend-common/common_host_interface.h b/src/frontend-common/common_host_interface.h index 40b57ebb3..671b3cb7e 100644 --- a/src/frontend-common/common_host_interface.h +++ b/src/frontend-common/common_host_interface.h @@ -183,8 +183,8 @@ public: /// Parses a fullscreen mode into its components (width * height @ refresh hz) static bool ParseFullscreenMode(const std::string_view& mode, u32* width, u32* height, float* refresh_rate); - /// Returns true if fast forwarding is currently active. - bool IsFastForwardEnabled() const { return m_fast_forward_enabled; } + /// Returns true if fast forwarding or slow motion is currently active. + bool IsRunningAtNonStandardSpeed() const; /// Requests the specified size for the render window. Not guaranteed to succeed (e.g. if in fullscreen). virtual bool RequestRenderWindowSize(s32 new_window_width, s32 new_window_height); @@ -351,7 +351,7 @@ protected: bool m_fast_forward_enabled = false; bool m_turbo_enabled = false; bool m_timer_resolution_increased = false; - bool m_speed_limiter_enabled = true; + bool m_throttler_enabled = true; private: void InitializeUserDirectory();