2022-03-08 03:13:53 -05:00
|
|
|
package funkin;
|
2020-10-03 02:50:15 -04:00
|
|
|
|
2023-07-02 15:33:34 -04:00
|
|
|
import funkin.util.Constants;
|
2022-09-23 00:49:42 -04:00
|
|
|
import flixel.util.FlxSignal;
|
2023-07-02 15:33:34 -04:00
|
|
|
import flixel.math.FlxMath;
|
2023-09-08 17:46:44 -04:00
|
|
|
import funkin.data.song.SongData.SongTimeChange;
|
2023-10-31 14:43:01 -04:00
|
|
|
import funkin.data.song.SongDataUtils;
|
2024-01-09 19:57:33 -05:00
|
|
|
import funkin.save.Save;
|
2024-01-16 05:37:06 -05:00
|
|
|
import haxe.Timer;
|
2024-02-12 20:06:43 -05:00
|
|
|
import flixel.sound.FlxSound;
|
2021-02-11 17:06:26 -05:00
|
|
|
|
2023-06-15 00:13:18 -04:00
|
|
|
/**
|
2023-07-02 15:33:34 -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
|
|
|
*/
|
2024-01-02 14:14:26 -05:00
|
|
|
@:nullSafety
|
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
|
|
|
|
2023-12-14 16:56:20 -05:00
|
|
|
/**
|
|
|
|
* The current instance of the Conductor.
|
|
|
|
* If one doesn't currently exist, a new one will be created.
|
|
|
|
*
|
|
|
|
* You can also do stuff like store a reference to the Conductor and pass it around or temporarily replace it,
|
|
|
|
* or have a second Conductor running at the same time, or other weird stuff like that if you need to.
|
|
|
|
*/
|
2024-03-22 02:47:56 -04:00
|
|
|
public static var instance(get, never):Conductor;
|
|
|
|
|
|
|
|
static var _instance:Null<Conductor> = null;
|
|
|
|
|
2023-12-14 16:56:20 -05:00
|
|
|
/**
|
2024-03-22 19:51:47 -04:00
|
|
|
* Signal fired when the current static Conductor instance advances to a new measure.
|
2023-12-14 16:56:20 -05:00
|
|
|
*/
|
|
|
|
public static var measureHit(default, null):FlxSignal = new FlxSignal();
|
|
|
|
|
2024-03-22 19:51:47 -04:00
|
|
|
/**
|
|
|
|
* Signal fired when THIS Conductor instance advances to a new measure.
|
|
|
|
* TODO: This naming sucks but we can't make a static and instance field with the same name!
|
|
|
|
*/
|
|
|
|
public var onMeasureHit(default, null):FlxSignal = new FlxSignal();
|
|
|
|
|
2023-12-14 16:56:20 -05:00
|
|
|
/**
|
|
|
|
* Signal fired when the current Conductor instance advances to a new beat.
|
|
|
|
*/
|
|
|
|
public static var beatHit(default, null):FlxSignal = new FlxSignal();
|
|
|
|
|
2024-03-22 19:51:47 -04:00
|
|
|
/**
|
|
|
|
* Signal fired when THIS Conductor instance advances to a new beat.
|
|
|
|
* TODO: This naming sucks but we can't make a static and instance field with the same name!
|
|
|
|
*/
|
|
|
|
public var onBeatHit(default, null):FlxSignal = new FlxSignal();
|
|
|
|
|
2023-12-14 16:56:20 -05:00
|
|
|
/**
|
|
|
|
* Signal fired when the current Conductor instance advances to a new step.
|
|
|
|
*/
|
|
|
|
public static var stepHit(default, null):FlxSignal = new FlxSignal();
|
|
|
|
|
2024-03-22 19:51:47 -04:00
|
|
|
/**
|
|
|
|
* Signal fired when THIS Conductor instance advances to a new step.
|
|
|
|
* TODO: This naming sucks but we can't make a static and instance field with the same name!
|
|
|
|
*/
|
|
|
|
public var onStepHit(default, null):FlxSignal = new FlxSignal();
|
|
|
|
|
2023-07-02 15:33:34 -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.
|
|
|
|
*/
|
2023-12-14 16:56:20 -05:00
|
|
|
var timeChanges:Array<SongTimeChange> = [];
|
2023-07-02 15:33:34 -04:00
|
|
|
|
|
|
|
/**
|
2023-11-28 21:44:09 -05:00
|
|
|
* The most recent time change for the current song position.
|
2023-07-02 15:33:34 -04:00
|
|
|
*/
|
2024-01-02 14:14:26 -05:00
|
|
|
public var currentTimeChange(default, null):Null<SongTimeChange>;
|
2023-07-02 15:33:34 -04:00
|
|
|
|
2023-01-22 19:55:30 -05:00
|
|
|
/**
|
|
|
|
* The current position in the song in milliseconds.
|
2023-12-14 16:56:20 -05:00
|
|
|
* Update this every frame based on the audio position using `Conductor.instance.update()`.
|
2023-01-22 19:55:30 -05:00
|
|
|
*/
|
2023-12-14 16:56:20 -05:00
|
|
|
public var songPosition(default, null):Float = 0;
|
2023-01-22 19:55:30 -05:00
|
|
|
|
2024-01-16 05:37:06 -05:00
|
|
|
var prevTimestamp:Float = 0;
|
|
|
|
var prevTime:Float = 0;
|
|
|
|
|
2023-01-22 19:55:30 -05:00
|
|
|
/**
|
|
|
|
* Beats per minute of the current song at the current time.
|
|
|
|
*/
|
2023-12-14 16:56:20 -05:00
|
|
|
public var bpm(get, never):Float;
|
2023-01-22 19:55:30 -05:00
|
|
|
|
2023-12-14 16:56:20 -05:00
|
|
|
function get_bpm():Float
|
2023-01-22 19:55:30 -05:00
|
|
|
{
|
2023-01-22 22:25:45 -05:00
|
|
|
if (bpmOverride != null) return bpmOverride;
|
2023-01-22 19:55:30 -05:00
|
|
|
|
2023-07-02 15:33:34 -04:00
|
|
|
if (currentTimeChange == null) return Constants.DEFAULT_BPM;
|
2023-01-22 19:55:30 -05:00
|
|
|
|
|
|
|
return currentTimeChange.bpm;
|
|
|
|
}
|
|
|
|
|
2023-12-08 01:15:22 -05:00
|
|
|
/**
|
|
|
|
* Beats per minute of the current song at the start time.
|
|
|
|
*/
|
2023-12-14 16:56:20 -05:00
|
|
|
public var startingBPM(get, never):Float;
|
2023-12-08 01:15:22 -05:00
|
|
|
|
2023-12-14 16:56:20 -05:00
|
|
|
function get_startingBPM():Float
|
2023-12-08 01:15:22 -05:00
|
|
|
{
|
|
|
|
if (bpmOverride != null) return bpmOverride;
|
|
|
|
|
|
|
|
var timeChange = timeChanges[0];
|
|
|
|
if (timeChange == null) return Constants.DEFAULT_BPM;
|
|
|
|
|
|
|
|
return timeChange.bpm;
|
|
|
|
}
|
|
|
|
|
2023-06-15 00:13:18 -04:00
|
|
|
/**
|
2023-07-02 15:33:34 -04:00
|
|
|
* The current value set by `forceBPM`.
|
|
|
|
* If false, BPM is determined by time changes.
|
2023-06-15 00:13:18 -04:00
|
|
|
*/
|
2023-12-14 16:56:20 -05:00
|
|
|
var bpmOverride:Null<Float> = null;
|
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.
|
|
|
|
*/
|
2023-12-14 16:56:20 -05:00
|
|
|
public var measureLengthMs(get, never):Float;
|
2023-06-06 17:38:31 -04:00
|
|
|
|
2023-12-14 16:56:20 -05:00
|
|
|
function get_measureLengthMs():Float
|
2023-06-06 17:38:31 -04:00
|
|
|
{
|
2023-07-02 16:16:49 -04:00
|
|
|
return beatLengthMs * timeSignatureNumerator;
|
2023-06-06 17:38:31 -04:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:55:30 -05:00
|
|
|
/**
|
2023-07-22 20:16:43 -04:00
|
|
|
* Duration of a beat (quarter note) in milliseconds. Calculated based on bpm.
|
2023-01-22 19:55:30 -05:00
|
|
|
*/
|
2023-12-14 16:56:20 -05:00
|
|
|
public var beatLengthMs(get, never):Float;
|
2023-01-22 19:55:30 -05:00
|
|
|
|
2023-12-14 16:56:20 -05:00
|
|
|
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-07-22 20:16:43 -04:00
|
|
|
return ((Constants.SECS_PER_MIN / bpm) * Constants.MS_PER_SEC);
|
2023-01-22 19:55:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-07-22 20:16:43 -04:00
|
|
|
* Duration of a step (sixtennth note) in milliseconds. Calculated based on bpm.
|
2023-01-22 19:55:30 -05:00
|
|
|
*/
|
2023-12-14 16:56:20 -05:00
|
|
|
public var stepLengthMs(get, never):Float;
|
2023-01-22 19:55:30 -05:00
|
|
|
|
2023-12-14 16:56:20 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-03-16 22:20:22 -04:00
|
|
|
/**
|
|
|
|
* The numerator for the current time signature (the `3` in `3/4`).
|
|
|
|
*/
|
2023-12-14 16:56:20 -05:00
|
|
|
public var timeSignatureNumerator(get, never):Int;
|
2023-01-22 19:55:30 -05:00
|
|
|
|
2023-12-14 16:56:20 -05:00
|
|
|
function get_timeSignatureNumerator():Int
|
2023-01-22 19:55:30 -05:00
|
|
|
{
|
2023-07-02 15:33:34 -04:00
|
|
|
if (currentTimeChange == null) return Constants.DEFAULT_TIME_SIGNATURE_NUM;
|
2023-01-22 19:55:30 -05:00
|
|
|
|
|
|
|
return currentTimeChange.timeSignatureNum;
|
|
|
|
}
|
|
|
|
|
2024-03-16 22:20:22 -04:00
|
|
|
/**
|
|
|
|
* The denominator for the current time signature (the `4` in `3/4`).
|
|
|
|
*/
|
2023-12-14 16:56:20 -05:00
|
|
|
public var timeSignatureDenominator(get, never):Int;
|
2023-01-22 19:55:30 -05:00
|
|
|
|
2023-12-14 16:56:20 -05:00
|
|
|
function get_timeSignatureDenominator():Int
|
2023-01-22 19:55:30 -05:00
|
|
|
{
|
2023-07-02 15:33:34 -04:00
|
|
|
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.
|
|
|
|
*/
|
2024-01-02 14:14:26 -05:00
|
|
|
public var currentMeasure(default, null):Int = 0;
|
2023-07-02 16:16:49 -04:00
|
|
|
|
2023-01-22 19:55:30 -05:00
|
|
|
/**
|
2023-07-02 15:33:34 -04:00
|
|
|
* Current position in the song, in beats.
|
2023-07-02 16:16:49 -04:00
|
|
|
*/
|
2024-01-02 14:14:26 -05:00
|
|
|
public var currentBeat(default, null):Int = 0;
|
2023-06-15 00:13:18 -04:00
|
|
|
|
|
|
|
/**
|
2023-07-02 15:33:34 -04:00
|
|
|
* Current position in the song, in steps.
|
2023-06-15 00:13:18 -04:00
|
|
|
*/
|
2024-01-02 14:14:26 -05:00
|
|
|
public var currentStep(default, null):Int = 0;
|
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.
|
|
|
|
*/
|
2024-01-02 14:14:26 -05:00
|
|
|
public var currentMeasureTime(default, null):Float = 0;
|
2023-07-02 16:16:49 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current position in the song, in beats and fractions of a measure.
|
|
|
|
*/
|
2024-01-02 14:14:26 -05:00
|
|
|
public var currentBeatTime(default, null):Float = 0;
|
2023-07-02 16:16:49 -04:00
|
|
|
|
2023-06-15 00:13:18 -04:00
|
|
|
/**
|
2023-07-02 15:33:34 -04:00
|
|
|
* Current position in the song, in steps and fractions of a step.
|
2023-06-15 00:13:18 -04:00
|
|
|
*/
|
2024-01-02 14:14:26 -05:00
|
|
|
public var currentStepTime(default, null):Float = 0;
|
2023-06-15 00:13:18 -04:00
|
|
|
|
2023-12-05 02:44:57 -05:00
|
|
|
/**
|
|
|
|
* An offset tied to the current chart file to compensate for a delay in the instrumental.
|
|
|
|
*/
|
2023-12-14 16:56:20 -05:00
|
|
|
public var instrumentalOffset:Float = 0;
|
2023-12-05 02:44:57 -05:00
|
|
|
|
2023-12-08 01:15:22 -05:00
|
|
|
/**
|
|
|
|
* The instrumental offset, in terms of steps.
|
|
|
|
*/
|
2023-12-14 16:56:20 -05:00
|
|
|
public var instrumentalOffsetSteps(get, never):Float;
|
2023-12-08 01:15:22 -05:00
|
|
|
|
2023-12-14 16:56:20 -05:00
|
|
|
function get_instrumentalOffsetSteps():Float
|
2023-12-08 01:15:22 -05:00
|
|
|
{
|
|
|
|
var startingStepLengthMs:Float = ((Constants.SECS_PER_MIN / startingBPM) * Constants.MS_PER_SEC) / timeSignatureNumerator;
|
|
|
|
|
|
|
|
return instrumentalOffset / startingStepLengthMs;
|
|
|
|
}
|
|
|
|
|
2023-12-05 02:44:57 -05:00
|
|
|
/**
|
|
|
|
* An offset tied to the file format of the audio file being played.
|
|
|
|
*/
|
2023-12-14 16:56:20 -05:00
|
|
|
public var formatOffset:Float = 0;
|
2023-12-05 02:44:57 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An offset set by the user to compensate for input lag.
|
2024-02-12 22:44:59 -05:00
|
|
|
* No matter if you're using a local conductor or not, this always loads
|
|
|
|
* to/from the save file
|
2023-12-05 02:44:57 -05:00
|
|
|
*/
|
2024-01-09 19:57:33 -05:00
|
|
|
public var inputOffset(get, set):Int;
|
2024-01-09 19:48:01 -05:00
|
|
|
|
2024-02-12 22:14:35 -05:00
|
|
|
/**
|
|
|
|
* An offset set by the user to compensate for audio/visual lag
|
2024-02-12 22:44:59 -05:00
|
|
|
* No matter if you're using a local conductor or not, this always loads
|
|
|
|
* to/from the save file
|
2024-02-12 22:14:35 -05:00
|
|
|
*/
|
|
|
|
public var audioVisualOffset(get, set):Int;
|
|
|
|
|
2024-01-09 19:57:33 -05:00
|
|
|
function get_inputOffset():Int
|
2024-01-09 19:48:01 -05:00
|
|
|
{
|
2024-04-30 20:03:24 -04:00
|
|
|
return Save?.instance?.options?.inputOffset ?? 0;
|
2024-01-09 19:48:01 -05:00
|
|
|
}
|
|
|
|
|
2024-01-09 19:57:33 -05:00
|
|
|
function set_inputOffset(value:Int):Int
|
2024-01-09 19:48:01 -05:00
|
|
|
{
|
2024-03-23 15:34:37 -04:00
|
|
|
Save.instance.options.inputOffset = value;
|
|
|
|
Save.instance.flush();
|
|
|
|
return Save.instance.options.inputOffset;
|
2024-01-09 19:48:01 -05:00
|
|
|
}
|
2023-01-22 19:55:30 -05:00
|
|
|
|
2024-02-12 22:14:35 -05:00
|
|
|
function get_audioVisualOffset():Int
|
|
|
|
{
|
2024-04-30 20:03:24 -04:00
|
|
|
return Save?.instance?.options?.audioVisualOffset ?? 0;
|
2024-02-12 22:14:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function set_audioVisualOffset(value:Int):Int
|
|
|
|
{
|
2024-03-23 15:34:37 -04:00
|
|
|
Save.instance.options.audioVisualOffset = value;
|
|
|
|
Save.instance.flush();
|
|
|
|
return Save.instance.options.audioVisualOffset;
|
2024-02-12 22:14:35 -05:00
|
|
|
}
|
|
|
|
|
2024-09-21 06:33:47 -04:00
|
|
|
public var combinedOffset(get, never):Float;
|
|
|
|
|
|
|
|
function get_combinedOffset():Float
|
|
|
|
{
|
|
|
|
return instrumentalOffset + audioVisualOffset + inputOffset;
|
|
|
|
}
|
|
|
|
|
2023-12-08 01:15:22 -05:00
|
|
|
/**
|
|
|
|
* The number of beats in a measure. May be fractional depending on the time signature.
|
|
|
|
*/
|
2023-12-14 16:56:20 -05:00
|
|
|
public var beatsPerMeasure(get, never):Float;
|
2023-01-22 19:55:30 -05:00
|
|
|
|
2023-12-14 16:56:20 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-12-08 01:15:22 -05:00
|
|
|
/**
|
|
|
|
* The number of steps in a measure.
|
|
|
|
* TODO: I don't think this can be fractional?
|
|
|
|
*/
|
2023-12-14 16:56:20 -05:00
|
|
|
public var stepsPerMeasure(get, never):Int;
|
2023-01-22 19:55:30 -05:00
|
|
|
|
2023-12-14 16:56:20 -05:00
|
|
|
function get_stepsPerMeasure():Int
|
2023-01-22 19:55:30 -05:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2024-03-22 19:51:47 -04:00
|
|
|
/**
|
|
|
|
* Reset the Conductor, replacing the current instance with a fresh one.
|
|
|
|
*/
|
|
|
|
public static function reset():Void
|
|
|
|
{
|
|
|
|
set_instance(new Conductor());
|
|
|
|
}
|
|
|
|
|
|
|
|
static function dispatchMeasureHit():Void
|
|
|
|
{
|
|
|
|
Conductor.measureHit.dispatch();
|
|
|
|
}
|
|
|
|
|
|
|
|
static function dispatchBeatHit():Void
|
|
|
|
{
|
|
|
|
Conductor.beatHit.dispatch();
|
|
|
|
}
|
|
|
|
|
|
|
|
static function dispatchStepHit():Void
|
|
|
|
{
|
|
|
|
Conductor.stepHit.dispatch();
|
|
|
|
}
|
|
|
|
|
|
|
|
static function setupSingleton(input:Conductor):Void
|
|
|
|
{
|
|
|
|
input.onMeasureHit.add(dispatchMeasureHit);
|
|
|
|
|
|
|
|
input.onBeatHit.add(dispatchBeatHit);
|
|
|
|
|
|
|
|
input.onStepHit.add(dispatchStepHit);
|
|
|
|
}
|
|
|
|
|
|
|
|
static function clearSingleton(input:Conductor):Void
|
|
|
|
{
|
|
|
|
input.onMeasureHit.remove(dispatchMeasureHit);
|
|
|
|
|
|
|
|
input.onBeatHit.remove(dispatchBeatHit);
|
|
|
|
|
|
|
|
input.onStepHit.remove(dispatchStepHit);
|
|
|
|
}
|
|
|
|
|
|
|
|
static function get_instance():Conductor
|
|
|
|
{
|
|
|
|
if (Conductor._instance == null) set_instance(new Conductor());
|
|
|
|
if (Conductor._instance == null) throw "Could not initialize singleton Conductor!";
|
|
|
|
return Conductor._instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
static function set_instance(instance:Conductor):Conductor
|
|
|
|
{
|
|
|
|
// Use _instance in here to avoid recursion
|
|
|
|
if (Conductor._instance != null) clearSingleton(Conductor._instance);
|
|
|
|
|
|
|
|
Conductor._instance = instance;
|
|
|
|
|
|
|
|
if (Conductor._instance != null) setupSingleton(Conductor._instance);
|
|
|
|
|
|
|
|
return Conductor._instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The constructor.
|
|
|
|
*/
|
2023-12-14 16:56:20 -05:00
|
|
|
public function new() {}
|
2023-07-02 15:33:34 -04:00
|
|
|
|
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.
|
2024-03-22 19:51:47 -04:00
|
|
|
* We should probably deprecate this in the future.
|
2023-01-22 19:55:30 -05:00
|
|
|
*/
|
2024-03-16 22:20:22 -04:00
|
|
|
public function forceBPM(?bpm:Float):Void
|
2023-01-22 19:55:30 -05:00
|
|
|
{
|
2023-08-22 04:27:30 -04:00
|
|
|
if (bpm != null)
|
|
|
|
{
|
|
|
|
trace('[CONDUCTOR] Forcing BPM to ${bpm}');
|
|
|
|
}
|
2023-01-22 19:55:30 -05:00
|
|
|
else
|
2023-08-22 04:27:30 -04:00
|
|
|
{
|
2024-03-16 22:20:22 -04:00
|
|
|
trace('[CONDUCTOR] Resetting BPM to default');
|
2023-08-22 04:27:30 -04:00
|
|
|
}
|
|
|
|
|
2023-12-14 16:56:20 -05:00
|
|
|
this.bpmOverride = bpm;
|
2023-01-22 19:55:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2024-02-12 22:14:35 -05:00
|
|
|
* @param applyOffsets If it should apply the instrumentalOffset + formatOffset + audioVisualOffset
|
2023-01-22 19:55:30 -05:00
|
|
|
*/
|
2024-02-12 22:14:35 -05:00
|
|
|
public function update(?songPos:Float, applyOffsets:Bool = true, forceDispatch:Bool = false)
|
2023-01-22 19:55:30 -05:00
|
|
|
{
|
2024-09-19 03:02:59 -04:00
|
|
|
var currentTime:Float = (FlxG.sound.music != null) ? FlxG.sound.music.time : 0.0;
|
|
|
|
var currentLength:Float = (FlxG.sound.music != null) ? FlxG.sound.music.length : 0.0;
|
|
|
|
|
2023-12-14 16:56:20 -05:00
|
|
|
if (songPos == null)
|
2023-12-05 02:44:57 -05:00
|
|
|
{
|
2024-09-19 03:02:59 -04:00
|
|
|
songPos = currentTime;
|
2023-12-05 02:44:57 -05:00
|
|
|
}
|
2023-01-22 19:55:30 -05:00
|
|
|
|
2024-03-23 14:38:50 -04:00
|
|
|
// Take into account instrumental and file format song offsets.
|
2024-10-03 07:28:01 -04:00
|
|
|
songPos += applyOffsets ? (combinedOffset) : 0;
|
2024-03-23 14:38:50 -04:00
|
|
|
|
2024-03-16 22:20:22 -04:00
|
|
|
var oldMeasure:Float = this.currentMeasure;
|
|
|
|
var oldBeat:Float = this.currentBeat;
|
|
|
|
var oldStep:Float = this.currentStep;
|
2023-01-22 19:55:30 -05:00
|
|
|
|
2024-09-19 04:20:16 -04:00
|
|
|
// If the song is playing, limit the song position to the length of the song or beginning of the song.
|
|
|
|
if (FlxG.sound.music != null && FlxG.sound.music.playing)
|
|
|
|
{
|
|
|
|
this.songPosition = Math.min(currentLength, Math.max(0, songPos));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.songPosition = songPos;
|
|
|
|
}
|
|
|
|
|
2023-12-05 02:44:57 -05:00
|
|
|
// Set the song position we are at (for purposes of calculating note positions, etc).
|
2023-01-22 19:55:30 -05:00
|
|
|
|
|
|
|
currentTimeChange = timeChanges[0];
|
2023-12-14 16:56:20 -05:00
|
|
|
if (this.songPosition > 0.0)
|
2023-01-22 19:55:30 -05:00
|
|
|
{
|
2023-12-08 01:15:22 -05:00
|
|
|
for (i in 0...timeChanges.length)
|
|
|
|
{
|
2023-12-14 16:56:20 -05:00
|
|
|
if (this.songPosition >= timeChanges[i].timeStamp) currentTimeChange = timeChanges[i];
|
2023-01-22 19:55:30 -05:00
|
|
|
|
2023-12-14 16:56:20 -05:00
|
|
|
if (this.songPosition < timeChanges[i].timeStamp) break;
|
2023-12-08 01:15:22 -05:00
|
|
|
}
|
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.');
|
|
|
|
}
|
2023-12-14 16:56:20 -05:00
|
|
|
else if (currentTimeChange != null && this.songPosition > 0.0)
|
2023-01-22 19:55:30 -05:00
|
|
|
{
|
2023-07-02 15:33:34 -04:00
|
|
|
// roundDecimal prevents representing 8 as 7.9999999
|
2024-09-19 03:02:59 -04:00
|
|
|
this.currentStepTime = FlxMath.roundDecimal((currentTimeChange.beatTime * Constants.STEPS_PER_BEAT)
|
|
|
|
+ (this.songPosition - currentTimeChange.timeStamp) / stepLengthMs, 6);
|
2023-12-14 16:56:20 -05:00
|
|
|
this.currentBeatTime = currentStepTime / Constants.STEPS_PER_BEAT;
|
|
|
|
this.currentMeasureTime = currentStepTime / stepsPerMeasure;
|
|
|
|
this.currentStep = Math.floor(currentStepTime);
|
|
|
|
this.currentBeat = Math.floor(currentBeatTime);
|
|
|
|
this.currentMeasure = Math.floor(currentMeasureTime);
|
2023-01-22 19:55:30 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Assume a constant BPM equal to the forced value.
|
2023-12-14 16:56:20 -05:00
|
|
|
this.currentStepTime = FlxMath.roundDecimal((songPosition / stepLengthMs), 4);
|
|
|
|
this.currentBeatTime = currentStepTime / Constants.STEPS_PER_BEAT;
|
|
|
|
this.currentMeasureTime = currentStepTime / stepsPerMeasure;
|
|
|
|
this.currentStep = Math.floor(currentStepTime);
|
|
|
|
this.currentBeat = Math.floor(currentBeatTime);
|
|
|
|
this.currentMeasure = Math.floor(currentMeasureTime);
|
2023-01-22 19:55:30 -05:00
|
|
|
}
|
|
|
|
|
2024-03-22 19:51:47 -04:00
|
|
|
// FlxSignals are really cool.
|
|
|
|
if (currentStep != oldStep)
|
2023-05-17 16:42:58 -04:00
|
|
|
{
|
2024-03-22 19:51:47 -04:00
|
|
|
this.onStepHit.dispatch();
|
|
|
|
}
|
2023-01-22 19:55:30 -05:00
|
|
|
|
2024-03-22 19:51:47 -04:00
|
|
|
if (currentBeat != oldBeat)
|
|
|
|
{
|
|
|
|
this.onBeatHit.dispatch();
|
|
|
|
}
|
2023-12-08 01:15:22 -05:00
|
|
|
|
2024-03-22 19:51:47 -04:00
|
|
|
if (currentMeasure != oldMeasure)
|
|
|
|
{
|
|
|
|
this.onMeasureHit.dispatch();
|
2023-12-08 01:15:22 -05:00
|
|
|
}
|
2024-01-16 05:37:06 -05:00
|
|
|
|
|
|
|
// only update the timestamp if songPosition actually changed
|
|
|
|
// which it doesn't do every frame!
|
|
|
|
if (prevTime != this.songPosition)
|
|
|
|
{
|
|
|
|
// Update the timestamp for use in-between frames
|
|
|
|
prevTime = this.songPosition;
|
|
|
|
prevTimestamp = Std.int(Timer.stamp() * 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Can be called in-between frames, usually for input related things
|
|
|
|
* that can potentially get processed on exact milliseconds/timestmaps.
|
|
|
|
* If you need song position, use `Conductor.instance.songPosition` instead
|
|
|
|
* for use in update() related functions.
|
2024-02-12 20:06:43 -05:00
|
|
|
* @param soundToCheck Which FlxSound object to check, defaults to FlxG.sound.music if no input
|
2024-01-16 05:37:06 -05:00
|
|
|
* @return Float
|
|
|
|
*/
|
2024-02-12 20:06:43 -05:00
|
|
|
public function getTimeWithDiff(?soundToCheck:FlxSound):Float
|
2024-01-16 05:37:06 -05:00
|
|
|
{
|
2024-02-12 20:06:43 -05:00
|
|
|
if (soundToCheck == null) soundToCheck = FlxG.sound.music;
|
2024-01-27 06:16:46 -05:00
|
|
|
// trace(this.songPosition);
|
|
|
|
|
|
|
|
@:privateAccess
|
2024-02-12 20:06:43 -05:00
|
|
|
this.songPosition = soundToCheck._channel.position;
|
2024-01-27 06:16:46 -05:00
|
|
|
// return this.songPosition + (Std.int(Timer.stamp() * 1000) - prevTimestamp);
|
|
|
|
// trace("\t--> " + this.songPosition);
|
|
|
|
return this.songPosition;
|
2023-01-22 19:55:30 -05:00
|
|
|
}
|
|
|
|
|
2024-03-16 22:20:22 -04:00
|
|
|
/**
|
|
|
|
* Apply the `SongTimeChange` data from the song metadata to this Conductor.
|
|
|
|
* @param songTimeChanges The SongTimeChanges.
|
|
|
|
*/
|
|
|
|
public function mapTimeChanges(songTimeChanges:Array<SongTimeChange>):Void
|
2023-01-22 19:55:30 -05:00
|
|
|
{
|
|
|
|
timeChanges = [];
|
|
|
|
|
2023-10-31 14:43:01 -04:00
|
|
|
// Sort in place just in case it's out of order.
|
|
|
|
SongDataUtils.sortTimeChanges(songTimeChanges);
|
|
|
|
|
2024-03-16 22:20:22 -04:00
|
|
|
for (songTimeChange in songTimeChanges)
|
2023-01-22 19:55:30 -05:00
|
|
|
{
|
2023-07-02 15:33:34 -04:00
|
|
|
// TODO: Maybe handle this different?
|
|
|
|
// Do we care about BPM at negative timestamps?
|
|
|
|
// Without any custom handling, `currentStepTime` becomes non-zero at `songPosition = 0`.
|
2024-03-16 22:20:22 -04:00
|
|
|
if (songTimeChange.timeStamp < 0.0) songTimeChange.timeStamp = 0.0;
|
2023-07-02 15:33:34 -04:00
|
|
|
|
2024-03-16 22:20:22 -04:00
|
|
|
if (songTimeChange.timeStamp <= 0.0)
|
2023-07-02 15:33:34 -04:00
|
|
|
{
|
2024-03-16 22:20:22 -04:00
|
|
|
songTimeChange.beatTime = 0.0;
|
2024-01-05 17:22:01 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Calculate the beat time of this timestamp.
|
2024-03-16 22:20:22 -04:00
|
|
|
songTimeChange.beatTime = 0.0;
|
2024-01-05 17:22:01 -05:00
|
|
|
|
2024-03-16 22:20:22 -04:00
|
|
|
if (songTimeChange.timeStamp > 0.0 && timeChanges.length > 0)
|
2023-07-02 15:33:34 -04:00
|
|
|
{
|
2024-01-05 17:22:01 -05:00
|
|
|
var prevTimeChange:SongTimeChange = timeChanges[timeChanges.length - 1];
|
2024-03-16 22:20:22 -04:00
|
|
|
songTimeChange.beatTime = FlxMath.roundDecimal(prevTimeChange.beatTime
|
|
|
|
+ ((songTimeChange.timeStamp - prevTimeChange.timeStamp) * prevTimeChange.bpm / Constants.SECS_PER_MIN / Constants.MS_PER_SEC),
|
2024-01-05 17:22:01 -05:00
|
|
|
4);
|
2023-07-02 15:33:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-16 22:20:22 -04:00
|
|
|
timeChanges.push(songTimeChange);
|
2023-01-22 19:55:30 -05:00
|
|
|
}
|
|
|
|
|
2023-08-22 04:27:30 -04:00
|
|
|
if (timeChanges.length > 0)
|
|
|
|
{
|
2023-09-08 15:19:03 -04:00
|
|
|
trace('Done mapping time changes: ${timeChanges}');
|
2023-08-22 04:27:30 -04:00
|
|
|
}
|
2023-01-22 19:55:30 -05:00
|
|
|
|
2023-07-02 15:33:34 -04:00
|
|
|
// Update currentStepTime
|
2024-02-12 22:14:35 -05:00
|
|
|
this.update(this.songPosition, false);
|
2023-01-22 19:55:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a time in milliseconds, return a time in steps.
|
2024-03-16 22:20:22 -04:00
|
|
|
* @param ms The time in milliseconds.
|
|
|
|
* @return The time in steps.
|
2023-01-22 19:55:30 -05:00
|
|
|
*/
|
2023-12-14 16:56:20 -05:00
|
|
|
public 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
|
|
|
|
{
|
2023-07-02 15:33:34 -04:00
|
|
|
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;
|
2024-05-18 03:04:27 -04:00
|
|
|
resultStep = lastTimeChange.beatTime * Constants.STEPS_PER_BEAT;
|
2023-01-22 19:55:30 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// This time change is after the requested time.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-22 20:16:43 -04:00
|
|
|
var lastStepLengthMs:Float = ((Constants.SECS_PER_MIN / lastTimeChange.bpm) * Constants.MS_PER_SEC) / timeSignatureNumerator;
|
|
|
|
var resultFractionalStep:Float = (ms - lastTimeChange.timeStamp) / lastStepLengthMs;
|
2024-03-16 22:20:22 -04:00
|
|
|
resultStep += resultFractionalStep;
|
2023-01-22 19:55:30 -05:00
|
|
|
|
|
|
|
return resultStep;
|
|
|
|
}
|
|
|
|
}
|
2023-07-02 15:33:34 -04:00
|
|
|
|
2023-07-19 01:30:23 -04:00
|
|
|
/**
|
|
|
|
* Given a time in steps and fractional steps, return a time in milliseconds.
|
2024-03-16 22:20:22 -04:00
|
|
|
* @param stepTime The time in steps.
|
|
|
|
* @return The time in milliseconds.
|
2023-07-19 01:30:23 -04:00
|
|
|
*/
|
2023-12-14 16:56:20 -05:00
|
|
|
public function getStepTimeInMs(stepTime:Float):Float
|
2023-07-19 01:30:23 -04:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2024-05-18 03:04:27 -04:00
|
|
|
if (stepTime >= timeChange.beatTime * Constants.STEPS_PER_BEAT)
|
2023-07-19 01:30:23 -04:00
|
|
|
{
|
|
|
|
lastTimeChange = timeChange;
|
|
|
|
resultMs = lastTimeChange.timeStamp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// This time change is after the requested time.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-22 20:16:43 -04:00
|
|
|
var lastStepLengthMs:Float = ((Constants.SECS_PER_MIN / lastTimeChange.bpm) * Constants.MS_PER_SEC) / timeSignatureNumerator;
|
2024-05-18 03:04:27 -04:00
|
|
|
resultMs += (stepTime - lastTimeChange.beatTime * Constants.STEPS_PER_BEAT) * lastStepLengthMs;
|
2023-07-19 01:30:23 -04:00
|
|
|
|
|
|
|
return resultMs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a time in beats and fractional beats, return a time in milliseconds.
|
2024-03-16 22:20:22 -04:00
|
|
|
* @param beatTime The time in beats.
|
|
|
|
* @return The time in milliseconds.
|
2023-07-19 01:30:23 -04:00
|
|
|
*/
|
2023-12-14 16:56:20 -05:00
|
|
|
public function getBeatTimeInMs(beatTime:Float):Float
|
2023-07-19 01:30:23 -04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-22 20:16:43 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-12 22:14:35 -05:00
|
|
|
/**
|
2024-03-23 14:38:50 -04:00
|
|
|
* Adds Conductor fields to the Flixel debugger variable display.
|
|
|
|
* @param conductorToUse The conductor to use. Defaults to `Conductor.instance`.
|
2024-02-12 22:14:35 -05:00
|
|
|
*/
|
2024-03-27 01:52:58 -04:00
|
|
|
public static function watchQuick(?target:Conductor):Void
|
2023-07-02 15:33:34 -04:00
|
|
|
{
|
2024-03-27 01:52:58 -04:00
|
|
|
if (target == null) target = Conductor.instance;
|
2023-07-02 15:33:34 -04:00
|
|
|
|
2024-03-27 01:52:58 -04:00
|
|
|
FlxG.watch.addQuick('songPosition', target.songPosition);
|
|
|
|
FlxG.watch.addQuick('bpm', target.bpm);
|
|
|
|
FlxG.watch.addQuick('currentMeasureTime', target.currentMeasureTime);
|
|
|
|
FlxG.watch.addQuick('currentBeatTime', target.currentBeatTime);
|
|
|
|
FlxG.watch.addQuick('currentStepTime', target.currentStepTime);
|
2023-07-02 15:33:34 -04:00
|
|
|
}
|
2020-10-03 02:50:15 -04:00
|
|
|
}
|