Funkin/source/funkin/LatencyState.hx

321 lines
7.9 KiB
Haxe
Raw Normal View History

package funkin;
2020-10-06 21:56:14 -04:00
import flixel.FlxSprite;
2022-07-05 14:24:02 -04:00
import flixel.FlxSubState;
2020-10-06 21:56:14 -04:00
import flixel.group.FlxGroup.FlxTypedGroup;
2022-07-06 15:27:45 -04:00
import flixel.group.FlxGroup;
import flixel.math.FlxMath;
2022-07-12 19:02:38 -04:00
import flixel.system.FlxSound;
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;
import funkin.audiovis.PolygonSpectogram;
2022-07-12 19:02:38 -04:00
import funkin.ui.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
2022-07-05 14:24:02 -04:00
class LatencyState extends MusicBeatSubstate
2020-10-06 21:56:14 -04:00
{
var offsetText:FlxText;
var noteGrp:FlxTypedGroup<Note>;
var strumLine:FlxSprite;
2022-07-19 14:19:03 -04:00
var blocks:FlxTypedGroup<FlxSprite>;
2022-07-06 15:27:45 -04:00
var songPosVis:FlxSprite;
2022-07-06 20:37:35 -04:00
var songVisFollowVideo:FlxSprite;
var songVisFollowAudio:FlxSprite;
2022-07-06 15:27:45 -04:00
var beatTrail:FlxSprite;
2022-07-06 20:37:35 -04:00
var diffGrp:FlxTypedGroup<FlxText>;
2022-07-11 15:14:59 -04:00
var offsetsPerBeat:Array<Int> = [];
2022-07-12 19:02:38 -04:00
var swagSong:HomemadeMusic;
var funnyStatsGraph:CoolStatsGraph;
var realStats:CoolStatsGraph;
2022-07-05 14:24:02 -04:00
2020-10-06 21:56:14 -04:00
override function create()
{
2022-07-12 19:02:38 -04:00
swagSong = new HomemadeMusic();
swagSong.loadEmbedded(Paths.sound('soundTest'), true);
FlxG.sound.music = swagSong;
FlxG.sound.music.play();
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);
2022-07-06 20:37:35 -04:00
FlxG.stage.addEventListener(KeyboardEvent.KEY_DOWN, key ->
{
2022-07-12 19:02:38 -04:00
trace(key.charCode);
if (key.charCode == 120)
generateBeatStuff();
2022-07-06 20:58:13 -04:00
trace("\tEVENT PRESS: \t" + FlxG.sound.music.time + " " + Timer.stamp());
2022-07-12 19:02:38 -04:00
// 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());
2022-07-06 20:37:35 -04:00
// trace("EVENT LISTENER: " + key);
});
2022-07-12 19:02:38 -04:00
// FlxG.sound.playMusic(Paths.sound('soundTest'));
// funnyStatsGraph.hi
2022-07-06 20:37:35 -04:00
Conductor.bpm = 60;
2020-10-06 21:56:14 -04:00
noteGrp = new FlxTypedGroup<Note>();
add(noteGrp);
2022-07-06 20:37:35 -04:00
diffGrp = new FlxTypedGroup<FlxText>();
add(diffGrp);
2022-07-06 15:27:45 -04:00
// 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);
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);
2022-07-06 20:37:35 -04:00
var offsetTxt:FlxText = new FlxText(songPosToX(beat * Conductor.crochet), FlxG.height - 26, 0, "swag");
offsetTxt.alpha = 0.5;
diffGrp.add(offsetTxt);
2022-07-11 15:14:59 -04:00
offsetsPerBeat.push(0);
2022-07-06 15:27:45 -04:00
}
2022-07-06 20:37:35 -04:00
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);
2022-07-06 18:45:27 -04:00
2022-07-06 15:27:45 -04:00
songPosVis = new FlxSprite(0, FlxG.height - 20).makeGraphic(2, 20, FlxColor.RED);
add(songPosVis);
2022-07-05 14:34:08 -04:00
2022-07-06 15:27:45 -04:00
beatTrail = new FlxSprite(0, songPosVis.y).makeGraphic(2, 20, FlxColor.PURPLE);
beatTrail.alpha = 0.7;
add(beatTrail);
2022-07-19 14:19:03 -04:00
blocks = new FlxTypedGroup<FlxSprite>();
2022-07-06 15:27:45 -04:00
add(blocks);
for (i in 0...8)
{
var block = new FlxSprite(2, 50 * i).makeGraphic(48, 48);
2022-07-19 14:19:03 -04:00
block.alpha = 0;
2022-07-06 15:27:45 -04:00
blocks.add(block);
}
2022-07-05 14:24:02 -04:00
2020-10-06 21:56:14 -04:00
for (i in 0...32)
{
var note:Note = new Note(Conductor.crochet * i, 1);
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();
}
2022-07-19 14:19:03 -04:00
override function stepHit()
{
if (curStep % 4 == 2)
{
blocks.members[((curBeat % 8) + 1) % 8].alpha = 0.5;
}
super.stepHit();
}
2022-07-05 14:24:02 -04:00
override function beatHit()
{
2022-07-06 15:27:45 -04:00
if (curBeat % 8 == 0)
blocks.forEach(blok ->
{
2022-07-19 14:19:03 -04:00
blok.alpha = 0;
2022-07-06 15:27:45 -04:00
});
2022-07-19 14:19:03 -04:00
blocks.members[curBeat % 8].alpha = 1;
2022-07-06 15:27:45 -04:00
// block.visible = !block.visible;
2022-07-05 14:24:02 -04:00
super.beatHit();
}
2020-10-06 21:56:14 -04:00
override function update(elapsed:Float)
{
2022-07-12 19:02:38 -04:00
/* trace("1: " + swagSong.frfrTime);
@:privateAccess
trace(FlxG.sound.music._channel.position);
*/
funnyStatsGraph.update(FlxG.sound.music.time % 500);
realStats.update(swagSong.getTimeWithDiff() % 500);
2022-07-06 20:37:35 -04:00
if (FlxG.keys.justPressed.S)
{
2022-07-06 20:58:13 -04:00
trace("\tUPDATE PRESS: \t" + FlxG.sound.music.time + " " + Timer.stamp());
2022-07-06 20:37:35 -04:00
}
2022-07-06 18:45:27 -04:00
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;
2022-07-12 19:02:38 -04:00
Conductor.songPosition = swagSong.getTimeWithDiff() - Conductor.offset;
// Conductor.songPosition += (Timer.stamp() * 1000) - FlxG.sound.music.prevTimestamp;
2022-07-06 18:45:27 -04:00
2022-07-06 15:27:45 -04:00
songPosVis.x = songPosToX(Conductor.songPosition);
2022-07-06 20:37:35 -04:00
songVisFollowAudio.x = songPosToX(Conductor.songPosition - Conductor.audioOffset);
songVisFollowVideo.x = songPosToX(Conductor.songPosition - Conductor.visualOffset);
2022-07-06 15:27:45 -04:00
2022-07-06 20:37:35 -04:00
offsetText.text = "AUDIO Offset: " + Conductor.audioOffset + "ms";
offsetText.text += "\nVIDOE Offset: " + Conductor.visualOffset + "ms";
2022-07-06 15:27:45 -04:00
offsetText.text += "\ncurStep: " + curStep;
offsetText.text += "\ncurBeat: " + curBeat;
2020-10-06 21:56:14 -04:00
2022-07-11 15:14:59 -04:00
var avgOffsetInput:Float = 0;
for (offsetThing in offsetsPerBeat)
avgOffsetInput += offsetThing;
avgOffsetInput /= offsetsPerBeat.length;
offsetText.text += "\naverage input offset needed: " + avgOffsetInput;
2022-07-06 15:27:45 -04:00
var multiply:Float = 10;
2020-10-06 21:56:14 -04:00
if (FlxG.keys.pressed.SHIFT)
2022-07-06 15:27:45 -04:00
multiply = 1;
2020-10-06 21:56:14 -04:00
2022-07-06 20:37:35 -04:00
if (FlxG.keys.pressed.CONTROL)
2022-07-06 15:27:45 -04:00
{
2022-07-06 20:37:35 -04:00
if (FlxG.keys.justPressed.RIGHT)
{
Conductor.audioOffset += 1 * multiply;
}
2022-07-06 15:27:45 -04:00
2022-07-06 20:37:35 -04:00
if (FlxG.keys.justPressed.LEFT)
{
Conductor.audioOffset -= 1 * multiply;
}
}
else
2022-07-06 15:27:45 -04:00
{
2022-07-06 20:37:35 -04:00
if (FlxG.keys.justPressed.RIGHT)
{
Conductor.visualOffset += 1 * multiply;
}
if (FlxG.keys.justPressed.LEFT)
{
Conductor.visualOffset -= 1 * multiply;
}
2022-07-06 15:27:45 -04:00
}
2020-10-06 21:56:14 -04:00
2022-07-06 18:45:27 -04:00
/* if (FlxG.keys.justPressed.SPACE)
{
FlxG.sound.music.stop();
2020-10-06 21:56:14 -04:00
2022-07-06 18:45:27 -04:00
FlxG.resetState();
}*/
2020-10-06 21:56:14 -04:00
noteGrp.forEach(function(daNote:Note)
{
2022-07-06 20:37:35 -04:00
daNote.y = (strumLine.y - ((Conductor.songPosition - Conductor.audioOffset) - daNote.data.strumTime) * 0.45);
2020-10-06 21:56:14 -04:00
daNote.x = strumLine.x + 30;
if (daNote.y < strumLine.y)
2022-07-05 14:24:02 -04:00
daNote.alpha = 0.5;
if (daNote.y < 0 - daNote.height)
{
daNote.alpha = 1;
// daNote.data.strumTime += Conductor.crochet * 8;
}
2020-10-06 21:56:14 -04:00
});
super.update(elapsed);
}
2022-07-06 15:27:45 -04:00
2022-07-12 19:02:38 -04:00
function generateBeatStuff()
{
Conductor.songPosition = swagSong.getTimeWithDiff();
var closestBeat:Int = Math.round(Conductor.songPosition / Conductor.crochet) % diffGrp.members.length;
var getDiff:Float = Conductor.songPosition - (closestBeat * Conductor.crochet);
getDiff -= Conductor.visualOffset;
// lil fix for end of song
if (closestBeat == 0 && getDiff >= Conductor.crochet * 2)
getDiff -= FlxG.sound.music.length;
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);
}
2022-07-06 15:27:45 -04:00
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
{
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);
}
}