2023-12-06 20:04:24 +00:00
|
|
|
package funkin.audio;
|
|
|
|
|
|
|
|
#if flash11
|
|
|
|
import flash.media.Sound;
|
|
|
|
import flash.utils.ByteArray;
|
|
|
|
#end
|
|
|
|
import flixel.sound.FlxSound;
|
|
|
|
import flixel.group.FlxGroup.FlxTypedGroup;
|
|
|
|
import flixel.system.FlxAssets.FlxSoundAsset;
|
|
|
|
import openfl.Assets;
|
|
|
|
#if (openfl >= "8.0.0")
|
|
|
|
import openfl.utils.AssetType;
|
|
|
|
#end
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A FlxSound which adds additional functionality:
|
|
|
|
* - Delayed playback via negative song position.
|
|
|
|
*/
|
|
|
|
@:nullSafety
|
|
|
|
class FunkinSound extends FlxSound
|
|
|
|
{
|
|
|
|
static var cache(default, null):FlxTypedGroup<FunkinSound> = new FlxTypedGroup<FunkinSound>();
|
|
|
|
|
2023-12-07 03:03:36 +00:00
|
|
|
public var isPlaying(get, never):Bool;
|
|
|
|
|
|
|
|
function get_isPlaying():Bool
|
|
|
|
{
|
|
|
|
return this.playing || this._shouldPlay;
|
|
|
|
}
|
|
|
|
|
2023-12-06 20:04:24 +00:00
|
|
|
/**
|
|
|
|
* Are we in a state where the song should play but time is negative?
|
|
|
|
*/
|
2023-12-07 03:03:36 +00:00
|
|
|
var _shouldPlay:Bool = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For debug purposes.
|
|
|
|
*/
|
|
|
|
var _label:String = "unknown";
|
2023-12-06 20:04:24 +00:00
|
|
|
|
|
|
|
public function new()
|
|
|
|
{
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override function update(elapsedSec:Float)
|
|
|
|
{
|
2023-12-07 03:03:36 +00:00
|
|
|
if (!playing && !_shouldPlay) return;
|
2023-12-06 20:04:24 +00:00
|
|
|
|
|
|
|
if (_time < 0)
|
|
|
|
{
|
|
|
|
var elapsedMs = elapsedSec * Constants.MS_PER_SEC;
|
|
|
|
_time += elapsedMs;
|
|
|
|
if (_time >= 0)
|
|
|
|
{
|
|
|
|
super.play();
|
2023-12-07 03:03:36 +00:00
|
|
|
_shouldPlay = false;
|
2023-12-06 20:04:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
super.update(elapsedSec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override function play(forceRestart:Bool = false, startTime:Float = 0, ?endTime:Float):FunkinSound
|
|
|
|
{
|
|
|
|
if (!exists) return this;
|
|
|
|
|
|
|
|
if (forceRestart)
|
|
|
|
{
|
|
|
|
cleanup(false, true);
|
|
|
|
}
|
2023-12-07 03:03:36 +00:00
|
|
|
else if (playing)
|
2023-12-06 20:04:24 +00:00
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (startTime < 0)
|
|
|
|
{
|
2023-12-07 03:03:36 +00:00
|
|
|
this.active = true;
|
|
|
|
this._shouldPlay = true;
|
|
|
|
this._time = startTime;
|
2023-12-06 20:04:24 +00:00
|
|
|
this.endTime = endTime;
|
|
|
|
return this;
|
|
|
|
}
|
2023-12-07 03:03:36 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_paused)
|
|
|
|
{
|
|
|
|
resume();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
startSound(startTime);
|
|
|
|
}
|
2023-12-06 20:04:24 +00:00
|
|
|
|
2023-12-07 03:03:36 +00:00
|
|
|
this.endTime = endTime;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override function pause():FunkinSound
|
|
|
|
{
|
|
|
|
super.pause();
|
|
|
|
this._shouldPlay = false;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override function resume():FunkinSound
|
|
|
|
{
|
|
|
|
if (this._time < 0)
|
2023-12-06 20:04:24 +00:00
|
|
|
{
|
2023-12-07 03:03:36 +00:00
|
|
|
this._shouldPlay = true;
|
2023-12-06 20:04:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-12-07 03:03:36 +00:00
|
|
|
super.resume();
|
2023-12-06 20:04:24 +00:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new `FunkinSound` object.
|
|
|
|
*
|
|
|
|
* @param embeddedSound The embedded sound resource you want to play. To stream, use the optional URL parameter instead.
|
|
|
|
* @param volume How loud to play it (0 to 1).
|
|
|
|
* @param looped Whether to loop this sound.
|
|
|
|
* @param group The group to add this sound to.
|
|
|
|
* @param autoDestroy Whether to destroy this sound when it finishes playing.
|
|
|
|
* Leave this value set to `false` if you want to re-use this `FunkinSound` instance.
|
|
|
|
* @param autoPlay Whether to play the sound immediately or wait for a `play()` call.
|
|
|
|
* @param onComplete Called when the sound finished playing.
|
|
|
|
* @param onLoad Called when the sound finished loading. Called immediately for succesfully loaded embedded sounds.
|
|
|
|
* @return A `FunkinSound` object.
|
|
|
|
*/
|
|
|
|
public static function load(embeddedSound:FlxSoundAsset, volume:Float = 1.0, looped:Bool = false, autoDestroy:Bool = false, autoPlay:Bool = false,
|
|
|
|
?onComplete:Void->Void, ?onLoad:Void->Void):FunkinSound
|
|
|
|
{
|
|
|
|
var sound:FunkinSound = cache.recycle(construct);
|
|
|
|
|
|
|
|
sound.loadEmbedded(embeddedSound, looped, autoDestroy, onComplete);
|
|
|
|
|
2023-12-07 03:03:36 +00:00
|
|
|
if (embeddedSound is String)
|
|
|
|
{
|
|
|
|
sound._label = embeddedSound;
|
|
|
|
}
|
|
|
|
|
2023-12-06 20:04:24 +00:00
|
|
|
sound.volume = volume;
|
|
|
|
sound.group = FlxG.sound.defaultSoundGroup;
|
2023-12-07 03:03:36 +00:00
|
|
|
sound.persist = true;
|
2023-12-06 20:04:24 +00:00
|
|
|
if (autoPlay) sound.play();
|
|
|
|
|
|
|
|
// Call OnlLoad() because the sound already loaded
|
|
|
|
if (onLoad != null && sound._sound != null) onLoad();
|
|
|
|
|
|
|
|
return sound;
|
|
|
|
}
|
|
|
|
|
|
|
|
static function construct():FunkinSound
|
|
|
|
{
|
|
|
|
var sound:FunkinSound = new FunkinSound();
|
|
|
|
|
|
|
|
cache.add(sound);
|
|
|
|
FlxG.sound.list.add(sound);
|
|
|
|
|
|
|
|
return sound;
|
|
|
|
}
|
|
|
|
}
|