2023-12-06 15:04:24 -05:00
|
|
|
package funkin.audio;
|
|
|
|
|
|
|
|
import flixel.group.FlxGroup.FlxTypedGroup;
|
2024-03-26 23:24:59 -04:00
|
|
|
import flixel.math.FlxMath;
|
|
|
|
import flixel.sound.FlxSound;
|
2023-12-06 15:04:24 -05:00
|
|
|
import flixel.system.FlxAssets.FlxSoundAsset;
|
2024-03-26 23:24:59 -04:00
|
|
|
import flixel.tweens.FlxTween;
|
|
|
|
import flixel.util.FlxSignal.FlxTypedSignal;
|
2024-02-09 14:58:57 -05:00
|
|
|
import funkin.audio.waveform.WaveformData;
|
|
|
|
import funkin.audio.waveform.WaveformDataParser;
|
2024-03-26 23:24:59 -04:00
|
|
|
import funkin.data.song.SongData.SongMusicData;
|
|
|
|
import funkin.data.song.SongRegistry;
|
|
|
|
import funkin.util.tools.ICloneable;
|
2024-04-18 20:23:03 -04:00
|
|
|
import funkin.util.flixel.sound.FlxPartialSound;
|
|
|
|
import funkin.Paths.PathsFunction;
|
2023-12-06 15:04:24 -05:00
|
|
|
import openfl.Assets;
|
2024-04-18 20:23:03 -04:00
|
|
|
import lime.app.Future;
|
|
|
|
import lime.app.Promise;
|
2024-03-26 23:24:59 -04:00
|
|
|
import openfl.media.SoundMixer;
|
2024-05-28 01:34:28 -04:00
|
|
|
|
2023-12-06 15:04:24 -05:00
|
|
|
#if (openfl >= "8.0.0")
|
|
|
|
#end
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A FlxSound which adds additional functionality:
|
|
|
|
* - Delayed playback via negative song position.
|
2024-03-23 17:50:48 -04:00
|
|
|
* - Easy functions for immediate playback and recycling.
|
2023-12-06 15:04:24 -05:00
|
|
|
*/
|
|
|
|
@:nullSafety
|
2024-01-27 03:24:49 -05:00
|
|
|
class FunkinSound extends FlxSound implements ICloneable<FunkinSound>
|
2023-12-06 15:04:24 -05:00
|
|
|
{
|
2024-02-20 13:37:53 -05:00
|
|
|
static final MAX_VOLUME:Float = 1.0;
|
2024-01-30 21:49:49 -05:00
|
|
|
|
2024-03-19 15:56:00 -04:00
|
|
|
/**
|
|
|
|
* An FlxSignal which is dispatched when the volume changes.
|
|
|
|
*/
|
|
|
|
public static var onVolumeChanged(get, never):FlxTypedSignal<Float->Void>;
|
|
|
|
|
|
|
|
static var _onVolumeChanged:Null<FlxTypedSignal<Float->Void>> = null;
|
|
|
|
|
|
|
|
static function get_onVolumeChanged():FlxTypedSignal<Float->Void>
|
|
|
|
{
|
|
|
|
if (_onVolumeChanged == null)
|
|
|
|
{
|
|
|
|
_onVolumeChanged = new FlxTypedSignal<Float->Void>();
|
|
|
|
FlxG.sound.volumeHandler = function(volume:Float) {
|
|
|
|
_onVolumeChanged.dispatch(volume);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _onVolumeChanged;
|
|
|
|
}
|
|
|
|
|
2024-02-28 00:19:08 -05:00
|
|
|
/**
|
|
|
|
* Using `FunkinSound.load` will override a dead instance from here rather than creating a new one, if possible!
|
|
|
|
*/
|
2024-03-11 23:42:32 -04:00
|
|
|
static var pool(default, null):FlxTypedGroup<FunkinSound> = new FlxTypedGroup<FunkinSound>();
|
2023-12-06 15:04:24 -05:00
|
|
|
|
2024-03-26 19:38:56 -04:00
|
|
|
/**
|
|
|
|
* Calculate the current time of the sound.
|
|
|
|
* NOTE: You need to `add()` the sound to the scene for `update()` to increment the time.
|
|
|
|
*/
|
|
|
|
//
|
2024-01-30 21:49:49 -05:00
|
|
|
public var muted(default, set):Bool = false;
|
|
|
|
|
|
|
|
function set_muted(value:Bool):Bool
|
|
|
|
{
|
|
|
|
if (value == muted) return value;
|
|
|
|
muted = value;
|
|
|
|
updateTransform();
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
override function set_volume(value:Float):Float
|
|
|
|
{
|
|
|
|
// Uncap the volume.
|
|
|
|
_volume = FlxMath.bound(value, 0.0, MAX_VOLUME);
|
|
|
|
updateTransform();
|
|
|
|
return _volume;
|
|
|
|
}
|
|
|
|
|
2024-01-27 03:24:49 -05:00
|
|
|
public var paused(get, never):Bool;
|
|
|
|
|
|
|
|
function get_paused():Bool
|
|
|
|
{
|
|
|
|
return this._paused;
|
|
|
|
}
|
|
|
|
|
2023-12-06 22:03:36 -05:00
|
|
|
public var isPlaying(get, never):Bool;
|
|
|
|
|
|
|
|
function get_isPlaying():Bool
|
|
|
|
{
|
|
|
|
return this.playing || this._shouldPlay;
|
|
|
|
}
|
|
|
|
|
2024-02-09 14:58:57 -05:00
|
|
|
/**
|
|
|
|
* Waveform data for this sound.
|
|
|
|
* This is lazily loaded, so it will be built the first time it is accessed.
|
|
|
|
*/
|
|
|
|
public var waveformData(get, never):WaveformData;
|
|
|
|
|
|
|
|
var _waveformData:Null<WaveformData> = null;
|
|
|
|
|
|
|
|
function get_waveformData():WaveformData
|
|
|
|
{
|
|
|
|
if (_waveformData == null)
|
|
|
|
{
|
|
|
|
_waveformData = WaveformDataParser.interpretFlxSound(this);
|
|
|
|
if (_waveformData == null) throw 'Could not interpret waveform data!';
|
|
|
|
}
|
|
|
|
return _waveformData;
|
|
|
|
}
|
|
|
|
|
2023-12-06 15:04:24 -05:00
|
|
|
/**
|
|
|
|
* Are we in a state where the song should play but time is negative?
|
|
|
|
*/
|
2023-12-06 22:03:36 -05:00
|
|
|
var _shouldPlay:Bool = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For debug purposes.
|
|
|
|
*/
|
|
|
|
var _label:String = "unknown";
|
2023-12-06 15:04:24 -05:00
|
|
|
|
2024-03-04 05:59:10 -05:00
|
|
|
/**
|
|
|
|
* Whether we received a focus lost event.
|
|
|
|
*/
|
|
|
|
var _lostFocus:Bool = false;
|
|
|
|
|
2023-12-06 15:04:24 -05:00
|
|
|
public function new()
|
|
|
|
{
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override function update(elapsedSec:Float)
|
|
|
|
{
|
2023-12-06 22:03:36 -05:00
|
|
|
if (!playing && !_shouldPlay) return;
|
2023-12-06 15:04:24 -05:00
|
|
|
|
|
|
|
if (_time < 0)
|
|
|
|
{
|
|
|
|
var elapsedMs = elapsedSec * Constants.MS_PER_SEC;
|
|
|
|
_time += elapsedMs;
|
|
|
|
if (_time >= 0)
|
|
|
|
{
|
|
|
|
super.play();
|
2023-12-06 22:03:36 -05:00
|
|
|
_shouldPlay = false;
|
2023-12-06 15:04:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
super.update(elapsedSec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-20 14:07:31 -05:00
|
|
|
public function togglePlayback():FunkinSound
|
|
|
|
{
|
|
|
|
if (playing)
|
|
|
|
{
|
|
|
|
pause();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
resume();
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2023-12-06 15:04:24 -05:00
|
|
|
public override function play(forceRestart:Bool = false, startTime:Float = 0, ?endTime:Float):FunkinSound
|
|
|
|
{
|
|
|
|
if (!exists) return this;
|
|
|
|
|
|
|
|
if (forceRestart)
|
|
|
|
{
|
|
|
|
cleanup(false, true);
|
|
|
|
}
|
2023-12-06 22:03:36 -05:00
|
|
|
else if (playing)
|
2023-12-06 15:04:24 -05:00
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (startTime < 0)
|
|
|
|
{
|
2023-12-06 22:03:36 -05:00
|
|
|
this.active = true;
|
|
|
|
this._shouldPlay = true;
|
|
|
|
this._time = startTime;
|
2023-12-06 15:04:24 -05:00
|
|
|
this.endTime = endTime;
|
|
|
|
return this;
|
|
|
|
}
|
2023-12-06 22:03:36 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_paused)
|
|
|
|
{
|
|
|
|
resume();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
startSound(startTime);
|
|
|
|
}
|
2023-12-06 15:04:24 -05:00
|
|
|
|
2023-12-06 22:03:36 -05:00
|
|
|
this.endTime = endTime;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override function pause():FunkinSound
|
|
|
|
{
|
2024-03-04 06:43:17 -05:00
|
|
|
if (_shouldPlay)
|
|
|
|
{
|
|
|
|
// This sound will eventually play, but is still at a negative timestamp.
|
|
|
|
// Manually set the paused flag to ensure proper focus/unfocus behavior.
|
|
|
|
_shouldPlay = false;
|
|
|
|
_paused = true;
|
|
|
|
active = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
super.pause();
|
|
|
|
}
|
2023-12-06 22:03:36 -05:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2024-01-14 09:18:32 -05:00
|
|
|
/**
|
|
|
|
* Called when the user clicks to focus on the window.
|
|
|
|
*/
|
|
|
|
override function onFocus():Void
|
|
|
|
{
|
2024-03-04 05:59:10 -05:00
|
|
|
// Flixel can sometimes toss spurious `onFocus` events, e.g. if the Flixel debugger is toggled
|
|
|
|
// on and off. We only want to resume the sound if we actually lost focus, and if we weren't
|
|
|
|
// already paused before we lost focus.
|
|
|
|
if (_lostFocus && !_alreadyPaused)
|
2024-01-14 09:18:32 -05:00
|
|
|
{
|
2024-06-05 19:25:53 -04:00
|
|
|
// trace('Resuming audio (${this._label}) on focus!');
|
2024-01-14 09:18:32 -05:00
|
|
|
resume();
|
|
|
|
}
|
2024-02-28 03:01:20 -05:00
|
|
|
else
|
|
|
|
{
|
2024-06-05 19:25:53 -04:00
|
|
|
// trace('Not resuming audio (${this._label}) on focus!');
|
2024-02-28 03:01:20 -05:00
|
|
|
}
|
2024-03-04 05:59:10 -05:00
|
|
|
_lostFocus = false;
|
2024-01-14 09:18:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the user tabs away from the window.
|
|
|
|
*/
|
|
|
|
override function onFocusLost():Void
|
|
|
|
{
|
2024-06-05 19:25:53 -04:00
|
|
|
// trace('Focus lost, pausing audio!');
|
2024-03-04 05:59:10 -05:00
|
|
|
_lostFocus = true;
|
2024-01-14 09:18:32 -05:00
|
|
|
_alreadyPaused = _paused;
|
|
|
|
pause();
|
|
|
|
}
|
|
|
|
|
2023-12-06 22:03:36 -05:00
|
|
|
public override function resume():FunkinSound
|
|
|
|
{
|
|
|
|
if (this._time < 0)
|
2023-12-06 15:04:24 -05:00
|
|
|
{
|
2024-03-04 06:43:17 -05:00
|
|
|
// Sound with negative timestamp, restart the timer.
|
|
|
|
_shouldPlay = true;
|
|
|
|
_paused = false;
|
|
|
|
active = true;
|
2023-12-06 15:04:24 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-12-06 22:03:36 -05:00
|
|
|
super.resume();
|
2023-12-06 15:04:24 -05:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2024-01-30 21:49:49 -05:00
|
|
|
/**
|
|
|
|
* Call after adjusting the volume to update the sound channel's settings.
|
|
|
|
*/
|
|
|
|
@:allow(flixel.sound.FlxSoundGroup)
|
|
|
|
override function updateTransform():Void
|
|
|
|
{
|
2024-03-15 21:53:07 -04:00
|
|
|
if (_transform != null)
|
|
|
|
{
|
|
|
|
_transform.volume = #if FLX_SOUND_SYSTEM ((FlxG.sound.muted || this.muted) ? 0 : 1) * FlxG.sound.volume * #end
|
|
|
|
(group != null ? group.volume : 1) * _volume * _volumeAdjust;
|
|
|
|
}
|
2024-01-30 21:49:49 -05:00
|
|
|
|
2024-03-15 21:53:07 -04:00
|
|
|
if (_channel != null)
|
|
|
|
{
|
|
|
|
_channel.soundTransform = _transform;
|
|
|
|
}
|
2024-01-30 21:49:49 -05:00
|
|
|
}
|
|
|
|
|
2024-01-27 03:24:49 -05:00
|
|
|
public function clone():FunkinSound
|
|
|
|
{
|
|
|
|
var sound:FunkinSound = new FunkinSound();
|
|
|
|
|
|
|
|
// Clone the sound by creating one with the same data buffer.
|
|
|
|
// Reusing the `Sound` object directly causes issues with playback.
|
|
|
|
@:privateAccess
|
|
|
|
sound._sound = openfl.media.Sound.fromAudioBuffer(this._sound.__buffer);
|
|
|
|
|
|
|
|
// Call init to ensure the FlxSound is properly initialized.
|
|
|
|
sound.init(this.looped, this.autoDestroy, this.onComplete);
|
|
|
|
|
2024-02-09 14:58:57 -05:00
|
|
|
// Oh yeah, the waveform data is the same too!
|
|
|
|
@:privateAccess
|
|
|
|
sound._waveformData = this._waveformData;
|
|
|
|
|
2024-01-27 03:24:49 -05:00
|
|
|
return sound;
|
|
|
|
}
|
|
|
|
|
2023-12-06 15:04:24 -05:00
|
|
|
/**
|
2024-03-11 23:42:32 -04:00
|
|
|
* Creates a new `FunkinSound` object and loads it as the current music track.
|
2023-12-06 15:04:24 -05:00
|
|
|
*
|
2024-03-11 23:42:32 -04:00
|
|
|
* @param key The key of the music you want to play. Music should be at `music/<key>/<key>.ogg`.
|
2024-03-16 00:55:57 -04:00
|
|
|
* @param params A set of additional optional parameters.
|
2024-03-11 23:42:32 -04:00
|
|
|
* Data should be at `music/<key>/<key>-metadata.json`.
|
2024-03-23 23:38:07 -04:00
|
|
|
* @return Whether the music was started. `false` if music was already playing or could not be started
|
2024-03-11 23:42:32 -04:00
|
|
|
*/
|
2024-03-23 23:38:07 -04:00
|
|
|
public static function playMusic(key:String, params:FunkinSoundPlayMusicParams):Bool
|
2024-03-11 23:42:32 -04:00
|
|
|
{
|
2024-03-23 23:38:07 -04:00
|
|
|
if (!(params.overrideExisting ?? false) && (FlxG.sound.music?.exists ?? false) && FlxG.sound.music.playing) return false;
|
2024-03-11 23:42:32 -04:00
|
|
|
|
2024-03-16 00:55:57 -04:00
|
|
|
if (!(params.restartTrack ?? false) && FlxG.sound.music?.playing)
|
|
|
|
{
|
|
|
|
if (FlxG.sound.music != null && Std.isOfType(FlxG.sound.music, FunkinSound))
|
|
|
|
{
|
|
|
|
var existingSound:FunkinSound = cast FlxG.sound.music;
|
|
|
|
// Stop here if we would play a matching music track.
|
|
|
|
if (existingSound._label == Paths.music('$key/$key'))
|
|
|
|
{
|
2024-03-23 23:38:07 -04:00
|
|
|
return false;
|
2024-03-16 00:55:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-03 01:40:08 -04:00
|
|
|
if (FlxG.sound.music != null)
|
|
|
|
{
|
|
|
|
FlxG.sound.music.fadeTween?.cancel();
|
|
|
|
FlxG.sound.music.stop();
|
|
|
|
FlxG.sound.music.kill();
|
|
|
|
}
|
|
|
|
|
2024-03-16 00:55:57 -04:00
|
|
|
if (params?.mapTimeChanges ?? true)
|
2024-03-11 23:42:32 -04:00
|
|
|
{
|
|
|
|
var songMusicData:Null<SongMusicData> = SongRegistry.instance.parseMusicData(key);
|
|
|
|
// Will fall back and return null if the metadata doesn't exist or can't be parsed.
|
|
|
|
if (songMusicData != null)
|
|
|
|
{
|
|
|
|
Conductor.instance.mapTimeChanges(songMusicData.timeChanges);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FlxG.log.warn('Tried and failed to find music metadata for $key');
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 20:23:03 -04:00
|
|
|
var pathsFunction = params.pathsFunction ?? MUSIC;
|
2024-05-09 01:56:52 -04:00
|
|
|
var suffix = params.suffix ?? '';
|
2024-04-18 20:23:03 -04:00
|
|
|
var pathToUse = switch (pathsFunction)
|
|
|
|
{
|
|
|
|
case MUSIC: Paths.music('$key/$key');
|
2024-05-09 01:56:52 -04:00
|
|
|
case INST: Paths.inst('$key', suffix);
|
2024-04-18 20:23:03 -04:00
|
|
|
default: Paths.music('$key/$key');
|
|
|
|
}
|
2024-03-11 23:42:32 -04:00
|
|
|
|
2024-04-18 20:23:03 -04:00
|
|
|
var shouldLoadPartial = params.partialParams?.loadPartial ?? false;
|
|
|
|
|
2024-06-05 15:02:29 -04:00
|
|
|
// even if we arent' trying to partial load a song, we want to error out any songs in progress,
|
|
|
|
// so we don't get overlapping music if someone were to load a new song while a partial one is loading!
|
|
|
|
|
|
|
|
emptyPartialQueue();
|
|
|
|
|
2024-04-18 20:23:03 -04:00
|
|
|
if (shouldLoadPartial)
|
2024-03-23 23:38:07 -04:00
|
|
|
{
|
2024-05-31 05:39:06 -04:00
|
|
|
var music = FunkinSound.loadPartial(pathToUse, params.partialParams?.start ?? 0.0, params.partialParams?.end ?? 1.0, params?.startingVolume ?? 1.0,
|
2024-05-28 23:53:50 -04:00
|
|
|
params.loop ?? true, false, false, params.onComplete);
|
2024-03-11 23:42:32 -04:00
|
|
|
|
2024-04-18 20:23:03 -04:00
|
|
|
if (music != null)
|
|
|
|
{
|
2024-05-23 16:31:18 -04:00
|
|
|
partialQueue.push(music);
|
|
|
|
|
|
|
|
@:nullSafety(Off)
|
2024-05-28 23:53:50 -04:00
|
|
|
music.future.onComplete(function(partialMusic:Null<FunkinSound>) {
|
|
|
|
FlxG.sound.music = partialMusic;
|
|
|
|
FlxG.sound.list.remove(FlxG.sound.music);
|
|
|
|
|
2024-05-31 02:03:40 -04:00
|
|
|
if (FlxG.sound.music != null && params.onLoad != null) params.onLoad();
|
2024-04-18 20:23:03 -04:00
|
|
|
});
|
2024-03-23 23:38:07 -04:00
|
|
|
|
2024-04-18 20:23:03 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2024-03-23 23:38:07 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-04-18 20:23:03 -04:00
|
|
|
var music = FunkinSound.load(pathToUse, params?.startingVolume ?? 1.0, params.loop ?? true, false, true);
|
|
|
|
if (music != null)
|
|
|
|
{
|
|
|
|
FlxG.sound.music = music;
|
|
|
|
|
|
|
|
// Prevent repeat update() and onFocus() calls.
|
|
|
|
FlxG.sound.list.remove(FlxG.sound.music);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2024-03-23 23:38:07 -04:00
|
|
|
}
|
2024-03-11 23:42:32 -04:00
|
|
|
}
|
|
|
|
|
2024-06-05 15:02:29 -04:00
|
|
|
public static function emptyPartialQueue():Void
|
|
|
|
{
|
|
|
|
while (partialQueue.length > 0)
|
|
|
|
{
|
|
|
|
@:nullSafety(Off)
|
|
|
|
partialQueue.pop().error("Cancel loading partial sound");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-28 23:53:50 -04:00
|
|
|
static var partialQueue:Array<Promise<Null<FunkinSound>>> = [];
|
2024-05-23 16:31:18 -04:00
|
|
|
|
2024-03-11 23:42:32 -04:00
|
|
|
/**
|
|
|
|
* Creates a new `FunkinSound` object synchronously.
|
|
|
|
*
|
|
|
|
* @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.
|
2023-12-06 15:04:24 -05:00
|
|
|
* Leave this value set to `false` if you want to re-use this `FunkinSound` instance.
|
2024-03-11 23:42:32 -04:00
|
|
|
* @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.
|
2024-03-23 17:50:48 -04:00
|
|
|
* @return A `FunkinSound` object, or `null` if the sound could not be loaded.
|
2023-12-06 15:04:24 -05:00
|
|
|
*/
|
|
|
|
public static function load(embeddedSound:FlxSoundAsset, volume:Float = 1.0, looped:Bool = false, autoDestroy:Bool = false, autoPlay:Bool = false,
|
2024-03-23 17:50:48 -04:00
|
|
|
?onComplete:Void->Void, ?onLoad:Void->Void):Null<FunkinSound>
|
2023-12-06 15:04:24 -05:00
|
|
|
{
|
2024-03-23 17:50:48 -04:00
|
|
|
@:privateAccess
|
|
|
|
if (SoundMixer.__soundChannels.length >= SoundMixer.MAX_ACTIVE_CHANNELS)
|
|
|
|
{
|
|
|
|
FlxG.log.error('FunkinSound could not play sound, channels exhausted! Found ${SoundMixer.__soundChannels.length} active sound channels.');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-03-11 23:42:32 -04:00
|
|
|
var sound:FunkinSound = pool.recycle(construct);
|
2023-12-06 15:04:24 -05:00
|
|
|
|
2024-02-28 00:19:08 -05:00
|
|
|
// Load the sound.
|
|
|
|
// Sets `exists = true` as a side effect.
|
2023-12-06 15:04:24 -05:00
|
|
|
sound.loadEmbedded(embeddedSound, looped, autoDestroy, onComplete);
|
|
|
|
|
2023-12-06 22:03:36 -05:00
|
|
|
if (embeddedSound is String)
|
|
|
|
{
|
|
|
|
sound._label = embeddedSound;
|
|
|
|
}
|
2024-03-23 17:50:48 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
sound._label = 'unknown';
|
|
|
|
}
|
2023-12-06 22:03:36 -05:00
|
|
|
|
2024-03-16 00:55:57 -04:00
|
|
|
if (autoPlay) sound.play();
|
2023-12-06 15:04:24 -05:00
|
|
|
sound.volume = volume;
|
|
|
|
sound.group = FlxG.sound.defaultSoundGroup;
|
2023-12-06 22:03:36 -05:00
|
|
|
sound.persist = true;
|
2023-12-06 15:04:24 -05:00
|
|
|
|
2024-04-16 19:57:19 -04:00
|
|
|
// Make sure to add the sound to the list.
|
|
|
|
// If it's already in, it won't get re-added.
|
|
|
|
// If it's not in the list (it gets removed by FunkinSound.playMusic()),
|
|
|
|
// it will get re-added (then if this was called by playMusic(), removed again)
|
|
|
|
FlxG.sound.list.add(sound);
|
|
|
|
|
2024-03-11 23:42:32 -04:00
|
|
|
// Call onLoad() because the sound already loaded
|
2023-12-06 15:04:24 -05:00
|
|
|
if (onLoad != null && sound._sound != null) onLoad();
|
|
|
|
|
|
|
|
return sound;
|
|
|
|
}
|
|
|
|
|
2024-04-18 20:23:03 -04:00
|
|
|
/**
|
|
|
|
* Will load a section of a sound file, useful for Freeplay where we don't want to load all the bytes of a song
|
|
|
|
* @param path The path to the sound file
|
|
|
|
* @param start The start time of the sound file
|
|
|
|
* @param end The end time of the sound file
|
|
|
|
* @param volume Volume to start at
|
|
|
|
* @param looped Whether the sound file should loop
|
|
|
|
* @param autoDestroy Whether the sound file should be destroyed after it finishes playing
|
|
|
|
* @param autoPlay Whether the sound file should play immediately
|
2024-05-23 16:31:18 -04:00
|
|
|
* @param onComplete Callback when the sound finishes playing
|
|
|
|
* @param onLoad Callback when the sound finishes loading
|
2024-04-18 20:23:03 -04:00
|
|
|
* @return A FunkinSound object
|
|
|
|
*/
|
|
|
|
public static function loadPartial(path:String, start:Float = 0, end:Float = 1, volume:Float = 1.0, looped:Bool = false, autoDestroy:Bool = false,
|
2024-05-28 23:53:50 -04:00
|
|
|
autoPlay:Bool = true, ?onComplete:Void->Void, ?onLoad:Void->Void):Promise<Null<FunkinSound>>
|
2024-04-18 20:23:03 -04:00
|
|
|
{
|
|
|
|
var promise:lime.app.Promise<Null<FunkinSound>> = new lime.app.Promise<Null<FunkinSound>>();
|
|
|
|
|
|
|
|
// split the path and get only after first :
|
2024-06-09 05:47:17 -04:00
|
|
|
// we are bypassing the openfl/lime asset library fuss on web only
|
|
|
|
#if web
|
2024-04-18 20:23:03 -04:00
|
|
|
path = Paths.stripLibrary(path);
|
2024-06-09 05:47:17 -04:00
|
|
|
#end
|
2024-04-18 20:23:03 -04:00
|
|
|
|
|
|
|
var soundRequest = FlxPartialSound.partialLoadFromFile(path, start, end);
|
|
|
|
|
2024-05-31 02:03:40 -04:00
|
|
|
if (soundRequest == null)
|
|
|
|
{
|
|
|
|
promise.complete(null);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
promise.future.onError(function(e) {
|
|
|
|
soundRequest.error("Sound loading was errored or cancelled");
|
|
|
|
});
|
|
|
|
|
|
|
|
soundRequest.future.onComplete(function(partialSound) {
|
|
|
|
var snd = FunkinSound.load(partialSound, volume, looped, autoDestroy, autoPlay, onComplete, onLoad);
|
|
|
|
promise.complete(snd);
|
|
|
|
});
|
|
|
|
}
|
2024-04-18 20:23:03 -04:00
|
|
|
|
2024-05-28 23:53:50 -04:00
|
|
|
return promise;
|
2024-04-18 20:23:03 -04:00
|
|
|
}
|
|
|
|
|
2024-03-27 17:43:15 -04:00
|
|
|
@:nullSafety(Off)
|
2024-03-23 17:50:48 -04:00
|
|
|
public override function destroy():Void
|
|
|
|
{
|
|
|
|
// trace('[FunkinSound] Destroying sound "${this._label}"');
|
|
|
|
super.destroy();
|
2024-03-27 17:43:15 -04:00
|
|
|
if (fadeTween != null)
|
|
|
|
{
|
|
|
|
fadeTween.cancel();
|
|
|
|
fadeTween = null;
|
|
|
|
}
|
2024-03-26 23:24:59 -04:00
|
|
|
FlxTween.cancelTweensOf(this);
|
|
|
|
this._label = 'unknown';
|
2024-03-23 17:50:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Play a sound effect once, then destroy it.
|
|
|
|
* @param key
|
|
|
|
* @param volume
|
2024-06-27 15:00:25 -04:00
|
|
|
* @return A `FunkinSound` object, or `null` if the sound could not be loaded.
|
2024-03-23 17:50:48 -04:00
|
|
|
*/
|
2024-06-29 00:03:28 -04:00
|
|
|
public static function playOnce(key:String, volume:Float = 1.0, ?onComplete:Void->Void, ?onLoad:Void->Void):Null<FunkinSound>
|
2024-03-23 17:50:48 -04:00
|
|
|
{
|
|
|
|
var result = FunkinSound.load(key, volume, false, true, true, onComplete, onLoad);
|
2024-06-27 15:00:25 -04:00
|
|
|
return result;
|
2024-03-23 17:50:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop all sounds in the pool and allow them to be recycled.
|
|
|
|
*/
|
|
|
|
public static function stopAllAudio(musicToo:Bool = false):Void
|
|
|
|
{
|
|
|
|
for (sound in pool)
|
|
|
|
{
|
|
|
|
if (sound == null) continue;
|
|
|
|
if (!musicToo && sound == FlxG.sound.music) continue;
|
|
|
|
sound.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-06 15:04:24 -05:00
|
|
|
static function construct():FunkinSound
|
|
|
|
{
|
|
|
|
var sound:FunkinSound = new FunkinSound();
|
|
|
|
|
2024-03-11 23:42:32 -04:00
|
|
|
pool.add(sound);
|
2023-12-06 15:04:24 -05:00
|
|
|
FlxG.sound.list.add(sound);
|
|
|
|
|
|
|
|
return sound;
|
|
|
|
}
|
|
|
|
}
|
2024-03-16 00:55:57 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Additional parameters for `FunkinSound.playMusic()`
|
|
|
|
*/
|
|
|
|
typedef FunkinSoundPlayMusicParams =
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The volume you want the music to start at.
|
|
|
|
* @default `1.0`
|
|
|
|
*/
|
|
|
|
var ?startingVolume:Float;
|
|
|
|
|
2024-05-09 01:56:52 -04:00
|
|
|
/**
|
|
|
|
* The suffix of the music file to play. Usually for "-erect" tracks when loading an INST file
|
|
|
|
* @default ``
|
|
|
|
*/
|
|
|
|
var ?suffix:String;
|
|
|
|
|
2024-03-16 00:55:57 -04:00
|
|
|
/**
|
|
|
|
* Whether to override music if a different track is already playing.
|
|
|
|
* @default `false`
|
|
|
|
*/
|
|
|
|
var ?overrideExisting:Bool;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether to override music if the same track is already playing.
|
|
|
|
* @default `false`
|
|
|
|
*/
|
|
|
|
var ?restartTrack:Bool;
|
|
|
|
|
2024-03-23 23:38:07 -04:00
|
|
|
/**
|
|
|
|
* Whether the music should loop or play once.
|
|
|
|
* @default `true`
|
|
|
|
*/
|
|
|
|
var ?loop:Bool;
|
|
|
|
|
2024-03-16 00:55:57 -04:00
|
|
|
/**
|
|
|
|
* Whether to check for `SongMusicData` to update the Conductor with.
|
|
|
|
* @default `true`
|
|
|
|
*/
|
|
|
|
var ?mapTimeChanges:Bool;
|
2024-04-18 20:23:03 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Which Paths function to use to load a song
|
|
|
|
* @default `MUSIC`
|
|
|
|
*/
|
|
|
|
var ?pathsFunction:PathsFunction;
|
|
|
|
|
|
|
|
var ?partialParams:PartialSoundParams;
|
2024-05-09 01:56:52 -04:00
|
|
|
|
|
|
|
var ?onComplete:Void->Void;
|
|
|
|
var ?onLoad:Void->Void;
|
2024-04-18 20:23:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef PartialSoundParams =
|
|
|
|
{
|
|
|
|
var loadPartial:Bool;
|
|
|
|
var start:Float;
|
|
|
|
var end:Float;
|
2024-03-16 00:55:57 -04:00
|
|
|
}
|