Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<set name="expect-lime-version" value="${lime-version}" if="lime-version" />
<!-- <set name="lime-version" value="${expect-lime-version}" if="setup" unless="lime-version" /> -->

<define name="openfl-funkin" />

<haxedef name="native" if="cpp || neko" />
<set name="native" if="cpp || neko" />

Expand Down
24 changes: 12 additions & 12 deletions src/openfl/media/Sound.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
59 changes: 53 additions & 6 deletions src/openfl/media/SoundChannel.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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;
Expand Down
38 changes: 29 additions & 9 deletions src/openfl/utils/Assets.hx
Original file line number Diff line number Diff line change
Expand Up @@ -321,23 +321,21 @@ 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)
{
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);
}

/**
Expand Down Expand Up @@ -767,6 +765,17 @@ class Assets
#if !html5
var promise = new Promise<Sound>();

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)
{
Expand Down Expand Up @@ -868,6 +877,17 @@ class Assets
#if lime
var promise = new Promise<Sound>();

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)
{
Expand Down
Loading