diff --git a/include.xml b/include.xml index 97742f1110..d74c509db6 100644 --- a/include.xml +++ b/include.xml @@ -8,6 +8,8 @@ + + diff --git a/src/openfl/media/Sound.hx b/src/openfl/media/Sound.hx index f1214692a2..f9e63edaa4 100644 --- a/src/openfl/media/Sound.hx +++ b/src/openfl/media/Sound.hx @@ -864,29 +864,29 @@ class Sound extends EventDispatcher return new ID3Info(); } - @:noCompletion private function get_length():Int + @:noCompletion private function get_length():Float { #if lime if (__buffer != null) { #if (js && html5 && howlerjs) - return Std.int(__buffer.src.duration() * 1000); + return __buffer.src.duration() * 1000; #else - if (__buffer.data != null) - { - var samples = (__buffer.data.length * 8.0) / (__buffer.channels * __buffer.bitsPerSample); - return Std.int(samples / __buffer.sampleRate * 1000); - } - else if (__buffer.__srcVorbisFile != null) + #if lime_funkin + if (__buffer.decoder != null) { - var samples = Int64.toInt(__buffer.__srcVorbisFile.pcmTotal()); - return Std.int(samples / __buffer.sampleRate * 1000); + var samples:Int64 = __buffer.decoder.total(); + return (samples.high * 4294967296.0 + (samples.low >>> 0)) / __buffer.decoder.sampleRate * 1000; } - else + #else + if (__buffer.__srcVorbisFile != null) { - return 0; + var samples:Int64 = __buffer.__srcVorbisFile.pcmTotal(); + return (samples.high * 4294967296.0 + (samples.low >>> 0)) / __buffer.sampleRate * 1000; } #end + else if (__buffer.data != null) return __buffer.data.length / (__buffer.bitsPerSample >> 3) / __buffer.channels / __buffer.sampleRate * 1000; + #end } #end diff --git a/src/openfl/media/SoundChannel.hx b/src/openfl/media/SoundChannel.hx index c261c170d0..7284143ce7 100644 --- a/src/openfl/media/SoundChannel.hx +++ b/src/openfl/media/SoundChannel.hx @@ -48,7 +48,7 @@ import lime.utils.Int16Array; The current amplitude (volume) of the left channel, from 0 (silent) to 1 (full amplitude). **/ - public var leftPeak(default, null):Float; + public var leftPeak(get, never):Float; /** When the sound is playing, the `position` property indicates in @@ -69,7 +69,7 @@ import lime.utils.Int16Array; The current amplitude (volume) of the right channel, from 0 (silent) to 1 (full amplitude). **/ - public var rightPeak(default, null):Float; + public var rightPeak(get, never):Float; /** The SoundTransform object assigned to the sound channel. A SoundTransform @@ -81,6 +81,11 @@ import lime.utils.Int16Array; @:noCompletion private var __sound:Sound; @:noCompletion private var __isValid:Bool; @:noCompletion private var __soundTransform:SoundTransform; + #if lime_funkin + @:noCompletion private var __lastPeakPosition:Float; + @:noCompletion private var __leftPeak:Float; + @:noCompletion private var __rightPeak:Float; + #end #if lime @:noCompletion private var __audioSource:AudioSource; #end @@ -122,9 +127,9 @@ import lime.utils.Int16Array; super(this); __sound = sound; - - leftPeak = 1; - rightPeak = 1; + #if lime_funkin + __lastPeakPosition = -100; + #end if (soundTransform != null) { @@ -322,7 +327,32 @@ import lime.utils.Int16Array; #end } + #if lime_funkin + @:noCompletion private function __updatePeaks():Void + { + var position = __audioSource.currentTime; + if (Math.abs(__lastPeakPosition - position) < 8) return; + __lastPeakPosition = position; + + var peaks = __audioSource.peaks; + __leftPeak = peaks[0]; + __rightPeak = peaks[1]; + } + #end + // Get & Set Methods + @:noCompletion private function get_leftPeak():Float + { + if (!__isValid) return 0; + + #if lime_funkin + __updatePeaks(); + return __leftPeak; + #else + return 0; + #end + } + @:noCompletion private function get_position():Float { if (!__isValid) return 0; @@ -344,6 +374,18 @@ import lime.utils.Int16Array; return value; } + @:noCompletion private function get_rightPeak():Float + { + if (!__isValid) return 0; + + #if lime_funkin + __updatePeaks(); + return __rightPeak; + #else + return 0; + #end + } + @:noCompletion private function get_soundTransform():SoundTransform { return __soundTransform.clone(); @@ -365,7 +407,12 @@ import lime.utils.Int16Array; if (__isValid) { - #if lime + #if lime_funkin + __audioSource.gain = volume; + __audioSource.pan = pan; + + return value; + #else __audioSource.gain = volume; var position = __audioSource.position; diff --git a/src/openfl/utils/Assets.hx b/src/openfl/utils/Assets.hx index febda9e67b..2a8e1a74ac 100644 --- a/src/openfl/utils/Assets.hx +++ b/src/openfl/utils/Assets.hx @@ -321,7 +321,11 @@ class Assets public static function getMusic(id:String, useCache:Bool = true):Sound { - #if (lime_vorbis && lime > "7.9.0") + #if (lime_funkin && lime_native) + var path = getPath(id); + var buffer = AudioBuffer.fromFile(path, true); + if (buffer != null) return Sound.fromAudioBuffer(buffer); + #elseif (lime_vorbis && lime > "7.9.0") var path = getPath(id); var vorbisFile = VorbisFile.fromFile(path); if (vorbisFile != null) @@ -329,15 +333,9 @@ class Assets var buffer = AudioBuffer.fromVorbisFile(vorbisFile); return Sound.fromAudioBuffer(buffer); } - else - { - // TODO: Streaming sound - return getSound(id, useCache); - } - #else - // TODO: Streaming sound - return getSound(id, useCache); #end + + return getSound(id, useCache); } /** @@ -767,6 +765,17 @@ class Assets #if !html5 var promise = new Promise(); + if (useCache && cache.enabled && cache.hasSound(id)) + { + var sound = cache.getSound(id); + + if (isValidSound(sound)) + { + promise.complete(sound); + return promise.future; + } + } + LimeAssets.loadAudioBuffer(id, useCache) .onComplete(function(buffer) { @@ -868,6 +877,17 @@ class Assets #if lime var promise = new Promise(); + if (useCache && cache.enabled && cache.hasSound(id)) + { + var sound = cache.getSound(id); + + if (isValidSound(sound)) + { + promise.complete(sound); + return promise.future; + } + } + LimeAssets.loadAudioBuffer(id, useCache) .onComplete(function(buffer) {