Skip to content

Commit 0fd1a4f

Browse files
committed
Implement Audio Overhaul from FunkinCrew's Lime
- Implement leftPeak, rightPeak in SoundChannel for lime_funkin - Support for more audio formats (mp3, flac, opus) # Conflicts: # src/openfl/utils/Assets.hx
1 parent 8853450 commit 0fd1a4f

3 files changed

Lines changed: 92 additions & 27 deletions

File tree

src/openfl/media/Sound.hx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -864,29 +864,29 @@ class Sound extends EventDispatcher
864864
return new ID3Info();
865865
}
866866

867-
@:noCompletion private function get_length():Int
867+
@:noCompletion private function get_length():Float
868868
{
869869
#if lime
870870
if (__buffer != null)
871871
{
872872
#if (js && html5 && howlerjs)
873-
return Std.int(__buffer.src.duration() * 1000);
873+
return __buffer.src.duration() * 1000;
874874
#else
875-
if (__buffer.data != null)
876-
{
877-
var samples = (__buffer.data.length * 8.0) / (__buffer.channels * __buffer.bitsPerSample);
878-
return Std.int(samples / __buffer.sampleRate * 1000);
879-
}
880-
else if (__buffer.__srcVorbisFile != null)
875+
#if lime_funkin
876+
if (__buffer.decoder != null)
881877
{
882-
var samples = Int64.toInt(__buffer.__srcVorbisFile.pcmTotal());
883-
return Std.int(samples / __buffer.sampleRate * 1000);
878+
var samples:Int64 = __buffer.decoder.total();
879+
return (samples.high * 4294967296.0 + (samples.low >>> 0)) / __buffer.decoder.sampleRate * 1000;
884880
}
885-
else
881+
#else
882+
if (__buffer.__srcVorbisFile != null)
886883
{
887-
return 0;
884+
var samples:Int64 = __buffer.__srcVorbisFile.pcmTotal();
885+
return (samples.high * 4294967296.0 + (samples.low >>> 0)) / __buffer.sampleRate * 1000;
888886
}
889887
#end
888+
else if (__buffer.data != null) return __buffer.data.length / (__buffer.bitsPerSample >> 3) / __buffer.channels / __buffer.sampleRate * 1000;
889+
#end
890890
}
891891
#end
892892

src/openfl/media/SoundChannel.hx

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import lime.utils.Int16Array;
4848
The current amplitude (volume) of the left channel, from 0 (silent) to 1
4949
(full amplitude).
5050
**/
51-
public var leftPeak(default, null):Float;
51+
public var leftPeak(get, never):Float;
5252

5353
/**
5454
When the sound is playing, the `position` property indicates in
@@ -69,7 +69,7 @@ import lime.utils.Int16Array;
6969
The current amplitude (volume) of the right channel, from 0 (silent) to 1
7070
(full amplitude).
7171
**/
72-
public var rightPeak(default, null):Float;
72+
public var rightPeak(get, never):Float;
7373

7474
/**
7575
The SoundTransform object assigned to the sound channel. A SoundTransform
@@ -81,6 +81,11 @@ import lime.utils.Int16Array;
8181
@:noCompletion private var __sound:Sound;
8282
@:noCompletion private var __isValid:Bool;
8383
@:noCompletion private var __soundTransform:SoundTransform;
84+
#if lime_funkin
85+
@:noCompletion private var __lastPeakPosition:Float;
86+
@:noCompletion private var __leftPeak:Float;
87+
@:noCompletion private var __rightPeak:Float;
88+
#end
8489
#if lime
8590
@:noCompletion private var __audioSource:AudioSource;
8691
#end
@@ -122,9 +127,7 @@ import lime.utils.Int16Array;
122127
super(this);
123128

124129
__sound = sound;
125-
126-
leftPeak = 1;
127-
rightPeak = 1;
130+
__lastPeakPosition = -100;
128131

129132
if (soundTransform != null)
130133
{
@@ -322,7 +325,32 @@ import lime.utils.Int16Array;
322325
#end
323326
}
324327

328+
#if lime_funkin
329+
@:noCompletion private function __updatePeaks():Void
330+
{
331+
var position = __audioSource.currentTime;
332+
if (Math.abs(__lastPeakPosition - position) < 8) return;
333+
__lastPeakPosition = position;
334+
335+
var peaks = __audioSource.peaks;
336+
__leftPeak = peaks[0];
337+
__rightPeak = peaks[1];
338+
}
339+
#end
340+
325341
// Get & Set Methods
342+
@:noCompletion private function get_leftPeak():Float
343+
{
344+
if (!__isValid) return 0;
345+
346+
#if lime_funkin
347+
__updatePeaks();
348+
return __leftPeak;
349+
#else
350+
return 0;
351+
#end
352+
}
353+
326354
@:noCompletion private function get_position():Float
327355
{
328356
if (!__isValid) return 0;
@@ -344,6 +372,18 @@ import lime.utils.Int16Array;
344372
return value;
345373
}
346374

375+
@:noCompletion private function get_rightPeak():Float
376+
{
377+
if (!__isValid) return 0;
378+
379+
#if lime_funkin
380+
__updatePeaks();
381+
return __rightPeak;
382+
#else
383+
return 0;
384+
#end
385+
}
386+
347387
@:noCompletion private function get_soundTransform():SoundTransform
348388
{
349389
return __soundTransform.clone();
@@ -365,7 +405,12 @@ import lime.utils.Int16Array;
365405

366406
if (__isValid)
367407
{
368-
#if lime
408+
#if lime_funkin
409+
__audioSource.gain = volume;
410+
__audioSource.pan = pan;
411+
412+
return value;
413+
#else
369414
__audioSource.gain = volume;
370415

371416
var position = __audioSource.position;

src/openfl/utils/Assets.hx

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -321,23 +321,21 @@ class Assets
321321

322322
public static function getMusic(id:String, useCache:Bool = true):Sound
323323
{
324-
#if (lime_vorbis && lime > "7.9.0")
324+
#if (lime_funkin && lime_native)
325+
var path = getPath(id);
326+
var buffer = AudioBuffer.fromFile(path, true);
327+
if (buffer != null) return Sound.fromAudioBuffer(buffer);
328+
#elseif (lime_vorbis && lime > "7.9.0")
325329
var path = getPath(id);
326330
var vorbisFile = VorbisFile.fromFile(path);
327331
if (vorbisFile != null)
328332
{
329333
var buffer = AudioBuffer.fromVorbisFile(vorbisFile);
330334
return Sound.fromAudioBuffer(buffer);
331335
}
332-
else
333-
{
334-
// TODO: Streaming sound
335-
return getSound(id, useCache);
336-
}
337-
#else
338-
// TODO: Streaming sound
339-
return getSound(id, useCache);
340336
#end
337+
338+
return getSound(id, useCache);
341339
}
342340

343341
/**
@@ -767,6 +765,17 @@ class Assets
767765
#if !html5
768766
var promise = new Promise<Sound>();
769767

768+
if (useCache && cache.enabled && cache.hasSound(id))
769+
{
770+
var sound = cache.getSound(id);
771+
772+
if (isValidSound(sound))
773+
{
774+
promise.complete(sound);
775+
return promise.future;
776+
}
777+
}
778+
770779
LimeAssets.loadAudioBuffer(id, useCache)
771780
.onComplete(function(buffer)
772781
{
@@ -868,6 +877,17 @@ class Assets
868877
#if lime
869878
var promise = new Promise<Sound>();
870879

880+
if (useCache && cache.enabled && cache.hasSound(id))
881+
{
882+
var sound = cache.getSound(id);
883+
884+
if (isValidSound(sound))
885+
{
886+
promise.complete(sound);
887+
return promise.future;
888+
}
889+
}
890+
871891
LimeAssets.loadAudioBuffer(id, useCache)
872892
.onComplete(function(buffer)
873893
{

0 commit comments

Comments
 (0)