Skip to content

Commit

Permalink
hopefully fix the desyncs
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealJake12 committed Nov 2, 2024
1 parent 4ff479f commit 97c6afe
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 109 deletions.
3 changes: 0 additions & 3 deletions Project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@
<assets path="mods" embed="false" if="FEATURE_MODCORE" />
<assets path="art/readme.txt" rename="do NOT readme.txt" />
<assets path="LICENSE" rename="LICENSE.txt" />
<library name="videos" preload="true" if="VIDEOS" />
<library name="songs" preload="true" />
<library name="weeks" preload="true" />
<assets path="assets/fonts" />
<assets path="alsoft.ini" />
<!-- _______________________________ Libraries ______________________________ -->
Expand Down
2 changes: 1 addition & 1 deletion hmm.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{
"name": "openfl",
"type": "haxelib",
"version": null
"version": "9.3.4"
},
{
"name": "flixel-tools",
Expand Down
2 changes: 1 addition & 1 deletion source/kec/backend/Options.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,7 @@ class FPSCapOption extends Option
#end
if (FlxG.save.data.fpsCap > 900)
FlxG.save.data.fpsCap = 900;
else if (FlxG.save.data.fpsCap < 60)
else if (FlxG.save.data.fpsCap <= 60)
FlxG.save.data.fpsCap = 60;
else
FlxG.save.data.fpsCap--;
Expand Down
25 changes: 2 additions & 23 deletions source/kec/backend/util/CoolUtil.hx
Original file line number Diff line number Diff line change
Expand Up @@ -278,30 +278,9 @@ class CoolUtil
return maxKey;
}

public static var loadingVideos:Array<String> = [];
public static var loadedVideos:Array<String> = [];

public static function precacheVideo(name:String):Void
public static function expDecay(a:Float, b:Float, decay:Float)
{
#if VIDEOS
if (FileSystem.exists(Paths.video(name)))
{
if (!loadedVideos.contains(name))
{
var cache:VideoHandler = new VideoHandler();
cache.mute = true;
cache.load(Paths.video(name));
loadedVideos.push(name);
FlxG.log.add('Video file has been cached: ' + name);
}
else
FlxG.log.add('Video file has already been cached: ' + name);
}
else
FlxG.log.warn('Couldnt find video file: ' + name);
#else
FlxG.log.warn('Platform not supported!');
#end
return b + (a - b) * Math.exp(-decay * FlxG.elapsed);
}

public static inline function getFileStringFromPath(file:String):String
Expand Down
132 changes: 51 additions & 81 deletions source/kec/states/PlayState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ class PlayState extends MusicBeatState
var lastPos:Float = -5000;

public var correctY = 698;
private var desyncs:Int = 0;
private var iDesyncs:Int = 0;

// Adding Objects Using Lua
public function addObject(object:FlxBasic)
Expand Down Expand Up @@ -1821,6 +1823,15 @@ class PlayState extends MusicBeatState
}
}

inst.pitch = Conductor.rate;
if (!SONG.splitVoiceTracks)
vocals.pitch = Conductor.rate;
else
{
vocalsPlayer.pitch = Conductor.rate;
vocalsEnemy.pitch = Conductor.rate;
}

unspawnNotes.sort(Sort.sortNotes);

generatedMusic = true;
Expand Down Expand Up @@ -2020,6 +2031,26 @@ class PlayState extends MusicBeatState

override public function update(elapsed:Float)
{
if (startedCountdown)
{
Conductor.elapsedPosition += FlxG.elapsed * 1000;

if (Conductor.elapsedPosition > 0 && startingSong)
startSong();

if (songStarted && !endingSong && (inst.length / Conductor.rate) - Conductor.elapsedPosition <= 0)
endSong();
Conductor.songPosition = Conductor.elapsedPosition * Conductor.rate;
final curTime:Float = Math.max(0, Conductor.songPosition) / Conductor.rate;
songPositionBar = CoolUtil.fpsLerp(songPositionBar, (curTime / songLength), 0.15, 60 * Conductor.rate);
final songCalc:Float = (songLength - curTime);
var secondsTotal:Int = Math.floor(songCalc * 0.001);
if (secondsTotal < 0)
secondsTotal = 0;
songName.text = SONG.songName + ' (' + FlxStringUtil.formatTime(secondsTotal, false) + ')';
checkMusicSync();
}

if (unspawnNotes[0] != null)
{
var shit:Float = 2000;
Expand Down Expand Up @@ -2061,40 +2092,9 @@ class PlayState extends MusicBeatState
if (FlxG.save.data.background)
Stage.update(elapsed);

if (generatedMusic)
{
if (songStarted && !endingSong)
{
if ((inst.length / Conductor.rate) - Conductor.elapsedPosition <= 0)
{
endingSong = true;
endSong();
}
}
}

if (inst.playing)
{
executeEventCheck();

// handles BPM Change events for you lol

inst.pitch = Conductor.rate;
if (!SONG.splitVoiceTracks)
{
if (vocals.playing)
vocals.pitch = Conductor.rate;
}
else
{
if (vocalsPlayer.playing && vocalsEnemy.playing)
{
vocalsPlayer.pitch = Conductor.rate;
vocalsEnemy.pitch = Conductor.rate;
}
}
}

if (PlayStateChangeables.botPlay && FlxG.keys.justPressed.F1)
camHUD.visible = !camHUD.visible;

Expand Down Expand Up @@ -2233,28 +2233,6 @@ class PlayState extends MusicBeatState
skipActive = false;
}

if (startedCountdown)
{
Conductor.elapsedPosition += FlxG.elapsed * 1000;

if (Conductor.elapsedPosition > 0 && startingSong)
startSong();
}

if (Conductor.elapsedPosition > lastPos)
{
lastPos = Conductor.elapsedPosition;
Conductor.songPosition = lastPos * Conductor.rate;
final curTime:Float = Math.max(0, Conductor.songPosition) / Conductor.rate;
songPositionBar = CoolUtil.fpsLerp(songPositionBar, (curTime / songLength), 0.15, 60 * Conductor.rate);
final songCalc:Float = (songLength - curTime);
var secondsTotal:Int = Math.floor(songCalc * 0.001);
if (secondsTotal < 0)
secondsTotal = 0;
songName.text = SONG.songName + ' (' + FlxStringUtil.formatTime(secondsTotal, false) + ')';
checkMusicSync();
}

FlxG.watch.addQuick("curBPM", Conductor.bpm);
FlxG.watch.addQuick("beatShit", curBeat);
FlxG.watch.addQuick("secShit", curSection);
Expand Down Expand Up @@ -2514,43 +2492,35 @@ class PlayState extends MusicBeatState
{
if (!generatedMusic || paused || !songStarted || endingSong)
return;

if (Math.abs(Conductor.songPosition - inst.time) > 100)
{
if (Math.abs(Conductor.songPosition - inst.time) > 75)
resyncInstToPosition();
}
if (SONG.needsVoices)
{
switch (SONG.splitVoiceTracks)
var checkVocals = [vocals];
if (SONG.splitVoiceTracks)
checkVocals = [vocalsPlayer, vocalsEnemy];
for (voc in checkVocals)
{
case true:
if (Math.abs(inst.time - vocalsPlayer.time) > 25)
resyncVocalsToInst();
case false:
if (Math.abs(inst.time - vocals.time) > 25)
resyncVocalsToInst();
if (voc.playing)
if (Math.abs(inst.time - voc.time) > 20)
if (Conductor.songPosition <= voc.length)
{
voc.time = inst.time;
voc.pitch = inst.pitch;
desyncs++;
Debug.logTrace('Vocal Desyncs $desyncs');
}

}
}
}

function resyncVocalsToInst():Void
{
var checkVocals = [];
if (!SONG.splitVoiceTracks)
checkVocals = [vocals];
else
checkVocals = [vocalsPlayer, vocalsEnemy];
for (voc in checkVocals)
{
if (voc.playing)
if (Conductor.elapsedPosition <= voc.length)
voc.time = inst.time;
}
}

function resyncInstToPosition():Void
function resyncInstToPosition()
{
inst.time = Conductor.songPosition * Conductor.rate;
inst.time = Conductor.songPosition;
inst.pitch = Conductor.rate;
iDesyncs++;
Debug.logTrace('Inst Desyncs $iDesyncs');
}

function endSong():Void
Expand Down

0 comments on commit 97c6afe

Please sign in to comment.