Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AudioSource.latency #1896

Open
wants to merge 4 commits into
base: 8.3.0-Dev
Choose a base branch
from
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
40 changes: 40 additions & 0 deletions project/src/media/openal/OpenALBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2104,6 +2104,44 @@ namespace lime {
}


value lime_al_get_sourcedv_soft (value source, int param, int count) {

#ifdef LIME_OPENALSOFT
ALuint id = (ALuint)(uintptr_t)val_data (source);
ALdouble* values = new ALdouble[count];
alGetSourcedvSOFT (id, param, values);

value result = alloc_array (count);

for (int i = 0; i < count; i++) {

val_array_set_i (result, i, alloc_float (values[i]));

}

delete[] values;
return result;
#else
return alloc_null();
#endif

}


HL_PRIM varray* HL_NAME(hl_al_get_sourcedv_soft) (HL_CFFIPointer* source, int param, int count) {

#ifdef LIME_OPENALSOFT
ALuint id = (ALuint)(uintptr_t)source->ptr;
varray* result = hl_alloc_array (&hlt_f64, count);
alGetSourcedvSOFT (id, param, hl_aptr (result, double));
return result;
#else
return NULL;
#endif

}


value lime_al_get_sourcei (value source, int param) {

ALuint id = (ALuint)(uintptr_t)val_data (source);
Expand Down Expand Up @@ -3579,6 +3617,7 @@ namespace lime {
DEFINE_PRIME2 (lime_al_get_source3i);
DEFINE_PRIME2 (lime_al_get_sourcef);
DEFINE_PRIME3 (lime_al_get_sourcefv);
DEFINE_PRIME3 (lime_al_get_sourcedv_soft);
DEFINE_PRIME2 (lime_al_get_sourcei);
DEFINE_PRIME3 (lime_al_get_sourceiv);
DEFINE_PRIME1 (lime_al_get_string);
Expand Down Expand Up @@ -3703,6 +3742,7 @@ namespace lime {
DEFINE_HL_PRIM (_ARR, hl_al_get_source3i, _TCFFIPOINTER _I32);
DEFINE_HL_PRIM (_F32, hl_al_get_sourcef, _TCFFIPOINTER _I32);
DEFINE_HL_PRIM (_ARR, hl_al_get_sourcefv, _TCFFIPOINTER _I32 _I32);
DEFINE_HL_PRIM (_ARR, hl_al_get_sourcedv_soft, _TCFFIPOINTER _I32 _I32);
DEFINE_HL_PRIM (_DYN, hl_al_get_sourcei, _TCFFIPOINTER _I32);
DEFINE_HL_PRIM (_ARR, hl_al_get_sourceiv, _TCFFIPOINTER _I32 _I32);
DEFINE_HL_PRIM (_BYTES, hl_al_get_string, _I32);
Expand Down
5 changes: 5 additions & 0 deletions src/lime/_internal/backend/flash/FlashAudioSource.hx
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,9 @@ class FlashAudioSource

return position;
}

public function getLatency():Float
{
return 0;
}
}
15 changes: 15 additions & 0 deletions src/lime/_internal/backend/html5/HTML5AudioSource.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package lime._internal.backend.html5;

import lime.media.AudioManager;
import lime.math.Vector4;
import lime.media.AudioSource;

Expand Down Expand Up @@ -267,4 +268,18 @@ class HTML5AudioSource

return position;
}

public function getLatency():Float
{
var ctx = AudioManager.context.web;
if (ctx != null)
{
var baseLatency:Float = untyped ctx.baseLatency != null ? untyped ctx.baseLatency : 0;
var outputLatency:Float = untyped ctx.outputLatency != null ? untyped ctx.outputLatency : 0;

return (baseLatency + outputLatency) * 1000;
}

return 0;
}
}
21 changes: 21 additions & 0 deletions src/lime/_internal/backend/native/NativeAudioSource.hx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class NativeAudioSource
#end
private static var STREAM_TIMER_FREQUENCY = 100;

private static var hasALSoftLatencyExt:Null<Bool>;

private var buffers:Array<ALBuffer>;
private var bufferTimeBlocks:Array<Float>;
private var completed:Bool;
Expand Down Expand Up @@ -70,6 +72,11 @@ class NativeAudioSource

public function init():Void
{
if (hasALSoftLatencyExt == null)
{
hasALSoftLatencyExt = AL.isExtensionPresent("AL_SOFT_source_latency");
}

dataLength = 0;
format = 0;

Expand Down Expand Up @@ -588,4 +595,18 @@ class NativeAudioSource

return position;
}

public function getLatency():Float
{
if (hasALSoftLatencyExt)
{
var offsets = AL.getSourcedvSOFT(handle, AL.SEC_OFFSET_LATENCY_SOFT, 2);
if (offsets != null)
{
return offsets[1] * 1000;
}
}

return 0;
}
}
10 changes: 10 additions & 0 deletions src/lime/_internal/backend/native/NativeCFFI.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1580,6 +1580,8 @@ class NativeCFFI

@:cffi private static function lime_al_get_sourcefv(source:CFFIPointer, param:Int, count:Int):Array<Float>;

@:cffi private static function lime_al_get_sourcedv_soft(source:CFFIPointer, param:Int, count:Int):Array<Float>;

@:cffi private static function lime_al_get_sourcei(source:CFFIPointer, param:Int):Dynamic;

@:cffi private static function lime_al_get_sourceiv(source:CFFIPointer, param:Int, count:Int):Array<Int>;
Expand Down Expand Up @@ -1770,6 +1772,8 @@ class NativeCFFI
private static var lime_al_get_sourcef = new cpp.Callable<cpp.Object->Int->cpp.Float32>(cpp.Prime._loadPrime("lime", "lime_al_get_sourcef", "oif", false));
private static var lime_al_get_sourcefv = new cpp.Callable<cpp.Object->Int->Int->cpp.Object>(cpp.Prime._loadPrime("lime", "lime_al_get_sourcefv", "oiio",
false));
private static var lime_al_get_sourcedv_soft = new cpp.Callable<cpp.Object->Int->Int->cpp.Object>(cpp.Prime._loadPrime("lime", "lime_al_get_sourcedv_soft", "oiio",
false));
private static var lime_al_get_sourcei = new cpp.Callable<cpp.Object->Int->cpp.Object>(cpp.Prime._loadPrime("lime", "lime_al_get_sourcei", "oio", false));
private static var lime_al_get_sourceiv = new cpp.Callable<cpp.Object->Int->Int->cpp.Object>(cpp.Prime._loadPrime("lime", "lime_al_get_sourceiv", "oiio",
false));
Expand Down Expand Up @@ -1911,6 +1915,7 @@ class NativeCFFI
private static var lime_al_get_source3i = CFFI.load("lime", "lime_al_get_source3i", 2);
private static var lime_al_get_sourcef = CFFI.load("lime", "lime_al_get_sourcef", 2);
private static var lime_al_get_sourcefv = CFFI.load("lime", "lime_al_get_sourcefv", 3);
private static var lime_al_get_sourcedv_soft = CFFI.load("lime", "lime_al_get_sourcedv_soft", 3);
private static var lime_al_get_sourcei = CFFI.load("lime", "lime_al_get_sourcei", 2);
private static var lime_al_get_sourceiv = CFFI.load("lime", "lime_al_get_sourceiv", 3);
private static var lime_al_get_string = CFFI.load("lime", "lime_al_get_string", 1);
Expand Down Expand Up @@ -2168,6 +2173,11 @@ class NativeCFFI
return null;
}

@:hlNative("lime", "hl_al_get_sourcedv_soft") private static function lime_al_get_sourcedv_soft(source:CFFIPointer, param:Int, count:Int):hl.NativeArray<hl.F64>
{
return null;
}

@:hlNative("lime", "hl_al_get_sourcei") private static function lime_al_get_sourcei(source:CFFIPointer, param:Int):Dynamic
{
return null;
Expand Down
16 changes: 13 additions & 3 deletions src/lime/media/AudioSource.hx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import lime.math.Vector4;
@:noDebug
#end
/**
The `AudioSource` class provides a way to control audio playback in a Lime application.
It allows for playing, pausing, and stopping audio, as well as controlling various
The `AudioSource` class provides a way to control audio playback in a Lime application.
It allows for playing, pausing, and stopping audio, as well as controlling various
audio properties such as gain, pitch, and looping.

Depending on the platform, the audio backend may vary, but the API remains consistent.
Expand All @@ -24,7 +24,7 @@ class AudioSource
An event that is dispatched when the audio playback is complete.
**/
public var onComplete = new Event<Void->Void>();

/**
The `AudioBuffer` associated with this `AudioSource`.
**/
Expand Down Expand Up @@ -65,6 +65,11 @@ class AudioSource
**/
public var position(get, set):Vector4;

/**
The estimated output latency, in miliseconds, for this `AudioSource`. If not possible to retrieve will return `0`.
**/
public var latency(get, never):Float;

@:noCompletion private var __backend:AudioSourceBackend;

/**
Expand Down Expand Up @@ -191,6 +196,11 @@ class AudioSource
{
return __backend.setPosition(value);
}

@:noCompletion private function get_latency():Float
{
return __backend.getLatency();
}
}

#if flash
Expand Down
7 changes: 6 additions & 1 deletion src/lime/media/OpenALAudioContext.hx
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,12 @@ class OpenALAudioContext

public function getSourcefv(source:ALSource, param:Int, count:Int = 1):Array<Float>
{
return AL.getSourcefv(source, param);
return AL.getSourcefv(source, param, count);
}

public function getSourcedvSOFT(source:ALSource, param:Int, count:Int = 1):Array<Float>
{
return AL.getSourcedvSOFT(source, param, count);
}

public function getSourcei(source:ALSource, param:Int):Dynamic
Expand Down
21 changes: 21 additions & 0 deletions src/lime/media/openal/AL.hx
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ class AL
public static inline var FILTER_LOWPASS:Int = 0x0001;
public static inline var FILTER_HIGHPASS:Int = 0x0002;
public static inline var FILTER_BANDPASS:Int = 0x0003;
/* AL_SOFT_source_latency extension */
public static inline var SAMPLE_OFFSET_LATENCY_SOFT = 0x1200;
public static inline var SEC_OFFSET_LATENCY_SOFT = 0x1201;

public static function removeDirectFilter(source:ALSource)
{
Expand Down Expand Up @@ -968,6 +971,24 @@ class AL
#end
}

public static function getSourcedvSOFT(source:ALSource, param:Int, count:Int = 1):Array<Float>
{
#if (lime_cffi && lime_openal && !macro)
var result = NativeCFFI.lime_al_get_sourcedv_soft(source, param, count);
#if hl
if (result == null) return [];
var _result:Array<Float> = [];
for (i in 0...result.length)
_result[i] = result[i];
return _result;
#else
return result;
#end
#else
return null;
#end
}

public static function getSourcei(source:ALSource, param:Int):Dynamic
{
#if (lime_cffi && lime_openal && !macro)
Expand Down
Loading