commit fc4a1a886d94ee5132c8262e0eb8a7e33fec72ee
parent 407f4559907b5c5c32d929ee54f72425f4b300be
Author: anon <anon@example.com>
Date: Fri, 16 Oct 2026 06:13:46 +0000
Implement native music stream seeking and verify playback
Diffstat:
2 files changed, 81 insertions(+), 4 deletions(-)
diff --git a/decompiled/runtime/linux_audio.c b/decompiled/runtime/linux_audio.c
@@ -15,6 +15,8 @@ static uint32_t redist_directory, next_driver = 0xf6000000;
static uint32_t next_stream = 0xf6800000;
struct audio_stream {
uint32_t handle, frames, position;
+ uint32_t seek_position;
+ int seek_pending;
int status, volume;
float *samples;
struct audio_stream *next;
@@ -337,7 +339,7 @@ static void mem_free_lock(void)
g_esp += 8;
}
-enum stream_operation { STREAM_STATUS, STREAM_VOLUME, STREAM_START, STREAM_PAUSE, STREAM_CLOSE, STREAM_POSITION };
+enum stream_operation { STREAM_STATUS, STREAM_VOLUME, STREAM_START, STREAM_PAUSE, STREAM_CLOSE, STREAM_POSITION, STREAM_SEEK };
static void stream_control(enum stream_operation operation, unsigned arguments)
{
@@ -363,10 +365,30 @@ static void stream_control(enum stream_operation operation, unsigned arguments)
if (position) MEM32(position) = llrint((double)stream->position * 1000 / driver->format.freq);
}
else if (operation == STREAM_VOLUME) stream->volume = (int32_t)value;
- else if (operation == STREAM_START) { stream->position = 0; stream->status = 4; }
+ else if (operation == STREAM_SEEK) {
+ uint64_t frame = llrint((double)driver->format.freq * (int32_t)value * 0.001);
+ stream->seek_position = frame < stream->frames ? frame : stream->frames;
+ stream->seek_pending = 1;
+ if (stream->status == 4) {
+ stream->position = stream->seek_position;
+ stream->seek_pending = 0;
+ if (stream->position == stream->frames) stream->status = 2;
+ } else stream->status = 2;
+ }
+ else if (operation == STREAM_START) {
+ stream->position = stream->seek_pending ? stream->seek_position : 0;
+ stream->seek_pending = 0;
+ stream->status = stream->position < stream->frames ? 4 : 2;
+ }
else if (operation == STREAM_PAUSE) {
if (value) { if (stream->status == 4) stream->status = 8; }
- else if (stream->position < stream->frames) stream->status = 4;
+ else {
+ if (stream->seek_pending) {
+ stream->position = stream->seek_position;
+ stream->seek_pending = 0;
+ }
+ if (stream->position < stream->frames) stream->status = 4;
+ }
} else if (operation == STREAM_CLOSE) {
*link = stream->next;
free(stream->samples);
@@ -384,6 +406,7 @@ static void close_stream(void) { stream_control(STREAM_CLOSE, 1); }
static void stream_status(void) { stream_control(STREAM_STATUS, 1); }
static void set_stream_volume(void) { stream_control(STREAM_VOLUME, 2); }
static void stream_ms_position(void) { stream_control(STREAM_POSITION, 3); }
+static void set_stream_ms_position(void) { stream_control(STREAM_SEEK, 2); }
recomp_func_t linux_audio_lookup(const char *dll, const char *name)
{
@@ -408,6 +431,7 @@ recomp_func_t linux_audio_lookup(const char *dll, const char *name)
{"_AIL_stream_status@4", stream_status},
{"_AIL_set_stream_volume@8", set_stream_volume},
{"_AIL_stream_ms_position@12", stream_ms_position},
+ {"_AIL_set_stream_ms_position@8", set_stream_ms_position},
};
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
@@ -184,6 +184,44 @@ static void rejected_formats(uint32_t scratch)
}
}
+static void stream_seek(uint32_t scratch)
+{
+ FILE *file = fopen("miles-seek-test.wav", "wb");
+ assert(file);
+ const unsigned char header[] = {
+ 'R','I','F','F', 0x34,0xb1,2,0, 'W','A','V','E',
+ 'f','m','t',' ', 16,0,0,0, 1,0,1,0, 0x44,0xac,0,0,
+ 0x88,0x58,1,0, 2,0,16,0, 'd','a','t','a', 0x10,0xb1,2,0
+ };
+ assert(fwrite(header, 1, sizeof(header), file) == sizeof(header));
+ for (unsigned i = 0; i < 176400; ++i) assert(fputc(0, file) != EOF);
+ assert(!fclose(file));
+ uint16_t format[] = {1, 2, 44100, 0, 45328, 2, 4, 16};
+ memcpy(POINTER(scratch + 16), format, sizeof(format));
+ uint32_t args[] = {scratch, 0, UINT32_MAX, scratch + 16};
+ assert(!call("_AIL_waveOutOpen@16", 4, args));
+ uint32_t driver = *(uint32_t *)POINTER(scratch);
+ strcpy(POINTER(scratch + 64), "miles-seek-test.wav");
+ uint32_t open[] = {driver, scratch + 64, 0};
+ uint32_t stream = call("_AIL_open_stream@12", 3, open);
+ assert(stream);
+ const unsigned positions[] = {0, 1, 9, 250, 1999, 2000, 2500, 0};
+ for (unsigned i = 0; i < 8; ++i) {
+ uint32_t seek[] = {stream, positions[i]};
+ call("_AIL_set_stream_ms_position@8", 2, seek);
+ uint32_t position[] = {stream, scratch + 128, scratch + 132};
+ call("_AIL_stream_ms_position@12", 3, position);
+
+ assert(*(uint32_t *)POINTER(scratch + 132) == 0);
+ assert(*(uint32_t *)POINTER(scratch + 128) == 2000);
+ assert(call("_AIL_stream_status@4", 1, &stream) == 2);
+ }
+ call("_AIL_close_stream@4", 1, &stream);
+ call("_AIL_waveOutClose@4", 1, &driver);
+ assert(!remove("miles-seek-test.wav"));
+ puts("audio seek: pending seeks preserve the stopped playback cursor");
+}
+
#ifndef _WIN32
static void stream_playback(uint32_t scratch)
{
@@ -237,6 +275,20 @@ static void stream_playback(uint32_t scratch)
for (unsigned i = 0; call("_AIL_stream_status@4", 1, &stream) == 4 && i < 1000; ++i) SDL_Delay(1);
assert(call("_AIL_stream_status@4", 1, &stream) == 2);
+ uint32_t seek[] = {stream, 50};
+ nonzero_samples = bad_samples = 0;
+
+ call("_AIL_set_stream_ms_position@8", 2, seek);
+ capture = 1;
+ call("_AIL_start_stream@4", 1, &stream);
+ for (unsigned i = 0; call("_AIL_stream_status@4", 1, &stream) == 4 && i < 1000; ++i) SDL_Delay(1);
+ capture = 0;
+
+ assert(call("_AIL_stream_status@4", 1, &stream) == 2);
+ assert(nonzero_samples == 4410 && !bad_samples);
+ call("_AIL_stream_ms_position@12", 3, position);
+ assert(MEM32(scratch + 136) == 100);
+
/* A muted stream still advances to completion while producing only silence. */
uint32_t volume[] = {stream, 0};
call("_AIL_set_stream_volume@8", 2, volume);
@@ -294,9 +346,10 @@ int main(int argc, char **argv)
fprintf(stderr, "Windows reports no wave output device; device opening is not compared.\n");
call("_AIL_shutdown@0", 0, NULL);
assert(call("_AIL_startup@0", 0, NULL) == 1);
- } else driver_lifetime(address);
+ } else { driver_lifetime(address); stream_seek(address); }
#else
driver_lifetime(address);
+ stream_seek(address);
stream_playback(address);
fprintf(stderr, "Linux SDL: device enumeration and 4 PCM format open/close/shutdown cases passed.\n");
#endif