2022-03-08 03:13:53 -05:00
|
|
|
package funkin;
|
2020-10-03 02:50:15 -04:00
|
|
|
|
2023-06-15 00:13:18 -04:00
|
|
|
import funkin.play.song.SongData.SongTimeChange;
|
2022-09-23 00:49:42 -04:00
|
|
|
import flixel.util.FlxSignal;
|
2022-09-22 06:34:03 -04:00
|
|
|
import funkin.play.song.Song.SongDifficulty;
|
2021-02-11 17:06:26 -05:00
|
|
|
|
|
|
|
typedef BPMChangeEvent =
|
|
|
|
{
|
2023-01-22 19:55:30 -05:00
|
|
|
var stepTime:Int;
|
|
|
|
var songTime:Float;
|
|
|
|
var bpm:Float;
|
2021-02-11 17:06:26 -05:00
|
|
|
}
|
|
|
|
|
2023-06-15 00:13:18 -04:00
|
|
|
/**
|
|
|
|
* A global source of truth for timing information.
|
|
|
|
*/
|
2020-10-03 02:50:15 -04:00
|
|
|
class Conductor
|
|
|
|
{
|
2023-06-15 00:13:18 -04:00
|
|
|
static final STEPS_PER_BEAT:Int = 4;
|
|
|
|
|
|
|
|
// onBeatHit is called every quarter note
|
|
|
|
// onStepHit is called every sixteenth note
|
|
|
|
// 4/4 = 4 beats per measure = 16 steps per measure
|
|
|
|
// 120 BPM = 120 quarter notes per minute = 2 onBeatHit per second
|
|
|
|
// 120 BPM = 480 sixteenth notes per minute = 8 onStepHit per second
|
|
|
|
// 60 BPM = 60 quarter notes per minute = 1 onBeatHit per second
|
|
|
|
// 60 BPM = 240 sixteenth notes per minute = 4 onStepHit per second
|
|
|
|
// 3/4 = 3 beats per measure = 12 steps per measure
|
|
|
|
// (IDENTICAL TO 4/4 but shorter measure length)
|
|
|
|
// 120 BPM = 120 quarter notes per minute = 2 onBeatHit per second
|
|
|
|
// 120 BPM = 480 sixteenth notes per minute = 8 onStepHit per second
|
|
|
|
// 60 BPM = 60 quarter notes per minute = 1 onBeatHit per second
|
|
|
|
// 60 BPM = 240 sixteenth notes per minute = 4 onStepHit per second
|
|
|
|
// 7/8 = 3.5 beats per measure = 14 steps per measure
|
2022-09-22 06:34:03 -04:00
|
|
|
|
2023-01-22 19:55:30 -05: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;
|
|
|
|
|
|
|
|
static function get_bpm():Float
|
|
|
|
{
|
2023-01-22 22:25:45 -05:00
|
|
|
if (bpmOverride != null) return bpmOverride;
|
2023-01-22 19:55:30 -05:00
|
|
|
|
2023-01-22 22:25:45 -05:00
|
|
|
if (currentTimeChange == null) return 100;
|
2023-01-22 19:55:30 -05:00
|
|
|
|
|
|
|
return currentTimeChange.bpm;
|
|
|
|
}
|
|
|
|
|
|
|
|
static var bpmOverride:Null<Float> = null;
|
|
|
|
|
2023-06-15 00:13:18 -04:00
|
|
|
/**
|
|
|
|
* Current position in the song, in whole measures.
|
|
|
|
*/
|
|
|
|
public static var currentMeasure(default, null):Int;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current position in the song, in whole beats.
|
|
|
|
**/
|
|
|
|
public static var currentBeat(default, null):Int;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current position in the song, in whole steps.
|
|
|
|
*/
|
|
|
|
public static var currentStep(default, null):Int;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current position in the song, in steps and fractions of a step.
|
|
|
|
*/
|
|
|
|
public static var currentStepTime(default, null):Float;
|
2023-01-22 19:55:30 -05:00
|
|
|
|
2023-06-06 17:38:31 -04:00
|
|
|
/**
|
|
|
|
* Duration of a measure in milliseconds. Calculated based on bpm.
|
|
|
|
*/
|
|
|
|
public static var measureLengthMs(get, null):Float;
|
|
|
|
|
|
|
|
static function get_measureLengthMs():Float
|
|
|
|
{
|
2023-06-15 00:13:18 -04:00
|
|
|
return beatLengthMs * timeSignatureNumerator;
|
2023-06-06 17:38:31 -04:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:55:30 -05:00
|
|
|
/**
|
2023-06-15 00:13:18 -04:00
|
|
|
* Duration of a beat (quarter note) in milliseconds. Calculated based on bpm.
|
2023-01-22 19:55:30 -05:00
|
|
|
*/
|
2023-06-15 00:13:18 -04:00
|
|
|
public static var beatLengthMs(get, null):Float;
|
2023-01-22 19:55:30 -05:00
|
|
|
|
2023-06-15 00:13:18 -04:00
|
|
|
static function get_beatLengthMs():Float
|
2023-01-22 19:55:30 -05:00
|
|
|
{
|
2023-06-15 00:13:18 -04:00
|
|
|
// Tied directly to BPM.
|
2023-01-22 19:55:30 -05:00
|
|
|
return ((60 / bpm) * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-06-15 00:13:18 -04:00
|
|
|
* Duration of a step (sixteenth) in milliseconds. Calculated based on bpm.
|
2023-01-22 19:55:30 -05:00
|
|
|
*/
|
2023-06-15 00:13:18 -04:00
|
|
|
public static var stepLengthMs(get, null):Float;
|
2023-01-22 19:55:30 -05:00
|
|
|
|
2023-06-15 00:13:18 -04:00
|
|
|
static function get_stepLengthMs():Float
|
2023-01-22 19:55:30 -05:00
|
|
|
{
|
2023-06-15 00:13:18 -04:00
|
|
|
return beatLengthMs / STEPS_PER_BEAT;
|
2023-01-22 19:55:30 -05:00
|
|
|
}
|
|
|
|
|
2023-06-15 00:13:18 -04:00
|
|
|
/**
|
|
|
|
* The numerator of the current time signature (number of notes in a measure)
|
|
|
|
*/
|
2023-01-22 19:55:30 -05:00
|
|
|
public static var timeSignatureNumerator(get, null):Int;
|
|
|
|
|
|
|
|
static function get_timeSignatureNumerator():Int
|
|
|
|
{
|
2023-01-22 22:25:45 -05:00
|
|
|
if (currentTimeChange == null) return 4;
|
2023-01-22 19:55:30 -05:00
|
|
|
|
|
|
|
return currentTimeChange.timeSignatureNum;
|
|
|
|
}
|
|
|
|
|
2023-06-15 00:13:18 -04:00
|
|
|
/**
|
|
|
|
* The numerator of the current time signature (length of notes in a measure)
|
|
|
|
*/
|
2023-01-22 19:55:30 -05:00
|
|
|
public static var timeSignatureDenominator(get, null):Int;
|
|
|
|
|
|
|
|
static function get_timeSignatureDenominator():Int
|
|
|
|
{
|
2023-01-22 22:25:45 -05:00
|
|
|
if (currentTimeChange == null) return 4;
|
2023-01-22 19:55:30 -05:00
|
|
|
|
|
|
|
return currentTimeChange.timeSignatureDen;
|
|
|
|
}
|
|
|
|
|
2023-06-15 00:13:18 -04:00
|
|
|
public static var offset:Float = 0;
|
|
|
|
|
|
|
|
// TODO: What's the difference between visualOffset and audioOffset?
|
|
|
|
public static var visualOffset:Float = 0;
|
|
|
|
public static var audioOffset:Float = 0;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Signals
|
|
|
|
//
|
2023-01-22 19:55:30 -05:00
|
|
|
|
|
|
|
/**
|
2023-06-15 00:13:18 -04:00
|
|
|
* Signal that is dispatched every measure.
|
|
|
|
* At 120 BPM 4/4, this is dispatched every 2 seconds.
|
|
|
|
* At 120 BPM 3/4, this is dispatched every 1.5 seconds.
|
2023-01-22 19:55:30 -05:00
|
|
|
*/
|
2023-06-15 00:13:18 -04:00
|
|
|
public static var measureHit(default, null):FlxSignal = new FlxSignal();
|
2023-01-22 19:55:30 -05:00
|
|
|
|
|
|
|
/**
|
2023-06-15 00:13:18 -04:00
|
|
|
* Signal that is dispatched every beat.
|
|
|
|
* At 120 BPM 4/4, this is dispatched every 0.5 seconds.
|
|
|
|
* At 120 BPM 3/4, this is dispatched every 0.5 seconds.
|
2023-01-22 19:55:30 -05:00
|
|
|
*/
|
|
|
|
public static var beatHit(default, null):FlxSignal = new FlxSignal();
|
2023-06-15 00:13:18 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Signal that is dispatched when a step is hit.
|
|
|
|
* At 120 BPM 4/4, this is dispatched every 0.125 seconds.
|
|
|
|
* At 120 BPM 3/4, this is dispatched every 0.125 seconds.
|
|
|
|
*/
|
2023-01-22 19:55:30 -05:00
|
|
|
public static var stepHit(default, null):FlxSignal = new FlxSignal();
|
|
|
|
|
2023-06-15 00:13:18 -04:00
|
|
|
//
|
|
|
|
// Internal Variables
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
static var timeChanges:Array<SongTimeChange> = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current time change.
|
|
|
|
*/
|
|
|
|
static var currentTimeChange:SongTimeChange;
|
|
|
|
|
2023-01-22 19:55:30 -05:00
|
|
|
public static var lastSongPos:Float;
|
|
|
|
|
2023-06-15 00:13:18 -04:00
|
|
|
/**
|
|
|
|
* The number of beats (whole notes) in a measure.
|
|
|
|
*/
|
2023-01-22 19:55:30 -05:00
|
|
|
public static var beatsPerMeasure(get, null):Int;
|
|
|
|
|
|
|
|
static function get_beatsPerMeasure():Int
|
|
|
|
{
|
|
|
|
return timeSignatureNumerator;
|
|
|
|
}
|
|
|
|
|
2023-06-15 00:13:18 -04:00
|
|
|
/**
|
|
|
|
* The number of steps (quarter-notes) in a measure.
|
|
|
|
*/
|
2023-01-22 19:55:30 -05:00
|
|
|
public static var stepsPerMeasure(get, null):Int;
|
|
|
|
|
|
|
|
static function get_stepsPerMeasure():Int
|
|
|
|
{
|
2023-06-15 00:13:18 -04:00
|
|
|
// This is always 4, b
|
2023-01-22 19:55:30 -05:00
|
|
|
return timeSignatureNumerator * 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Forcibly defines the current BPM of the song.
|
|
|
|
* Useful for things like the chart editor that need to manipulate BPM in real time.
|
2023-06-08 16:30:45 -04:00
|
|
|
*
|
2023-01-22 19:55:30 -05:00
|
|
|
* Set to null to reset to the BPM defined by the timeChanges.
|
2023-06-08 16:30:45 -04:00
|
|
|
*
|
2023-01-22 19:55:30 -05:00
|
|
|
* WARNING: Avoid this for things like setting the BPM of the title screen music,
|
|
|
|
* you should have a metadata file for it instead.
|
|
|
|
*/
|
2023-06-15 00:13:18 -04:00
|
|
|
public static function forceBPM(?bpm:Float = null):Void
|
2023-01-22 19:55:30 -05:00
|
|
|
{
|
2023-06-15 00:13:18 -04:00
|
|
|
if (bpm != null)
|
|
|
|
{
|
|
|
|
trace('[CONDUCTOR] Forcing BPM to ' + bpm);
|
|
|
|
}
|
2023-01-22 19:55:30 -05:00
|
|
|
else
|
2023-06-15 00:13:18 -04:00
|
|
|
{
|
2023-01-22 19:55:30 -05:00
|
|
|
trace('[CONDUCTOR] Resetting BPM to default');
|
2023-06-15 00:13:18 -04:00
|
|
|
}
|
2023-01-22 19:55:30 -05:00
|
|
|
Conductor.bpmOverride = bpm;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the conductor with the current song position.
|
|
|
|
* BPM, current step, etc. will be re-calculated based on the song position.
|
2023-06-08 16:30:45 -04:00
|
|
|
*
|
2023-01-22 19:55:30 -05:00
|
|
|
* @param songPosition The current position in the song in milliseconds.
|
|
|
|
* Leave blank to use the FlxG.sound.music position.
|
|
|
|
*/
|
2023-06-15 00:13:18 -04:00
|
|
|
public static function update(songPosition:Float = null):Void
|
2023-01-22 19:55:30 -05:00
|
|
|
{
|
2023-01-22 22:25:45 -05:00
|
|
|
if (songPosition == null) songPosition = (FlxG.sound.music != null) ? FlxG.sound.music.time + Conductor.offset : 0.0;
|
2023-01-22 19:55:30 -05:00
|
|
|
|
2023-06-15 00:13:18 -04:00
|
|
|
var oldMeasure:Int = currentMeasure;
|
|
|
|
var oldBeat:Int = currentBeat;
|
|
|
|
var oldStep:Int = currentStep;
|
2023-01-22 19:55:30 -05:00
|
|
|
|
|
|
|
Conductor.songPosition = songPosition;
|
|
|
|
|
|
|
|
currentTimeChange = timeChanges[0];
|
|
|
|
for (i in 0...timeChanges.length)
|
|
|
|
{
|
2023-01-22 22:25:45 -05:00
|
|
|
if (songPosition >= timeChanges[i].timeStamp) currentTimeChange = timeChanges[i];
|
2023-01-22 19:55:30 -05:00
|
|
|
|
2023-01-22 22:25:45 -05:00
|
|
|
if (songPosition < timeChanges[i].timeStamp) break;
|
2023-01-22 19:55:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (currentTimeChange == null && bpmOverride == null && FlxG.sound.music != null)
|
|
|
|
{
|
|
|
|
trace('WARNING: Conductor is broken, timeChanges is empty.');
|
|
|
|
}
|
|
|
|
else if (currentTimeChange != null)
|
|
|
|
{
|
2023-06-15 00:13:18 -04:00
|
|
|
currentStepTime = (currentTimeChange.beatTime * 4) + (songPosition - currentTimeChange.timeStamp) / stepLengthMs;
|
2023-01-22 19:55:30 -05:00
|
|
|
currentStep = Math.floor(currentStepTime);
|
|
|
|
currentBeat = Math.floor(currentStep / 4);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Assume a constant BPM equal to the forced value.
|
2023-06-15 00:13:18 -04:00
|
|
|
currentStepTime = (songPosition / stepLengthMs);
|
2023-01-22 19:55:30 -05:00
|
|
|
currentStep = Math.floor(currentStepTime);
|
|
|
|
currentBeat = Math.floor(currentStep / 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlxSignals are really cool.
|
2023-05-17 16:42:58 -04:00
|
|
|
if (currentStep != oldStep)
|
|
|
|
{
|
|
|
|
stepHit.dispatch();
|
|
|
|
}
|
2023-01-22 19:55:30 -05:00
|
|
|
|
2023-05-17 16:42:58 -04:00
|
|
|
if (currentBeat != oldBeat)
|
|
|
|
{
|
|
|
|
beatHit.dispatch();
|
|
|
|
}
|
2023-01-22 19:55:30 -05:00
|
|
|
|
2023-06-15 00:13:18 -04:00
|
|
|
if (currentMeasure != oldMeasure)
|
2023-01-22 19:55:30 -05:00
|
|
|
{
|
2023-06-15 00:13:18 -04:00
|
|
|
measureHit.dispatch();
|
2023-01-22 19:55:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-15 00:13:18 -04:00
|
|
|
public static function mapTimeChanges(songTimeChanges:Array<SongTimeChange>):Void
|
2023-01-22 19:55:30 -05:00
|
|
|
{
|
|
|
|
timeChanges = [];
|
|
|
|
|
|
|
|
for (currentTimeChange in songTimeChanges)
|
|
|
|
{
|
|
|
|
timeChanges.push(currentTimeChange);
|
|
|
|
}
|
|
|
|
|
|
|
|
trace('Done mapping time changes: ' + timeChanges);
|
|
|
|
|
|
|
|
// 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.
|
2023-06-15 00:13:18 -04:00
|
|
|
return Math.floor(ms / stepLengthMs);
|
2023-01-22 19:55:30 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var resultStep:Int = 0;
|
|
|
|
|
|
|
|
var lastTimeChange:SongTimeChange = timeChanges[0];
|
|
|
|
for (timeChange in timeChanges)
|
|
|
|
{
|
|
|
|
if (ms >= timeChange.timeStamp)
|
|
|
|
{
|
|
|
|
lastTimeChange = timeChange;
|
|
|
|
resultStep = lastTimeChange.beatTime * 4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// This time change is after the requested time.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-15 00:13:18 -04:00
|
|
|
resultStep += Math.floor((ms - lastTimeChange.timeStamp) / stepLengthMs);
|
2023-01-22 19:55:30 -05:00
|
|
|
|
|
|
|
return resultStep;
|
|
|
|
}
|
|
|
|
}
|
2020-10-03 02:50:15 -04:00
|
|
|
}
|