aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit 65fad732907531012ddf88facbc58015c7aacf2c
parent b19dc59119ef309581bc0bd1ef0bfc8196e7524d
Author: anon <anon@example.com>
Date:   Fri, 16 Oct 2026 06:20:09 +0000

Identify native WAV and MPEG sound assets with Miles comparison

Diffstat:
Mdecompiled/runtime/linux_audio.c | 44++++++++++++++++++++++++++++++++++++++++++++
Mdecompiled/tests/audio.c | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 97 insertions(+), 0 deletions(-)

diff --git a/decompiled/runtime/linux_audio.c b/decompiled/runtime/linux_audio.c @@ -333,6 +333,49 @@ static void file_read(void) g_eax = result; } +static void file_type(void) +{ + uint32_t data = MEM32(g_esp + 4), size = MEM32(g_esp + 8); + g_esp += 12; + g_eax = 0; + if (!data || size < 8) return; + + /* RIFF chunks identify PCM/IMA WAV or the embedded MPEG payload. */ + if (size >= 12 && MEM32(data) == 0x46464952 && MEM32(data + 8) == 0x45564157) { + uint32_t format = 0, payload = 0, length = 0; + for (uint32_t offset = 12; offset <= size - 8;) { + uint32_t chunk = MEM32(data + offset + 4); + if (chunk > size - offset - 8) return; + if (MEM32(data + offset) == 0x20746d66 && chunk >= 16) format = data + offset + 8; + if (MEM32(data + offset) == 0x61746164) { payload = data + offset + 8; length = chunk; } + uint64_t next = (uint64_t)offset + 8 + chunk + (chunk & 1); + if (next > size) break; + offset = next; + } + if (format && payload) { + uint16_t tag = MEM16(format); + if (tag == 1) { g_eax = 1; return; } + if (tag == 17) { g_eax = MEM16(format + 14) == 4 ? 2 : 3; return; } + if (tag != 85) { g_eax = 3; return; } + data = payload; + size = length; + } + } + + /* Miles recognizes MPEG layers from the first 3072 bytes of frame headers. */ + if (size > 3072) size = 3072; + for (uint32_t offset = 0; offset + 4 <= size; ++offset) { + uint32_t header = MEM32(data + offset); + if ((header & 0xf0ff) != 0xf0ff) continue; + unsigned layer = (header >> 9) & 3; + if (header != UINT32_MAX && (header & 0xfc0000) != 0xfc0000 && layer) { + g_eax = 14 - layer; + return; + } + offset += 3; + } +} + static void mem_free_lock(void) { guest_free(MEM32(g_esp + 4)); @@ -424,6 +467,7 @@ recomp_func_t linux_audio_lookup(const char *dll, const char *name) {"_AIL_digital_latency@4", digital_latency}, {"_AIL_open_stream@12", open_stream}, {"_AIL_file_read@8", file_read}, + {"_AIL_file_type@8", file_type}, {"_AIL_mem_free_lock@4", mem_free_lock}, {"_AIL_start_stream@4", start_stream}, {"_AIL_pause_stream@8", pause_stream}, diff --git a/decompiled/tests/audio.c b/decompiled/tests/audio.c @@ -75,6 +75,58 @@ static uint32_t call(const char *name, unsigned count, const uint32_t *args) #endif } +static void file_types(uint32_t scratch) +{ + const struct { unsigned format, bits, mpeg, result; } cases[] = { + {1, 8, 0, 1}, {1, 16, 0, 1}, {17, 4, 0, 2}, {17, 8, 0, 3}, + {2, 4, 0, 3}, {3, 32, 0, 3}, {85, 0, 0xfffb9064, 13}, + {85, 0, 0xfffd9064, 12}, {85, 0, 0xffff9064, 11}, + {85, 0, 0, 0} + }; + for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) { + unsigned char *bytes = POINTER(scratch); + memset(bytes, 0, 128); + memcpy(bytes, "RIFF", 4); + bytes[4] = 120; + memcpy(bytes + 8, "WAVEfmt ", 8); + bytes[16] = 16; + bytes[20] = cases[i].format; + bytes[22] = 1; + bytes[24] = 0x44; bytes[25] = 0xac; + bytes[28] = 0x88; bytes[29] = 0x58; bytes[30] = 1; + bytes[32] = 2; + bytes[34] = cases[i].bits; + memcpy(bytes + 36, "data", 4); + bytes[40] = 84; + for (unsigned j = 0; j < 4; ++j) bytes[44 + j] = cases[i].mpeg >> (24 - j * 8); + uint32_t args[] = {scratch, 128}; + + unsigned type = call("_AIL_file_type@8", 2, args); + + assert(type == cases[i].result); + + /* Also recognize a raw MPEG payload without its RIFF wrapper. */ + if (cases[i].format == 85) { + args[0] = scratch + 44; args[1] = 84; + assert(call("_AIL_file_type@8", 2, args) == cases[i].result); + } + + /* Skip an odd-sized RIFF metadata chunk and its alignment byte. */ + memmove(bytes + 22, bytes + 12, 116); + memcpy(bytes + 12, "JUNK", 4); + memset(bytes + 16, 0, 6); + bytes[16] = 1; + bytes[4] = 130; + args[0] = scratch; args[1] = 138; + assert(call("_AIL_file_type@8", 2, args) == cases[i].result); + } + uint32_t empty[] = {0, 0}; + assert(!call("_AIL_file_type@8", 2, empty)); + empty[0] = scratch; empty[1] = 7; + assert(!call("_AIL_file_type@8", 2, empty)); + puts("audio types: PCM, IMA ADPCM, other WAV, three MPEG layers and empty passed"); +} + static void read_files(uint32_t scratch) { const unsigned sizes[] = {0, 7, 32769}; @@ -323,6 +375,7 @@ int main(int argc, char **argv) assert(g_eax > 0 && g_eax == (unsigned)SDL_GetNumAudioDevices(0)); assert(g_esp == 0x5000ff04); #endif + file_types(address); read_files(address); redist_paths(address); assert(call("_AIL_startup@0", 0, NULL) == 1);