aorecomp

Anarchy Online classic in the browser

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


commit 97d2de3d844324f3c6a74367b8017760e3c7dfdf
parent 65fad732907531012ddf88facbc58015c7aacf2c
Author: anon <anon@example.com>
Date:   Fri, 16 Oct 2026 06:27:34 +0000

Implement native sound-effect samples and verify PCM and IMA playback

Diffstat:
Mdecompiled/runtime/linux_audio.c | 186+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdecompiled/tests/audio.c | 140++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 325 insertions(+), 1 deletion(-)

diff --git a/decompiled/runtime/linux_audio.c b/decompiled/runtime/linux_audio.c @@ -13,6 +13,14 @@ static pthread_mutex_t audio_mutex = PTHREAD_MUTEX_INITIALIZER; static int miles_started, sample_capacity = 64; static uint32_t redist_directory, next_driver = 0xf6000000; static uint32_t next_stream = 0xf6800000; +static uint32_t next_sample = 0xf7000000; +struct audio_sample { + uint32_t handle, frames; + int status, volume, rate, channels, loops; + double position; + float *samples; + struct audio_sample *next; +}; struct audio_stream { uint32_t handle, frames, position; uint32_t seek_position; @@ -28,6 +36,7 @@ static struct audio_driver { int sample_capacity, volume, latency; SDL_AudioSpec format; struct audio_stream *streams; + struct audio_sample *samples; } *drivers; /* SDL holds its device lock during this callback; it never enters guest code. */ @@ -43,6 +52,20 @@ static void render_audio(void *userdata, Uint8 *output, int length) if (stream->status == 4) sample += stream->samples[stream->position * driver->format.channels + channel] * (stream->volume / 127.0f) * (driver->volume / 127.0f); + for (struct audio_sample *voice = driver->samples; voice; voice = voice->next) { + if (voice->status != 4) continue; + unsigned index = voice->position; + unsigned next = index + 1 < voice->frames ? index + 1 : index; + float fraction = voice->position - index; + float value = 0; + for (int source = 0; source < voice->channels; ++source) { + if (driver->format.channels == 2 && voice->channels == 2 && source != channel) continue; + float first = voice->samples[index * voice->channels + source]; + value += first + (voice->samples[next * voice->channels + source] - first) * fraction; + } + if (driver->format.channels == 1 && voice->channels == 2) value *= 0.5f; + sample += value * (voice->volume / 127.0f) * (driver->volume / 127.0f); + } if (sample < -1) sample = -1; if (sample > 1) sample = 1; int index = frame * driver->format.channels + channel; @@ -58,6 +81,15 @@ static void render_audio(void *userdata, Uint8 *output, int length) } for (struct audio_stream *stream = driver->streams; stream; stream = stream->next) if (stream->status == 4 && ++stream->position == stream->frames) stream->status = 2; + for (struct audio_sample *voice = driver->samples; voice; voice = voice->next) { + if (voice->status != 4) continue; + voice->position += (double)voice->rate / driver->format.freq; + while (voice->position >= voice->frames) { + if (voice->loops == 1) { voice->status = 2; voice->position = voice->frames; break; } + if (voice->loops > 1) --voice->loops; + voice->position -= voice->frames; + } + } } } @@ -70,6 +102,12 @@ static void destroy_driver_locked(struct audio_driver *driver) free(stream->samples); free(stream); } + while (driver->samples) { + struct audio_sample *sample = driver->samples; + driver->samples = sample->next; + free(sample->samples); + free(sample); + } free(driver); } @@ -382,6 +420,143 @@ static void mem_free_lock(void) g_esp += 8; } +/* libsndfile reads guest RIFF data through a bounded, read-only view. */ +struct audio_memory { const unsigned char *data; sf_count_t size, position; }; +static sf_count_t audio_length(void *context) { return ((struct audio_memory *)context)->size; } +static sf_count_t audio_tell(void *context) { return ((struct audio_memory *)context)->position; } +static sf_count_t audio_seek(sf_count_t offset, int whence, void *context) +{ + struct audio_memory *memory = context; + sf_count_t position = offset + (whence == SEEK_CUR ? memory->position : whence == SEEK_END ? memory->size : 0); + if (position < 0 || position > memory->size) return -1; + return memory->position = position; +} +static sf_count_t audio_read(void *output, sf_count_t count, void *context) +{ + struct audio_memory *memory = context; + if (count > memory->size - memory->position) count = memory->size - memory->position; + memcpy(output, memory->data + memory->position, count); + memory->position += count; + return count; +} + +static void init_sample_locked(struct audio_sample *sample) +{ + free(sample->samples); + sample->samples = NULL; + sample->frames = 0; + sample->position = 0; + sample->status = 2; + sample->volume = 127; + sample->rate = 11025; + sample->loops = 1; +} + +static void allocate_sample_handle(void) +{ + uint32_t handle = MEM32(g_esp + 4); + g_esp += 8; + g_eax = 0; + if (!handle) return; + if (pthread_mutex_lock(&audio_mutex)) abort(); + struct audio_driver *driver = drivers; + while (driver && driver->handle != handle) driver = driver->next; + if (!driver) abort(); + SDL_LockAudioDevice(driver->device); + unsigned used = 0; + struct audio_sample *available = NULL; + for (struct audio_sample *sample = driver->samples; sample; sample = sample->next) { + if (sample->status == 1) available = sample; + else ++used; + } + for (struct audio_stream *stream = driver->streams; stream; stream = stream->next) ++used; + if (used < (unsigned)driver->sample_capacity) { + if (!available) { + available = calloc(1, sizeof(*available)); + if (!available || next_sample >= 0xf7800000u) abort(); + available->handle = next_sample++; + available->next = driver->samples; + driver->samples = available; + } + init_sample_locked(available); + g_eax = available->handle; + } + SDL_UnlockAudioDevice(driver->device); + if (pthread_mutex_unlock(&audio_mutex)) abort(); +} + +enum sample_operation { SAMPLE_INIT, SAMPLE_RELEASE, SAMPLE_STATUS, SAMPLE_FILE, SAMPLE_START, + SAMPLE_END, SAMPLE_VOLUME, SAMPLE_LOOPS, SAMPLE_RATE, SAMPLE_GET_RATE }; + +static void sample_control(enum sample_operation operation, unsigned arguments) +{ + uint32_t handle = MEM32(g_esp + 4), value = arguments > 1 ? MEM32(g_esp + 8) : 0; + g_esp += 4 + arguments * 4; + g_eax = 0; + if (!handle) return; + if (pthread_mutex_lock(&audio_mutex)) abort(); + struct audio_driver *driver = drivers; + struct audio_sample *sample = NULL; + for (; driver; driver = driver->next) { + for (sample = driver->samples; sample && sample->handle != handle; sample = sample->next) {} + if (sample) break; + } + if (!sample) abort(); + SDL_LockAudioDevice(driver->device); + if (operation == SAMPLE_INIT) init_sample_locked(sample); + else if (operation == SAMPLE_RELEASE) { init_sample_locked(sample); sample->status = 1; } + else if (operation == SAMPLE_STATUS) g_eax = sample->status; + else if (operation == SAMPLE_GET_RATE) g_eax = sample->rate; + else if (operation == SAMPLE_RATE) { + if ((int32_t)value <= 0) abort(); + sample->rate = value; + } else if (operation == SAMPLE_VOLUME) sample->volume = (int32_t)value < 0 ? 0 : value > 127 ? 127 : value; + else if (operation == SAMPLE_LOOPS) sample->loops = value; + else if (operation == SAMPLE_END) { if (sample->status != 1) sample->status = 2; } + else if (operation == SAMPLE_START) { + if (sample->status != 1 && sample->frames) { sample->position = 0; sample->status = 4; } + } else if (operation == SAMPLE_FILE) { + if (value && MEM32(value) == 0x46464952 && MEM32(value + 8) == 0x45564157) { + struct audio_memory memory = {(const unsigned char *)ADDR(value), (uint64_t)MEM32(value + 4) + 8, 0}; + SF_VIRTUAL_IO io = {audio_length, audio_seek, audio_read, NULL, audio_tell}; + SF_INFO info = {0}; + SNDFILE *file = sf_open_virtual(&io, SFM_READ, &info, &memory); + if (file) { + if (info.frames <= 0 || info.channels < 1 || info.channels > 2 || + info.frames > INT_MAX / (int)sizeof(float) / info.channels) abort(); + float *decoded = malloc(info.frames * info.channels * sizeof(float)); + if (!decoded) abort(); + sf_count_t frames = sf_readf_float(file, decoded, info.frames); + if (frames > 0 && !sf_error(file)) { + free(sample->samples); + sample->samples = decoded; + sample->frames = frames; + sample->channels = info.channels; + sample->rate = info.samplerate; + sample->position = 0; + sample->status = 2; + g_eax = 1; + } else free(decoded); + if (sf_close(file)) abort(); + } + } + } else abort(); + SDL_UnlockAudioDevice(driver->device); + if (operation == SAMPLE_START) SDL_PauseAudioDevice(driver->device, 0); + if (pthread_mutex_unlock(&audio_mutex)) abort(); +} + +static void init_sample(void) { sample_control(SAMPLE_INIT, 1); } +static void release_sample_handle(void) { sample_control(SAMPLE_RELEASE, 1); } +static void sample_status(void) { sample_control(SAMPLE_STATUS, 1); } +static void set_sample_file(void) { sample_control(SAMPLE_FILE, 3); } +static void start_sample(void) { sample_control(SAMPLE_START, 1); } +static void end_sample(void) { sample_control(SAMPLE_END, 1); } +static void set_sample_volume(void) { sample_control(SAMPLE_VOLUME, 2); } +static void set_sample_loop_count(void) { sample_control(SAMPLE_LOOPS, 2); } +static void set_sample_playback_rate(void) { sample_control(SAMPLE_RATE, 2); } +static void sample_playback_rate(void) { sample_control(SAMPLE_GET_RATE, 1); } + 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) @@ -468,6 +643,17 @@ recomp_func_t linux_audio_lookup(const char *dll, const char *name) {"_AIL_open_stream@12", open_stream}, {"_AIL_file_read@8", file_read}, {"_AIL_file_type@8", file_type}, + {"_AIL_allocate_sample_handle@4", allocate_sample_handle}, + {"_AIL_init_sample@4", init_sample}, + {"_AIL_release_sample_handle@4", release_sample_handle}, + {"_AIL_sample_status@4", sample_status}, + {"_AIL_set_sample_file@12", set_sample_file}, + {"_AIL_start_sample@4", start_sample}, + {"_AIL_end_sample@4", end_sample}, + {"_AIL_set_sample_volume@8", set_sample_volume}, + {"_AIL_set_sample_loop_count@8", set_sample_loop_count}, + {"_AIL_set_sample_playback_rate@8", set_sample_playback_rate}, + {"_AIL_sample_playback_rate@4", sample_playback_rate}, {"_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 @@ -236,6 +236,111 @@ static void rejected_formats(uint32_t scratch) } } +static void sample_handles(uint32_t scratch) +{ + uint32_t preference[] = {1, 4}; + uint32_t previous = call("_AIL_set_preference@8", 2, preference); + uint16_t format[] = {1, 2, 44100, 0, 45328, 2, 4, 16}; + memcpy(POINTER(scratch + 16), format, sizeof(format)); + uint32_t open[] = {scratch, 0, UINT32_MAX, scratch + 16}; + assert(!call("_AIL_waveOutOpen@16", 4, open)); + uint32_t driver = *(uint32_t *)POINTER(scratch), handles[4]; + + for (unsigned i = 0; i < 4; ++i) handles[i] = call("_AIL_allocate_sample_handle@4", 1, &driver); + + for (unsigned i = 0; i < 4; ++i) { + assert(handles[i]); + for (unsigned j = 0; j < i; ++j) assert(handles[i] != handles[j]); + assert(call("_AIL_sample_status@4", 1, &handles[i]) == 2); + assert(call("_AIL_sample_playback_rate@4", 1, &handles[i]) == 11025); + } + assert(!call("_AIL_allocate_sample_handle@4", 1, &driver)); + call("_AIL_release_sample_handle@4", 1, &handles[1]); + assert(call("_AIL_sample_status@4", 1, &handles[1]) == 1); + assert(call("_AIL_allocate_sample_handle@4", 1, &driver) == handles[1]); + uint32_t empty[] = {handles[0], 0, 1}; + assert(!call("_AIL_set_sample_file@12", 3, empty)); + call("_AIL_start_sample@4", 1, handles); + assert(call("_AIL_sample_status@4", 1, handles) == 2); + + const unsigned char wave[] = { + 'R','I','F','F', 40,0,0,0, 'W','A','V','E', + 'f','m','t',' ', 16,0,0,0, 1,0,1,0, 0x40,0x1f,0,0, + 0x80,0x3e,0,0, 2,0,16,0, 'd','a','t','a', 4,0,0,0, 0,32,0,32 + }; + memcpy(POINTER(scratch + 64), wave, sizeof(wave)); + uint32_t file[] = {handles[0], scratch + 64, 1}; + + assert(call("_AIL_set_sample_file@12", 3, file) == 1); + uint32_t loops[] = {handles[0], 0}; + call("_AIL_set_sample_loop_count@8", 2, loops); + call("_AIL_start_sample@4", 1, handles); + + assert(call("_AIL_sample_playback_rate@4", 1, handles) == 8000); + assert(call("_AIL_sample_status@4", 1, handles) == 4); + call("_AIL_end_sample@4", 1, handles); + assert(call("_AIL_sample_status@4", 1, handles) == 2); + uint32_t rate[] = {handles[0], 22050}; + call("_AIL_set_sample_playback_rate@8", 2, rate); + assert(call("_AIL_sample_playback_rate@4", 1, handles) == 22050); + call("_AIL_init_sample@4", 1, handles); + assert(call("_AIL_sample_playback_rate@4", 1, handles) == 11025); + call("_AIL_start_sample@4", 1, handles); + assert(call("_AIL_sample_status@4", 1, handles) == 2); + for (unsigned i = 0; i < 4; ++i) call("_AIL_release_sample_handle@4", 1, &handles[i]); + call("_AIL_waveOutClose@4", 1, &driver); + preference[1] = previous; + call("_AIL_set_preference@8", 2, preference); + puts("audio samples: capacity, reuse, defaults, file, start/end, looping and rate passed"); +} + +static void adpcm_sample(uint32_t scratch) +{ + uint16_t format[] = {1, 2, 44100, 0, 45328, 2, 4, 16}; + memcpy(POINTER(scratch + 16), format, sizeof(format)); + uint32_t open[] = {scratch, 0, UINT32_MAX, scratch + 16}; + assert(!call("_AIL_waveOutOpen@16", 4, open)); + uint32_t driver = *(uint32_t *)POINTER(scratch); + uint32_t sample = call("_AIL_allocate_sample_handle@4", 1, &driver); + assert(sample); + const unsigned char header[] = { + 'R','I','F','F', 0x34,1,0,0, 'W','A','V','E', + 'f','m','t',' ', 20,0,0,0, 17,0,1,0, 0x44,0xac,0,0, + 0x53,0x57,0,0, 0,1,4,0, 2,0,0xf9,1, + 'f','a','c','t', 4,0,0,0, 0xf9,1,0,0, + 'd','a','t','a', 0,1,0,0 + }; + unsigned char *wave = POINTER(scratch + 64); + memset(wave, 0, 316); + memcpy(wave, header, sizeof(header)); + wave[61] = 32; /* Predictor 8192, index zero, all zero nibbles: 505 constant samples. */ + uint32_t file[] = {sample, scratch + 64, 1}; + assert(call("_AIL_set_sample_file@12", 3, file) == 1); +#ifndef _WIN32 + nonzero_samples = bad_samples = 0; + capture = 1; +#endif + + call("_AIL_start_sample@4", 1, &sample); + for (unsigned i = 0; call("_AIL_sample_status@4", 1, &sample) == 4 && i < 1000; ++i) { +#ifdef _WIN32 + Sleep(1); +#else + SDL_Delay(1); +#endif + } + + assert(call("_AIL_sample_status@4", 1, &sample) == 2); + assert(call("_AIL_sample_playback_rate@4", 1, &sample) == 44100); +#ifndef _WIN32 + capture = 0; + assert(nonzero_samples == 1010 && !bad_samples); +#endif + call("_AIL_release_sample_handle@4", 1, &sample); + call("_AIL_waveOutClose@4", 1, &driver); + puts("audio ADPCM: one 505-frame IMA block decodes and completes playback"); +} + static void stream_seek(uint32_t scratch) { FILE *file = fopen("miles-seek-test.wav", "wb"); @@ -300,6 +405,7 @@ static void stream_playback(uint32_t scratch) uint32_t position[] = {stream, scratch + 132, scratch + 136}; call("_AIL_stream_ms_position@12", 3, position); assert(MEM32(scratch + 132) == 100 && MEM32(scratch + 136) == 0); + nonzero_samples = bad_samples = 0; capture = 1; call("_AIL_start_stream@4", 1, &stream); assert(call("_AIL_stream_status@4", 1, &stream) == 4); @@ -352,6 +458,36 @@ static void stream_playback(uint32_t scratch) assert(!nonzero_samples && !bad_samples); capture = 0; call("_AIL_close_stream@4", 1, &stream); + + /* Compare actual mixed PCM counts for single, repeated, half-rate and muted samples. */ + uint32_t read[] = {scratch + 64, 0}; + uint32_t wave = call("_AIL_file_read@8", 2, read); + assert(wave); + uint32_t sample = call("_AIL_allocate_sample_handle@4", 1, &driver); + assert(sample); + for (unsigned mode = 0; mode < 4; ++mode) { + call("_AIL_init_sample@4", 1, &sample); + uint32_t file_args[] = {sample, wave, 1}; + assert(call("_AIL_set_sample_file@12", 3, file_args)); + uint32_t settings[] = {sample, mode == 1 ? 2 : 1}; + call("_AIL_set_sample_loop_count@8", 2, settings); + settings[1] = mode == 2 ? 22050 : 44100; + call("_AIL_set_sample_playback_rate@8", 2, settings); + settings[1] = mode == 3 ? 0 : 127; + call("_AIL_set_sample_volume@8", 2, settings); + nonzero_samples = bad_samples = 0; + + capture = 1; + call("_AIL_start_sample@4", 1, &sample); + for (unsigned i = 0; call("_AIL_sample_status@4", 1, &sample) == 4 && i < 1000; ++i) SDL_Delay(1); + capture = 0; + + assert(call("_AIL_sample_status@4", 1, &sample) == 2); + const unsigned expected[] = {8820, 17640, 17640, 0}; + assert(nonzero_samples == expected[mode] && !bad_samples); + } + call("_AIL_release_sample_handle@4", 1, &sample); + call("_AIL_mem_free_lock@4", 1, &wave); call("_AIL_waveOutClose@4", 1, &driver); assert(!unlink(path)); fprintf(stderr, "Linux SDL: 8820 PCM samples verified, pause/resume, restart, mute and stream cleanup passed.\n"); @@ -399,9 +535,11 @@ 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); stream_seek(address); } + } else { driver_lifetime(address); sample_handles(address); adpcm_sample(address); stream_seek(address); } #else driver_lifetime(address); + sample_handles(address); + adpcm_sample(address); stream_seek(address); stream_playback(address); fprintf(stderr, "Linux SDL: device enumeration and 4 PCM format open/close/shutdown cases passed.\n");