diff --git a/UI/BackgroundAudio.cpp b/UI/BackgroundAudio.cpp index f5bdd81da4..dc585e6a0d 100644 --- a/UI/BackgroundAudio.cpp +++ b/UI/BackgroundAudio.cpp @@ -11,12 +11,13 @@ #include "Common/FixedSizeQueue.h" #include "GameInfoCache.h" #include "Core/Config.h" +#include "UI/BackgroundAudio.h" // Really simple looping in-memory AT3 player that also takes care of reading the file format. // Turns out that AT3 files used for this are modified WAVE files so fairly easy to parse. class AT3PlusReader { public: - AT3PlusReader(const std::string &data) + explicit AT3PlusReader(const std::string &data) : file_((const uint8_t *)&data[0], (int32_t)data.size()) { // Normally 8k but let's be safe. buffer_ = new short[32 * 1024]; @@ -223,18 +224,9 @@ private: SimpleAudio *decoder_ = nullptr; }; -static std::mutex g_bgMutex; -static std::string bgGamePath; -static int playbackOffset; -static AT3PlusReader *at3Reader; -static double gameLastChanged; -static double lastPlaybackTime; -static int buffer[44100]; -static bool fadingOut = true; -static float volume; -static float delta = -0.0001f; +BackgroundAudio g_BackgroundAudio; -static void ClearBackgroundAudio(bool hard) { +void BackgroundAudio::Clear(bool hard) { if (!hard) { fadingOut = true; volume = 1.0f; @@ -248,7 +240,7 @@ static void ClearBackgroundAudio(bool hard) { playbackOffset = 0; } -void SetBackgroundAudioGame(const std::string &path) { +void BackgroundAudio::SetGame(const std::string &path) { time_update(); std::lock_guard lock(g_bgMutex); @@ -258,10 +250,10 @@ void SetBackgroundAudioGame(const std::string &path) { } if (path.size() == 0) { - ClearBackgroundAudio(false); + Clear(false); fadingOut = true; } else { - ClearBackgroundAudio(true); + Clear(true); gameLastChanged = time_now_d(); fadingOut = false; } @@ -269,14 +261,14 @@ void SetBackgroundAudioGame(const std::string &path) { bgGamePath = path; } -int PlayBackgroundAudio() { +int BackgroundAudio::Play() { time_update(); std::lock_guard lock(g_bgMutex); // Immediately stop the sound if it is turned off while playing. if (!g_Config.bEnableSound) { - ClearBackgroundAudio(true); + Clear(true); __PushExternalAudio(0, 0); return 0; } @@ -297,7 +289,7 @@ int PlayBackgroundAudio() { } __PushExternalAudio(buffer, sz); if (volume <= 0.0f) { - ClearBackgroundAudio(true); + Clear(true); fadingOut = false; gameLastChanged = 0; } @@ -313,9 +305,7 @@ int PlayBackgroundAudio() { return 0; } -// Stuff that should be on the UI thread only, like anything to do with -// g_gameInfoCache. -void UpdateBackgroundAudio() { +void BackgroundAudio::Update() { // If there's a game, and some time has passed since the selected game // last changed... (to prevent crazy amount of reads when skipping through a list) if (bgGamePath.size() && (time_now_d() - gameLastChanged > 0.5)) { diff --git a/UI/BackgroundAudio.h b/UI/BackgroundAudio.h index 5d9ae1e040..04adcb8a25 100644 --- a/UI/BackgroundAudio.h +++ b/UI/BackgroundAudio.h @@ -2,6 +2,25 @@ #include -void SetBackgroundAudioGame(const std::string &path); -int PlayBackgroundAudio(); -void UpdateBackgroundAudio(); +class AT3PlusReader; + +class BackgroundAudio { +public: + void Clear(bool hard); + void SetGame(const std::string &path); + void Update(); + int Play(); +private: + std::mutex g_bgMutex; + std::string bgGamePath; + int playbackOffset = 0; + AT3PlusReader *at3Reader; + double gameLastChanged = 0.0; + double lastPlaybackTime = 0.0; + int buffer[44100]; + bool fadingOut = true; + float volume = 0.0f; + float delta = -0.0001f; +}; + +extern BackgroundAudio g_BackgroundAudio; diff --git a/UI/EmuScreen.cpp b/UI/EmuScreen.cpp index b517d6c206..a2b476fa25 100644 --- a/UI/EmuScreen.cpp +++ b/UI/EmuScreen.cpp @@ -193,7 +193,7 @@ void EmuScreen::bootGame(const std::string &filename) { return; } - SetBackgroundAudioGame(""); + g_BackgroundAudio.SetGame(""); // Check permission status first, in case we came from a shortcut. if (!bootAllowStorage(filename)) diff --git a/UI/GameScreen.cpp b/UI/GameScreen.cpp index 1c900beecd..6b4f0b05e1 100644 --- a/UI/GameScreen.cpp +++ b/UI/GameScreen.cpp @@ -38,7 +38,7 @@ #include "UI/BackgroundAudio.h" GameScreen::GameScreen(const std::string &gamePath) : UIDialogScreenWithGameBackground(gamePath) { - SetBackgroundAudioGame(gamePath); + g_BackgroundAudio.SetGame(gamePath); } GameScreen::~GameScreen() { diff --git a/UI/MainScreen.cpp b/UI/MainScreen.cpp index e9803ac0f6..c444568214 100644 --- a/UI/MainScreen.cpp +++ b/UI/MainScreen.cpp @@ -935,12 +935,12 @@ UI::EventReturn GameBrowser::OnHomebrewStore(UI::EventParams &e) { MainScreen::MainScreen() { System_SendMessage("event", "mainscreen"); - SetBackgroundAudioGame(""); + g_BackgroundAudio.SetGame(""); lastVertical_ = UseVerticalLayout(); } MainScreen::~MainScreen() { - SetBackgroundAudioGame(""); + g_BackgroundAudio.SetGame(""); } void MainScreen::CreateViews() { @@ -1283,7 +1283,7 @@ UI::EventReturn MainScreen::OnGameSelected(UI::EventParams &e) { // Restore focus if it was highlighted (e.g. by gamepad.) restoreFocusGamePath_ = highlightedGamePath_; - SetBackgroundAudioGame(path); + g_BackgroundAudio.SetGame(path); lockBackgroundAudio_ = true; screenManager()->push(new GameScreen(path)); return UI::EVENT_DONE; @@ -1314,7 +1314,7 @@ UI::EventReturn MainScreen::OnGameHighlight(UI::EventParams &e) { } if ((!highlightedGamePath_.empty() || e.a == FF_LOSTFOCUS) && !lockBackgroundAudio_) { - SetBackgroundAudioGame(highlightedGamePath_); + g_BackgroundAudio.SetGame(highlightedGamePath_); } lockBackgroundAudio_ = false; @@ -1401,7 +1401,7 @@ void MainScreen::dialogFinished(const Screen *dialog, DialogResult result) { restoreFocusGamePath_.clear(); } else { // Not refocusing, so we need to stop the audio. - SetBackgroundAudioGame(""); + g_BackgroundAudio.SetGame(""); } } } diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index 1689e333cd..1ee82a3620 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -271,7 +271,7 @@ std::string NativeQueryConfig(std::string query) { int NativeMix(short *audio, int num_samples) { if (GetUIState() != UISTATE_INGAME) { - PlayBackgroundAudio(); + g_BackgroundAudio.Play(); } int sample_rate = System_GetPropertyInt(SYSPROP_AUDIO_SAMPLE_RATE); @@ -1040,7 +1040,7 @@ void NativeRender(GraphicsContext *graphicsContext) { if (GetUIState() != UISTATE_INGAME) { // Note: We do this from NativeRender so that the graphics context is // guaranteed valid, to be safe - g_gameInfoCache messes around with textures. - UpdateBackgroundAudio(); + g_BackgroundAudio.Update(); } float xres = dp_xres;