From dca648c160071caa0ad2af196eab8f747d6e9434 Mon Sep 17 00:00:00 2001 From: Conn O'Griofa Date: Thu, 14 Sep 2017 02:18:52 +0100 Subject: [PATCH 1/5] TextListComponent: rework onScroll function Modify onScroll so that AudioManager is initialized only when playback is necessary. Before this change, ViewController::preload() was initializing audio during startup for any theme that has the scroll sound, even if navigation sounds are disabled in the settings. --- es-app/src/components/TextListComponent.h | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/es-app/src/components/TextListComponent.h b/es-app/src/components/TextListComponent.h index 0aa078ac2..336cbac50 100644 --- a/es-app/src/components/TextListComponent.h +++ b/es-app/src/components/TextListComponent.h @@ -76,13 +76,11 @@ public: inline void setSelectorOffsetY(float selectorOffsetY) { mSelectorOffsetY = selectorOffsetY; } inline void setSelectorColor(unsigned int color) { mSelectorColor = color; } inline void setSelectedColor(unsigned int color) { mSelectedColor = color; } - inline void setScrollSound(const std::shared_ptr& sound) { mScrollSound = sound; } inline void setColor(unsigned int id, unsigned int color) { mColors[id] = color; } - inline void setSound(const std::shared_ptr& sound) { mScrollSound = sound; } inline void setLineSpacing(float lineSpacing) { mLineSpacing = lineSpacing; } protected: - virtual void onScroll(int amt) { if(mScrollSound) mScrollSound->play(); } + virtual void onScroll(int amt) { if(!mScrollSound.empty()) Sound::get(mScrollSound)->play(); } virtual void onCursorChanged(const CursorState& state); private: @@ -105,7 +103,7 @@ private: float mSelectorOffsetY; unsigned int mSelectorColor; unsigned int mSelectedColor; - std::shared_ptr mScrollSound; + std::string mScrollSound; static const unsigned int COLOR_ID_COUNT = 2; unsigned int mColors[COLOR_ID_COUNT]; @@ -354,7 +352,7 @@ void TextListComponent::applyTheme(const std::shared_ptr& theme, c setSelectorHeight(selectorHeight); if(properties & SOUND && elem->has("scrollSound")) - setSound(Sound::get(elem->get("scrollSound"))); + mScrollSound = elem->get("scrollSound"); if(properties & ALIGNMENT) { From c08c24e615fc41f05e3ba437adaa64c5fe19665f Mon Sep 17 00:00:00 2001 From: Conn O'Griofa Date: Thu, 14 Sep 2017 02:26:33 +0100 Subject: [PATCH 2/5] Remove explicit calls to AudioManager::init() and fix deinit() behaviour Ensure that deinit() removes the current sInstance so that the next call to AudioManager::getInstance() will re-initialize audio correctly. Remove explicit calls to AudioManager::init() and instead rely on Sound::play() to initialize audio when needed. --- es-app/src/FileData.cpp | 3 +-- es-core/src/AudioManager.cpp | 1 + es-core/src/Sound.cpp | 2 ++ es-core/src/components/VideoPlayerComponent.cpp | 5 ----- 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/es-app/src/FileData.cpp b/es-app/src/FileData.cpp index 892c701a3..f77756cd8 100644 --- a/es-app/src/FileData.cpp +++ b/es-app/src/FileData.cpp @@ -199,7 +199,6 @@ void FileData::launchGame(Window* window) window->init(); VolumeControl::getInstance()->init(); - AudioManager::getInstance()->init(); window->normalizeNextUpdate(); //update number of times the game has been launched @@ -274,4 +273,4 @@ FileData::SortType getSortTypeFromString(std::string desc) { } // if not found default to name, ascending return FileSorts::SortTypes.at(0); -} \ No newline at end of file +} diff --git a/es-core/src/AudioManager.cpp b/es-core/src/AudioManager.cpp index cbc0821ed..e4e3dce12 100644 --- a/es-core/src/AudioManager.cpp +++ b/es-core/src/AudioManager.cpp @@ -106,6 +106,7 @@ void AudioManager::deinit() //completely tear down SDL audio. else SDL hogs audio resources and emulators might fail to start... SDL_CloseAudio(); SDL_QuitSubSystem(SDL_INIT_AUDIO); + sInstance = NULL; } void AudioManager::registerSound(std::shared_ptr & sound) diff --git a/es-core/src/Sound.cpp b/es-core/src/Sound.cpp index c915ea544..22d17b959 100644 --- a/es-core/src/Sound.cpp +++ b/es-core/src/Sound.cpp @@ -114,6 +114,8 @@ void Sound::play() if(!Settings::getInstance()->getBool("EnableSounds")) return; + AudioManager::getInstance(); + SDL_LockAudio(); if (playing) { diff --git a/es-core/src/components/VideoPlayerComponent.cpp b/es-core/src/components/VideoPlayerComponent.cpp index abeae844c..b1aa98617 100644 --- a/es-core/src/components/VideoPlayerComponent.cpp +++ b/es-core/src/components/VideoPlayerComponent.cpp @@ -191,11 +191,6 @@ void VideoPlayerComponent::stopVideo() int status; kill(mPlayerPid, SIGKILL); waitpid(mPlayerPid, &status, WNOHANG); - // Restart AudioManager - if (boost::starts_with(Settings::getInstance()->getString("OMXAudioDev").c_str(), "alsa")) - { - AudioManager::getInstance()->init(); - } mPlayerPid = -1; } } From 1bfcfb1f16dc4c16acc6335dd3ac498be899042c Mon Sep 17 00:00:00 2001 From: Conn O'Griofa Date: Thu, 14 Sep 2017 02:33:21 +0100 Subject: [PATCH 3/5] PowerSaver: close audio during PS when possible If SDL audio device is paused, deinit audio when PS mode kicks in so that full power savings can be achieved. --- es-core/src/PowerSaver.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/es-core/src/PowerSaver.cpp b/es-core/src/PowerSaver.cpp index cfa648856..a7805ca71 100644 --- a/es-core/src/PowerSaver.cpp +++ b/es-core/src/PowerSaver.cpp @@ -1,4 +1,5 @@ #include "PowerSaver.h" +#include "AudioManager.h" #include "Settings.h" #include @@ -17,6 +18,9 @@ void PowerSaver::init() int PowerSaver::getTimeout() { + if (SDL_GetAudioStatus() == SDL_AUDIO_PAUSED) + AudioManager::getInstance()->deinit(); + // Used only for SDL_WaitEventTimeout. Use `getMode()` for modes. return mRunningScreenSaver ? mWakeupTimeout : mScreenSaverTimeout; } From 35abc91d30b883e432463d59e07805a0814dbac5 Mon Sep 17 00:00:00 2001 From: Conn O'Griofa Date: Thu, 14 Sep 2017 04:45:28 +0100 Subject: [PATCH 4/5] PowerSaver: force-disable sounds for INSTANT profile --- es-app/src/guis/GuiMenu.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/es-app/src/guis/GuiMenu.cpp b/es-app/src/guis/GuiMenu.cpp index 05a782289..76f2f0a26 100644 --- a/es-app/src/guis/GuiMenu.cpp +++ b/es-app/src/guis/GuiMenu.cpp @@ -119,7 +119,16 @@ void GuiMenu::openSoundSettings() auto sounds_enabled = std::make_shared(mWindow); sounds_enabled->setState(Settings::getInstance()->getBool("EnableSounds")); s->addWithLabel("ENABLE NAVIGATION SOUNDS", sounds_enabled); - s->addSaveFunc([sounds_enabled] { Settings::getInstance()->setBool("EnableSounds", sounds_enabled->getState()); }); + s->addSaveFunc([sounds_enabled] { + if (sounds_enabled->getState() + && !Settings::getInstance()->getBool("EnableSounds") + && PowerSaver::getMode() == PowerSaver::INSTANT) + { + Settings::getInstance()->setString("PowerSaverMode", "default"); + PowerSaver::init(); + } + Settings::getInstance()->setBool("EnableSounds", sounds_enabled->getState()); + }); auto video_audio = std::make_shared(mWindow); video_audio->setState(Settings::getInstance()->getBool("VideoAudio")); @@ -300,6 +309,7 @@ void GuiMenu::openOtherSettings() if (Settings::getInstance()->getString("PowerSaverMode") != "instant" && power_saver->getSelected() == "instant") { Settings::getInstance()->setString("TransitionStyle", "instant"); Settings::getInstance()->setBool("MoveCarousel", false); + Settings::getInstance()->setBool("EnableSounds", false); } Settings::getInstance()->setString("PowerSaverMode", power_saver->getSelected()); PowerSaver::init(); From 98c170f8298f8ff2947fc0a1f7677cd05030dbab Mon Sep 17 00:00:00 2001 From: Conn O'Griofa Date: Thu, 14 Sep 2017 07:20:46 +0100 Subject: [PATCH 5/5] AudioManager: don't initialize when unnecessary If navigation sounds are disabled, don't initialize AudioManager unnecessarily. --- es-core/src/AudioManager.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/es-core/src/AudioManager.cpp b/es-core/src/AudioManager.cpp index e4e3dce12..735099bc8 100644 --- a/es-core/src/AudioManager.cpp +++ b/es-core/src/AudioManager.cpp @@ -1,4 +1,5 @@ #include "AudioManager.h" +#include "Settings.h" #include #include "Log.h" @@ -62,7 +63,7 @@ AudioManager::~AudioManager() std::shared_ptr & AudioManager::getInstance() { //check if an AudioManager instance is already created, if not create one - if (sInstance == nullptr) { + if (sInstance == nullptr && Settings::getInstance()->getBool("EnableSounds")) { sInstance = std::shared_ptr(new AudioManager); } return sInstance;