2023-11-07 09:04:22 +00:00
|
|
|
package funkin.ui.debug.latency;
|
2020-10-07 01:56:14 +00:00
|
|
|
|
2023-07-14 00:27:45 +00:00
|
|
|
import funkin.data.notestyle.NoteStyleRegistry;
|
2020-10-07 01:56:14 +00:00
|
|
|
import flixel.FlxSprite;
|
2022-07-05 18:24:02 +00:00
|
|
|
import flixel.FlxSubState;
|
2022-07-06 19:27:45 +00:00
|
|
|
import flixel.group.FlxGroup;
|
2023-06-22 05:41:01 +00:00
|
|
|
import flixel.group.FlxGroup.FlxTypedGroup;
|
2022-07-06 19:27:45 +00:00
|
|
|
import flixel.math.FlxMath;
|
2023-11-07 09:04:22 +00:00
|
|
|
import funkin.ui.MusicBeatSubState;
|
2023-05-25 22:33:39 +00:00
|
|
|
import flixel.sound.FlxSound;
|
2020-10-07 01:56:14 +00:00
|
|
|
import flixel.text.FlxText;
|
2022-07-05 18:34:08 +00:00
|
|
|
import flixel.util.FlxColor;
|
2022-10-07 04:37:21 +00:00
|
|
|
import funkin.audio.visualize.PolygonSpectogram;
|
2023-06-22 05:41:01 +00:00
|
|
|
import funkin.play.notes.NoteSprite;
|
2023-11-07 09:04:22 +00:00
|
|
|
import funkin.ui.debug.latency.CoolStatsGraph;
|
2022-07-07 00:37:35 +00:00
|
|
|
import openfl.events.KeyboardEvent;
|
2024-01-09 18:16:00 +00:00
|
|
|
import funkin.input.PreciseInputManager;
|
|
|
|
import funkin.play.notes.Strumline;
|
|
|
|
import funkin.play.notes.notestyle.NoteStyle;
|
|
|
|
import funkin.data.notestyle.NoteStyleData;
|
|
|
|
import funkin.data.notestyle.NoteStyleRegistry;
|
2024-01-16 10:37:06 +00:00
|
|
|
import funkin.data.song.SongData.SongNoteData;
|
|
|
|
import haxe.Timer;
|
2020-10-07 01:56:14 +00:00
|
|
|
|
2023-06-09 19:44:29 +00:00
|
|
|
class LatencyState extends MusicBeatSubState
|
2020-10-07 01:56:14 +00:00
|
|
|
{
|
2024-02-10 20:42:29 +00:00
|
|
|
var visualOffsetText:FlxText;
|
2023-01-23 03:25:45 +00:00
|
|
|
var offsetText:FlxText;
|
2024-01-16 10:37:06 +00:00
|
|
|
var noteGrp:Array<SongNoteData>;
|
2024-01-09 18:16:00 +00:00
|
|
|
var strumLine:Strumline;
|
2020-10-07 01:56:14 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
var blocks:FlxTypedGroup<FlxSprite>;
|
2022-07-06 19:27:45 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
var songPosVis:FlxSprite;
|
|
|
|
var songVisFollowVideo:FlxSprite;
|
|
|
|
var songVisFollowAudio:FlxSprite;
|
2022-07-06 19:27:45 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
var beatTrail:FlxSprite;
|
|
|
|
var diffGrp:FlxTypedGroup<FlxText>;
|
|
|
|
var offsetsPerBeat:Array<Int> = [];
|
|
|
|
var swagSong:HomemadeMusic;
|
2022-07-12 23:02:38 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
override function create()
|
|
|
|
{
|
2023-12-13 04:28:16 +00:00
|
|
|
super.create();
|
|
|
|
|
|
|
|
if (FlxG.sound.music != null) FlxG.sound.music.stop();
|
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
swagSong = new HomemadeMusic();
|
|
|
|
swagSong.loadEmbedded(Paths.sound('soundTest'), true);
|
2022-07-12 23:02:38 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
FlxG.sound.music = swagSong;
|
|
|
|
FlxG.sound.music.play();
|
2022-07-06 19:27:45 +00:00
|
|
|
|
2024-01-09 18:16:00 +00:00
|
|
|
PreciseInputManager.instance.onInputPressed.add(function(event:PreciseInputEvent) {
|
2024-01-27 11:16:46 +00:00
|
|
|
generateBeatStuff(event);
|
2024-01-09 18:16:00 +00:00
|
|
|
strumLine.pressKey(event.noteDirection);
|
|
|
|
strumLine.playPress(event.noteDirection);
|
2023-01-23 03:25:45 +00:00
|
|
|
});
|
|
|
|
|
2024-01-09 18:16:00 +00:00
|
|
|
PreciseInputManager.instance.onInputReleased.add(function(event:PreciseInputEvent) {
|
|
|
|
strumLine.playStatic(event.noteDirection);
|
|
|
|
strumLine.releaseKey(event.noteDirection);
|
|
|
|
});
|
2023-01-23 03:25:45 +00:00
|
|
|
|
2023-12-14 21:56:20 +00:00
|
|
|
Conductor.instance.forceBPM(60);
|
2023-01-23 03:25:45 +00:00
|
|
|
|
2024-01-16 10:37:06 +00:00
|
|
|
noteGrp = [];
|
2023-01-23 03:25:45 +00:00
|
|
|
|
|
|
|
diffGrp = new FlxTypedGroup<FlxText>();
|
|
|
|
add(diffGrp);
|
|
|
|
|
2024-01-08 20:46:16 +00:00
|
|
|
for (beat in 0...Math.floor(FlxG.sound.music.length / (Conductor.instance.stepLengthMs * 2)))
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
2024-01-08 20:46:16 +00:00
|
|
|
var beatTick:FlxSprite = new FlxSprite(songPosToX(beat * (Conductor.instance.stepLengthMs * 2)), FlxG.height - 15);
|
2023-01-23 03:25:45 +00:00
|
|
|
beatTick.makeGraphic(2, 15);
|
|
|
|
beatTick.alpha = 0.3;
|
|
|
|
add(beatTick);
|
|
|
|
|
2024-01-08 20:46:16 +00:00
|
|
|
var offsetTxt:FlxText = new FlxText(songPosToX(beat * (Conductor.instance.stepLengthMs * 2)), FlxG.height - 26, 0, "swag");
|
2023-01-23 03:25:45 +00: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)
|
|
|
|
{
|
2024-02-10 20:42:29 +00:00
|
|
|
var block = new FlxSprite(2, ((FlxG.height / 8) + 2) * i).makeGraphic(Std.int(FlxG.height / 8), Std.int((FlxG.height / 8) - 4));
|
|
|
|
block.alpha = 0.1;
|
2023-01-23 03:25:45 +00:00
|
|
|
blocks.add(block);
|
|
|
|
}
|
|
|
|
|
2024-02-10 20:42:29 +00:00
|
|
|
var strumlineBG:FlxSprite = new FlxSprite();
|
|
|
|
add(strumlineBG);
|
2023-01-23 03:25:45 +00:00
|
|
|
|
2024-01-09 18:16:00 +00:00
|
|
|
strumLine = new Strumline(NoteStyleRegistry.instance.fetchDefault(), true);
|
2024-02-10 20:42:29 +00:00
|
|
|
strumLine.screenCenter();
|
2023-01-23 03:25:45 +00:00
|
|
|
add(strumLine);
|
2024-01-16 10:37:06 +00:00
|
|
|
|
2024-02-10 20:42:29 +00:00
|
|
|
strumlineBG.x = strumLine.x;
|
|
|
|
strumlineBG.makeGraphic(Std.int(strumLine.width), FlxG.height, 0xFFFFFFFF);
|
|
|
|
strumlineBG.alpha = 0.1;
|
|
|
|
|
|
|
|
visualOffsetText = new FlxText();
|
|
|
|
visualOffsetText.setFormat(Paths.font("vcr.ttf"), 20);
|
|
|
|
visualOffsetText.x = (FlxG.height / 8) + 10;
|
|
|
|
visualOffsetText.y = 10;
|
|
|
|
visualOffsetText.fieldWidth = strumLine.x - visualOffsetText.x - 10;
|
|
|
|
add(visualOffsetText);
|
|
|
|
|
|
|
|
offsetText = new FlxText();
|
|
|
|
offsetText.setFormat(Paths.font("vcr.ttf"), 20);
|
|
|
|
offsetText.x = strumLine.x + strumLine.width + 10;
|
|
|
|
offsetText.y = 10;
|
|
|
|
offsetText.fieldWidth = FlxG.width - offsetText.x - 10;
|
|
|
|
add(offsetText);
|
|
|
|
|
2024-01-16 10:37:06 +00:00
|
|
|
regenNoteData();
|
|
|
|
}
|
|
|
|
|
|
|
|
function regenNoteData()
|
|
|
|
{
|
|
|
|
for (i in 0...32)
|
|
|
|
{
|
|
|
|
var note:SongNoteData = new SongNoteData((Conductor.instance.stepLengthMs * 2) * i, 1);
|
|
|
|
noteGrp.push(note);
|
|
|
|
}
|
|
|
|
|
|
|
|
strumLine.applyNoteData(noteGrp);
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override function stepHit():Bool
|
|
|
|
{
|
2023-12-14 21:56:20 +00:00
|
|
|
if (Conductor.instance.currentStep % 4 == 2)
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
2023-12-14 21:56:20 +00:00
|
|
|
blocks.members[((Conductor.instance.currentBeat % 8) + 1) % 8].alpha = 0.5;
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return super.stepHit();
|
|
|
|
}
|
|
|
|
|
|
|
|
override function beatHit():Bool
|
|
|
|
{
|
2023-12-14 21:56:20 +00:00
|
|
|
if (Conductor.instance.currentBeat % 8 == 0) blocks.forEach(blok -> {
|
2024-02-10 20:42:29 +00:00
|
|
|
blok.alpha = 0.1;
|
2023-01-23 03:25:45 +00:00
|
|
|
});
|
|
|
|
|
2023-12-14 21:56:20 +00:00
|
|
|
blocks.members[Conductor.instance.currentBeat % 8].alpha = 1;
|
2023-01-23 03:25:45 +00:00
|
|
|
// block.visible = !block.visible;
|
|
|
|
|
|
|
|
return super.beatHit();
|
|
|
|
}
|
|
|
|
|
|
|
|
override function update(elapsed:Float)
|
|
|
|
{
|
|
|
|
/* trace("1: " + swagSong.frfrTime);
|
|
|
|
@:privateAccess
|
|
|
|
trace(FlxG.sound.music._channel.position);
|
|
|
|
*/
|
|
|
|
|
2024-01-16 10:37:06 +00:00
|
|
|
Conductor.instance.update();
|
2023-01-23 03:25:45 +00:00
|
|
|
|
2023-12-14 21:56:20 +00:00
|
|
|
// Conductor.instance.songPosition += (Timer.stamp() * 1000) - FlxG.sound.music.prevTimestamp;
|
2023-01-23 03:25:45 +00:00
|
|
|
|
2023-12-14 21:56:20 +00:00
|
|
|
songPosVis.x = songPosToX(Conductor.instance.songPosition);
|
|
|
|
songVisFollowAudio.x = songPosToX(Conductor.instance.songPosition - Conductor.instance.instrumentalOffset);
|
|
|
|
songVisFollowVideo.x = songPosToX(Conductor.instance.songPosition - Conductor.instance.inputOffset);
|
2023-01-23 03:25:45 +00:00
|
|
|
|
2024-02-10 20:42:29 +00:00
|
|
|
visualOffsetText.text = "Visual Offset: " + Conductor.instance.instrumentalOffset + "ms";
|
|
|
|
visualOffsetText.text += "\nYou can press SPACE+Left/Right to change this value.";
|
|
|
|
|
|
|
|
offsetText.text = "INPUT Offset (Left/Right to change): " + Conductor.instance.inputOffset + "ms";
|
2023-01-23 03:25:45 +00:00
|
|
|
|
|
|
|
var avgOffsetInput:Float = 0;
|
|
|
|
|
|
|
|
for (offsetThing in offsetsPerBeat)
|
|
|
|
avgOffsetInput += offsetThing;
|
|
|
|
|
|
|
|
avgOffsetInput /= offsetsPerBeat.length;
|
|
|
|
|
2024-02-10 20:42:29 +00:00
|
|
|
offsetText.text += "\nEstimated average input offset needed: " + avgOffsetInput;
|
2023-01-23 03:25:45 +00:00
|
|
|
|
2024-01-10 00:57:33 +00:00
|
|
|
var multiply:Int = 10;
|
2023-01-23 03:25:45 +00:00
|
|
|
|
|
|
|
if (FlxG.keys.pressed.SHIFT) multiply = 1;
|
|
|
|
|
2023-12-13 04:28:16 +00:00
|
|
|
if (FlxG.keys.pressed.CONTROL || FlxG.keys.pressed.SPACE)
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
|
|
|
if (FlxG.keys.justPressed.RIGHT)
|
|
|
|
{
|
2024-01-10 00:57:33 +00:00
|
|
|
Conductor.instance.instrumentalOffset += 1 * multiply;
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (FlxG.keys.justPressed.LEFT)
|
|
|
|
{
|
2024-01-10 00:57:33 +00:00
|
|
|
Conductor.instance.instrumentalOffset -= 1 * multiply;
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (FlxG.keys.justPressed.RIGHT)
|
|
|
|
{
|
2024-01-10 00:57:33 +00:00
|
|
|
Conductor.instance.inputOffset += 1 * multiply;
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (FlxG.keys.justPressed.LEFT)
|
|
|
|
{
|
2024-01-10 00:57:33 +00:00
|
|
|
Conductor.instance.inputOffset -= 1 * multiply;
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
super.update(elapsed);
|
|
|
|
}
|
|
|
|
|
2024-01-09 18:16:00 +00:00
|
|
|
function generateBeatStuff(event:PreciseInputEvent)
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
2024-01-09 18:16:00 +00:00
|
|
|
// Conductor.instance.update(swagSong.getTimeWithDiff());
|
|
|
|
|
|
|
|
var inputLatencyMs:Float = haxe.Int64.toInt(PreciseInputManager.getCurrentTimestamp() - event.timestamp) / 1000.0 / 1000.0;
|
|
|
|
trace("input latency: " + inputLatencyMs + "ms");
|
|
|
|
trace("cur timestamp: " + PreciseInputManager.getCurrentTimestamp() + "ns");
|
|
|
|
trace("event timestamp: " + event.timestamp + "ns");
|
2024-01-31 08:24:00 +00:00
|
|
|
trace("songtime: " + Conductor.instance.getTimeWithDiff() + "ms");
|
2023-01-23 03:25:45 +00:00
|
|
|
|
2024-01-16 10:37:06 +00:00
|
|
|
var closestBeat:Int = Math.round(Conductor.instance.getTimeWithDiff() / (Conductor.instance.stepLengthMs * 2)) % diffGrp.members.length;
|
|
|
|
var getDiff:Float = Conductor.instance.getTimeWithDiff() - (closestBeat * (Conductor.instance.stepLengthMs * 2));
|
2024-01-31 08:24:00 +00:00
|
|
|
// getDiff -= Conductor.instance.inputOffset;
|
2024-01-09 18:16:00 +00:00
|
|
|
getDiff -= inputLatencyMs;
|
2023-01-23 03:25:45 +00:00
|
|
|
|
|
|
|
// lil fix for end of song
|
2024-01-08 20:46:16 +00:00
|
|
|
if (closestBeat == 0 && getDiff >= Conductor.instance.stepLengthMs * 2) getDiff -= FlxG.sound.music.length;
|
2023-01-23 03:25:45 +00:00
|
|
|
|
|
|
|
beatTrail.x = songPosVis.x;
|
|
|
|
|
|
|
|
diffGrp.members[closestBeat].text = getDiff + "ms";
|
2024-01-27 11:16:46 +00:00
|
|
|
offsetsPerBeat[closestBeat] = Math.round(getDiff);
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function songPosToX(pos:Float):Float
|
|
|
|
{
|
|
|
|
return FlxMath.remapToRange(pos, 0, FlxG.sound.music.length, 0, FlxG.width);
|
|
|
|
}
|
2020-10-07 01:56:14 +00:00
|
|
|
}
|
2022-07-12 23:02:38 +00:00
|
|
|
|
|
|
|
class HomemadeMusic extends FlxSound
|
|
|
|
{
|
2023-01-23 03:25:45 +00:00
|
|
|
public var prevTimestamp:Int = 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 23:02:38 +00:00
|
|
|
}
|