From f797f6a7b1f795eb667cff8af80db343e2973b27 Mon Sep 17 00:00:00 2001
From: Cameron Taylor <cameron.taylor.ninja@gmail.com>
Date: Wed, 6 Jul 2022 15:27:45 -0400
Subject: [PATCH] music debug stuff in progress

---
 source/funkin/Conductor.hx         |  2 +
 source/funkin/LatencyState.hx      | 86 +++++++++++++++++++++++-------
 source/funkin/MusicBeatSubstate.hx |  4 +-
 3 files changed, 72 insertions(+), 20 deletions(-)

diff --git a/source/funkin/Conductor.hx b/source/funkin/Conductor.hx
index 331dd46f7..0337a479e 100644
--- a/source/funkin/Conductor.hx
+++ b/source/funkin/Conductor.hx
@@ -42,6 +42,8 @@ class Conductor
 
 	public static var songPosition:Float;
 	public static var lastSongPos:Float;
+	public static var visualOffset:Float = 0;
+	public static var audioOffset:Float = 0;
 	public static var offset:Float = 0;
 
 	public static var bpmChangeMap:Array<BPMChangeEvent> = [];
diff --git a/source/funkin/LatencyState.hx b/source/funkin/LatencyState.hx
index 9a9df4768..33855321e 100644
--- a/source/funkin/LatencyState.hx
+++ b/source/funkin/LatencyState.hx
@@ -3,6 +3,8 @@ package funkin;
 import flixel.FlxSprite;
 import flixel.FlxSubState;
 import flixel.group.FlxGroup.FlxTypedGroup;
+import flixel.group.FlxGroup;
+import flixel.math.FlxMath;
 import flixel.text.FlxText;
 import flixel.util.FlxColor;
 import funkin.audiovis.PolygonSpectogram;
@@ -13,25 +15,52 @@ class LatencyState extends MusicBeatSubstate
 	var noteGrp:FlxTypedGroup<Note>;
 	var strumLine:FlxSprite;
 
-	var block:FlxSprite;
+	var blocks:FlxGroup;
+
+	var songPosVis:FlxSprite;
+
+	var beatTrail:FlxSprite;
 
 	override function create()
 	{
 		FlxG.sound.playMusic(Paths.sound('soundTest'));
+		Conductor.bpm = 120;
 
 		noteGrp = new FlxTypedGroup<Note>();
 		add(noteGrp);
 
-		var musSpec:PolygonSpectogram = new PolygonSpectogram(FlxG.sound.music, FlxColor.RED, FlxG.height, Math.floor(FlxG.height / 2));
-		musSpec.x += 170;
-		musSpec.scrollFactor.set();
-		musSpec.waveAmplitude = 100;
-		musSpec.realtimeVisLenght = 0.45;
-		// musSpec.visType = FREQUENCIES;
-		add(musSpec);
+		// var musSpec:PolygonSpectogram = new PolygonSpectogram(FlxG.sound.music, FlxColor.RED, FlxG.height, Math.floor(FlxG.height / 2));
+		// musSpec.x += 170;
+		// musSpec.scrollFactor.set();
+		// musSpec.waveAmplitude = 100;
+		// musSpec.realtimeVisLenght = 0.45;
+		// // musSpec.visType = FREQUENCIES;
+		// add(musSpec);
 
-		block = new FlxSprite().makeGraphic(100, 100);
-		add(block);
+		for (beat in 0...Math.floor(FlxG.sound.music.length / Conductor.crochet))
+		{
+			var beatTick:FlxSprite = new FlxSprite(songPosToX(beat * Conductor.crochet), FlxG.height - 15);
+			beatTick.makeGraphic(2, 15);
+			beatTick.alpha = 0.3;
+			add(beatTick);
+		}
+
+		songPosVis = new FlxSprite(0, FlxG.height - 20).makeGraphic(2, 20, FlxColor.RED);
+		add(songPosVis);
+
+		beatTrail = new FlxSprite(0, songPosVis.y).makeGraphic(2, 20, FlxColor.PURPLE);
+		beatTrail.alpha = 0.7;
+		add(beatTrail);
+
+		blocks = new FlxGroup();
+		add(blocks);
+
+		for (i in 0...8)
+		{
+			var block = new FlxSprite(2, 50 * i).makeGraphic(48, 48);
+			block.visible = false;
+			blocks.add(block);
+		}
 
 		for (i in 0...32)
 		{
@@ -46,33 +75,49 @@ class LatencyState extends MusicBeatSubstate
 		strumLine = new FlxSprite(FlxG.width / 2, 100).makeGraphic(FlxG.width, 5);
 		add(strumLine);
 
-		Conductor.bpm = 120;
-
 		super.create();
 	}
 
 	override function beatHit()
 	{
-		block.visible = !block.visible;
+		beatTrail.x = songPosVis.x;
+
+		if (curBeat % 8 == 0)
+			blocks.forEach(blok ->
+			{
+				blok.visible = false;
+			});
+
+		blocks.members[curBeat % 8].visible = true;
+		// block.visible = !block.visible;
 
 		super.beatHit();
 	}
 
 	override function update(elapsed:Float)
 	{
-		offsetText.text = "Offset: " + Conductor.offset + "ms";
+		songPosVis.x = songPosToX(Conductor.songPosition);
+
+		offsetText.text = "Offset: " + Conductor.visualOffset + "ms";
+		offsetText.text += "\ncurStep: " + curStep;
+		offsetText.text += "\ncurBeat: " + curBeat;
 
 		Conductor.songPosition = FlxG.sound.music.time - Conductor.offset;
 
-		var multiply:Float = 1;
+		var multiply:Float = 10;
 
 		if (FlxG.keys.pressed.SHIFT)
-			multiply = 10;
+			multiply = 1;
 
 		if (FlxG.keys.justPressed.RIGHT)
-			Conductor.offset += 1 * multiply;
+		{
+			Conductor.visualOffset += 1 * multiply;
+		}
+
 		if (FlxG.keys.justPressed.LEFT)
-			Conductor.offset -= 1 * multiply;
+		{
+			Conductor.visualOffset -= 1 * multiply;
+		}
 
 		if (FlxG.keys.justPressed.SPACE)
 		{
@@ -98,4 +143,9 @@ class LatencyState extends MusicBeatSubstate
 
 		super.update(elapsed);
 	}
+
+	function songPosToX(pos:Float):Float
+	{
+		return FlxMath.remapToRange(pos, 0, FlxG.sound.music.length, 0, FlxG.width);
+	}
 }
diff --git a/source/funkin/MusicBeatSubstate.hx b/source/funkin/MusicBeatSubstate.hx
index 001f8d420..1f8090725 100644
--- a/source/funkin/MusicBeatSubstate.hx
+++ b/source/funkin/MusicBeatSubstate.hx
@@ -45,11 +45,11 @@ class MusicBeatSubstate extends FlxSubState
 		}
 		for (i in 0...Conductor.bpmChangeMap.length)
 		{
-			if (Conductor.songPosition - Conductor.offset > Conductor.bpmChangeMap[i].songTime)
+			if (Conductor.songPosition > Conductor.bpmChangeMap[i].songTime)
 				lastChange = Conductor.bpmChangeMap[i];
 		}
 
-		curStep = lastChange.stepTime + Math.floor((Conductor.songPosition - lastChange.songTime) / Conductor.stepCrochet);
+		curStep = lastChange.stepTime + Math.floor(((Conductor.songPosition - Conductor.visualOffset) - lastChange.songTime) / Conductor.stepCrochet);
 	}
 
 	public function stepHit():Void