From 70feed637534d43deb6e1d2acea40cc81fea0347 Mon Sep 17 00:00:00 2001 From: Isaac0-dev <62234577+Isaac0-dev@users.noreply.github.com> Date: Tue, 19 May 2026 16:42:03 +1000 Subject: [PATCH] apply review changes --- src/pc/lua/utils/smlua_audio_utils.c | 43 +++++++++++++++++----------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/src/pc/lua/utils/smlua_audio_utils.c b/src/pc/lua/utils/smlua_audio_utils.c index 14e2d89db..74989d3e4 100644 --- a/src/pc/lua/utils/smlua_audio_utils.c +++ b/src/pc/lua/utils/smlua_audio_utils.c @@ -273,7 +273,7 @@ struct ModAudio* audio_load_internal(const char* filename, bool isStream) { // find stream in ModAudio list struct ModAudio* audio = find_mod_audio(filepath); - if (audio) { + if (audio && audio->loaded) { if (isStream != audio->isStream) { if (isStream) { LOG_LUA_LINE("Tried to load a stream, when a sample already exists for '%s'", filename); @@ -282,9 +282,7 @@ struct ModAudio* audio_load_internal(const char* filename, bool isStream) { } return NULL; } - if (audio->loaded) { - return audio; - } + return audio; } // allocate in ModAudio pool if needed @@ -690,8 +688,9 @@ void audio_sample_play(struct ModAudio* audio, Vec3f position, f32 volume) { if (!audio_sanity_check(audio, false, "play")) { return; } ma_sound *sound = &audio->sound; + struct ModAudioSampleCopies* copy = NULL; if (ma_sound_is_playing(sound)) { - struct ModAudioSampleCopies* copy = calloc(1, sizeof(struct ModAudioSampleCopies)); + copy = calloc(1, sizeof(struct ModAudioSampleCopies)); if (!copy) { LOG_ERROR("Failed to allocate memory for sample copy track."); return; @@ -710,15 +709,6 @@ void audio_sample_play(struct ModAudio* audio, Vec3f position, f32 volume) { ma_sound_set_end_callback(©->sound, audio_sample_copy_end_callback, copy); copy->parent = audio; - // Add to list - pthread_mutex_lock(&sSampleCopyMutex); - if (audio->sampleCopiesTail) { - copy->prev = audio->sampleCopiesTail; - audio->sampleCopiesTail->next = copy; - } - audio->sampleCopiesTail = copy; - pthread_mutex_unlock(&sSampleCopyMutex); - sound = ©->sound; } @@ -745,11 +735,32 @@ void audio_sample_play(struct ModAudio* audio, Vec3f position, f32 volume) { ma_sound_set_volume(sound, gMasterVolume * sfxVolume * volume * intensity); } ma_sound_set_pan(sound, pan); + + ma_result startResult = ma_sound_start(sound); + if (startResult != MA_SUCCESS) { + if (copy) { + ma_sound_uninit(©->sound); + ma_decoder_uninit(©->decoder); + free(copy); + } + LOG_ERROR("Failed to start mod audio sample: %d", startResult); + return; + } + + // Only add the copy to the list after a successful start + if (copy) { + pthread_mutex_lock(&sSampleCopyMutex); + if (audio->sampleCopiesTail) { + copy->prev = audio->sampleCopiesTail; + audio->sampleCopiesTail->next = copy; + } + audio->sampleCopiesTail = copy; + pthread_mutex_unlock(&sSampleCopyMutex); + } + if (sound == &audio->sound) { audio->baseVolume = volume; } - - ma_sound_start(sound); } void audio_custom_update_volume(void) {