From 804e08466801701a42a606015edb39881839f7fd Mon Sep 17 00:00:00 2001
From: Cameron Taylor <cameron.taylor.ninja@gmail.com>
Date: Fri, 11 Feb 2022 14:21:42 -0500
Subject: [PATCH] polygon spectogram stuff redo

---
 source/InitState.hx                  | 93 ++++++++++++++--------------
 source/audiovis/PolygonSpectogram.hx | 64 +++++++++++++++++--
 source/audiovis/SpectogramSprite.hx  |  8 +--
 source/charting/ChartingState.hx     |  6 +-
 source/rendering/MeshRender.hx       |  5 +-
 5 files changed, 113 insertions(+), 63 deletions(-)

diff --git a/source/InitState.hx b/source/InitState.hx
index 6f5b88208..270a7cfb8 100644
--- a/source/InitState.hx
+++ b/source/InitState.hx
@@ -111,51 +111,47 @@ class InitState extends FlxTransitionableState
 
 		// FlxTransitionableState.skipNextTransOut = true;
 		FlxTransitionableState.skipNextTransIn = true;
-		
+
 		#if song
-			
-			var song = getSong();
-			
-			var weeks = 
-			[ ['bopeebo', 'fresh', 'dadbattle']
-			, ['spookeez', 'south', 'monster']
-			, ['spooky', 'spooky', 'monster']
-			, ['pico', 'philly', 'blammed']
-			, ['satin-panties', 'high', 'milf']
-			, ['cocoa', 'eggnog', 'winter-horrorland']
-			, ['senpai', 'roses', 'thorns']
-			, ['ugh', 'guns', 'stress']
-			];
-			
-			var week = 0;
-			for (i in 0...weeks.length)
+		var song = getSong();
+
+		var weeks = [
+			['bopeebo', 'fresh', 'dadbattle'],
+			['spookeez', 'south', 'monster'],
+			['spooky', 'spooky', 'monster'],
+			['pico', 'philly', 'blammed'],
+			['satin-panties', 'high', 'milf'],
+			['cocoa', 'eggnog', 'winter-horrorland'],
+			['senpai', 'roses', 'thorns'],
+			['ugh', 'guns', 'stress']
+		];
+
+		var week = 0;
+		for (i in 0...weeks.length)
+		{
+			if (weeks[i].contains(song))
 			{
-				if (weeks[i].contains(song))
-				{
-					week = i + 1;
-					break;
-				}
+				week = i + 1;
+				break;
 			}
-			
-			if (week == 0)
-				throw 'Invalid -D song=$song';
-			
-			startSong(week, song, false);
-			
+		}
+
+		if (week == 0)
+			throw 'Invalid -D song=$song';
+
+		startSong(week, song, false);
 		#elseif week
-			
-			var week = getWeek();
-			
-			var songs = 
-			[ 'bopeebo', 'spookeez', 'spooky', 'pico'
-			, 'satin-panties', 'cocoa', 'senpai', 'ugh'
-			];
-			
-			if (week <= 0 || week >= songs.length)
-				throw "invalid -D week=" + week;
-			
-			startSong(week, songs[week - 1], true);
-			
+		var week = getWeek();
+
+		var songs = [
+			      'bopeebo', 'spookeez', 'spooky', 'pico',
+			'satin-panties',    'cocoa', 'senpai',  'ugh'
+		];
+
+		if (week <= 0 || week >= songs.length)
+			throw "invalid -D week=" + week;
+
+		startSong(week, songs[week - 1], true);
 		#elseif FREEPLAY
 		FlxG.switchState(new FreeplayState());
 		#elseif ANIMATE
@@ -173,11 +169,11 @@ class InitState extends FlxTransitionableState
 		FlxG.switchState(new TitleState());
 		#end
 	}
-	
+
 	function startSong(week, song, isStoryMode)
 	{
 		var dif = getDif();
-		
+
 		PlayState.SONG = SongLoad.loadFromJson(song, song);
 		PlayState.isStoryMode = isStoryMode;
 		PlayState.storyDifficulty = dif;
@@ -194,9 +190,14 @@ class InitState extends FlxTransitionableState
 }
 #end
 
-function getWeek() return Std.parseInt(getDefine("week"));
-function getSong() return getDefine("song");
-function getDif() return Std.parseInt(getDefine("dif", "1"));
+function getWeek()
+	return Std.parseInt(getDefine("week"));
+
+function getSong()
+	return getDefine("song");
+
+function getDif()
+	return Std.parseInt(getDefine("dif", "1"));
 
 macro function getDefine(key:String, defaultValue:String = null):haxe.macro.Expr
 {
diff --git a/source/audiovis/PolygonSpectogram.hx b/source/audiovis/PolygonSpectogram.hx
index b358bcd88..ab8c8eaba 100644
--- a/source/audiovis/PolygonSpectogram.hx
+++ b/source/audiovis/PolygonSpectogram.hx
@@ -13,6 +13,7 @@ class PolygonSpectogram extends MeshRender
 	var sampleRate:Int;
 
 	public var vis:VisShit;
+	public var visType:VISTYPE = UPDATED;
 	public var daHeight:Float = FlxG.height;
 
 	var numSamples:Int = 0;
@@ -23,7 +24,7 @@ class PolygonSpectogram extends MeshRender
 
 	public function new(daSound:FlxSound, ?col:FlxColor = FlxColor.WHITE, ?height:Float = 720, ?detail:Float = 1)
 	{
-		super(0, 0);
+		super(0, 0, col);
 
 		vis = new VisShit(daSound);
 
@@ -35,6 +36,20 @@ class PolygonSpectogram extends MeshRender
 		// col not in yet
 	}
 
+	override function update(elapsed:Float)
+	{
+		super.update(elapsed);
+
+		switch (visType)
+		{
+			case UPDATED:
+				realtimeVis();
+			default:
+		}
+	}
+
+	var prevAudioData:Int16Array;
+
 	/**
 	 * Generates and draws a section of the audio data to a visual waveform
 	 * @param start start of the song in milliseconds
@@ -54,18 +69,25 @@ class PolygonSpectogram extends MeshRender
 
 			var prevPoint:FlxPoint = new FlxPoint();
 
-			for (i in 0...500)
+			var funnyPixels:Int = Std.int(daHeight); // sorta redundant but just need it for different var...
+
+			if (prevAudioData == audioData.subarray(startSample, startSample + samplesToGen))
+				return; // optimize / finish funciton here, no need to re-render
+
+			prevAudioData = audioData.subarray(startSample, samplesToGen);
+
+			for (i in 0...funnyPixels)
 			{
-				var sampleApprox:Int = Std.int(FlxMath.remapToRange(i, 0, 500, startSample, startSample + samplesToGen));
+				var sampleApprox:Int = Std.int(FlxMath.remapToRange(i, 0, funnyPixels, startSample, startSample + samplesToGen));
 				var curAud:CurAudioInfo = VisShit.getCurAud(audioData, sampleApprox);
 
 				var waveAmplitude:Int = 200;
 
 				var coolPoint:FlxPoint = new FlxPoint();
 				coolPoint.x = (curAud.balanced * waveAmplitude / 2 + waveAmplitude / 2);
-				coolPoint.y = (i / 500 * daHeight);
+				coolPoint.y = (i / funnyPixels * daHeight);
 
-				add_quad(prevPoint.x, prevPoint.y, prevPoint.x + 1, prevPoint.y, coolPoint.x, coolPoint.y, coolPoint.x + 1, coolPoint.y + 1);
+				add_quad(prevPoint.x, prevPoint.y, prevPoint.x + 2, prevPoint.y, coolPoint.x, coolPoint.y, coolPoint.x + 2, coolPoint.y + 2);
 
 				prevPoint.x = coolPoint.x;
 				prevPoint.y = coolPoint.y;
@@ -73,6 +95,31 @@ class PolygonSpectogram extends MeshRender
 		}
 	}
 
+	var curTime:Float = 0;
+
+	function realtimeVis():Void
+	{
+		if (vis.snd != null)
+		{
+			if (curTime != vis.snd.time)
+			{
+				trace("DOIN SHIT" + FlxG.random.int(0, 200));
+
+				if (vis.snd.playing)
+					curTime = vis.snd.time;
+				else
+				{
+					if (Math.abs(curTime - vis.snd.time) > 10)
+						curTime = FlxMath.lerp(curTime, vis.snd.time, 0.5);
+				}
+
+				curTime = vis.snd.time;
+
+				generateSection(vis.snd.time, 0.2);
+			}
+		}
+	}
+
 	public function checkAndSetBuffer()
 	{
 		vis.checkAndSetBuffer();
@@ -86,3 +133,10 @@ class PolygonSpectogram extends MeshRender
 		}
 	}
 }
+
+enum VISTYPE
+{
+	STATIC;
+	UPDATED;
+	FREQUENCIES;
+}
diff --git a/source/audiovis/SpectogramSprite.hx b/source/audiovis/SpectogramSprite.hx
index f80a8b724..394bfadf6 100644
--- a/source/audiovis/SpectogramSprite.hx
+++ b/source/audiovis/SpectogramSprite.hx
@@ -1,5 +1,6 @@
 package audiovis;
 
+import audiovis.PolygonSpectogram.VISTYPE;
 import audiovis.VisShit.CurAudioInfo;
 import audiovis.dsp.FFT;
 import flixel.FlxSprite;
@@ -298,10 +299,3 @@ class SpectogramSprite extends FlxTypedSpriteGroup<FlxSprite>
 		}
 	}
 }
-
-enum VISTYPE
-{
-	STATIC;
-	UPDATED;
-	FREQUENCIES;
-}
diff --git a/source/charting/ChartingState.hx b/source/charting/ChartingState.hx
index d2baf3948..4b3bf95f3 100644
--- a/source/charting/ChartingState.hx
+++ b/source/charting/ChartingState.hx
@@ -422,7 +422,7 @@ class ChartingState extends MusicBeatState
 
 		FlxG.sound.playMusic(Paths.inst(daSong), 0.6);
 
-		var musSpec:SpectogramSprite = new SpectogramSprite(FlxG.sound.music, FlxColor.RED, FlxG.height / 2, Math.floor(FlxG.height / 2));
+		var musSpec:PolygonSpectogram = new PolygonSpectogram(FlxG.sound.music, FlxColor.RED, FlxG.height / 2, Math.floor(FlxG.height / 2));
 		musSpec.x += 70;
 		musSpec.scrollFactor.set();
 		// musSpec.visType = FREQUENCIES;
@@ -451,7 +451,7 @@ class ChartingState extends MusicBeatState
 
 		for (index => voc in vocals.members)
 		{
-			var vocalSpec:SpectogramSprite = new SpectogramSprite(voc, FlxG.random.color(0xFFAAAAAA, FlxColor.WHITE, 100), musSpec.daHeight,
+			var vocalSpec:PolygonSpectogram = new PolygonSpectogram(voc, FlxG.random.color(0xFFAAAAAA, FlxColor.WHITE, 100), musSpec.daHeight,
 				Math.floor(FlxG.height / 2));
 			vocalSpec.x = 70 - (50 * index);
 			// vocalSpec.visType = FREQUENCIES;
@@ -467,7 +467,7 @@ class ChartingState extends MusicBeatState
 			if (index == 1)
 				staticVocal.x = gridBG.width;
 
-			// staticVocal.visType = STATIC;
+			staticVocal.visType = STATIC;
 			staticSpecGrp.add(staticVocal);
 		}
 
diff --git a/source/rendering/MeshRender.hx b/source/rendering/MeshRender.hx
index 25cbbf96f..d981a1303 100644
--- a/source/rendering/MeshRender.hx
+++ b/source/rendering/MeshRender.hx
@@ -1,6 +1,7 @@
 package rendering;
 
 import flixel.FlxStrip;
+import flixel.util.FlxColor;
 
 /**
  * Yoinked from AustinEast, thanks hopefully u dont mind me using some of ur good code
@@ -13,10 +14,10 @@ class MeshRender extends FlxStrip
 
 	var tri_offset:Int = 0;
 
-	public function new(x, y)
+	public function new(x, y, ?col:FlxColor = FlxColor.WHITE)
 	{
 		super(x, y);
-		makeGraphic(1, 1);
+		makeGraphic(1, 1, col);
 	}
 
 	public inline function start()