commit 2f67192c583eb67dda3e907f3b01d70ac34b0298
parent 12dffe54681d185c95b77d0d86c6aad1a1bfdd08
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 11:26:54 +0000
Report native SDL output buffer latency to the game
Diffstat:
2 files changed, 29 insertions(+), 2 deletions(-)
diff --git a/decompiled/runtime/linux_audio.c b/decompiled/runtime/linux_audio.c
@@ -13,7 +13,7 @@ static struct audio_driver {
struct audio_driver *next;
uint32_t handle;
SDL_AudioDeviceID device;
- int sample_capacity, volume;
+ int sample_capacity, volume, latency;
} *drivers;
static void initialize_audio(void)
@@ -103,8 +103,9 @@ static void wave_out_open(void)
desired.channels = channels;
desired.format = bits == 16 ? AUDIO_S16LSB : AUDIO_U8;
desired.samples = 512;
+ SDL_AudioSpec obtained;
SDL_AudioDeviceID device = SDL_OpenAudioDevice(index == UINT32_MAX ? NULL :
- SDL_GetAudioDeviceName(index, 0), 0, &desired, NULL, 0);
+ SDL_GetAudioDeviceName(index, 0), 0, &desired, &obtained, 0);
if (device) {
struct audio_driver *driver = calloc(1, sizeof(*driver));
if (!driver) abort();
@@ -112,6 +113,7 @@ static void wave_out_open(void)
driver->device = device;
driver->sample_capacity = sample_capacity;
driver->volume = 127;
+ driver->latency = (unsigned)obtained.samples * 1000 / obtained.freq;
driver->next = drivers;
drivers = driver;
MEM32(output) = driver->handle;
@@ -165,6 +167,22 @@ static void miles_shutdown(void)
g_esp += 4;
}
+static void digital_latency(void)
+{
+ uint32_t handle = MEM32(g_esp + 4);
+ g_eax = 0;
+ if (pthread_mutex_lock(&audio_mutex)) abort();
+ if (handle) {
+ struct audio_driver *driver = drivers;
+ while (driver && driver->handle != handle) driver = driver->next;
+ if (!driver) abort();
+ /* SDL exposes its buffer duration, not the downstream hardware delay. */
+ g_eax = driver->latency;
+ }
+ if (pthread_mutex_unlock(&audio_mutex)) abort();
+ g_esp += 8;
+}
+
recomp_func_t linux_audio_lookup(const char *dll, const char *name)
{
if (!strcasecmp(dll, "winmm.dll") && !strcmp(name, "waveOutGetNumDevs"))
@@ -178,6 +196,7 @@ recomp_func_t linux_audio_lookup(const char *dll, const char *name)
{"_AIL_waveOutClose@4", wave_out_close},
{"_AIL_set_digital_master_volume@8", set_digital_master_volume},
{"_AIL_shutdown@0", miles_shutdown},
+ {"_AIL_digital_latency@4", digital_latency},
};
for (unsigned i = 0; i < sizeof(functions) / sizeof(functions[0]); ++i)
if (!strcmp(name, functions[i].name)) return functions[i].function;
diff --git a/decompiled/tests/audio.c b/decompiled/tests/audio.c
@@ -81,6 +81,12 @@ static void driver_lifetime(uint32_t scratch)
assert(result == 0 && driver && driver != 0xa5a5a5a5);
assert(bytes[0] == 0xa5 && bytes[3] == 0xa5 && bytes[8] == 0xa5 && bytes[11] == 0xa5);
+ uint32_t latency = call("_AIL_digital_latency@4", 1, &driver);
+ assert(latency > 0 && latency < 1000);
+ assert(call("_AIL_digital_latency@4", 1, &driver) == latency);
+#ifndef _WIN32
+ assert(latency == (i & 1 ? 11 : 23));
+#endif
uint32_t volume[] = {driver, i & 1 ? 127 : 0};
call("_AIL_set_digital_master_volume@8", 2, volume);
if (i == 3) {
@@ -129,6 +135,8 @@ int main(int argc, char **argv)
redist_paths(address);
assert(call("_AIL_startup@0", 0, NULL) == 1);
assert(call("_AIL_startup@0", 0, NULL) == 0);
+ uint32_t null_driver = 0;
+ assert(call("_AIL_digital_latency@4", 1, &null_driver) == 0);
uint32_t preference[] = {1, 16};
assert(call("_AIL_set_preference@8", 2, preference) == 64);
preference[1] = 32;