2023-05-25 18:34:26 -04:00
|
|
|
package funkin.audio;
|
2022-03-08 03:13:53 -05:00
|
|
|
|
2021-09-20 11:32:35 -04:00
|
|
|
import flixel.group.FlxGroup.FlxTypedGroup;
|
2023-05-25 18:34:26 -04:00
|
|
|
import flixel.sound.FlxSound;
|
2021-09-20 11:32:35 -04:00
|
|
|
|
2023-05-25 18:34:26 -04:00
|
|
|
/**
|
|
|
|
* A group of FlxSounds that are all synced together.
|
|
|
|
* Unlike FlxSoundGroup, you cann also control their time and pitch.
|
|
|
|
*/
|
|
|
|
class SoundGroup extends FlxTypedGroup<FlxSound>
|
2021-09-20 11:32:35 -04:00
|
|
|
{
|
2023-01-22 22:25:45 -05:00
|
|
|
public var time(get, set):Float;
|
|
|
|
|
|
|
|
public var volume(get, set):Float;
|
|
|
|
|
|
|
|
public var pitch(get, set):Float;
|
|
|
|
|
|
|
|
public function new()
|
|
|
|
{
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2023-05-25 18:34:26 -04:00
|
|
|
@:deprecated("Create sound files and call add() instead")
|
|
|
|
public static function build(song:String, ?files:Array<String> = null):SoundGroup
|
2023-01-22 22:25:45 -05:00
|
|
|
{
|
2023-05-25 18:34:26 -04:00
|
|
|
var result = new SoundGroup();
|
2023-01-22 22:25:45 -05:00
|
|
|
|
|
|
|
if (files == null)
|
|
|
|
{
|
|
|
|
// Add an empty voice.
|
|
|
|
result.add(new FlxSound());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (sndFile in files)
|
|
|
|
{
|
|
|
|
var snd:FlxSound = new FlxSound().loadEmbedded(Paths.voices(song, '$sndFile'));
|
|
|
|
FlxG.sound.list.add(snd); // adds it to sound group for proper volumes
|
|
|
|
result.add(snd); // adds it to main group for other shit
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-25 18:34:26 -04:00
|
|
|
* Finds the largest deviation from the desired time inside this SoundGroup.
|
2023-06-08 16:30:45 -04:00
|
|
|
*
|
2023-01-22 22:25:45 -05:00
|
|
|
* @param targetTime The time to check against.
|
2023-05-25 18:34:26 -04:00
|
|
|
* If none is provided, it checks the time of all members against the first member of this SoundGroup.
|
2023-01-22 22:25:45 -05:00
|
|
|
* @return The largest deviation from the target time found.
|
|
|
|
*/
|
|
|
|
public function checkSyncError(?targetTime:Float):Float
|
|
|
|
{
|
|
|
|
var error:Float = 0;
|
|
|
|
|
2023-05-25 18:34:26 -04:00
|
|
|
forEachAlive(function(snd) {
|
2023-01-22 22:25:45 -05:00
|
|
|
if (targetTime == null) targetTime = snd.time;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var diff:Float = snd.time - targetTime;
|
|
|
|
if (Math.abs(diff) > Math.abs(error)) error = diff;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2023-05-25 18:34:26 -04:00
|
|
|
/**
|
|
|
|
* Add a sound to the group.
|
|
|
|
*/
|
|
|
|
public override function add(sound:FlxSound):FlxSound
|
|
|
|
{
|
|
|
|
var result:FlxSound = super.add(sound);
|
|
|
|
|
|
|
|
if (result == null) return null;
|
|
|
|
|
|
|
|
// We have to play, then pause the sound to set the time,
|
|
|
|
// else the sound will restart immediately when played.
|
|
|
|
result.play(true, 0.0);
|
|
|
|
result.pause();
|
|
|
|
result.time = this.time;
|
|
|
|
|
2023-05-25 19:36:32 -04:00
|
|
|
result.onComplete = function() {
|
|
|
|
this.onComplete();
|
|
|
|
}
|
|
|
|
|
2023-05-25 18:34:26 -04:00
|
|
|
// Apply parameters to the new sound.
|
|
|
|
result.pitch = this.pitch;
|
|
|
|
result.volume = this.volume;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-25 19:36:32 -04:00
|
|
|
public dynamic function onComplete():Void {}
|
|
|
|
|
2023-05-25 18:34:26 -04:00
|
|
|
/**
|
|
|
|
* Pause all the sounds in the group.
|
|
|
|
*/
|
2023-01-22 22:25:45 -05:00
|
|
|
public function pause()
|
|
|
|
{
|
2023-05-25 18:34:26 -04:00
|
|
|
forEachAlive(function(sound:FlxSound) {
|
|
|
|
sound.pause();
|
2023-01-22 22:25:45 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-25 18:34:26 -04:00
|
|
|
/**
|
|
|
|
* Play all the sounds in the group.
|
|
|
|
*/
|
|
|
|
public function play(forceRestart:Bool = false, startTime:Float = 0.0, ?endTime:Float)
|
2023-01-22 22:25:45 -05:00
|
|
|
{
|
2023-05-25 18:34:26 -04:00
|
|
|
forEachAlive(function(sound:FlxSound) {
|
|
|
|
sound.play(forceRestart, startTime, endTime);
|
2023-01-22 22:25:45 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-25 18:34:26 -04:00
|
|
|
/**
|
|
|
|
* Resume all the sounds in the group.
|
|
|
|
*/
|
|
|
|
public function resume()
|
|
|
|
{
|
|
|
|
forEachAlive(function(sound:FlxSound) {
|
|
|
|
sound.resume();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop all the sounds in the group.
|
|
|
|
*/
|
2023-01-22 22:25:45 -05:00
|
|
|
public function stop()
|
|
|
|
{
|
2023-05-25 18:34:26 -04:00
|
|
|
forEachAlive(function(sound:FlxSound) {
|
|
|
|
sound.stop();
|
2023-01-22 22:25:45 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-25 18:34:26 -04:00
|
|
|
/**
|
|
|
|
* Remove all sounds from the group.
|
|
|
|
*/
|
|
|
|
public override function clear():Void
|
|
|
|
{
|
|
|
|
this.stop();
|
|
|
|
|
|
|
|
super.clear();
|
|
|
|
}
|
|
|
|
|
2023-01-22 22:25:45 -05:00
|
|
|
function get_time():Float
|
|
|
|
{
|
|
|
|
if (getFirstAlive() != null) return getFirstAlive().time;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function set_time(time:Float):Float
|
|
|
|
{
|
2023-05-25 18:34:26 -04:00
|
|
|
forEachAlive(function(snd) {
|
2023-01-22 22:25:45 -05:00
|
|
|
// account for different offsets per sound?
|
|
|
|
snd.time = time;
|
|
|
|
});
|
|
|
|
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_volume():Float
|
|
|
|
{
|
|
|
|
if (getFirstAlive() != null) return getFirstAlive().volume;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// in PlayState, adjust the code so that it only mutes the player1 vocal tracks?
|
|
|
|
function set_volume(volume:Float):Float
|
|
|
|
{
|
2023-05-25 18:34:26 -04:00
|
|
|
forEachAlive(function(snd) {
|
2023-01-22 22:25:45 -05:00
|
|
|
snd.volume = volume;
|
|
|
|
});
|
|
|
|
|
|
|
|
return volume;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_pitch():Float
|
|
|
|
{
|
|
|
|
#if FLX_PITCH
|
|
|
|
if (getFirstAlive() != null) return getFirstAlive().pitch;
|
|
|
|
else
|
|
|
|
#end
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function set_pitch(val:Float):Float
|
|
|
|
{
|
|
|
|
#if FLX_PITCH
|
|
|
|
trace('Setting audio pitch to ' + val);
|
2023-05-25 18:34:26 -04:00
|
|
|
forEachAlive(function(snd) {
|
2023-01-22 22:25:45 -05:00
|
|
|
snd.pitch = val;
|
|
|
|
});
|
|
|
|
#end
|
|
|
|
return val;
|
|
|
|
}
|
2021-09-20 11:32:35 -04:00
|
|
|
}
|