diff --git a/UnleashedRecomp/CMakeLists.txt b/UnleashedRecomp/CMakeLists.txt index 0d8f561..0aa0bbd 100644 --- a/UnleashedRecomp/CMakeLists.txt +++ b/UnleashedRecomp/CMakeLists.txt @@ -67,12 +67,14 @@ set(SWA_KERNEL_CXX_SOURCES ) set(SWA_OS_CXX_SOURCES + "os/media.cpp" "os/process.cpp" "os/version.cpp" ) if (WIN32) list(APPEND SWA_OS_CXX_SOURCES + "os/win32/media_win32.cpp" "os/win32/process_win32.cpp" "os/win32/version_win32.cpp" ) diff --git a/UnleashedRecomp/os/media.cpp b/UnleashedRecomp/os/media.cpp new file mode 100644 index 0000000..40d2b97 --- /dev/null +++ b/UnleashedRecomp/os/media.cpp @@ -0,0 +1,7 @@ +#include +#include + +bool os::media::IsExternalMediaPlaying() +{ + return detail::IsExternalMediaPlaying(); +} diff --git a/UnleashedRecomp/os/media.h b/UnleashedRecomp/os/media.h new file mode 100644 index 0000000..02ec355 --- /dev/null +++ b/UnleashedRecomp/os/media.h @@ -0,0 +1,6 @@ +#pragma once + +namespace os::media +{ + bool IsExternalMediaPlaying(); +} diff --git a/UnleashedRecomp/os/media_detail.h b/UnleashedRecomp/os/media_detail.h new file mode 100644 index 0000000..9f66096 --- /dev/null +++ b/UnleashedRecomp/os/media_detail.h @@ -0,0 +1,6 @@ +#pragma once + +namespace os::media::detail +{ + bool IsExternalMediaPlaying(); +} diff --git a/UnleashedRecomp/os/win32/media_win32.cpp b/UnleashedRecomp/os/win32/media_win32.cpp new file mode 100644 index 0000000..377e044 --- /dev/null +++ b/UnleashedRecomp/os/win32/media_win32.cpp @@ -0,0 +1,72 @@ +#include +#include +#include + +using namespace winrt; +using namespace winrt::Windows::Foundation; +using namespace winrt::Windows::Media::Control; + +static GlobalSystemMediaTransportControlsSessionManager g_sessionManager = nullptr; + +static GlobalSystemMediaTransportControlsSessionManager GetSessionManager() +{ + if (g_sessionManager) + return g_sessionManager; + + try + { + init_apartment(); + return g_sessionManager = GlobalSystemMediaTransportControlsSessionManager::RequestAsync().get(); + } + catch (...) + { + printf("[*] Failed to retrieve GSMTC session manager: 0x%08X\n", to_hresult().value); + return nullptr; + } +} + +static GlobalSystemMediaTransportControlsSession GetCurrentSession() +{ + auto sessionManager = GetSessionManager(); + + if (!sessionManager) + return nullptr; + + try + { + return sessionManager.GetCurrentSession(); + } + catch (...) + { + printf("[*] Failed to retrieve current GSMTC session: 0x%08X\n", to_hresult().value); + return nullptr; + } +} + +static GlobalSystemMediaTransportControlsSessionPlaybackInfo GetPlaybackInfo() +{ + auto session = GetCurrentSession(); + + if (!session) + return nullptr; + + try + { + return session.GetPlaybackInfo(); + } + catch (...) + { + printf("[*] Failed to retrieve GSMTC playback info: 0x%08X\n", to_hresult().value); + return nullptr; + } +} + +bool os::media::detail::IsExternalMediaPlaying() +{ + auto playbackInfo = GetPlaybackInfo(); + + if (!playbackInfo) + return false; + + return playbackInfo.PlaybackStatus() == GlobalSystemMediaTransportControlsSessionPlaybackStatus::Playing; +} diff --git a/UnleashedRecomp/patches/audio_patches.cpp b/UnleashedRecomp/patches/audio_patches.cpp index f0b69fc..2d8754c 100644 --- a/UnleashedRecomp/patches/audio_patches.cpp +++ b/UnleashedRecomp/patches/audio_patches.cpp @@ -1,85 +1,12 @@ #include #include #include +#include #include #include #include -#if _WIN32 -#include -#include - -using namespace winrt; -using namespace winrt::Windows::Foundation; -using namespace winrt::Windows::Media::Control; - -static GlobalSystemMediaTransportControlsSessionManager g_sessionManager = nullptr; - -static GlobalSystemMediaTransportControlsSessionManager GetSessionManager() -{ - if (g_sessionManager) - return g_sessionManager; - - try - { - init_apartment(); - return g_sessionManager = GlobalSystemMediaTransportControlsSessionManager::RequestAsync().get(); - } - catch (...) - { - printf("[*] Failed to retrieve GSMTC session manager: 0x%08X\n", to_hresult().value); - return nullptr; - } -} - -static GlobalSystemMediaTransportControlsSession GetCurrentSession() -{ - auto sessionManager = GetSessionManager(); - - if (!sessionManager) - return nullptr; - - try - { - return sessionManager.GetCurrentSession(); - } - catch (...) - { - printf("[*] Failed to retrieve current GSMTC session: 0x%08X\n", to_hresult().value); - return nullptr; - } -} - -static GlobalSystemMediaTransportControlsSessionPlaybackInfo GetPlaybackInfo() -{ - auto session = GetCurrentSession(); - - if (!session) - return nullptr; - - try - { - return session.GetPlaybackInfo(); - } - catch (...) - { - printf("[*] Failed to retrieve GSMTC playback info: 0x%08X\n", to_hresult().value); - return nullptr; - } -} - -static bool IsExternalAudioPlaying() -{ - auto playbackInfo = GetPlaybackInfo(); - - if (!playbackInfo) - return false; - - return playbackInfo.PlaybackStatus() == GlobalSystemMediaTransportControlsSessionPlaybackStatus::Playing; -} - int AudioPatches::m_isAttenuationSupported = -1; -#endif static be* GetVolume(bool isMusic = true) { @@ -116,12 +43,11 @@ void AudioPatches::Update(float deltaTime) if (!pMusicVolume || !pEffectsVolume) return; -#if _WIN32 if (Config::MusicAttenuation && CanAttenuate()) { auto time = 1.0f - expf(2.5f * -deltaTime); - if (IsExternalAudioPlaying()) + if (os::media::IsExternalMediaPlaying()) { *pMusicVolume = std::lerp(*pMusicVolume, 0.0f, time); } @@ -131,7 +57,6 @@ void AudioPatches::Update(float deltaTime) } } else -#endif { *pMusicVolume = Config::MusicVolume; }