From 27926022526ea87fa2e6582b9abc5a160e280dc3 Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Thu, 29 Jan 2015 14:58:03 +0100 Subject: [PATCH] Pass the game selection screen audio through the resampler. Fixes #7316 . --- Core/HLE/__sceAudio.cpp | 8 ++++++++ Core/HLE/__sceAudio.h | 3 ++- UI/BackgroundAudio.cpp | 22 +++++++++++++++++----- UI/BackgroundAudio.h | 2 +- UI/NativeApp.cpp | 10 +++++----- 5 files changed, 33 insertions(+), 12 deletions(-) diff --git a/Core/HLE/__sceAudio.cpp b/Core/HLE/__sceAudio.cpp index cb0e7a32fb..6fb5b42d6c 100644 --- a/Core/HLE/__sceAudio.cpp +++ b/Core/HLE/__sceAudio.cpp @@ -377,4 +377,12 @@ int __AudioMix(short *outstereo, int numFrames, int sampleRate) { const AudioDebugStats *__AudioGetDebugStats() { resampler.GetAudioDebugStats(&g_AudioDebugStats); return &g_AudioDebugStats; +} + +void __PushExternalAudio(const s32 *audio, int numSamples) { + if (audio) { + resampler.PushSamples(audio, numSamples); + } else { + resampler.Clear(); + } } \ No newline at end of file diff --git a/Core/HLE/__sceAudio.h b/Core/HLE/__sceAudio.h index 808929bb8b..83948414bd 100644 --- a/Core/HLE/__sceAudio.h +++ b/Core/HLE/__sceAudio.h @@ -43,4 +43,5 @@ void __AudioWakeThreads(AudioChannel &chan, int result, int step); void __AudioWakeThreads(AudioChannel &chan, int result); int __AudioMix(short *outstereo, int numSamples, int sampleRate); -const AudioDebugStats *__AudioGetDebugStats(); \ No newline at end of file +const AudioDebugStats *__AudioGetDebugStats(); +void __PushExternalAudio(const s32 *audio, int numSamples); // Should not be used in-game, only at the menu! diff --git a/UI/BackgroundAudio.cpp b/UI/BackgroundAudio.cpp index 5116b2c7b9..1a02ffe30f 100644 --- a/UI/BackgroundAudio.cpp +++ b/UI/BackgroundAudio.cpp @@ -6,6 +6,7 @@ #include "Common/CommonTypes.h" #include "Core/HW/SimpleAudioDec.h" +#include "Core/HLE/__sceAudio.h" #include "Common/FixedSizeQueue.h" #include "GameInfoCache.h" @@ -120,7 +121,7 @@ public: bool IsOK() { return raw_data_ != 0; } - bool Read(short *buffer, int len) { + bool Read(int *buffer, int len) { if (!raw_data_) return false; while (bgQueue.size() < (size_t)(len * 2)) { @@ -163,6 +164,8 @@ static std::string bgGamePath; static int playbackOffset; static AT3PlusReader *at3Reader; static double gameLastChanged; +static double lastPlaybackTime; +static int buffer[44100]; void SetBackgroundAudioGame(const std::string &path) { time_update(); @@ -183,7 +186,7 @@ void SetBackgroundAudioGame(const std::string &path) { bgGamePath = path; } -int MixBackgroundAudio(short *buffer, int size) { +int PlayBackgroundAudio() { time_update(); lock_guard lock(bgMutex); @@ -198,12 +201,21 @@ int MixBackgroundAudio(short *buffer, int size) { if (gameInfo->sndFileData.size()) { const std::string &data = gameInfo->sndFileData; at3Reader = new AT3PlusReader(data); + lastPlaybackTime = 0.0; } } - if (!at3Reader || !at3Reader->Read(buffer, size)) { - memset(buffer, 0, size * 2 * sizeof(s16)); + double now = time_now(); + if (at3Reader) { + const int sz = (now - lastPlaybackTime) * 44100; + if (sz >= 16 && sz < ARRAY_SIZE(buffer)/2) { + if (at3Reader->Read(buffer, sz)) + __PushExternalAudio(buffer, sz); + } + lastPlaybackTime = now; + } else { + __PushExternalAudio(0, 0); } + return 0; } - diff --git a/UI/BackgroundAudio.h b/UI/BackgroundAudio.h index 7511f34023..34b5dbd04a 100644 --- a/UI/BackgroundAudio.h +++ b/UI/BackgroundAudio.h @@ -3,4 +3,4 @@ #include void SetBackgroundAudioGame(const std::string &path); -int MixBackgroundAudio(short *buffer, int size); +int PlayBackgroundAudio(); diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index 8e321719ef..3f8249cddd 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -232,13 +232,13 @@ std::string NativeQueryConfig(std::string query) { } int NativeMix(short *audio, int num_samples) { - if (GetUIState() == UISTATE_INGAME) { - int sample_rate = System_GetPropertyInt(SYSPROP_AUDIO_SAMPLE_RATE); - num_samples = __AudioMix(audio, num_samples, sample_rate > 0 ? sample_rate : 44100); - } else { - MixBackgroundAudio(audio, num_samples); + if (GetUIState() != UISTATE_INGAME) { + PlayBackgroundAudio(); } + int sample_rate = System_GetPropertyInt(SYSPROP_AUDIO_SAMPLE_RATE); + num_samples = __AudioMix(audio, num_samples, sample_rate > 0 ? sample_rate : 44100); + #ifdef _WIN32 winAudioBackend->Update(); #endif