2022-03-08 08:13:53 +00:00
|
|
|
package funkin;
|
2020-10-03 06:50:15 +00:00
|
|
|
|
2022-09-23 04:49:42 +00:00
|
|
|
import flixel.util.FlxSignal;
|
2022-03-08 08:13:53 +00:00
|
|
|
import funkin.SongLoad.SwagSong;
|
2022-09-22 10:34:03 +00:00
|
|
|
import funkin.play.song.Song.SongDifficulty;
|
|
|
|
import funkin.play.song.SongData.SongTimeChange;
|
2021-02-11 22:06:26 +00:00
|
|
|
|
|
|
|
typedef BPMChangeEvent =
|
|
|
|
{
|
|
|
|
var stepTime:Int;
|
|
|
|
var songTime:Float;
|
2021-03-20 16:33:29 +00:00
|
|
|
var bpm:Float;
|
2021-02-11 22:06:26 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 06:50:15 +00:00
|
|
|
class Conductor
|
|
|
|
{
|
2022-03-30 01:56:04 +00:00
|
|
|
/**
|
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-03-30 01:56:04 +00:00
|
|
|
*/
|
2022-09-23 04:49:42 +00:00
|
|
|
private static var timeChanges:Array<SongTimeChange> = [];
|
2022-03-30 01:56:04 +00:00
|
|
|
|
|
|
|
/**
|
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.
|
|
|
|
*/
|
2022-09-26 21:18:19 +00:00
|
|
|
public static var bpm(get, null):Float;
|
2022-09-22 10:34:03 +00:00
|
|
|
|
|
|
|
static function get_bpm():Float
|
|
|
|
{
|
2022-09-26 21:18:19 +00:00
|
|
|
if (bpmOverride != null)
|
|
|
|
return bpmOverride;
|
|
|
|
|
2022-09-22 10:34:03 +00:00
|
|
|
if (currentTimeChange == null)
|
|
|
|
return 100;
|
|
|
|
|
|
|
|
return currentTimeChange.bpm;
|
|
|
|
}
|
|
|
|
|
2022-09-26 21:18:19 +00:00
|
|
|
static var bpmOverride:Null<Float> = null;
|
|
|
|
|
2022-09-22 10:34:03 +00:00
|
|
|
// OLD, replaced with timeChanges.
|
|
|
|
public static var bpmChangeMap:Array<BPMChangeEvent> = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Duration of a beat in millisecond. Calculated based on bpm.
|
2022-03-30 01:56:04 +00:00
|
|
|
*/
|
|
|
|
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.
|
2022-03-30 01:56:04 +00:00
|
|
|
*/
|
|
|
|
public static var stepCrochet(get, null):Float;
|
|
|
|
|
|
|
|
static function get_stepCrochet():Float
|
|
|
|
{
|
|
|
|
return crochet / 4;
|
|
|
|
}
|
|
|
|
|
2022-10-07 04:37:21 +00:00
|
|
|
/**
|
|
|
|
* Current position in the song, in beats.
|
|
|
|
**/
|
|
|
|
public static var currentBeat(default, null):Int;
|
2022-09-22 10:34:03 +00:00
|
|
|
|
2022-10-07 04:37:21 +00:00
|
|
|
/**
|
|
|
|
* Current position in the song, in steps.
|
|
|
|
*/
|
|
|
|
public static var currentStep(default, null):Int;
|
2022-09-22 10:34:03 +00:00
|
|
|
|
2022-10-07 04:37:21 +00:00
|
|
|
/**
|
|
|
|
* Current position in the song, in steps and fractions of a step.
|
|
|
|
*/
|
|
|
|
public static var currentStepTime(default, null):Float;
|
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-10-07 04:37:21 +00:00
|
|
|
// TODO: Add code to update this.
|
|
|
|
public static var beatsPerMeasure:Int = 4;
|
|
|
|
|
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;
|
|
|
|
}
|
2021-02-11 22:06:26 +00:00
|
|
|
|
2022-10-07 04:37:21 +00:00
|
|
|
/**
|
|
|
|
* Forcibly defines the current BPM of the song.
|
|
|
|
* Useful for things like the chart editor that need to manipulate BPM in real time.
|
|
|
|
*
|
|
|
|
* WARNING: Avoid this for things like setting the BPM of the title screen music,
|
|
|
|
* you should have a metadata file for it instead.
|
|
|
|
*/
|
2022-09-22 10:34:03 +00:00
|
|
|
public static function forceBPM(bpm:Float)
|
|
|
|
{
|
2022-09-26 21:18:19 +00:00
|
|
|
Conductor.bpmOverride = bpm;
|
2022-09-22 10:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the conductor with the current song position.
|
|
|
|
* BPM, current step, etc. will be re-calculated based on the song position.
|
2022-09-26 21:18:19 +00:00
|
|
|
*
|
|
|
|
* @param songPosition The current position in the song in milliseconds.
|
|
|
|
* Leave blank to use the FlxG.sound.music position.
|
2022-09-22 10:34:03 +00:00
|
|
|
*/
|
2022-09-26 21:18:19 +00:00
|
|
|
public static function update(songPosition:Float = null)
|
2022-09-22 10:34:03 +00:00
|
|
|
{
|
2022-09-26 21:18:19 +00:00
|
|
|
if (songPosition == null)
|
|
|
|
songPosition = (FlxG.sound.music != null) ? (FlxG.sound.music.time + Conductor.offset) : 0;
|
|
|
|
|
2022-09-23 04:49:42 +00:00
|
|
|
var oldBeat = currentBeat;
|
|
|
|
var oldStep = currentStep;
|
|
|
|
|
2022-09-24 05:28:39 +00:00
|
|
|
Conductor.songPosition = songPosition;
|
2022-09-26 21:18:19 +00:00
|
|
|
// Conductor.bpm = Conductor.getLastBPMChange().bpm;
|
2022-09-23 04:49:42 +00:00
|
|
|
|
2022-09-24 05:28:39 +00:00
|
|
|
currentTimeChange = timeChanges[0];
|
2022-09-23 04:49:42 +00:00
|
|
|
for (i in 0...timeChanges.length)
|
|
|
|
{
|
2022-09-24 05:28:39 +00:00
|
|
|
if (songPosition >= timeChanges[i].timeStamp)
|
2022-09-23 04:49:42 +00:00
|
|
|
currentTimeChange = timeChanges[i];
|
|
|
|
|
2022-09-24 05:28:39 +00:00
|
|
|
if (songPosition < timeChanges[i].timeStamp)
|
2022-09-23 04:49:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-26 21:18:19 +00:00
|
|
|
if (currentTimeChange == null && bpmOverride == null)
|
2022-09-24 05:28:39 +00:00
|
|
|
{
|
|
|
|
trace('WARNING: Conductor is broken, timeChanges is empty.');
|
|
|
|
}
|
2022-09-26 21:18:19 +00:00
|
|
|
else if (currentTimeChange != null)
|
2022-09-24 05:28:39 +00:00
|
|
|
{
|
2022-10-07 04:37:21 +00:00
|
|
|
currentStepTime = (currentTimeChange.beatTime * 4) + (songPosition - currentTimeChange.timeStamp) / stepCrochet;
|
|
|
|
currentStep = Math.floor(currentStepTime);
|
2022-09-24 05:28:39 +00:00
|
|
|
currentBeat = Math.floor(currentStep / 4);
|
|
|
|
}
|
2022-09-26 21:18:19 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Assume a constant BPM equal to the forced value.
|
2022-10-07 04:37:21 +00:00
|
|
|
currentStepTime = (songPosition / stepCrochet);
|
|
|
|
currentStep = Math.floor(currentStepTime);
|
2022-09-26 21:18:19 +00:00
|
|
|
currentBeat = Math.floor(currentStep / 4);
|
|
|
|
}
|
2022-09-23 04:49:42 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2022-09-26 21:18:19 +00:00
|
|
|
@:deprecated // Switch to TimeChanges instead.
|
2021-02-11 22:06:26 +00:00
|
|
|
public static function mapBPMChanges(song:SwagSong)
|
|
|
|
{
|
|
|
|
bpmChangeMap = [];
|
|
|
|
|
2021-03-20 16:33:29 +00:00
|
|
|
var curBPM:Float = song.bpm;
|
2021-02-11 22:06:26 +00:00
|
|
|
var totalSteps:Int = 0;
|
|
|
|
var totalPos:Float = 0;
|
2021-12-06 22:49:05 +00:00
|
|
|
for (i in 0...SongLoad.getSong().length)
|
2021-02-11 22:06:26 +00:00
|
|
|
{
|
2021-12-06 22:49:05 +00:00
|
|
|
if (SongLoad.getSong()[i].changeBPM && SongLoad.getSong()[i].bpm != curBPM)
|
2021-02-11 22:06:26 +00:00
|
|
|
{
|
2021-12-06 22:49:05 +00:00
|
|
|
curBPM = SongLoad.getSong()[i].bpm;
|
2021-02-11 22:06:26 +00:00
|
|
|
var event:BPMChangeEvent = {
|
|
|
|
stepTime: totalSteps,
|
|
|
|
songTime: totalPos,
|
|
|
|
bpm: curBPM
|
|
|
|
};
|
|
|
|
bpmChangeMap.push(event);
|
|
|
|
}
|
|
|
|
|
2021-12-06 22:49:05 +00:00
|
|
|
var deltaSteps:Int = SongLoad.getSong()[i].lengthInSteps;
|
2021-02-11 22:06:26 +00:00
|
|
|
totalSteps += deltaSteps;
|
|
|
|
totalPos += ((60 / curBPM) * 1000 / 4) * deltaSteps;
|
|
|
|
}
|
|
|
|
}
|
2022-09-22 10:34:03 +00:00
|
|
|
|
|
|
|
public static function mapTimeChanges(currentChart:SongDifficulty)
|
|
|
|
{
|
|
|
|
var songTimeChanges:Array<SongTimeChange> = currentChart.timeChanges;
|
|
|
|
|
|
|
|
timeChanges = [];
|
|
|
|
|
2022-09-24 05:28:39 +00:00
|
|
|
for (currentTimeChange in songTimeChanges)
|
2022-09-22 10:34:03 +00:00
|
|
|
{
|
2022-10-07 04:37:21 +00:00
|
|
|
timeChanges.push(currentTimeChange);
|
|
|
|
}
|
2022-09-23 04:49:42 +00:00
|
|
|
|
2022-10-07 04:37:21 +00:00
|
|
|
trace('Done mapping time changes: ' + timeChanges);
|
2022-09-23 04:49:42 +00:00
|
|
|
|
2022-10-07 04:37:21 +00:00
|
|
|
// Done.
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a time in milliseconds, return a time in steps.
|
|
|
|
*/
|
|
|
|
public static function getTimeInSteps(ms:Float):Int
|
|
|
|
{
|
|
|
|
if (timeChanges.length == 0)
|
|
|
|
{
|
|
|
|
// Assume a constant BPM equal to the forced value.
|
|
|
|
return Math.floor(ms / stepCrochet);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var resultStep:Int = 0;
|
|
|
|
|
|
|
|
var lastTimeChange:SongTimeChange = timeChanges[0];
|
|
|
|
for (timeChange in timeChanges)
|
|
|
|
{
|
|
|
|
if (ms >= timeChange.timeStamp)
|
|
|
|
{
|
|
|
|
lastTimeChange = timeChange;
|
|
|
|
resultStep = lastTimeChange.beatTime * 4;
|
2022-09-23 04:49:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-07 04:37:21 +00:00
|
|
|
// This time change is after the requested time.
|
|
|
|
break;
|
2022-09-23 04:49:42 +00:00
|
|
|
}
|
2022-10-07 04:37:21 +00:00
|
|
|
}
|
2022-09-22 10:34:03 +00:00
|
|
|
|
2022-10-07 04:37:21 +00:00
|
|
|
resultStep += Math.floor((ms - lastTimeChange.timeStamp) / stepCrochet);
|
2022-09-24 05:28:39 +00:00
|
|
|
|
2022-10-07 04:37:21 +00:00
|
|
|
return resultStep;
|
|
|
|
}
|
2022-09-22 10:34:03 +00:00
|
|
|
}
|
2020-10-03 06:50:15 +00:00
|
|
|
}
|