Skip to content

Commit

Permalink
Implement AudioSource.latency
Browse files Browse the repository at this point in the history
Fix spaces
  • Loading branch information
ACrazyTown committed Jan 22, 2025
1 parent 8bfc0be commit 3f9041a
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 5 deletions.
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;
}
}
19 changes: 17 additions & 2 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 @@ -218,10 +219,10 @@ class HTML5AudioSource
#if lime_howlerjs
parent.buffer.__srcHowl.rate(value);
#end

return getPitch();
}


public function getPosition():Vector4
{
Expand Down Expand Up @@ -252,4 +253,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;
}
}
9 changes: 9 additions & 0 deletions src/lime/_internal/backend/native/NativeAudioSource.hx
Original file line number Diff line number Diff line change
Expand Up @@ -588,4 +588,13 @@ class NativeAudioSource

return position;
}

public function getLatency():Float
{
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 @@ -1562,6 +1562,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 @@ -1752,6 +1754,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 @@ -1893,6 +1897,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 @@ -2150,6 +2155,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
5 changes: 5 additions & 0 deletions src/lime/media/OpenALAudioContext.hx
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,11 @@ class OpenALAudioContext
return AL.getSourcefv(source, param);
}

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

public function getSourcei(source:ALSource, param:Int):Dynamic
{
return AL.getSourcei(source, param);
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

0 comments on commit 3f9041a

Please sign in to comment.