From 5e630dc214c463ff76db101a8287f91af1023b5f Mon Sep 17 00:00:00 2001
From: Cameron Taylor <cameron.taylor.ninja@gmail.com>
Date: Wed, 6 Jul 2022 20:37:35 -0400
Subject: [PATCH] latency debugging stuff in progress

---
 source/funkin/LatencyState.hx      | 80 ++++++++++++++++++++++++------
 source/funkin/MusicBeatSubstate.hx |  2 +-
 2 files changed, 67 insertions(+), 15 deletions(-)

diff --git a/source/funkin/LatencyState.hx b/source/funkin/LatencyState.hx
index 9760e4b2d..b127fb8c6 100644
--- a/source/funkin/LatencyState.hx
+++ b/source/funkin/LatencyState.hx
@@ -8,6 +8,7 @@ import flixel.math.FlxMath;
 import flixel.text.FlxText;
 import flixel.util.FlxColor;
 import funkin.audiovis.PolygonSpectogram;
+import openfl.events.KeyboardEvent;
 
 class LatencyState extends MusicBeatSubstate
 {
@@ -18,18 +19,29 @@ class LatencyState extends MusicBeatSubstate
 	var blocks:FlxGroup;
 
 	var songPosVis:FlxSprite;
-	var songVisFollow:FlxSprite;
+	var songVisFollowVideo:FlxSprite;
+	var songVisFollowAudio:FlxSprite;
 
 	var beatTrail:FlxSprite;
+	var diffGrp:FlxTypedGroup<FlxText>;
 
 	override function create()
 	{
+		FlxG.stage.addEventListener(KeyboardEvent.KEY_DOWN, key ->
+		{
+			trace("EVENT PRESS: " + FlxG.sound.music.time + " " + Sys.time());
+			// trace("EVENT LISTENER: " + key);
+		});
+
 		FlxG.sound.playMusic(Paths.sound('soundTest'));
-		Conductor.bpm = 120;
+		Conductor.bpm = 60;
 
 		noteGrp = new FlxTypedGroup<Note>();
 		add(noteGrp);
 
+		diffGrp = new FlxTypedGroup<FlxText>();
+		add(diffGrp);
+
 		// var musSpec:PolygonSpectogram = new PolygonSpectogram(FlxG.sound.music, FlxColor.RED, FlxG.height, Math.floor(FlxG.height / 2));
 		// musSpec.x += 170;
 		// musSpec.scrollFactor.set();
@@ -44,10 +56,17 @@ class LatencyState extends MusicBeatSubstate
 			beatTick.makeGraphic(2, 15);
 			beatTick.alpha = 0.3;
 			add(beatTick);
+
+			var offsetTxt:FlxText = new FlxText(songPosToX(beat * Conductor.crochet), FlxG.height - 26, 0, "swag");
+			offsetTxt.alpha = 0.5;
+			diffGrp.add(offsetTxt);
 		}
 
-		songVisFollow = new FlxSprite(0, FlxG.height - 20).makeGraphic(2, 20, FlxColor.YELLOW);
-		add(songVisFollow);
+		songVisFollowAudio = new FlxSprite(0, FlxG.height - 20).makeGraphic(2, 20, FlxColor.YELLOW);
+		add(songVisFollowAudio);
+
+		songVisFollowVideo = new FlxSprite(0, FlxG.height - 20).makeGraphic(2, 20, FlxColor.BLUE);
+		add(songVisFollowVideo);
 
 		songPosVis = new FlxSprite(0, FlxG.height - 20).makeGraphic(2, 20, FlxColor.RED);
 		add(songPosVis);
@@ -84,8 +103,6 @@ class LatencyState extends MusicBeatSubstate
 
 	override function beatHit()
 	{
-		beatTrail.x = songPosVis.x;
-
 		if (curBeat % 8 == 0)
 			blocks.forEach(blok ->
 			{
@@ -100,6 +117,24 @@ class LatencyState extends MusicBeatSubstate
 
 	override function update(elapsed:Float)
 	{
+		if (FlxG.keys.justPressed.S)
+		{
+			trace("UPDATE PRESS: " + FlxG.sound.music.time + " " + Sys.time());
+		}
+
+		if (FlxG.keys.justPressed.X)
+		{
+			var closestBeat:Int = Math.round(Conductor.songPosition / Conductor.crochet);
+			var getDiff:Float = Conductor.songPosition - (closestBeat * Conductor.crochet);
+			getDiff -= Conductor.visualOffset;
+
+			trace("\tDISTANCE TO CLOSEST BEAT: " + getDiff + "ms");
+			trace("\tCLOSEST BEAT: " + closestBeat);
+			beatTrail.x = songPosVis.x;
+			if (closestBeat < FlxG.sound.music.length / Conductor.crochet)
+				diffGrp.members[closestBeat].text = getDiff + "ms";
+		}
+
 		if (FlxG.keys.justPressed.SPACE)
 		{
 			if (FlxG.sound.music.playing)
@@ -114,9 +149,11 @@ class LatencyState extends MusicBeatSubstate
 		Conductor.songPosition = FlxG.sound.music.time - Conductor.offset;
 
 		songPosVis.x = songPosToX(Conductor.songPosition);
-		songVisFollow.x = songPosToX(Conductor.songPosition - Conductor.visualOffset);
+		songVisFollowAudio.x = songPosToX(Conductor.songPosition - Conductor.audioOffset);
+		songVisFollowVideo.x = songPosToX(Conductor.songPosition - Conductor.visualOffset);
 
-		offsetText.text = "Offset: " + Conductor.visualOffset + "ms";
+		offsetText.text = "AUDIO Offset: " + Conductor.audioOffset + "ms";
+		offsetText.text += "\nVIDOE Offset: " + Conductor.visualOffset + "ms";
 		offsetText.text += "\ncurStep: " + curStep;
 		offsetText.text += "\ncurBeat: " + curBeat;
 
@@ -125,14 +162,29 @@ class LatencyState extends MusicBeatSubstate
 		if (FlxG.keys.pressed.SHIFT)
 			multiply = 1;
 
-		if (FlxG.keys.justPressed.RIGHT)
+		if (FlxG.keys.pressed.CONTROL)
 		{
-			Conductor.visualOffset += 1 * multiply;
-		}
+			if (FlxG.keys.justPressed.RIGHT)
+			{
+				Conductor.audioOffset += 1 * multiply;
+			}
 
-		if (FlxG.keys.justPressed.LEFT)
+			if (FlxG.keys.justPressed.LEFT)
+			{
+				Conductor.audioOffset -= 1 * multiply;
+			}
+		}
+		else
 		{
-			Conductor.visualOffset -= 1 * multiply;
+			if (FlxG.keys.justPressed.RIGHT)
+			{
+				Conductor.visualOffset += 1 * multiply;
+			}
+
+			if (FlxG.keys.justPressed.LEFT)
+			{
+				Conductor.visualOffset -= 1 * multiply;
+			}
 		}
 
 		/* if (FlxG.keys.justPressed.SPACE)
@@ -144,7 +196,7 @@ class LatencyState extends MusicBeatSubstate
 
 		noteGrp.forEach(function(daNote:Note)
 		{
-			daNote.y = (strumLine.y - (Conductor.songPosition - daNote.data.strumTime) * 0.45);
+			daNote.y = (strumLine.y - ((Conductor.songPosition - Conductor.audioOffset) - daNote.data.strumTime) * 0.45);
 			daNote.x = strumLine.x + 30;
 
 			if (daNote.y < strumLine.y)
diff --git a/source/funkin/MusicBeatSubstate.hx b/source/funkin/MusicBeatSubstate.hx
index 1f8090725..790d8ee90 100644
--- a/source/funkin/MusicBeatSubstate.hx
+++ b/source/funkin/MusicBeatSubstate.hx
@@ -49,7 +49,7 @@ class MusicBeatSubstate extends FlxSubState
 				lastChange = Conductor.bpmChangeMap[i];
 		}
 
-		curStep = lastChange.stepTime + Math.floor(((Conductor.songPosition - Conductor.visualOffset) - lastChange.songTime) / Conductor.stepCrochet);
+		curStep = lastChange.stepTime + Math.floor(((Conductor.songPosition - Conductor.audioOffset) - lastChange.songTime) / Conductor.stepCrochet);
 	}
 
 	public function stepHit():Void