Funkin/source/funkin/Conductor.hx

410 lines
12 KiB
Haxe
Raw Normal View History

package funkin;
2020-10-03 02:50:15 -04:00
import funkin.util.Constants;
2022-09-23 00:49:42 -04:00
import flixel.util.FlxSignal;
import flixel.math.FlxMath;
2022-09-22 06:34:03 -04:00
import funkin.play.song.Song.SongDifficulty;
import funkin.play.song.SongData.SongTimeChange;
2023-06-15 00:13:18 -04:00
/**
* A core class which handles musical timing throughout the game,
* both in gameplay and in menus.
2023-06-15 00:13:18 -04:00
*/
2020-10-03 02:50:15 -04:00
class Conductor
{
2023-06-15 00:13:18 -04:00
// 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
/**
* 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
/**
* The current position in the song in milliseconds.
* Updated every frame based on the audio position.
*/
public static var songPosition:Float = 0;
2023-01-22 19:55:30 -05:00
/**
* Beats per minute of the current song at the current time.
*/
public static var bpm(get, null):Float;
static function get_bpm():Float
{
if (bpmOverride != null) return bpmOverride;
2023-01-22 19:55:30 -05:00
if (currentTimeChange == null) return Constants.DEFAULT_BPM;
2023-01-22 19:55:30 -05:00
return currentTimeChange.bpm;
}
2023-06-15 00:13:18 -04:00
/**
* The current value set by `forceBPM`.
* If false, BPM is determined by time changes.
2023-06-15 00:13:18 -04:00
*/
static var bpmOverride:Null<Float> = null;
2023-01-22 19:55:30 -05:00
/**
* Duration of a measure in milliseconds. Calculated based on bpm.
*/
public static var measureLengthMs(get, null):Float;
static function get_measureLengthMs():Float
{
2023-07-02 16:16:49 -04:00
return beatLengthMs * timeSignatureNumerator;
}
2023-01-22 19:55:30 -05:00
/**
* Duration of a beat (quarter note) in milliseconds. Calculated based on bpm.
2023-01-22 19:55:30 -05:00
*/
2023-07-02 16:16:49 -04:00
public static var beatLengthMs(get, null):Float;
2023-01-22 19:55:30 -05:00
2023-07-02 16:16:49 -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.
return ((Constants.SECS_PER_MIN / bpm) * Constants.MS_PER_SEC);
2023-01-22 19:55:30 -05:00
}
/**
* Duration of a step (sixtennth note) in milliseconds. Calculated based on bpm.
2023-01-22 19:55:30 -05:00
*/
2023-07-02 16:16:49 -04:00
public static var stepLengthMs(get, null):Float;
2023-01-22 19:55:30 -05:00
2023-07-02 16:16:49 -04:00
static function get_stepLengthMs():Float
2023-01-22 19:55:30 -05:00
{
2023-07-02 16:16:49 -04:00
return beatLengthMs / timeSignatureNumerator;
2023-01-22 19:55:30 -05:00
}
public static var timeSignatureNumerator(get, null):Int;
static function get_timeSignatureNumerator():Int
{
if (currentTimeChange == null) return Constants.DEFAULT_TIME_SIGNATURE_NUM;
2023-01-22 19:55:30 -05:00
return currentTimeChange.timeSignatureNum;
}
public static var timeSignatureDenominator(get, null):Int;
static function get_timeSignatureDenominator():Int
{
if (currentTimeChange == null) return Constants.DEFAULT_TIME_SIGNATURE_DEN;
2023-01-22 19:55:30 -05:00
return currentTimeChange.timeSignatureDen;
}
2023-07-02 16:16:49 -04:00
/**
* Current position in the song, in measures.
*/
public static var currentMeasure(default, null):Int;
2023-01-22 19:55:30 -05:00
/**
* Current position in the song, in beats.
2023-07-02 16:16:49 -04:00
*/
public static var currentBeat(default, null):Int;
2023-06-15 00:13:18 -04:00
/**
* Current position in the song, in steps.
2023-06-15 00:13:18 -04:00
*/
public static var currentStep(default, null):Int;
2023-06-15 00:13:18 -04:00
2023-07-02 16:16:49 -04:00
/**
* Current position in the song, in measures and fractions of a measure.
*/
public static var currentMeasureTime(default, null):Float;
/**
* Current position in the song, in beats and fractions of a measure.
*/
public static var currentBeatTime(default, null):Float;
2023-06-15 00:13:18 -04:00
/**
* Current position in the song, in steps and fractions of a step.
2023-06-15 00:13:18 -04:00
*/
public static var currentStepTime(default, null):Float;
2023-06-15 00:13:18 -04:00
public static var beatHit(default, null):FlxSignal = new FlxSignal();
public static var stepHit(default, null):FlxSignal = new FlxSignal();
2023-06-15 00:13:18 -04:00
2023-01-22 19:55:30 -05:00
public static var lastSongPos:Float;
public static var visualOffset:Float = 0;
public static var audioOffset:Float = 0;
public static var offset:Float = 0;
2023-01-22 19:55:30 -05:00
2023-07-02 16:46:49 -04:00
public static var beatsPerMeasure(get, null):Float;
2023-01-22 19:55:30 -05:00
2023-07-02 16:46:49 -04:00
static function get_beatsPerMeasure():Float
2023-01-22 19:55:30 -05:00
{
2023-07-02 16:46:49 -04:00
// NOTE: Not always an integer, for example 7/8 is 3.5 beats per measure
2023-07-02 16:16:49 -04:00
return stepsPerMeasure / Constants.STEPS_PER_BEAT;
2023-01-22 19:55:30 -05:00
}
public static var stepsPerMeasure(get, null):Int;
static function get_stepsPerMeasure():Int
{
2023-07-02 16:46:49 -04:00
// TODO: Is this always an integer?
return Std.int(timeSignatureNumerator / timeSignatureDenominator * Constants.STEPS_PER_BEAT * Constants.STEPS_PER_BEAT);
2023-01-22 19:55:30 -05:00
}
function new() {}
2023-01-22 19:55:30 -05:00
/**
* 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.
*/
public static function forceBPM(?bpm:Float = null)
2023-01-22 19:55:30 -05:00
{
if (bpm != null) trace('[CONDUCTOR] Forcing BPM to ' + bpm);
2023-01-22 19:55:30 -05:00
else
trace('[CONDUCTOR] Resetting BPM to default');
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.
*/
public static function update(songPosition:Float = null)
2023-01-22 19:55:30 -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
var oldBeat = currentBeat;
var oldStep = currentStep;
2023-01-22 19:55:30 -05:00
Conductor.songPosition = songPosition;
currentTimeChange = timeChanges[0];
for (i in 0...timeChanges.length)
{
if (songPosition >= timeChanges[i].timeStamp) currentTimeChange = timeChanges[i];
2023-01-22 19:55:30 -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)
{
// roundDecimal prevents representing 8 as 7.9999999
2023-07-02 16:16:49 -04:00
currentStepTime = FlxMath.roundDecimal((currentTimeChange.beatTime * 4) + (songPosition - currentTimeChange.timeStamp) / stepLengthMs, 6);
currentBeatTime = currentStepTime / Constants.STEPS_PER_BEAT;
currentMeasureTime = currentStepTime / stepsPerMeasure;
2023-01-22 19:55:30 -05:00
currentStep = Math.floor(currentStepTime);
2023-07-02 16:16:49 -04:00
currentBeat = Math.floor(currentBeatTime);
currentMeasure = Math.floor(currentMeasureTime);
2023-01-22 19:55:30 -05:00
}
else
{
// Assume a constant BPM equal to the forced value.
2023-07-02 16:16:49 -04:00
currentStepTime = FlxMath.roundDecimal((songPosition / stepLengthMs), 4);
currentBeatTime = currentStepTime / Constants.STEPS_PER_BEAT;
currentMeasureTime = currentStepTime / stepsPerMeasure;
2023-01-22 19:55:30 -05:00
currentStep = Math.floor(currentStepTime);
2023-07-02 16:16:49 -04:00
currentBeat = Math.floor(currentBeatTime);
currentMeasure = Math.floor(currentMeasureTime);
2023-01-22 19:55:30 -05:00
}
// 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
}
public static function mapTimeChanges(songTimeChanges:Array<SongTimeChange>)
2023-01-22 19:55:30 -05:00
{
timeChanges = [];
for (currentTimeChange in songTimeChanges)
{
// TODO: Maybe handle this different?
// Do we care about BPM at negative timestamps?
// Without any custom handling, `currentStepTime` becomes non-zero at `songPosition = 0`.
if (currentTimeChange.timeStamp < 0.0) currentTimeChange.timeStamp = 0.0;
if (currentTimeChange.beatTime == null)
{
if (currentTimeChange.timeStamp <= 0.0)
{
currentTimeChange.beatTime = 0.0;
}
else
{
// Calculate the beat time of this timestamp.
currentTimeChange.beatTime = 0.0;
if (currentTimeChange.timeStamp > 0.0 && timeChanges.length > 0)
{
var prevTimeChange:SongTimeChange = timeChanges[timeChanges.length - 1];
currentTimeChange.beatTime = prevTimeChange.beatTime
+ ((currentTimeChange.timeStamp - prevTimeChange.timeStamp) * prevTimeChange.bpm / Constants.SECS_PER_MIN / Constants.MS_PER_SEC)
+ 0.01;
}
}
}
2023-01-22 19:55:30 -05:00
timeChanges.push(currentTimeChange);
}
trace('Done mapping time changes: ' + timeChanges);
// Update currentStepTime
Conductor.update(Conductor.songPosition);
2023-01-22 19:55:30 -05:00
}
/**
* Given a time in milliseconds, return a time in steps.
*/
public static function getTimeInSteps(ms:Float):Float
2023-01-22 19:55:30 -05:00
{
if (timeChanges.length == 0)
{
// Assume a constant BPM equal to the forced value.
2023-07-02 16:16:49 -04:00
return Math.floor(ms / stepLengthMs);
2023-01-22 19:55:30 -05:00
}
else
{
var resultStep:Float = 0;
2023-01-22 19:55:30 -05:00
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;
}
}
var lastStepLengthMs:Float = ((Constants.SECS_PER_MIN / lastTimeChange.bpm) * Constants.MS_PER_SEC) / timeSignatureNumerator;
var resultFractionalStep:Float = (ms - lastTimeChange.timeStamp) / lastStepLengthMs;
resultStep += resultFractionalStep; // Math.floor();
2023-01-22 19:55:30 -05:00
return resultStep;
}
}
2023-07-19 01:30:23 -04:00
/**
* Given a time in steps and fractional steps, return a time in milliseconds.
*/
public static function getStepTimeInMs(stepTime:Float):Float
{
if (timeChanges.length == 0)
{
// Assume a constant BPM equal to the forced value.
return stepTime * stepLengthMs;
}
else
{
var resultMs:Float = 0;
var lastTimeChange:SongTimeChange = timeChanges[0];
for (timeChange in timeChanges)
{
if (stepTime >= timeChange.beatTime * 4)
{
lastTimeChange = timeChange;
resultMs = lastTimeChange.timeStamp;
}
else
{
// This time change is after the requested time.
break;
}
}
var lastStepLengthMs:Float = ((Constants.SECS_PER_MIN / lastTimeChange.bpm) * Constants.MS_PER_SEC) / timeSignatureNumerator;
resultMs += (stepTime - lastTimeChange.beatTime * 4) * lastStepLengthMs;
2023-07-19 01:30:23 -04:00
return resultMs;
}
}
/**
* Given a time in beats and fractional beats, return a time in milliseconds.
*/
public static function getBeatTimeInMs(beatTime:Float):Float
{
if (timeChanges.length == 0)
{
// Assume a constant BPM equal to the forced value.
return beatTime * stepLengthMs * Constants.STEPS_PER_BEAT;
}
else
{
var resultMs:Float = 0;
var lastTimeChange:SongTimeChange = timeChanges[0];
for (timeChange in timeChanges)
{
if (beatTime >= timeChange.beatTime)
{
lastTimeChange = timeChange;
resultMs = lastTimeChange.timeStamp;
}
else
{
// This time change is after the requested time.
break;
}
}
var lastStepLengthMs:Float = ((Constants.SECS_PER_MIN / lastTimeChange.bpm) * Constants.MS_PER_SEC) / timeSignatureNumerator;
resultMs += (beatTime - lastTimeChange.beatTime) * lastStepLengthMs * Constants.STEPS_PER_BEAT;
2023-07-19 01:30:23 -04:00
return resultMs;
}
}
public static function reset():Void
{
beatHit.removeAll();
stepHit.removeAll();
mapTimeChanges([]);
forceBPM(null);
update(0);
}
2020-10-03 02:50:15 -04:00
}