2023-11-07 04:04:22 -05:00
|
|
|
package funkin.ui.debug.latency;
|
2020-10-06 21:56:14 -04:00
|
|
|
|
2023-07-13 20:27:45 -04:00
|
|
|
import funkin.data.notestyle.NoteStyleRegistry;
|
2020-10-06 21:56:14 -04:00
|
|
|
import flixel.FlxSprite;
|
2022-07-05 14:24:02 -04:00
|
|
|
import flixel.FlxSubState;
|
2022-07-06 15:27:45 -04:00
|
|
|
import flixel.group.FlxGroup;
|
2023-06-22 01:41:01 -04:00
|
|
|
import flixel.group.FlxGroup.FlxTypedGroup;
|
2022-07-06 15:27:45 -04:00
|
|
|
import flixel.math.FlxMath;
|
2023-11-07 04:04:22 -05:00
|
|
|
import funkin.ui.MusicBeatSubState;
|
2023-05-25 18:33:39 -04:00
|
|
|
import flixel.sound.FlxSound;
|
2022-07-12 19:02:38 -04:00
|
|
|
import flixel.system.debug.stats.StatsGraph;
|
2020-10-06 21:56:14 -04:00
|
|
|
import flixel.text.FlxText;
|
2022-07-05 14:34:08 -04:00
|
|
|
import flixel.util.FlxColor;
|
2022-10-07 00:37:21 -04:00
|
|
|
import funkin.audio.visualize.PolygonSpectogram;
|
2023-06-22 01:41:01 -04:00
|
|
|
import funkin.play.notes.NoteSprite;
|
2023-11-07 04:04:22 -05:00
|
|
|
import funkin.ui.debug.latency.CoolStatsGraph;
|
2022-07-06 20:54:07 -04:00
|
|
|
import haxe.Timer;
|
2022-07-06 20:37:35 -04:00
|
|
|
import openfl.events.KeyboardEvent;
|
2020-10-06 21:56:14 -04:00
|
|
|
|
2023-06-09 15:44:29 -04:00
|
|
|
class LatencyState extends MusicBeatSubState
|
2020-10-06 21:56:14 -04:00
|
|
|
{
|
2023-01-22 22:25:45 -05:00
|
|
|
var offsetText:FlxText;
|
2023-06-22 01:41:01 -04:00
|
|
|
var noteGrp:FlxTypedGroup<NoteSprite>;
|
2023-01-22 22:25:45 -05:00
|
|
|
var strumLine:FlxSprite;
|
2020-10-06 21:56:14 -04:00
|
|
|
|
2023-01-22 22:25:45 -05:00
|
|
|
var blocks:FlxTypedGroup<FlxSprite>;
|
2022-07-06 15:27:45 -04:00
|
|
|
|
2023-01-22 22:25:45 -05:00
|
|
|
var songPosVis:FlxSprite;
|
|
|
|
var songVisFollowVideo:FlxSprite;
|
|
|
|
var songVisFollowAudio:FlxSprite;
|
2022-07-06 15:27:45 -04:00
|
|
|
|
2023-01-22 22:25:45 -05:00
|
|
|
var beatTrail:FlxSprite;
|
|
|
|
var diffGrp:FlxTypedGroup<FlxText>;
|
|
|
|
var offsetsPerBeat:Array<Int> = [];
|
|
|
|
var swagSong:HomemadeMusic;
|
2022-07-12 19:02:38 -04:00
|
|
|
|
2023-05-22 20:55:53 -04:00
|
|
|
#if FLX_DEBUG
|
2023-01-22 22:25:45 -05:00
|
|
|
var funnyStatsGraph:CoolStatsGraph;
|
|
|
|
var realStats:CoolStatsGraph;
|
|
|
|
#end
|
2022-07-12 19:02:38 -04:00
|
|
|
|
2023-01-22 22:25:45 -05:00
|
|
|
override function create()
|
|
|
|
{
|
|
|
|
swagSong = new HomemadeMusic();
|
|
|
|
swagSong.loadEmbedded(Paths.sound('soundTest'), true);
|
2022-07-12 19:02:38 -04:00
|
|
|
|
2023-01-22 22:25:45 -05:00
|
|
|
FlxG.sound.music = swagSong;
|
|
|
|
FlxG.sound.music.play();
|
2022-07-06 15:27:45 -04:00
|
|
|
|
2023-05-22 20:55:53 -04:00
|
|
|
#if FLX_DEBUG
|
2023-01-22 22:25:45 -05:00
|
|
|
funnyStatsGraph = new CoolStatsGraph(0, Std.int(FlxG.height / 2), FlxG.width, Std.int(FlxG.height / 2), FlxColor.PINK, "time");
|
|
|
|
FlxG.addChildBelowMouse(funnyStatsGraph);
|
|
|
|
|
|
|
|
realStats = new CoolStatsGraph(0, Std.int(FlxG.height / 2), FlxG.width, Std.int(FlxG.height / 2), FlxColor.YELLOW, "REAL");
|
|
|
|
FlxG.addChildBelowMouse(realStats);
|
|
|
|
#end
|
|
|
|
|
2023-05-25 18:33:39 -04:00
|
|
|
FlxG.stage.addEventListener(KeyboardEvent.KEY_DOWN, key -> {
|
2023-01-22 22:25:45 -05:00
|
|
|
trace(key.charCode);
|
|
|
|
|
|
|
|
if (key.charCode == 120) generateBeatStuff();
|
|
|
|
|
|
|
|
trace("\tEVENT PRESS: \t" + FlxG.sound.music.time + " " + Timer.stamp());
|
|
|
|
// trace(FlxG.sound.music.prevTimestamp);
|
|
|
|
trace(FlxG.sound.music.time);
|
|
|
|
trace("\tFR FR PRESS: \t" + swagSong.getTimeWithDiff());
|
|
|
|
|
|
|
|
// trace("\tREDDIT: \t" + swagSong.frfrTime + " " + Timer.stamp());
|
|
|
|
@:privateAccess
|
|
|
|
trace("\tREDDIT: \t" + FlxG.sound.music._channel.position + " " + Timer.stamp());
|
|
|
|
// trace("EVENT LISTENER: " + key);
|
|
|
|
});
|
|
|
|
|
|
|
|
// FlxG.sound.playMusic(Paths.sound('soundTest'));
|
|
|
|
|
|
|
|
// funnyStatsGraph.hi
|
|
|
|
|
|
|
|
Conductor.forceBPM(60);
|
|
|
|
|
2023-06-22 01:41:01 -04:00
|
|
|
noteGrp = new FlxTypedGroup<NoteSprite>();
|
2023-01-22 22:25:45 -05:00
|
|
|
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();
|
|
|
|
// musSpec.waveAmplitude = 100;
|
|
|
|
// musSpec.realtimeVisLenght = 0.45;
|
|
|
|
// // musSpec.visType = FREQUENCIES;
|
|
|
|
// add(musSpec);
|
|
|
|
|
2023-06-15 00:15:57 -04:00
|
|
|
for (beat in 0...Math.floor(FlxG.sound.music.length / Conductor.beatLengthMs))
|
2023-01-22 22:25:45 -05:00
|
|
|
{
|
2023-06-15 00:15:57 -04:00
|
|
|
var beatTick:FlxSprite = new FlxSprite(songPosToX(beat * Conductor.beatLengthMs), FlxG.height - 15);
|
2023-01-22 22:25:45 -05:00
|
|
|
beatTick.makeGraphic(2, 15);
|
|
|
|
beatTick.alpha = 0.3;
|
|
|
|
add(beatTick);
|
|
|
|
|
2023-06-15 00:15:57 -04:00
|
|
|
var offsetTxt:FlxText = new FlxText(songPosToX(beat * Conductor.beatLengthMs), FlxG.height - 26, 0, "swag");
|
2023-01-22 22:25:45 -05:00
|
|
|
offsetTxt.alpha = 0.5;
|
|
|
|
diffGrp.add(offsetTxt);
|
|
|
|
|
|
|
|
offsetsPerBeat.push(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
beatTrail = new FlxSprite(0, songPosVis.y).makeGraphic(2, 20, FlxColor.PURPLE);
|
|
|
|
beatTrail.alpha = 0.7;
|
|
|
|
add(beatTrail);
|
|
|
|
|
|
|
|
blocks = new FlxTypedGroup<FlxSprite>();
|
|
|
|
add(blocks);
|
|
|
|
|
|
|
|
for (i in 0...8)
|
|
|
|
{
|
|
|
|
var block = new FlxSprite(2, 50 * i).makeGraphic(48, 48);
|
|
|
|
block.alpha = 0;
|
|
|
|
blocks.add(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i in 0...32)
|
|
|
|
{
|
2023-07-13 20:27:45 -04:00
|
|
|
var note:NoteSprite = new NoteSprite(NoteStyleRegistry.instance.fetchDefault(), Conductor.beatLengthMs * i);
|
2023-01-22 22:25:45 -05:00
|
|
|
noteGrp.add(note);
|
|
|
|
}
|
|
|
|
|
|
|
|
offsetText = new FlxText();
|
|
|
|
offsetText.screenCenter();
|
|
|
|
add(offsetText);
|
|
|
|
|
|
|
|
strumLine = new FlxSprite(FlxG.width / 2, 100).makeGraphic(FlxG.width, 5);
|
|
|
|
add(strumLine);
|
|
|
|
|
|
|
|
super.create();
|
|
|
|
}
|
|
|
|
|
|
|
|
override function stepHit():Bool
|
|
|
|
{
|
2023-06-15 00:15:57 -04:00
|
|
|
if (Conductor.currentStep % 4 == 2)
|
2023-01-22 22:25:45 -05:00
|
|
|
{
|
2023-06-15 00:15:57 -04:00
|
|
|
blocks.members[((Conductor.currentBeat % 8) + 1) % 8].alpha = 0.5;
|
2023-01-22 22:25:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return super.stepHit();
|
|
|
|
}
|
|
|
|
|
|
|
|
override function beatHit():Bool
|
|
|
|
{
|
2023-06-15 00:15:57 -04:00
|
|
|
if (Conductor.currentBeat % 8 == 0) blocks.forEach(blok -> {
|
2023-01-22 22:25:45 -05:00
|
|
|
blok.alpha = 0;
|
|
|
|
});
|
|
|
|
|
2023-06-15 00:15:57 -04:00
|
|
|
blocks.members[Conductor.currentBeat % 8].alpha = 1;
|
2023-01-22 22:25:45 -05:00
|
|
|
// block.visible = !block.visible;
|
|
|
|
|
|
|
|
return super.beatHit();
|
|
|
|
}
|
|
|
|
|
|
|
|
override function update(elapsed:Float)
|
|
|
|
{
|
|
|
|
/* trace("1: " + swagSong.frfrTime);
|
|
|
|
@:privateAccess
|
|
|
|
trace(FlxG.sound.music._channel.position);
|
|
|
|
*/
|
|
|
|
|
2023-05-22 20:55:53 -04:00
|
|
|
#if FLX_DEBUG
|
2023-01-22 22:25:45 -05:00
|
|
|
funnyStatsGraph.update(FlxG.sound.music.time % 500);
|
|
|
|
realStats.update(swagSong.getTimeWithDiff() % 500);
|
|
|
|
#end
|
|
|
|
|
|
|
|
if (FlxG.keys.justPressed.S)
|
|
|
|
{
|
|
|
|
trace("\tUPDATE PRESS: \t" + FlxG.sound.music.time + " " + Timer.stamp());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FlxG.keys.justPressed.SPACE)
|
|
|
|
{
|
|
|
|
if (FlxG.sound.music.playing) FlxG.sound.music.pause();
|
|
|
|
else
|
|
|
|
FlxG.sound.music.resume();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FlxG.keys.pressed.D) FlxG.sound.music.time += 1000 * FlxG.elapsed;
|
|
|
|
|
2023-12-05 02:44:57 -05:00
|
|
|
Conductor.update(swagSong.getTimeWithDiff() - Conductor.inputOffset);
|
2023-01-22 22:25:45 -05:00
|
|
|
// Conductor.songPosition += (Timer.stamp() * 1000) - FlxG.sound.music.prevTimestamp;
|
|
|
|
|
|
|
|
songPosVis.x = songPosToX(Conductor.songPosition);
|
2023-12-05 02:44:57 -05:00
|
|
|
songVisFollowAudio.x = songPosToX(Conductor.songPosition - Conductor.instrumentalOffset);
|
|
|
|
songVisFollowVideo.x = songPosToX(Conductor.songPosition - Conductor.inputOffset);
|
2023-01-22 22:25:45 -05:00
|
|
|
|
2023-12-05 02:44:57 -05:00
|
|
|
offsetText.text = "INST Offset: " + Conductor.instrumentalOffset + "ms";
|
|
|
|
offsetText.text += "\nINPUT Offset: " + Conductor.inputOffset + "ms";
|
2023-06-15 00:15:57 -04:00
|
|
|
offsetText.text += "\ncurrentStep: " + Conductor.currentStep;
|
|
|
|
offsetText.text += "\ncurrentBeat: " + Conductor.currentBeat;
|
2023-01-22 22:25:45 -05:00
|
|
|
|
|
|
|
var avgOffsetInput:Float = 0;
|
|
|
|
|
|
|
|
for (offsetThing in offsetsPerBeat)
|
|
|
|
avgOffsetInput += offsetThing;
|
|
|
|
|
|
|
|
avgOffsetInput /= offsetsPerBeat.length;
|
|
|
|
|
|
|
|
offsetText.text += "\naverage input offset needed: " + avgOffsetInput;
|
|
|
|
|
|
|
|
var multiply:Float = 10;
|
|
|
|
|
|
|
|
if (FlxG.keys.pressed.SHIFT) multiply = 1;
|
|
|
|
|
|
|
|
if (FlxG.keys.pressed.CONTROL)
|
|
|
|
{
|
|
|
|
if (FlxG.keys.justPressed.RIGHT)
|
|
|
|
{
|
2023-12-05 02:44:57 -05:00
|
|
|
Conductor.instrumentalOffset += 1.0 * multiply;
|
2023-01-22 22:25:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (FlxG.keys.justPressed.LEFT)
|
|
|
|
{
|
2023-12-05 02:44:57 -05:00
|
|
|
Conductor.instrumentalOffset -= 1.0 * multiply;
|
2023-01-22 22:25:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (FlxG.keys.justPressed.RIGHT)
|
|
|
|
{
|
2023-12-05 02:44:57 -05:00
|
|
|
Conductor.inputOffset += 1.0 * multiply;
|
2023-01-22 22:25:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (FlxG.keys.justPressed.LEFT)
|
|
|
|
{
|
2023-12-05 02:44:57 -05:00
|
|
|
Conductor.inputOffset -= 1.0 * multiply;
|
2023-01-22 22:25:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if (FlxG.keys.justPressed.SPACE)
|
|
|
|
{
|
|
|
|
FlxG.sound.music.stop();
|
|
|
|
|
|
|
|
FlxG.resetState();
|
|
|
|
}*/
|
|
|
|
|
2023-06-22 01:41:01 -04:00
|
|
|
noteGrp.forEach(function(daNote:NoteSprite) {
|
2023-12-05 02:44:57 -05:00
|
|
|
daNote.y = (strumLine.y - ((Conductor.songPosition - Conductor.instrumentalOffset) - daNote.noteData.time) * 0.45);
|
2023-01-22 22:25:45 -05:00
|
|
|
daNote.x = strumLine.x + 30;
|
|
|
|
|
|
|
|
if (daNote.y < strumLine.y) daNote.alpha = 0.5;
|
|
|
|
|
|
|
|
if (daNote.y < 0 - daNote.height)
|
|
|
|
{
|
|
|
|
daNote.alpha = 1;
|
2023-06-15 00:15:57 -04:00
|
|
|
// daNote.data.strumTime += Conductor.beatLengthMs * 8;
|
2023-01-22 22:25:45 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
super.update(elapsed);
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateBeatStuff()
|
|
|
|
{
|
2023-11-28 21:44:09 -05:00
|
|
|
Conductor.update(swagSong.getTimeWithDiff());
|
2023-01-22 22:25:45 -05:00
|
|
|
|
2023-06-15 00:15:57 -04:00
|
|
|
var closestBeat:Int = Math.round(Conductor.songPosition / Conductor.beatLengthMs) % diffGrp.members.length;
|
|
|
|
var getDiff:Float = Conductor.songPosition - (closestBeat * Conductor.beatLengthMs);
|
2023-12-05 02:44:57 -05:00
|
|
|
getDiff -= Conductor.inputOffset;
|
2023-01-22 22:25:45 -05:00
|
|
|
|
|
|
|
// lil fix for end of song
|
2023-06-15 00:15:57 -04:00
|
|
|
if (closestBeat == 0 && getDiff >= Conductor.beatLengthMs * 2) getDiff -= FlxG.sound.music.length;
|
2023-01-22 22:25:45 -05:00
|
|
|
|
|
|
|
trace("\tDISTANCE TO CLOSEST BEAT: " + getDiff + "ms");
|
|
|
|
trace("\tCLOSEST BEAT: " + closestBeat);
|
|
|
|
beatTrail.x = songPosVis.x;
|
|
|
|
|
|
|
|
diffGrp.members[closestBeat].text = getDiff + "ms";
|
|
|
|
offsetsPerBeat[closestBeat] = Std.int(getDiff);
|
|
|
|
}
|
|
|
|
|
|
|
|
function songPosToX(pos:Float):Float
|
|
|
|
{
|
|
|
|
return FlxMath.remapToRange(pos, 0, FlxG.sound.music.length, 0, FlxG.width);
|
|
|
|
}
|
2020-10-06 21:56:14 -04:00
|
|
|
}
|
2022-07-12 19:02:38 -04:00
|
|
|
|
|
|
|
class HomemadeMusic extends FlxSound
|
|
|
|
{
|
2023-01-22 22:25:45 -05:00
|
|
|
public var prevTimestamp:Int = 0;
|
|
|
|
public var timeWithDiff:Float = 0;
|
|
|
|
|
|
|
|
public function new()
|
|
|
|
{
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
var prevTime:Float = 0;
|
|
|
|
|
|
|
|
override function update(elapsed:Float)
|
|
|
|
{
|
|
|
|
super.update(elapsed);
|
|
|
|
if (prevTime != time)
|
|
|
|
{
|
|
|
|
prevTime = time;
|
|
|
|
prevTimestamp = Std.int(Timer.stamp() * 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTimeWithDiff():Float
|
|
|
|
{
|
|
|
|
return time + (Std.int(Timer.stamp() * 1000) - prevTimestamp);
|
|
|
|
}
|
2022-07-12 19:02:38 -04:00
|
|
|
}
|