diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d765c1bc..e8c5f4de5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -243,6 +243,7 @@ Feature #4882: Support for NiPalette node Feature #4887: Add openmw command option to set initial random seed Feature #4890: Make Distant Terrain configurable + Feature #4944: Pause audio when OpenMW is minimized Feature #4958: Support eight blood types Feature #4962: Add casting animations for magic items Feature #4968: Scalable UI widget skins diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp index 435db0841..ec8c1e305 100644 --- a/apps/openmw/engine.cpp +++ b/apps/openmw/engine.cpp @@ -96,7 +96,12 @@ bool OMW::Engine::frame(float frametime) // If we are not currently rendering, then RenderItems will not be reused resulting in a memory leak upon changing widget textures (fixed in MyGUI 3.3.2), // and destroyed widgets will not be deleted (not fixed yet, https://github.com/MyGUI/mygui/issues/21) if (!mEnvironment.getInputManager()->isWindowVisible()) + { + mEnvironment.getSoundManager()->pausePlayback(); return false; + } + else + mEnvironment.getSoundManager()->resumePlayback(); // sound if (mUseSound) diff --git a/apps/openmw/mwbase/soundmanager.hpp b/apps/openmw/mwbase/soundmanager.hpp index a780e5ccd..ac3e05ac7 100644 --- a/apps/openmw/mwbase/soundmanager.hpp +++ b/apps/openmw/mwbase/soundmanager.hpp @@ -174,6 +174,9 @@ namespace MWBase virtual void resumeSounds(const std::string& blockerId) = 0; ///< Resumes all previously paused sounds. + virtual void pausePlayback() = 0; + virtual void resumePlayback() = 0; + virtual void update(float duration) = 0; virtual void setListenerPosDir(const osg::Vec3f &pos, const osg::Vec3f &dir, const osg::Vec3f &up, bool underwater) = 0; diff --git a/apps/openmw/mwgui/videowidget.cpp b/apps/openmw/mwgui/videowidget.cpp index 5dec6e48a..2aea0018d 100644 --- a/apps/openmw/mwgui/videowidget.cpp +++ b/apps/openmw/mwgui/videowidget.cpp @@ -76,6 +76,21 @@ void VideoWidget::stop() mPlayer->close(); } +void VideoWidget::pause() +{ + mPlayer->pause(); +} + +void VideoWidget::resume() +{ + mPlayer->play(); +} + +bool VideoWidget::isPaused() const +{ + return mPlayer->isPaused(); +} + bool VideoWidget::hasAudioStream() { return mPlayer->hasAudioStream(); diff --git a/apps/openmw/mwgui/videowidget.hpp b/apps/openmw/mwgui/videowidget.hpp index dadd1471a..814b9ca73 100644 --- a/apps/openmw/mwgui/videowidget.hpp +++ b/apps/openmw/mwgui/videowidget.hpp @@ -47,6 +47,10 @@ namespace MWGui /// Stop video and free resources (done automatically on destruction) void stop(); + void pause(); + void resume(); + bool isPaused() const; + /// Adjust the coordinates of this video widget relative to its parent, /// based on the dimensions of the playing video. /// @param stretch Stretch the video to fill the whole screen? If false, diff --git a/apps/openmw/mwgui/windowmanagerimp.cpp b/apps/openmw/mwgui/windowmanagerimp.cpp index ac2d5f27f..c6bc06f8f 100644 --- a/apps/openmw/mwgui/windowmanagerimp.cpp +++ b/apps/openmw/mwgui/windowmanagerimp.cpp @@ -1905,9 +1905,15 @@ namespace MWGui MWBase::Environment::get().getInputManager()->update(dt, true, false); if (!MWBase::Environment::get().getInputManager()->isWindowVisible()) + { + mVideoWidget->pause(); OpenThreads::Thread::microSleep(5000); + } else { + if (mVideoWidget->isPaused()) + mVideoWidget->resume(); + mViewer->eventTraversal(); mViewer->updateTraversal(); mViewer->renderingTraversals(); diff --git a/apps/openmw/mwsound/openal_output.cpp b/apps/openmw/mwsound/openal_output.cpp index 9c87cac19..595518726 100644 --- a/apps/openmw/mwsound/openal_output.cpp +++ b/apps/openmw/mwsound/openal_output.cpp @@ -1454,6 +1454,38 @@ void OpenAL_Output::pauseSounds(int types) } } +void OpenAL_Output::pauseActiveDevice() +{ + if (mDevice == nullptr) + return; + + if(alcIsExtensionPresent(mDevice, "ALC_SOFT_PAUSE_DEVICE")) + { + LPALCDEVICEPAUSESOFT alcDevicePauseSOFT = 0; + getALCFunc(alcDevicePauseSOFT, mDevice, "alcDevicePauseSOFT"); + alcDevicePauseSOFT(mDevice); + getALCError(mDevice); + } + + alListenerf(AL_GAIN, 0.0f); +} + +void OpenAL_Output::resumeActiveDevice() +{ + if (mDevice == nullptr) + return; + + if(alcIsExtensionPresent(mDevice, "ALC_SOFT_PAUSE_DEVICE")) + { + LPALCDEVICERESUMESOFT alcDeviceResumeSOFT = 0; + getALCFunc(alcDeviceResumeSOFT, mDevice, "alcDeviceResumeSOFT"); + alcDeviceResumeSOFT(mDevice); + getALCError(mDevice); + } + + alListenerf(AL_GAIN, 1.0f); +} + void OpenAL_Output::resumeSounds(int types) { std::vector sources; diff --git a/apps/openmw/mwsound/openal_output.hpp b/apps/openmw/mwsound/openal_output.hpp index d91320ab4..6039d97d6 100644 --- a/apps/openmw/mwsound/openal_output.hpp +++ b/apps/openmw/mwsound/openal_output.hpp @@ -92,6 +92,9 @@ namespace MWSound virtual void pauseSounds(int types); virtual void resumeSounds(int types); + virtual void pauseActiveDevice(); + virtual void resumeActiveDevice(); + OpenAL_Output(SoundManager &mgr); virtual ~OpenAL_Output(); }; diff --git a/apps/openmw/mwsound/sound_output.hpp b/apps/openmw/mwsound/sound_output.hpp index eb830c8d0..4075e36cc 100644 --- a/apps/openmw/mwsound/sound_output.hpp +++ b/apps/openmw/mwsound/sound_output.hpp @@ -62,6 +62,9 @@ namespace MWSound virtual void pauseSounds(int types) = 0; virtual void resumeSounds(int types) = 0; + virtual void pauseActiveDevice() = 0; + virtual void resumeActiveDevice() = 0; + Sound_Output& operator=(const Sound_Output &rhs); Sound_Output(const Sound_Output &rhs); diff --git a/apps/openmw/mwsound/soundmanagerimp.cpp b/apps/openmw/mwsound/soundmanagerimp.cpp index d8d59e243..8462f0929 100644 --- a/apps/openmw/mwsound/soundmanagerimp.cpp +++ b/apps/openmw/mwsound/soundmanagerimp.cpp @@ -52,6 +52,7 @@ namespace MWSound , mListenerUp(0,0,1) , mUnderwaterSound(nullptr) , mNearWaterSound(nullptr) + , mPlaybackPaused(false) { mMasterVolume = Settings::Manager::getFloat("master volume", "Sound"); mMasterVolume = std::min(std::max(mMasterVolume, 0.0f), 1.0f); @@ -856,7 +857,6 @@ namespace MWSound return false; } - void SoundManager::pauseSounds(const std::string& blockerId, int types) { if(mOutput->isInitialized()) @@ -887,6 +887,24 @@ namespace MWSound } } + void SoundManager::pausePlayback() + { + if (mPlaybackPaused) + return; + + mPlaybackPaused = true; + mOutput->pauseActiveDevice(); + } + + void SoundManager::resumePlayback() + { + if (!mPlaybackPaused) + return; + + mPlaybackPaused = false; + mOutput->resumeActiveDevice(); + } + void SoundManager::updateRegionSound(float duration) { static float sTimeToNextEnvSound = 0.0f; @@ -1209,7 +1227,7 @@ namespace MWSound void SoundManager::update(float duration) { - if(!mOutput->isInitialized()) + if(!mOutput->isInitialized() || mPlaybackPaused) return; updateSounds(duration); @@ -1408,5 +1426,6 @@ namespace MWSound } mActiveTracks.clear(); mPausedSoundTypes.clear(); + mPlaybackPaused = false; } } diff --git a/apps/openmw/mwsound/soundmanagerimp.hpp b/apps/openmw/mwsound/soundmanagerimp.hpp index a8ebfeca3..b2ccc702a 100644 --- a/apps/openmw/mwsound/soundmanagerimp.hpp +++ b/apps/openmw/mwsound/soundmanagerimp.hpp @@ -112,6 +112,9 @@ namespace MWSound Sound *mUnderwaterSound; Sound *mNearWaterSound; + std::string mNextMusic; + bool mPlaybackPaused; + Sound_Buffer *insertSound(const std::string &soundId, const ESM::Sound *sound); Sound_Buffer *lookupSound(const std::string &soundId) const; @@ -134,8 +137,6 @@ namespace MWSound void updateWaterSound(float duration); void updateMusic(float duration); - std::string mNextMusic; - float volumeFromType(Type type) const; SoundManager(const SoundManager &rhs); @@ -250,6 +251,9 @@ namespace MWSound virtual void resumeSounds(const std::string& blockerId); ///< Resumes all previously paused sounds. + virtual void pausePlayback(); + virtual void resumePlayback(); + virtual void update(float duration); virtual void setListenerPosDir(const osg::Vec3f &pos, const osg::Vec3f &dir, const osg::Vec3f &up, bool underwater);