From 66611c5d2f509e2b9669a9847d7950995c216cc3 Mon Sep 17 00:00:00 2001 From: EliteMasterEric Date: Wed, 6 Dec 2023 22:03:36 -0500 Subject: [PATCH] Fixes to FlxSound when pausing before starting --- source/funkin/audio/FunkinSound.hx | 66 +++++++++++++++++++++++------ source/funkin/data/song/SongData.hx | 1 + source/funkin/play/PlayState.hx | 52 +++-------------------- source/funkin/play/song/Song.hx | 6 ++- 4 files changed, 66 insertions(+), 59 deletions(-) diff --git a/source/funkin/audio/FunkinSound.hx b/source/funkin/audio/FunkinSound.hx index 88925f92d..40293b0ce 100644 --- a/source/funkin/audio/FunkinSound.hx +++ b/source/funkin/audio/FunkinSound.hx @@ -21,10 +21,22 @@ class FunkinSound extends FlxSound { static var cache(default, null):FlxTypedGroup = new FlxTypedGroup(); + public var isPlaying(get, never):Bool; + + function get_isPlaying():Bool + { + return this.playing || this._shouldPlay; + } + /** * Are we in a state where the song should play but time is negative? */ - var shouldPlay:Bool = false; + var _shouldPlay:Bool = false; + + /** + * For debug purposes. + */ + var _label:String = "unknown"; public function new() { @@ -33,7 +45,7 @@ class FunkinSound extends FlxSound public override function update(elapsedSec:Float) { - if (!playing && !shouldPlay) return; + if (!playing && !_shouldPlay) return; if (_time < 0) { @@ -41,9 +53,8 @@ class FunkinSound extends FlxSound _time += elapsedMs; if (_time >= 0) { - _time = 0; - shouldPlay = false; super.play(); + _shouldPlay = false; } } else @@ -60,29 +71,52 @@ class FunkinSound extends FlxSound { cleanup(false, true); } - else if (playing || shouldPlay) + else if (playing) { return this; } if (startTime < 0) { - shouldPlay = true; - _time = startTime; + this.active = true; + this._shouldPlay = true; + this._time = startTime; this.endTime = endTime; return this; } - - if (_paused) + else { - resume(); + if (_paused) + { + resume(); + } + else + { + startSound(startTime); + } + + this.endTime = endTime; + return this; + } + } + + public override function pause():FunkinSound + { + super.pause(); + this._shouldPlay = false; + return this; + } + + public override function resume():FunkinSound + { + if (this._time < 0) + { + this._shouldPlay = true; } else { - startSound(startTime); + super.resume(); } - - this.endTime = endTime; return this; } @@ -107,8 +141,14 @@ class FunkinSound extends FlxSound sound.loadEmbedded(embeddedSound, looped, autoDestroy, onComplete); + if (embeddedSound is String) + { + sound._label = embeddedSound; + } + sound.volume = volume; sound.group = FlxG.sound.defaultSoundGroup; + sound.persist = true; if (autoPlay) sound.play(); // Call OnlLoad() because the sound already loaded diff --git a/source/funkin/data/song/SongData.hx b/source/funkin/data/song/SongData.hx index c0fd96855..7886ada4f 100644 --- a/source/funkin/data/song/SongData.hx +++ b/source/funkin/data/song/SongData.hx @@ -90,6 +90,7 @@ class SongMetadata result.version = this.version; result.timeFormat = this.timeFormat; result.divisions = this.divisions; + result.offsets = this.offsets; result.timeChanges = this.timeChanges; result.looped = this.looped; result.playData = this.playData; diff --git a/source/funkin/play/PlayState.hx b/source/funkin/play/PlayState.hx index 6136cf1b7..bd4b0f5e1 100644 --- a/source/funkin/play/PlayState.hx +++ b/source/funkin/play/PlayState.hx @@ -326,12 +326,6 @@ class PlayState extends MusicBeatSubState */ var overrideMusic:Bool = false; - /** - * After the song starts, the song offset may dictate we wait before the instrumental starts. - * This variable represents that delay, and is subtracted from until it reaches 0, before calling `music.play()` - */ - var songStartDelay:Float = 0.0; - /** * Forcibly disables all update logic while the game moves back to the Menu state. * This is used only when a critical error occurs and the game absolutely cannot continue. @@ -781,26 +775,7 @@ class PlayState extends MusicBeatSubState Conductor.formatOffset = 0.0; } - if (songStartDelay > 0) - { - // Code to handle the song not starting yet (positive instrumental offset in metadata). - // Wait for offset to elapse before actually hitting play on the instrumental. - songStartDelay -= elapsed * 1000; - if (songStartDelay <= 0) - { - FlxG.sound.music.play(); - Conductor.update(); // Normal conductor update. - } - else - { - // Make beat events still happen. - Conductor.update(Conductor.songPosition + elapsed * 1000); - } - } - else - { - Conductor.update(); // Normal conductor update. - } + Conductor.update(); // Normal conductor update. if (!isGamePaused) { @@ -1046,7 +1021,7 @@ class PlayState extends MusicBeatSubState if (event.eventCanceled) return; // Resume - FlxG.sound.music.play(); + FlxG.sound.music.play(FlxG.sound.music.time); if (FlxG.sound.music != null && !startingSong && !isInCutscene) resyncVocals(); @@ -1730,21 +1705,9 @@ class PlayState extends MusicBeatSubState trace('Playing vocals...'); add(vocals); - if (FlxG.sound.music.time < 0) - { - // A positive instrumentalOffset means Conductor.songPosition will still be negative after the countdown elapses. - trace('POSITIVE OFFSET: Waiting to start song...'); - songStartDelay = Math.abs(FlxG.sound.music.time); - FlxG.sound.music.time = 0; - FlxG.sound.music.pause(); - vocals.pause(); - } - else - { - FlxG.sound.music.play(); - vocals.play(); - resyncVocals(); - } + FlxG.sound.music.play(FlxG.sound.music.time); + vocals.play(); + resyncVocals(); #if discord_rpc // Updating Discord Rich Presence (with Time Left) @@ -1753,7 +1716,7 @@ class PlayState extends MusicBeatSubState if (startTimestamp > 0) { - FlxG.sound.music.time = startTimestamp - Conductor.instrumentalOffset; + // FlxG.sound.music.time = startTimestamp - Conductor.instrumentalOffset; handleSkippedNotes(); } } @@ -1767,11 +1730,10 @@ class PlayState extends MusicBeatSubState // Skip this if the music is paused (GameOver, Pause menu, start-of-song offset, etc.) if (!FlxG.sound.music.playing) return; - if (songStartDelay > 0) return; vocals.pause(); - FlxG.sound.music.play(); + FlxG.sound.music.play(FlxG.sound.music.time); vocals.time = FlxG.sound.music.time; vocals.play(false, FlxG.sound.music.time); diff --git a/source/funkin/play/song/Song.hx b/source/funkin/play/song/Song.hx index afb9f0dd2..9e5de6143 100644 --- a/source/funkin/play/song/Song.hx +++ b/source/funkin/play/song/Song.hx @@ -455,7 +455,11 @@ class SongDifficulty public inline function playInst(volume:Float = 1.0, looped:Bool = false):Void { var suffix:String = (variation != null && variation != '' && variation != 'default') ? '-$variation' : ''; - FlxG.sound.playMusic(Paths.inst(this.song.id, suffix), volume, looped); + + FlxG.sound.music = FunkinSound.load(Paths.inst(this.song.id, suffix), volume, looped); + + // Workaround for a bug where FlxG.sound.music.update() was being called twice. + FlxG.sound.list.remove(FlxG.sound.music); } /**