From 510ec07021c5b7e26db5439c2e18cd93ebf3e230 Mon Sep 17 00:00:00 2001 From: Misa Date: Fri, 2 Apr 2021 12:56:25 -0700 Subject: [PATCH] Re-fix resumemusic/musicfadein once again So it looks like facb079b3597b380f876537523cd351a0e637b62 (PR #316) had a few issues. The SDL performance counter doesn't really work that well. Testing reveals that unfocusing and focusing the game again results in the resumemusic() script command resuming the track at the wrong time. Even when not unfocusing the game at all, stopping a track and resuming it resumes it at the wrong time. (Only disabling the unfocus pause fixes this.) Furthermore, there's also the fact that the SDL performance counter keeps incrementing when the game is paused under GDB. So... yeah. Instead of dealing with the SDL performance counter, I'm just going to pause and resume the music directly (so the stopmusic() script command just pauses the music instead). As a result, we no longer can keep constantly calling Mix_PauseMusic() or Mix_ResumeMusic() when focused or unfocused, so I've moved those calls to happen directly when the relevant SDL events are received (the constant calls were originally in VCE, and whoever added them (I'm pretty sure it was Leo) was not the sharpest tool in the shed...). And we are going to switch over to using our own fade system instead of the SDL mixer fade system. In fact, we were already using our own fade system for fadeins after collecting a trinket or a custom level crewmate, but we were still using the mixer system for the rest. This is an inconsistency that I am glad to correct, so we're also doing our own fadeouts now. There is, however, an issue with the fade system where the length it goes for is inaccurate, because it's based on a volume-per-frame second calculation that gets truncated. But that's an issue to fix later - at least what I'm doing right now makes resumemusic() and musicfadein() work better than before. --- desktop_version/src/KeyPoll.cpp | 15 ++---- desktop_version/src/KeyPoll.h | 2 - desktop_version/src/Music.cpp | 94 +++++++++++++++++++-------------- desktop_version/src/Music.h | 12 ++--- desktop_version/src/Script.cpp | 2 +- desktop_version/src/main.cpp | 6 +-- 6 files changed, 67 insertions(+), 64 deletions(-) diff --git a/desktop_version/src/KeyPoll.cpp b/desktop_version/src/KeyPoll.cpp index 697ca615..b22467b2 100644 --- a/desktop_version/src/KeyPoll.cpp +++ b/desktop_version/src/KeyPoll.cpp @@ -56,8 +56,6 @@ KeyPoll::KeyPoll(void) linealreadyemptykludge = false; - pauseStart = 0; - isActive = true; } @@ -287,6 +285,8 @@ void KeyPoll::Poll(void) if (!game.disablepause) { isActive = true; + music.resume(); + music.resumeef(); } if (!useFullscreenSpaces) { @@ -300,16 +300,13 @@ void KeyPoll::Poll(void) } } SDL_DisableScreenSaver(); - if (!game.disablepause && Mix_PlayingMusic()) - { - // Correct songStart for how long we were paused - music.songStart += SDL_GetPerformanceCounter() - pauseStart; - } break; case SDL_WINDOWEVENT_FOCUS_LOST: if (!game.disablepause) { isActive = false; + music.pause(); + music.pauseef(); } if (!useFullscreenSpaces) { @@ -321,10 +318,6 @@ void KeyPoll::Poll(void) ); } SDL_EnableScreenSaver(); - if (!game.disablepause) - { - pauseStart = SDL_GetPerformanceCounter(); - } break; /* Mouse Focus */ diff --git a/desktop_version/src/KeyPoll.h b/desktop_version/src/KeyPoll.h index 2c5c7532..b8851b46 100644 --- a/desktop_version/src/KeyPoll.h +++ b/desktop_version/src/KeyPoll.h @@ -68,8 +68,6 @@ public: bool linealreadyemptykludge; - Uint64 pauseStart; - private: std::map controllers; std::map buttonmap; diff --git a/desktop_version/src/Music.cpp b/desktop_version/src/Music.cpp index bff2d3ff..e7e24ffd 100644 --- a/desktop_version/src/Music.cpp +++ b/desktop_version/src/Music.cpp @@ -9,26 +9,19 @@ #include "Map.h" #include "UtilityClass.h" -static void songend(void); - musicclass::musicclass(void) { safeToProcessMusic= false; m_doFadeInVol = false; - musicVolume = MIX_MAX_VOLUME; + m_doFadeOutVol = false; + musicVolume = 0; FadeVolAmountPerFrame = 0; currentsong = 0; nicechange = -1; nicefade = false; - resumesong = 0; quick_fade = true; - songStart = 0; - songEnd = 0; - - Mix_HookMusicFinished(&songend); - usingmmmmmm = false; } @@ -142,14 +135,6 @@ void musicclass::init(void) } } -static void songend(void) -{ - extern musicclass music; - music.songEnd = SDL_GetPerformanceCounter(); - music.resumesong = music.currentsong; - music.currentsong = -1; -} - void musicclass::destroy(void) { for (size_t i = 0; i < soundTracks.size(); ++i) @@ -172,7 +157,7 @@ void musicclass::destroy(void) mmmmmm_blob.clear(); } -void musicclass::play(int t, const double position_sec /*= 0.0*/, const int fadein_ms /*= 3000*/) +void musicclass::play(int t) { if (mmmmmm && usingmmmmmm) { @@ -193,9 +178,8 @@ void musicclass::play(int t, const double position_sec /*= 0.0*/, const int fade } safeToProcessMusic = true; - musicVolume = MIX_MAX_VOLUME; - if (currentsong == t && Mix_FadingMusic() != MIX_FADING_OUT) + if (currentsong == t && !m_doFadeOutVol) { return; } @@ -217,14 +201,18 @@ void musicclass::play(int t, const double position_sec /*= 0.0*/, const int fade if (currentsong == 0 || currentsong == 7 || (!map.custommode && (currentsong == 0+num_mmmmmm_tracks || currentsong == 7+num_mmmmmm_tracks))) { // Level Complete theme, no fade in or repeat - if (Mix_FadeInMusicPos(musicTracks[t].m_music, 0, 0, position_sec) == -1) + if (Mix_PlayMusic(musicTracks[t].m_music, 0) == -1) { - printf("Mix_FadeInMusicPos: %s\n", Mix_GetError()); + printf("Mix_PlayMusic: %s\n", Mix_GetError()); + } + else + { + musicVolume = MIX_MAX_VOLUME; } } else { - if (Mix_FadingMusic() == MIX_FADING_OUT) + if (m_doFadeOutVol) { // We're already fading out nicechange = t; @@ -233,35 +221,39 @@ void musicclass::play(int t, const double position_sec /*= 0.0*/, const int fade if (quick_fade) { - Mix_FadeOutMusic(500); // fade out quicker + fadeMusicVolumeOut(500); // fade out quicker } else { quick_fade = true; } } - else if (Mix_FadeInMusicPos(musicTracks[t].m_music, -1, fadein_ms, position_sec) == -1) + else if (Mix_PlayMusic(musicTracks[t].m_music, -1) == -1) { - printf("Mix_FadeInMusicPos: %s\n", Mix_GetError()); + printf("Mix_PlayMusic: %s\n", Mix_GetError()); + } + else + { + fadeMusicVolumeIn(3000); + musicVolume = 0; } } - - songStart = SDL_GetPerformanceCounter(); } -void musicclass::resume(const int fadein_ms /*= 0*/) +void musicclass::resume() { - const double offset = static_cast(songEnd - songStart); - const double frequency = static_cast(SDL_GetPerformanceFrequency()); + Mix_ResumeMusic(); +} - const double position_sec = offset / frequency; - - play(resumesong, position_sec, fadein_ms); +void musicclass::resumefade(const int fadein_ms) +{ + resume(); + fadeMusicVolumeIn(fadein_ms); } void musicclass::fadein(void) { - resume(3000); // 3000 ms fadein + resumefade(3000); // 3000 ms fadein } void musicclass::pause(void) @@ -271,7 +263,8 @@ void musicclass::pause(void) void musicclass::haltdasmusik(void) { - Mix_HaltMusic(); + /* Just pauses music. This is intended. */ + pause(); } void musicclass::silencedasmusik(void) @@ -292,13 +285,20 @@ void musicclass::setfadeamount(const int fade_ms) void musicclass::fadeMusicVolumeIn(int ms) { m_doFadeInVol = true; + m_doFadeOutVol = false; setfadeamount(ms); } +void musicclass::fadeMusicVolumeOut(const int fadeout_ms) +{ + m_doFadeInVol = false; + m_doFadeOutVol = true; + setfadeamount(fadeout_ms); +} + void musicclass::fadeout(const bool quick_fade_ /*= true*/) { - Mix_FadeOutMusic(2000); - resumesong = currentsong; + fadeMusicVolumeOut(2000); quick_fade = quick_fade_; } @@ -311,6 +311,17 @@ void musicclass::processmusicfadein(void) } } +void musicclass::processmusicfadeout(void) +{ + musicVolume -= FadeVolAmountPerFrame; + if (musicVolume < 0) + { + musicVolume = 0; + m_doFadeOutVol = false; + pause(); + } +} + void musicclass::processmusic(void) { if(!safeToProcessMusic) @@ -318,7 +329,7 @@ void musicclass::processmusic(void) return; } - if (nicefade && Mix_PlayingMusic() == 0) + if (nicefade && Mix_PausedMusic() == 1) { play(nicechange); nicechange = -1; @@ -329,6 +340,11 @@ void musicclass::processmusic(void) { processmusicfadein(); } + + if (m_doFadeOutVol) + { + processmusicfadeout(); + } } diff --git a/desktop_version/src/Music.h b/desktop_version/src/Music.h index 28b2914d..a03bcce1 100644 --- a/desktop_version/src/Music.h +++ b/desktop_version/src/Music.h @@ -15,23 +15,25 @@ public: void init(void); void destroy(void); - void play(int t, const double position_sec = 0.0, const int fadein_ms = 3000); - void resume(const int fadein_ms = 0); + void play(int t); + void resume(); + void resumefade(const int fadein_ms); void pause(void); void haltdasmusik(void); void silencedasmusik(void); void setfadeamount(const int fade_ms); void fadeMusicVolumeIn(int ms); + void fadeMusicVolumeOut(const int fadeout_ms); void fadeout(const bool quick_fade_ = true); void fadein(void); void processmusicfadein(void); + void processmusicfadeout(void); void processmusic(void); void niceplay(int t); void changemusicarea(int x, int y); int currentsong; - int resumesong; void playef(int t); void pauseef(void); @@ -46,6 +48,7 @@ public: bool nicefade; bool m_doFadeInVol; + bool m_doFadeOutVol; int FadeVolAmountPerFrame; int musicVolume; @@ -59,9 +62,6 @@ public: binaryBlob mmmmmm_blob; int num_pppppp_tracks; int num_mmmmmm_tracks; - - Uint64 songStart; - Uint64 songEnd; }; #ifndef MUSIC_DEFINITION diff --git a/desktop_version/src/Script.cpp b/desktop_version/src/Script.cpp index 1ee14250..476eae91 100644 --- a/desktop_version/src/Script.cpp +++ b/desktop_version/src/Script.cpp @@ -284,7 +284,7 @@ void scriptclass::run(void) } if (words[0] == "resumemusic") { - music.resume(); + music.resumefade(0); } if (words[0] == "musicfadeout") { diff --git a/desktop_version/src/main.cpp b/desktop_version/src/main.cpp index 2ed8693a..501b6f2e 100644 --- a/desktop_version/src/main.cpp +++ b/desktop_version/src/main.cpp @@ -705,9 +705,6 @@ static enum LoopCode loop_begin(void) static void unfocused_run(void) { - Mix_Pause(-1); - Mix_PauseMusic(); - if (!game.blackout) { ClearSurface(graphics.backBuffer); @@ -726,8 +723,7 @@ static void unfocused_run(void) static void focused_begin(void) { - Mix_Resume(-1); - Mix_ResumeMusic(); + /* no-op. */ } static void focused_end(void)