Skip to content

Commit b0e2991

Browse files
committed
Rework Audio, FlxSound Stuffs
Look at below for more information about this commit - FunkinCrew#11
1 parent c48897a commit b0e2991

5 files changed

Lines changed: 201 additions & 217 deletions

File tree

flixel/FlxGame.hx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,11 @@ class FlxGame extends Sprite
597597
FlxG.sound.clearCache();
598598
#end
599599

600+
// sound datas too.
601+
#if FLX_SOUND_SYSTEM
602+
FlxG.sound.clearCache();
603+
#end
604+
600605
// Finally assign and create the new state
601606
_state = _nextState.createInstance();
602607
_state._constructor = _nextState.getConstructor();

flixel/sound/FlxSound.hx

Lines changed: 79 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package flixel.sound;
22

33
import lime.media.AudioBuffer;
4+
import lime.media.AudioEffect;
45
import lime.media.AudioSource;
56

67
import openfl.events.Event;
@@ -37,7 +38,7 @@ class FlxSound extends FlxBasic
3738
/**
3839
* The default value for the `timeScaledPitch` variable at creation if none is specified in the constructor.
3940
*/
40-
public static var defaultTimeScaledPitch:Bool = true;
41+
public static var defaultTimeScaledPitch:Bool = false;
4142
#end
4243

4344
/**
@@ -251,11 +252,6 @@ class FlxSound extends FlxBasic
251252
*/
252253
public var pan(get, set):Float;
253254

254-
/**
255-
* The current sound playback filter used for this sound.
256-
*/
257-
public var filter(default, set):FlxSoundFilter;
258-
259255
/**
260256
* The duration/length of the sound in milliseconds.
261257
* @since 4.2.0
@@ -436,18 +432,19 @@ class FlxSound extends FlxBasic
436432

437433
if (force)
438434
{
435+
clearEffects();
436+
439437
persist = false;
440438
loopUntil = -1;
441439
proximityEnabled = false;
442440
proximityPan = true;
443441
scrollFactor?.set(0, 0);
444442
target = null;
445443
radius = 1024;
446-
filter = null;
447444

448-
_looped = false;
449-
_loopTime = 0;
450-
_endTime = null;
445+
looped = false;
446+
loopTime = 0;
447+
endTime = null;
451448

452449
#if FLX_PITCH
453450
_timeScaledPitch = defaultTimeScaledPitch;
@@ -567,8 +564,8 @@ class FlxSound extends FlxBasic
567564
alive = false;
568565
exists = false;
569566

570-
_loopTime = 0;
571-
_endTime = null;
567+
loopTime = 0;
568+
endTime = null;
572569

573570
return this;
574571
}
@@ -733,10 +730,6 @@ class FlxSound extends FlxBasic
733730
this.autoDestroy = autoDestroy;
734731
this.onComplete = onComplete;
735732

736-
if (looped != null) _looped = looped;
737-
if (loopTime != null) _loopTime = loopTime;
738-
if (endTime != null) _endTime = endTime;
739-
740733
if (this.data != data)
741734
{
742735
if (data != null && !data.isDestroyed)
@@ -747,8 +740,6 @@ class FlxSound extends FlxBasic
747740
source.buffer = data.buffer;
748741
source.load();
749742

750-
source.loopTime = _loopTime;
751-
source.length = _endTime;
752743
loaded = true;
753744
}
754745
else
@@ -757,6 +748,10 @@ class FlxSound extends FlxBasic
757748
}
758749
}
759750

751+
if (looped != null) this.looped = looped;
752+
if (loopTime != null) this.loopTime = loopTime;
753+
if (endTime != null) this.endTime = endTime;
754+
760755
return this;
761756
}
762757

@@ -838,7 +833,7 @@ class FlxSound extends FlxBasic
838833
if (pitch != null) _pitch = pitch;
839834
#end
840835
if (pan != null) _pan = pan;
841-
if (endTime != null) _endTime = endTime;
836+
if (endTime != null) this.endTime = endTime;
842837

843838
if (!loaded || (playing && !forceRestart)) return this;
844839

@@ -881,7 +876,7 @@ class FlxSound extends FlxBasic
881876
if (pitch != null) _pitch = pitch;
882877
#end
883878
if (pan != null) _pan = pan;
884-
if (endTime != null) _endTime = endTime;
879+
if (endTime != null) this.endTime = endTime;
885880

886881
if (!loaded) return this;
887882

@@ -959,7 +954,8 @@ class FlxSound extends FlxBasic
959954
* @param ease EaseFunction to use for the tween.
960955
* @return This FlxSound instance (nice for chaining stuff together, if you're into that).
961956
*/
962-
public function fadeOut(duration = 1.0, to = 0.0, ?onComplete:FlxTween->Void, ?ease:EaseFunction):FlxSound {
957+
public function fadeOut(duration = 1.0, to = 0.0, ?onComplete:FlxTween->Void, ?ease:EaseFunction):FlxSound
958+
{
963959
fadeTween?.cancel();
964960
fadeTween = FlxTween.num(_volume, to, duration, {ease: ease ?? FlxEase.quadIn, onComplete: onComplete}, volumeTween);
965961

@@ -977,7 +973,8 @@ class FlxSound extends FlxBasic
977973
* @param ease EaseFunction to use for the tween.
978974
* @return This FlxSound instance (nice for chaining stuff together, if you're into that).
979975
*/
980-
public function fadeIn(duration = 1.0, from = 0.0, to = 1.0, ?onComplete:FlxTween->Void, ?ease:EaseFunction):FlxSound {
976+
public function fadeIn(duration = 1.0, from = 0.0, to = 1.0, ?onComplete:FlxTween->Void, ?ease:EaseFunction):FlxSound
977+
{
981978
if (!playing) play();
982979

983980
fadeTween?.cancel();
@@ -988,6 +985,61 @@ class FlxSound extends FlxBasic
988985

989986
function volumeTween(f:Float) volume = f;
990987

988+
/**
989+
* Adds an audio playback effect for this sound.
990+
*
991+
* @param effect A `lime.media.AudioEffect` instance.
992+
* @since FunkinCrew's Flixel & Lime
993+
*/
994+
public function addEffect(effect:AudioEffect):Void
995+
{
996+
source.addEffect(effect);
997+
}
998+
999+
/**
1000+
* Removes an audio playback effect from this sound.
1001+
*
1002+
* @param effect A `lime.media.AudioEffect` instance.
1003+
* @since FunkinCrew's Flixel & Lime
1004+
*/
1005+
public function removeEffect(effect:AudioEffect):Void
1006+
{
1007+
source.removeEffect(effect);
1008+
}
1009+
1010+
/**
1011+
* Clears any existing audio playback effects added in this sound.
1012+
* @since FunkinCrew's Flixel & Lime
1013+
*/
1014+
public function clearEffects():Void
1015+
{
1016+
source.clearEffects();
1017+
}
1018+
1019+
/**
1020+
* Returns the audio playback effect stred at the specified index.
1021+
*
1022+
* @param index An index to the `lime.media.AudioEffect`.
1023+
* @return The specified `lime.media.AudioEffect` from the index.
1024+
* @since FunkinCrew's Flixel & Lime
1025+
*/
1026+
public function getEffectAt(index:Int):AudioEffect
1027+
{
1028+
return source.getEffectAt(index);
1029+
}
1030+
1031+
/**
1032+
* Returns the index of a desired audio playback effect stored.
1033+
*
1034+
* @param effect A `lime.media.AudioEffect` instance.
1035+
* @return The index stored for the desired audio playback effect in this sound.
1036+
* @since FunkinCrew's Flixel & Lime
1037+
*/
1038+
public function getEffectIndex(effect:AudioEffect):Int
1039+
{
1040+
return source.getEffectIndex(effect);
1041+
}
1042+
9911043
/**
9921044
* Returns the currently selected "real" volume of the sound (takes fades and proximity).
9931045
*
@@ -1188,27 +1240,6 @@ class FlxSound extends FlxBasic
11881240
return value;
11891241
}
11901242

1191-
function set_filter(value:FlxSoundFilter):FlxSoundFilter
1192-
{
1193-
if (filter != value)
1194-
{
1195-
if (filter != null) filter.decrementUseCount();
1196-
1197-
if (value != null && !value.filter.disposed)
1198-
{
1199-
filter = value;
1200-
value.incrementUseCount();
1201-
source.filter = value.filter;
1202-
}
1203-
else
1204-
{
1205-
filter = null;
1206-
source.filter = null;
1207-
}
1208-
}
1209-
return filter;
1210-
}
1211-
12121243
function get_length():Float
12131244
{
12141245
return loaded ? data.length - offset : 0;
@@ -1369,18 +1400,17 @@ class FlxSound extends FlxBasic
13691400
@:allow(flixel.sound.FlxSoundGroup)
13701401
function _updateVolume():Void
13711402
{
1372-
if (_muted) source.gain = 0;
1403+
if (_muted || FlxG.sound._muted) source.gain = 0;
13731404
else source.gain = calcTransformVolume();
13741405
}
13751406

13761407
function calcTransformVolume():Float
13771408
{
1378-
final volume = getActualVolume();
1379-
13801409
#if FLX_SOUND_SYSTEM
1381-
return FlxG.sound.applySoundCurve(volume);
1410+
if (FlxG.sound._muted) return 0;
1411+
return FlxG.sound.applySoundCurve(getActualVolume() * FlxG.sound._volume);
13821412
#else
1383-
return volume;
1413+
return getActualVolume();
13841414
#end
13851415
}
13861416

@@ -1417,7 +1447,7 @@ class FlxSound extends FlxBasic
14171447
_amplitudeUpdated = true;
14181448
_amplitude = 0;
14191449

1420-
var peaks = source.__backend.getPeaks(get_time() - _lastTime);
1450+
final peaks = source.peaks;
14211451
for (i in 0...peaks.length)
14221452
{
14231453
_amplitudes[i] = peaks[i];

0 commit comments

Comments
 (0)