1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2024-09-20 01:00:53 +00:00
Funkin/source/funkin/Conductor.hx

211 lines
4.8 KiB
Haxe
Raw Normal View History

package funkin;
2020-10-03 06:50:15 +00:00
2022-09-23 04:49:42 +00:00
import flixel.util.FlxSignal;
import funkin.SongLoad.SwagSong;
2022-09-22 10:34:03 +00:00
import funkin.play.song.Song.SongDifficulty;
import funkin.play.song.SongData.ConductorTimeChange;
import funkin.play.song.SongData.SongTimeChange;
typedef BPMChangeEvent =
{
var stepTime:Int;
var songTime:Float;
var bpm:Float;
}
2020-10-03 06:50:15 +00:00
class Conductor
{
/**
2022-09-22 10:34:03 +00:00
* The list of time changes in the song.
* There should be at least one time change (at the beginning of the song) to define the BPM.
*/
2022-09-23 04:49:42 +00:00
private static var timeChanges:Array<SongTimeChange> = [];
/**
2022-09-22 10:34:03 +00:00
* The current time change.
*/
2022-09-23 04:49:42 +00:00
private static var currentTimeChange:SongTimeChange;
2022-09-22 10:34:03 +00:00
/**
* The current position in the song in milliseconds.
* Updated every frame based on the audio position.
*/
public static var songPosition:Float;
/**
* Beats per minute of the current song at the current time.
*/
public static var bpm(get, null):Float = 100;
static function get_bpm():Float
{
if (currentTimeChange == null)
return 100;
return currentTimeChange.bpm;
}
// OLD, replaced with timeChanges.
public static var bpmChangeMap:Array<BPMChangeEvent> = [];
/**
* Duration of a beat in millisecond. Calculated based on bpm.
*/
public static var crochet(get, null):Float;
static function get_crochet():Float
{
return ((60 / bpm) * 1000);
}
/**
2022-09-22 10:34:03 +00:00
* Duration of a step in milliseconds. Calculated based on bpm.
*/
public static var stepCrochet(get, null):Float;
static function get_stepCrochet():Float
{
return crochet / 4;
}
2022-09-22 10:34:03 +00:00
public static var currentBeat(get, null):Float;
static function get_currentBeat():Float
{
return currentBeat;
}
public static var currentStep(get, null):Int;
static function get_currentStep():Int
{
return currentStep;
}
2022-06-02 02:07:42 +00:00
2022-09-23 04:49:42 +00:00
public static var beatHit(default, null):FlxSignal = new FlxSignal();
public static var stepHit(default, null):FlxSignal = new FlxSignal();
2020-10-24 09:19:13 +00:00
public static var lastSongPos:Float;
2022-07-06 19:27:45 +00:00
public static var visualOffset:Float = 0;
public static var audioOffset:Float = 0;
2020-10-03 06:50:15 +00:00
public static var offset:Float = 0;
2022-09-23 04:49:42 +00:00
private function new()
2022-09-22 10:34:03 +00:00
{
}
public static function getLastBPMChange()
{
var lastChange:BPMChangeEvent = {
stepTime: 0,
songTime: 0,
bpm: 0
}
for (i in 0...Conductor.bpmChangeMap.length)
{
if (Conductor.songPosition >= Conductor.bpmChangeMap[i].songTime)
lastChange = Conductor.bpmChangeMap[i];
if (Conductor.songPosition < Conductor.bpmChangeMap[i].songTime)
break;
}
return lastChange;
}
2022-09-22 10:34:03 +00:00
public static function forceBPM(bpm:Float)
{
// TODO: Get rid of this and use song metadata instead.
Conductor.bpm = bpm;
}
/**
* Update the conductor with the current song position.
* BPM, current step, etc. will be re-calculated based on the song position.
*/
public static function update(songPosition:Float)
{
2022-09-23 04:49:42 +00:00
var oldBeat = currentBeat;
var oldStep = currentStep;
songPosition = songPosition;
bpm = Conductor.getLastBPMChange().bpm;
for (i in 0...timeChanges.length)
{
if (songPosition >= timeChanges[i].songTime)
currentTimeChange = timeChanges[i];
if (songPosition < timeChanges[i].songTime)
break;
}
currentStep = (currentTimeChange.beatTime * 4) + (songPosition - currentTimeChange.songTime) / stepCrochet;
currentBeat = Math.floor(currentStep / 4);
// FlxSignals are really cool.
if (currentStep != oldStep)
stepHit.dispatch();
if (currentBeat != oldBeat)
beatHit.dispatch();
2022-09-22 10:34:03 +00:00
}
2020-10-05 00:53:49 +00:00
public static function mapBPMChanges(song:SwagSong)
{
bpmChangeMap = [];
var curBPM:Float = song.bpm;
var totalSteps:Int = 0;
var totalPos:Float = 0;
for (i in 0...SongLoad.getSong().length)
{
if (SongLoad.getSong()[i].changeBPM && SongLoad.getSong()[i].bpm != curBPM)
{
curBPM = SongLoad.getSong()[i].bpm;
var event:BPMChangeEvent = {
stepTime: totalSteps,
songTime: totalPos,
bpm: curBPM
};
bpmChangeMap.push(event);
}
var deltaSteps:Int = SongLoad.getSong()[i].lengthInSteps;
totalSteps += deltaSteps;
totalPos += ((60 / curBPM) * 1000 / 4) * deltaSteps;
}
trace("new BPM map BUDDY " + bpmChangeMap);
}
2022-09-22 10:34:03 +00:00
public static function mapTimeChanges(currentChart:SongDifficulty)
{
var songTimeChanges:Array<SongTimeChange> = currentChart.timeChanges;
timeChanges = [];
2022-09-23 04:49:42 +00:00
for (currentTimeChange in timeChanges)
2022-09-22 10:34:03 +00:00
{
2022-09-23 04:49:42 +00:00
var prevTimeChange:SongTimeChange = timeChanges.length == 0 ? null : timeChanges[timeChanges.length - 1];
/*
if (prevTimeChange != null)
{
var deltaTime:Float = currentTimeChange.timeStamp - prevTimeChange.timeStamp;
var deltaSteps:Int = Math.round(deltaTime / (60 / prevTimeChange.bpm) * 1000 / 4);
currentTimeChange.stepTime = prevTimeChange.stepTime + deltaSteps;
}
else
{
// We know the time and steps of this time change is 0, since this is the first time change.
currentTimeChange.stepTime = 0;
}
*/
2022-09-22 10:34:03 +00:00
timeChanges.push(currentTimeChange);
}
// Done.
}
2020-10-03 06:50:15 +00:00
}