Skip to content

Commit

Permalink
Limit the song time to 0 and the songs max length
Browse files Browse the repository at this point in the history
  • Loading branch information
Kade-github committed Sep 19, 2024
1 parent a27c4ae commit 142e38e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
10 changes: 7 additions & 3 deletions source/funkin/Conductor.hx
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,12 @@ class Conductor
*/
public function update(?songPos:Float, applyOffsets:Bool = true, forceDispatch:Bool = false)
{
var currentTime:Float = (FlxG.sound.music != null) ? FlxG.sound.music.time : 0.0;
var currentLength:Float = (FlxG.sound.music != null) ? FlxG.sound.music.length : 0.0;

if (songPos == null)
{
songPos = (FlxG.sound.music != null) ? FlxG.sound.music.time : 0.0;
songPos = currentTime;
}

// Take into account instrumental and file format song offsets.
Expand All @@ -410,7 +413,7 @@ class Conductor
var oldStep:Float = this.currentStep;

// Set the song position we are at (for purposes of calculating note positions, etc).
this.songPosition = songPos;
this.songPosition = Math.min(currentLength, Math.max(0, songPos));

currentTimeChange = timeChanges[0];
if (this.songPosition > 0.0)
Expand All @@ -430,7 +433,8 @@ class Conductor
else if (currentTimeChange != null && this.songPosition > 0.0)
{
// roundDecimal prevents representing 8 as 7.9999999
this.currentStepTime = FlxMath.roundDecimal((currentTimeChange.beatTime * Constants.STEPS_PER_BEAT) + (this.songPosition - currentTimeChange.timeStamp) / stepLengthMs, 6);
this.currentStepTime = FlxMath.roundDecimal((currentTimeChange.beatTime * Constants.STEPS_PER_BEAT)
+ (this.songPosition - currentTimeChange.timeStamp) / stepLengthMs, 6);
this.currentBeatTime = currentStepTime / Constants.STEPS_PER_BEAT;
this.currentMeasureTime = currentStepTime / stepsPerMeasure;
this.currentStep = Math.floor(currentStepTime);
Expand Down
18 changes: 11 additions & 7 deletions source/funkin/play/PlayState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ class PlayState extends MusicBeatSubState
// Reset music properly.
if (FlxG.sound.music != null)
{
FlxG.sound.music.time = startTimestamp - Conductor.instance.instrumentalOffset;
FlxG.sound.music.time = Math.min(FlxG.sound.music.length, Math.max(0, startTimestamp - Conductor.instance.instrumentalOffset));
FlxG.sound.music.pitch = playbackRate;
FlxG.sound.music.pause();
}
Expand Down Expand Up @@ -915,7 +915,7 @@ class PlayState extends MusicBeatSubState
{
// Do NOT apply offsets at this point, because they already got applied the previous frame!
Conductor.instance.update(Conductor.instance.songPosition + elapsed * 1000, false);
if (Conductor.instance.songPosition >= (startTimestamp)) startSong();
if (Conductor.instance.songPosition - Conductor.instance.instrumentalOffset >= (startTimestamp)) startSong();
}
}
else
Expand Down Expand Up @@ -1391,14 +1391,16 @@ class PlayState extends MusicBeatSubState
// activeNotes.sort(SortUtil.byStrumtime, FlxSort.DESCENDING);
}

var correctSync:Float = Math.min(FlxG.sound.music.length, Math.max(0, Conductor.instance.songPosition - Conductor.instance.instrumentalOffset));

if (!startingSong
&& FlxG.sound.music != null
&& (Math.abs(FlxG.sound.music.time - (Conductor.instance.songPosition + Conductor.instance.instrumentalOffset)) > 100
|| Math.abs(vocals.checkSyncError(Conductor.instance.songPosition + Conductor.instance.instrumentalOffset)) > 100))
&& (Math.abs(FlxG.sound.music.time - correctSync) > 100 || Math.abs(vocals.checkSyncError(correctSync)) > 100))
{
trace("VOCALS NEED RESYNC");
if (vocals != null) trace(vocals.checkSyncError(Conductor.instance.songPosition + Conductor.instance.instrumentalOffset));
trace(FlxG.sound.music.time - (Conductor.instance.songPosition + Conductor.instance.instrumentalOffset));
if (vocals != null) trace(vocals.checkSyncError(correctSync));
trace(FlxG.sound.music.time);
trace(correctSync);
resyncVocals();
}

Expand Down Expand Up @@ -1993,7 +1995,9 @@ class PlayState extends MusicBeatSubState

// Skip this if the music is paused (GameOver, Pause menu, start-of-song offset, etc.)
if (!(FlxG.sound.music?.playing ?? false)) return;
var timeToPlayAt:Float = Conductor.instance.songPosition - Conductor.instance.instrumentalOffset;

var timeToPlayAt:Float = Math.min(FlxG.sound.music.length, Math.max(0, Conductor.instance.songPosition - Conductor.instance.instrumentalOffset));
trace('Resyncing vocals to ${timeToPlayAt}');
FlxG.sound.music.pause();
vocals.pause();

Expand Down

0 comments on commit 142e38e

Please sign in to comment.