Skip to content

Commit 3f9041a

Browse files
committed
Implement AudioSource.latency
Fix spaces
1 parent 8bfc0be commit 3f9041a

File tree

8 files changed

+120
-5
lines changed

8 files changed

+120
-5
lines changed

project/src/media/openal/OpenALBindings.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -2104,6 +2104,44 @@ namespace lime {
21042104
}
21052105

21062106

2107+
value lime_al_get_sourcedv_soft (value source, int param, int count) {
2108+
2109+
#ifdef LIME_OPENALSOFT
2110+
ALuint id = (ALuint)(uintptr_t)val_data (source);
2111+
ALdouble* values = new ALdouble[count];
2112+
alGetSourcedvSOFT (id, param, values);
2113+
2114+
value result = alloc_array (count);
2115+
2116+
for (int i = 0; i < count; i++) {
2117+
2118+
val_array_set_i (result, i, alloc_float (values[i]));
2119+
2120+
}
2121+
2122+
delete[] values;
2123+
return result;
2124+
#else
2125+
return alloc_null();
2126+
#endif
2127+
2128+
}
2129+
2130+
2131+
HL_PRIM varray* HL_NAME(hl_al_get_sourcedv_soft) (HL_CFFIPointer* source, int param, int count) {
2132+
2133+
#ifdef LIME_OPENALSOFT
2134+
ALuint id = (ALuint)(uintptr_t)source->ptr;
2135+
varray* result = hl_alloc_array (&hlt_f64, count);
2136+
alGetSourcedvSOFT (id, param, hl_aptr (result, double));
2137+
return result;
2138+
#else
2139+
return NULL;
2140+
#endif
2141+
2142+
}
2143+
2144+
21072145
value lime_al_get_sourcei (value source, int param) {
21082146

21092147
ALuint id = (ALuint)(uintptr_t)val_data (source);
@@ -3579,6 +3617,7 @@ namespace lime {
35793617
DEFINE_PRIME2 (lime_al_get_source3i);
35803618
DEFINE_PRIME2 (lime_al_get_sourcef);
35813619
DEFINE_PRIME3 (lime_al_get_sourcefv);
3620+
DEFINE_PRIME3 (lime_al_get_sourcedv_soft);
35823621
DEFINE_PRIME2 (lime_al_get_sourcei);
35833622
DEFINE_PRIME3 (lime_al_get_sourceiv);
35843623
DEFINE_PRIME1 (lime_al_get_string);
@@ -3703,6 +3742,7 @@ namespace lime {
37033742
DEFINE_HL_PRIM (_ARR, hl_al_get_source3i, _TCFFIPOINTER _I32);
37043743
DEFINE_HL_PRIM (_F32, hl_al_get_sourcef, _TCFFIPOINTER _I32);
37053744
DEFINE_HL_PRIM (_ARR, hl_al_get_sourcefv, _TCFFIPOINTER _I32 _I32);
3745+
DEFINE_HL_PRIM (_ARR, hl_al_get_sourcedv_soft, _TCFFIPOINTER _I32 _I32);
37063746
DEFINE_HL_PRIM (_DYN, hl_al_get_sourcei, _TCFFIPOINTER _I32);
37073747
DEFINE_HL_PRIM (_ARR, hl_al_get_sourceiv, _TCFFIPOINTER _I32 _I32);
37083748
DEFINE_HL_PRIM (_BYTES, hl_al_get_string, _I32);

src/lime/_internal/backend/flash/FlashAudioSource.hx

+5
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,9 @@ class FlashAudioSource
148148

149149
return position;
150150
}
151+
152+
public function getLatency():Float
153+
{
154+
return 0;
155+
}
151156
}

src/lime/_internal/backend/html5/HTML5AudioSource.hx

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package lime._internal.backend.html5;
22

3+
import lime.media.AudioManager;
34
import lime.math.Vector4;
45
import lime.media.AudioSource;
56

@@ -218,10 +219,10 @@ class HTML5AudioSource
218219
#if lime_howlerjs
219220
parent.buffer.__srcHowl.rate(value);
220221
#end
221-
222+
222223
return getPitch();
223224
}
224-
225+
225226

226227
public function getPosition():Vector4
227228
{
@@ -252,4 +253,18 @@ class HTML5AudioSource
252253

253254
return position;
254255
}
256+
257+
public function getLatency():Float
258+
{
259+
var ctx = AudioManager.context.web;
260+
if (ctx != null)
261+
{
262+
var baseLatency:Float = untyped ctx.baseLatency != null ? untyped ctx.baseLatency : 0;
263+
var outputLatency:Float = untyped ctx.outputLatency != null ? untyped ctx.outputLatency : 0;
264+
265+
return (baseLatency + outputLatency) * 1000;
266+
}
267+
268+
return 0;
269+
}
255270
}

src/lime/_internal/backend/native/NativeAudioSource.hx

+9
Original file line numberDiff line numberDiff line change
@@ -588,4 +588,13 @@ class NativeAudioSource
588588

589589
return position;
590590
}
591+
592+
public function getLatency():Float
593+
{
594+
var offsets = AL.getSourcedvSOFT(handle, AL.SEC_OFFSET_LATENCY_SOFT, 2);
595+
if (offsets != null)
596+
return offsets[1] * 1000;
597+
598+
return 0;
599+
}
591600
}

src/lime/_internal/backend/native/NativeCFFI.hx

+10
Original file line numberDiff line numberDiff line change
@@ -1562,6 +1562,8 @@ class NativeCFFI
15621562

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

1565+
@:cffi private static function lime_al_get_sourcedv_soft(source:CFFIPointer, param:Int, count:Int):Array<Float>;
1566+
15651567
@:cffi private static function lime_al_get_sourcei(source:CFFIPointer, param:Int):Dynamic;
15661568

15671569
@:cffi private static function lime_al_get_sourceiv(source:CFFIPointer, param:Int, count:Int):Array<Int>;
@@ -1752,6 +1754,8 @@ class NativeCFFI
17521754
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));
17531755
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",
17541756
false));
1757+
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",
1758+
false));
17551759
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));
17561760
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",
17571761
false));
@@ -1893,6 +1897,7 @@ class NativeCFFI
18931897
private static var lime_al_get_source3i = CFFI.load("lime", "lime_al_get_source3i", 2);
18941898
private static var lime_al_get_sourcef = CFFI.load("lime", "lime_al_get_sourcef", 2);
18951899
private static var lime_al_get_sourcefv = CFFI.load("lime", "lime_al_get_sourcefv", 3);
1900+
private static var lime_al_get_sourcedv_soft = CFFI.load("lime", "lime_al_get_sourcedv_soft", 3);
18961901
private static var lime_al_get_sourcei = CFFI.load("lime", "lime_al_get_sourcei", 2);
18971902
private static var lime_al_get_sourceiv = CFFI.load("lime", "lime_al_get_sourceiv", 3);
18981903
private static var lime_al_get_string = CFFI.load("lime", "lime_al_get_string", 1);
@@ -2150,6 +2155,11 @@ class NativeCFFI
21502155
return null;
21512156
}
21522157

2158+
@: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>
2159+
{
2160+
return null;
2161+
}
2162+
21532163
@:hlNative("lime", "hl_al_get_sourcei") private static function lime_al_get_sourcei(source:CFFIPointer, param:Int):Dynamic
21542164
{
21552165
return null;

src/lime/media/AudioSource.hx

+13-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import lime.math.Vector4;
1010
@:noDebug
1111
#end
1212
/**
13-
The `AudioSource` class provides a way to control audio playback in a Lime application.
14-
It allows for playing, pausing, and stopping audio, as well as controlling various
13+
The `AudioSource` class provides a way to control audio playback in a Lime application.
14+
It allows for playing, pausing, and stopping audio, as well as controlling various
1515
audio properties such as gain, pitch, and looping.
1616
1717
Depending on the platform, the audio backend may vary, but the API remains consistent.
@@ -24,7 +24,7 @@ class AudioSource
2424
An event that is dispatched when the audio playback is complete.
2525
**/
2626
public var onComplete = new Event<Void->Void>();
27-
27+
2828
/**
2929
The `AudioBuffer` associated with this `AudioSource`.
3030
**/
@@ -65,6 +65,11 @@ class AudioSource
6565
**/
6666
public var position(get, set):Vector4;
6767

68+
/**
69+
The estimated output latency, in miliseconds, for this `AudioSource`. If not possible to retrieve will return `0`.
70+
**/
71+
public var latency(get, never):Float;
72+
6873
@:noCompletion private var __backend:AudioSourceBackend;
6974

7075
/**
@@ -191,6 +196,11 @@ class AudioSource
191196
{
192197
return __backend.setPosition(value);
193198
}
199+
200+
@:noCompletion private function get_latency():Float
201+
{
202+
return __backend.getLatency();
203+
}
194204
}
195205

196206
#if flash

src/lime/media/OpenALAudioContext.hx

+5
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,11 @@ class OpenALAudioContext
391391
return AL.getSourcefv(source, param);
392392
}
393393

394+
public function getSourcedvSOFT(source:ALSource, param:Int, count:Int = 1):Array<Float>
395+
{
396+
return AL.getSourcedvSOFT(source, param);
397+
}
398+
394399
public function getSourcei(source:ALSource, param:Int):Dynamic
395400
{
396401
return AL.getSourcei(source, param);

src/lime/media/openal/AL.hx

+21
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ class AL
232232
public static inline var FILTER_LOWPASS:Int = 0x0001;
233233
public static inline var FILTER_HIGHPASS:Int = 0x0002;
234234
public static inline var FILTER_BANDPASS:Int = 0x0003;
235+
/* AL_SOFT_source_latency extension */
236+
public static inline var SAMPLE_OFFSET_LATENCY_SOFT = 0x1200;
237+
public static inline var SEC_OFFSET_LATENCY_SOFT = 0x1201;
235238

236239
public static function removeDirectFilter(source:ALSource)
237240
{
@@ -968,6 +971,24 @@ class AL
968971
#end
969972
}
970973

974+
public static function getSourcedvSOFT(source:ALSource, param:Int, count:Int = 1):Array<Float>
975+
{
976+
#if (lime_cffi && lime_openal && !macro)
977+
var result = NativeCFFI.lime_al_get_sourcedv_soft(source, param, count);
978+
#if hl
979+
if (result == null) return [];
980+
var _result:Array<Float> = [];
981+
for (i in 0...result.length)
982+
_result[i] = result[i];
983+
return _result;
984+
#else
985+
return result;
986+
#end
987+
#else
988+
return null;
989+
#end
990+
}
991+
971992
public static function getSourcei(source:ALSource, param:Int):Dynamic
972993
{
973994
#if (lime_cffi && lime_openal && !macro)

0 commit comments

Comments
 (0)