From 00a60d5b30aad0ee08a853951b155749fa10c3dd Mon Sep 17 00:00:00 2001 From: George FunBook Date: Wed, 8 Nov 2023 17:11:41 -0600 Subject: [PATCH] fix crash from openfl 9.3.2 --- flixel/sound/FlxSound.hx | 232 ++++++++++++++++++++------------------- 1 file changed, 120 insertions(+), 112 deletions(-) diff --git a/flixel/sound/FlxSound.hx b/flixel/sound/FlxSound.hx index 0ae17fc948..177e305b83 100644 --- a/flixel/sound/FlxSound.hx +++ b/flixel/sound/FlxSound.hx @@ -1,11 +1,5 @@ package flixel.sound; -import openfl.events.IEventDispatcher; -import openfl.events.Event; -import openfl.media.Sound; -import openfl.media.SoundChannel; -import openfl.media.SoundTransform; -import openfl.net.URLRequest; import flixel.FlxBasic; import flixel.FlxG; import flixel.math.FlxMath; @@ -14,6 +8,12 @@ import flixel.system.FlxAssets.FlxSoundAsset; import flixel.tweens.FlxTween; import flixel.util.FlxStringUtil; import openfl.Assets; +import openfl.events.Event; +import openfl.events.IEventDispatcher; +import openfl.media.Sound; +import openfl.media.SoundChannel; +import openfl.media.SoundTransform; +import openfl.net.URLRequest; #if flash11 import openfl.utils.ByteArray; #end @@ -31,181 +31,185 @@ class FlxSound extends FlxBasic * Only really matters if you are doing proximity/panning stuff. */ public var x:Float; - + /** * The y position of this sound in world coordinates. * Only really matters if you are doing proximity/panning stuff. */ public var y:Float; - + /** * Whether or not this sound should be automatically destroyed when you switch states. */ public var persist:Bool; - + /** * The ID3 song name. Defaults to null. Currently only works for streamed sounds. */ public var name(default, null):String; - + /** * The ID3 artist name. Defaults to null. Currently only works for streamed sounds. */ public var artist(default, null):String; - + /** * Stores the average wave amplitude of both stereo channels */ public var amplitude(default, null):Float; - + /** * Just the amplitude of the left stereo channel */ public var amplitudeLeft(default, null):Float; - + /** * Just the amplitude of the right stereo channel */ public var amplitudeRight(default, null):Float; - + /** * Whether to call `destroy()` when the sound has finished playing. */ public var autoDestroy:Bool; - + /** * Tracker for sound complete callback. If assigned, will be called * each time when sound reaches its end. */ public var onComplete:Void->Void; - + /** * Pan amount. -1 = full left, 1 = full right. Proximity based panning overrides this. */ public var pan(get, set):Float; - + /** * Whether or not the sound is currently playing. */ public var playing(get, never):Bool; - + /** * Set volume to a value between 0 and 1 to change how this sound is. */ public var volume(get, set):Float; + #if FLX_PITCH /** * Set pitch, which also alters the playback speed. Default is 1. */ public var pitch(get, set):Float; #end + /** * The position in runtime of the music playback in milliseconds. * If set while paused, changes only come into effect after a `resume()` call. */ public var time(get, set):Float; - + /** * The length of the sound in milliseconds. * @since 4.2.0 */ public var length(get, never):Float; - + /** * The sound group this sound belongs to */ public var group(default, set):FlxSoundGroup; - + /** * Whether or not this sound should loop. */ public var looped:Bool; - + /** * In case of looping, the point (in milliseconds) from where to restart the sound when it loops back * @since 4.1.0 */ public var loopTime:Float = 0; - + /** * At which point to stop playing the sound, in milliseconds. * If not set / `null`, the sound completes normally. * @since 4.2.0 */ public var endTime:Null; - + /** * The tween used to fade this sound's volume in and out (set via `fadeIn()` and `fadeOut()`) * @since 4.1.0 */ public var fadeTween:FlxTween; - + /** * Internal tracker for a Flash sound object. */ @:allow(flixel.system.frontEnds.SoundFrontEnd.load) var _sound:Sound; - + /** * Internal tracker for a Flash sound channel object. */ var _channel:SoundChannel; - + /** * Internal tracker for a Flash sound transform object. */ var _transform:SoundTransform; - + /** * Internal tracker for whether the sound is paused or not (not the same as stopped). */ var _paused:Bool; - + /** * Internal tracker for volume. */ var _volume:Float; - + /** * Internal tracker for sound channel position. */ var _time:Float = 0; - + /** * Internal tracker for sound length, so that length can still be obtained while a sound is paused, because _sound becomes null. */ var _length:Float = 0; + #if FLX_PITCH /** * Internal tracker for pitch. */ var _pitch:Float = 1.0; #end + /** * Internal tracker for total volume adjustment. */ var _volumeAdjust:Float = 1.0; - + /** * Internal tracker for the sound's "target" (for proximity and panning). */ var _target:FlxObject; - + /** * Internal tracker for the maximum effective radius of this sound (for proximity and panning). */ var _radius:Float; - + /** * Internal tracker for whether to pan the sound left and right. Default is false. */ var _proximityPan:Bool; - + /** * Helper var to prevent the sound from playing after focus was regained when it was already paused. */ var _alreadyPaused:Bool = false; - + /** * The FlxSound constructor gets all the variables initialized, but NOT ready to play a sound yet. */ @@ -214,17 +218,17 @@ class FlxSound extends FlxBasic super(); reset(); } - + /** * An internal function for clearing all the variables used by sounds. */ function reset():Void { destroy(); - + x = 0; y = 0; - + _time = 0; _paused = false; _volume = 1.0; @@ -240,12 +244,12 @@ class FlxSound extends FlxBasic amplitudeLeft = 0; amplitudeRight = 0; autoDestroy = false; - + if (_transform == null) _transform = new SoundTransform(); _transform.pan = 0; } - + override public function destroy():Void { _transform = null; @@ -254,25 +258,25 @@ class FlxSound extends FlxBasic _target = null; name = null; artist = null; - + if (_channel != null) { _channel.removeEventListener(Event.SOUND_COMPLETE, stopped); _channel.stop(); _channel = null; } - + if (_sound != null) { _sound.removeEventListener(Event.ID3, gotID3); _sound = null; } - + onComplete = null; - + super.destroy(); } - + /** * Handles fade out, fade in, panning, proximity, and amplitude operations each frame. */ @@ -280,11 +284,11 @@ class FlxSound extends FlxBasic { if (!playing) return; - + _time = _channel.position; - + var radialMultiplier:Float = 1.0; - + // Distance-based volume control if (_target != null) { @@ -292,17 +296,17 @@ class FlxSound extends FlxBasic radialMultiplier = targetPosition.distanceTo(FlxPoint.weak(x, y)) / _radius; targetPosition.put(); radialMultiplier = 1 - FlxMath.bound(radialMultiplier, 0, 1); - + if (_proximityPan) { var d:Float = (x - _target.x) / _radius; _transform.pan = FlxMath.bound(d, -1, 1); } } - + _volumeAdjust = radialMultiplier; updateTransform(); - + if (_transform.volume > 0) { amplitudeLeft = _channel.leftPeak / _transform.volume; @@ -315,17 +319,17 @@ class FlxSound extends FlxBasic amplitudeRight = 0; amplitude = 0; } - + if (endTime != null && _time >= endTime) stopped(); } - + override public function kill():Void { super.kill(); cleanup(false); } - + /** * One of the main setup functions for sounds, this function loads a sound from an embedded MP3. * @@ -340,9 +344,9 @@ class FlxSound extends FlxBasic { if (EmbeddedSound == null) return this; - + cleanup(true); - + if ((EmbeddedSound is Sound)) { _sound = EmbeddedSound; @@ -358,11 +362,11 @@ class FlxSound extends FlxBasic else FlxG.log.error('Could not find a Sound asset with an ID of \'$EmbeddedSound\'.'); } - + // NOTE: can't pull ID3 info from embedded sound currently return init(Looped, AutoDestroy, OnComplete); } - + /** * One of the main setup functions for sounds, this function loads a sound from a URL. * @@ -377,7 +381,7 @@ class FlxSound extends FlxBasic public function loadStream(SoundURL:String, Looped:Bool = false, AutoDestroy:Bool = false, ?OnComplete:Void->Void, ?OnLoad:Void->Void):FlxSound { cleanup(true); - + _sound = new Sound(); _sound.addEventListener(Event.ID3, gotID3); var loadCallback:Event->Void = null; @@ -395,10 +399,10 @@ class FlxSound extends FlxBasic // Use a weak reference so this can be garbage collected if destroyed before loading. _sound.addEventListener(Event.COMPLETE, loadCallback, false, 0, true); _sound.load(new URLRequest(SoundURL)); - + return init(Looped, AutoDestroy, OnComplete); } - + #if flash11 /** * One of the main setup functions for sounds, this function loads a sound from a ByteArray. @@ -412,15 +416,15 @@ class FlxSound extends FlxBasic public function loadByteArray(Bytes:ByteArray, Looped:Bool = false, AutoDestroy:Bool = false, ?OnComplete:Void->Void):FlxSound { cleanup(true); - + _sound = new Sound(); _sound.addEventListener(Event.ID3, gotID3); _sound.loadCompressedDataFromByteArray(Bytes, Bytes.length); - + return init(Looped, AutoDestroy, OnComplete); } #end - + function init(Looped:Bool = false, AutoDestroy:Bool = false, ?OnComplete:Void->Void):FlxSound { looped = Looped; @@ -435,7 +439,7 @@ class FlxSound extends FlxBasic endTime = _length; return this; } - + /** * Call this function if you want this sound's volume to change * based on distance from a particular FlxObject. @@ -456,7 +460,7 @@ class FlxSound extends FlxBasic _proximityPan = Pan; return this; } - + /** * Call this function to play the sound - also works on paused sounds. * @@ -472,21 +476,21 @@ class FlxSound extends FlxBasic { if (!exists) return this; - + if (ForceRestart) cleanup(false, true); else if (playing) // Already playing sound return this; - + if (_paused) resume(); else startSound(StartTime); - + endTime = EndTime; return this; } - + /** * Unpause a sound. Only works on sounds that have been paused. */ @@ -496,7 +500,7 @@ class FlxSound extends FlxBasic startSound(_time); return this; } - + /** * Call this function to pause this sound. */ @@ -504,13 +508,13 @@ class FlxSound extends FlxBasic { if (!playing) return this; - + _time = _channel.position; _paused = true; cleanup(false, false); return this; } - + /** * Call this function to stop this sound. */ @@ -519,7 +523,7 @@ class FlxSound extends FlxBasic cleanup(autoDestroy, true); return this; } - + /** * Helper function that tweens this sound's volume. * @@ -531,10 +535,10 @@ class FlxSound extends FlxBasic if (fadeTween != null) fadeTween.cancel(); fadeTween = FlxTween.num(volume, To, Duration, {onComplete: onComplete}, volumeTween); - + return this; } - + /** * Helper function that tweens this sound's volume. * @@ -546,19 +550,19 @@ class FlxSound extends FlxBasic { if (!playing) play(); - + if (fadeTween != null) fadeTween.cancel(); - + fadeTween = FlxTween.num(From, To, Duration, {onComplete: onComplete}, volumeTween); return this; } - + function volumeTween(f:Float):Void { volume = f; } - + /** * Returns the currently selected "real" volume of the sound (takes fades and proximity into account). * @@ -568,7 +572,7 @@ class FlxSound extends FlxBasic { return _volume * _volumeAdjust; } - + /** * Helper function to set the coordinates of this object. * Sound positioning is used in conjunction with proximity/panning. @@ -581,7 +585,7 @@ class FlxSound extends FlxBasic x = X; y = Y; } - + /** * Call after adjusting the volume to update the sound channel's settings. */ @@ -590,11 +594,11 @@ class FlxSound extends FlxBasic { _transform.volume = #if FLX_SOUND_SYSTEM (FlxG.sound.muted ? 0 : 1) * FlxG.sound.volume * #end (group != null ? group.volume : 1) * _volume * _volumeAdjust; - + if (_channel != null) _channel.soundTransform = _transform; } - + /** * An internal helper function used to attempt to start playing * the sound and populate the _channel variable. @@ -603,7 +607,7 @@ class FlxSound extends FlxBasic { if (_sound == null) return; - + _time = StartTime; _paused = false; _channel = _sound.play(_time, 0, _transform); @@ -621,7 +625,7 @@ class FlxSound extends FlxBasic active = false; } } - + /** * An internal helper function used to help Flash * clean up finished sounds or restart looped sounds. @@ -630,7 +634,7 @@ class FlxSound extends FlxBasic { if (onComplete != null) onComplete(); - + if (looped) { cleanup(false); @@ -639,7 +643,7 @@ class FlxSound extends FlxBasic else cleanup(autoDestroy); } - + /** * An internal helper function used to help Flash clean up (and potentially re-use) finished sounds. * Will stop the current sound and destroy the associated SoundChannel, plus, @@ -656,23 +660,23 @@ class FlxSound extends FlxBasic reset(); return; } - + if (_channel != null) { _channel.removeEventListener(Event.SOUND_COMPLETE, stopped); _channel.stop(); _channel = null; } - + active = false; - + if (resetPosition) { _time = 0; _paused = false; } } - + /** * Internal event handler for ID3 info (i.e. fetching the song name). */ @@ -682,7 +686,7 @@ class FlxSound extends FlxBasic artist = _sound.id3.artist; _sound.removeEventListener(Event.ID3, gotID3); } - + #if FLX_SOUND_SYSTEM @:allow(flixel.system.frontEnds.SoundFrontEnd) function onFocus():Void @@ -690,7 +694,7 @@ class FlxSound extends FlxBasic if (!_alreadyPaused) resume(); } - + @:allow(flixel.system.frontEnds.SoundFrontEnd) function onFocusLost():Void { @@ -698,80 +702,84 @@ class FlxSound extends FlxBasic pause(); } #end - + function set_group(group:FlxSoundGroup):FlxSoundGroup { if (this.group != group) { var oldGroup:FlxSoundGroup = this.group; - + // New group must be set before removing sound to prevent infinite recursion this.group = group; - + if (oldGroup != null) oldGroup.remove(this); - + if (group != null) group.add(this); - + updateTransform(); } return group; } - + inline function get_playing():Bool { return _channel != null; } - + inline function get_volume():Float { return _volume; } - + function set_volume(Volume:Float):Float { _volume = FlxMath.bound(Volume, 0, 1); updateTransform(); return Volume; } + #if FLX_PITCH inline function get_pitch():Float { return _pitch; } - function set_pitch(v:Float):Float { if (_channel != null) #if openfl_legacy _channel.pitch = v; - #else + #elseif (openfl < "9.3.2") @:privateAccess if (_channel.__source != null) _channel.__source.pitch = v; + #else + @:privateAccess + if (_channel.__audioSource != null) + _channel.__audioSource.pitch = v; #end - + return _pitch = v; } #end - + inline function get_pan():Float { return _transform.pan; } - + inline function set_pan(pan:Float):Float { return _transform.pan = pan; } - + inline function get_time():Float { return _time; } - + function set_time(time:Float):Float { if (playing) @@ -781,12 +789,12 @@ class FlxSound extends FlxBasic } return _time = time; } - + inline function get_length():Float { return _length; } - + override public function toString():String { return FlxStringUtil.getDebugString([