diff --git a/autogen/convert_functions.py b/autogen/convert_functions.py index 1f9d03d12..f96009f3e 100644 --- a/autogen/convert_functions.py +++ b/autogen/convert_functions.py @@ -109,7 +109,7 @@ override_disallowed_functions = { "src/pc/lua/utils/smlua_obj_utils.h": [ "spawn_object_remember_field" ], "src/game/camera.h": [ "update_camera", "init_camera", "stub_camera", "^reset_camera", "move_point_along_spline" ], "src/game/behavior_actions.h": [ "bhv_dust_smoke_loop", "bhv_init_room" ], - "src/pc/lua/utils/smlua_audio_utils.h": [ "smlua_audio_utils_override", "audio_custom_shutdown", "smlua_audio_custom_init", "smlua_audio_custom_deinit", "audio_sample_destroy_pending_copies"], + "src/pc/lua/utils/smlua_audio_utils.h": [ "smlua_audio_utils_override", "audio_custom_shutdown", "smlua_audio_custom_init", "smlua_audio_custom_deinit", "audio_sample_destroy_pending_copies", "audio_custom_update_volume" ], "src/pc/djui/djui_hud_utils.h": [ "djui_hud_render_texture", "djui_hud_render_texture_raw", "djui_hud_render_texture_tile", "djui_hud_render_texture_tile_raw" ], "src/pc/lua/utils/smlua_level_utils.h": [ "smlua_level_util_reset" ], "src/pc/lua/utils/smlua_text_utils.h": [ "smlua_text_utils_init", "smlua_text_utils_shutdown", "smlua_text_utils_reset_all" ], diff --git a/autogen/lua_definitions/structs.lua b/autogen/lua_definitions/structs.lua index 2f58718f3..e0ef0b222 100644 --- a/autogen/lua_definitions/structs.lua +++ b/autogen/lua_definitions/structs.lua @@ -1066,6 +1066,7 @@ --- @field public selectable boolean --- @class ModAudio +--- @field public baseVolume number --- @field public file ModFile --- @field public isStream boolean --- @field public sampleCopiesTail ModAudioSampleCopies diff --git a/docs/lua/structs.md b/docs/lua/structs.md index b7590cfdd..f70633e52 100644 --- a/docs/lua/structs.md +++ b/docs/lua/structs.md @@ -1449,6 +1449,7 @@ | Field | Type | Access | | ----- | ---- | ------ | +| baseVolume | `number` | | | file | [ModFile](structs.md#ModFile) | | | isStream | `boolean` | | | sampleCopiesTail | [ModAudioSampleCopies](structs.md#ModAudioSampleCopies) | | diff --git a/src/pc/djui/djui_panel_sound.c b/src/pc/djui/djui_panel_sound.c index f22f2ab8e..9946b13e1 100644 --- a/src/pc/djui/djui_panel_sound.c +++ b/src/pc/djui/djui_panel_sound.c @@ -3,16 +3,20 @@ #include "djui_panel_menu.h" #include "pc/utils/misc.h" #include "pc/configfile.h" +#include "pc/lua/utils/smlua_audio_utils.h" + +static void djui_panel_sound_value_change(UNUSED struct DjuiBase* caller) { + audio_custom_update_volume(); +} void djui_panel_sound_create(struct DjuiBase* caller) { struct DjuiThreePanel* panel = djui_panel_menu_create(DLANG(SOUND, SOUND)); struct DjuiBase* body = djui_three_panel_get_body(panel); - { - djui_slider_create(body, DLANG(SOUND, MASTER_VOLUME), &configMasterVolume, 0, 127, NULL); - djui_slider_create(body, DLANG(SOUND, MUSIC_VOLUME), &configMusicVolume, 0, 127, NULL); - djui_slider_create(body, DLANG(SOUND, SFX_VOLUME), &configSfxVolume, 0, 127, NULL); - djui_slider_create(body, DLANG(SOUND, ENV_VOLUME), &configEnvVolume, 0, 127, NULL); + djui_slider_create(body, DLANG(SOUND, MASTER_VOLUME), &configMasterVolume, 0, 127, djui_panel_sound_value_change); + djui_slider_create(body, DLANG(SOUND, MUSIC_VOLUME), &configMusicVolume, 0, 127, djui_panel_sound_value_change); + djui_slider_create(body, DLANG(SOUND, SFX_VOLUME), &configSfxVolume, 0, 127, djui_panel_sound_value_change); + djui_slider_create(body, DLANG(SOUND, ENV_VOLUME), &configEnvVolume, 0, 127, djui_panel_sound_value_change); djui_checkbox_create(body, DLANG(SOUND, FADEOUT), &configFadeoutDistantSounds, NULL); djui_button_create(body, DLANG(MENU, BACK), DJUI_BUTTON_STYLE_BACK, djui_panel_menu_back); } diff --git a/src/pc/lua/smlua_cobject_autogen.c b/src/pc/lua/smlua_cobject_autogen.c index f89c1526a..3b08884e8 100644 --- a/src/pc/lua/smlua_cobject_autogen.c +++ b/src/pc/lua/smlua_cobject_autogen.c @@ -1197,8 +1197,9 @@ static struct LuaObjectField sModFields[LUA_MOD_FIELD_COUNT] = { // { "size", LVT_???, offsetof(struct Mod, size), true, LOT_??? }, <--- UNIMPLEMENTED }; -#define LUA_MOD_AUDIO_FIELD_COUNT 3 +#define LUA_MOD_AUDIO_FIELD_COUNT 4 static struct LuaObjectField sModAudioFields[LUA_MOD_AUDIO_FIELD_COUNT] = { + { "baseVolume", LVT_F32, offsetof(struct ModAudio, baseVolume), false, LOT_NONE }, { "file", LVT_COBJECT_P, offsetof(struct ModAudio, file), false, LOT_MODFILE }, { "isStream", LVT_BOOL, offsetof(struct ModAudio, isStream), false, LOT_NONE }, { "sampleCopiesTail", LVT_COBJECT_P, offsetof(struct ModAudio, sampleCopiesTail), false, LOT_MODAUDIOSAMPLECOPIES }, diff --git a/src/pc/lua/utils/smlua_audio_utils.c b/src/pc/lua/utils/smlua_audio_utils.c index 0a042f202..d15e2e232 100644 --- a/src/pc/lua/utils/smlua_audio_utils.c +++ b/src/pc/lua/utils/smlua_audio_utils.c @@ -15,6 +15,7 @@ #include "pc/mods/mods_utils.h" #include "pc/utils/misc.h" #include "pc/debuglog.h" +#include "pc/pc_main.h" #include "audio/external.h" struct AudioOverride { @@ -280,9 +281,10 @@ void audio_stream_play(struct ModAudio* audio, bool restart, f32 volume) { if (!audio_sanity_check(audio, true, "play")) { return; } - f32 masterVolume = (f32)configMasterVolume / 127.0f; - f32 musicVolume = (f32)configMusicVolume / 127.0f; + f32 masterVolume = (f32)configMasterVolume / 127.0f * gLuaVolumeMaster; + f32 musicVolume = (f32)configMusicVolume / 127.0f * gLuaVolumeLevel; ma_sound_set_volume(&audio->sound, masterVolume * musicVolume * volume); + audio->baseVolume = volume; if (restart || !ma_sound_is_playing(&audio->sound)) { ma_sound_seek_to_pcm_frame(&audio->sound, 0); } ma_sound_start(&audio->sound); } @@ -509,14 +511,29 @@ void audio_sample_play(struct ModAudio* audio, Vec3f position, f32 volume) { } f32 intensity = sound_get_level_intensity(dist); - f32 masterVolume = (f32)configMasterVolume / 127.0f; - f32 sfxVolume = (f32)configSfxVolume / 127.0f; + f32 masterVolume = (f32)configMasterVolume / 127.0f * gLuaVolumeMaster; + f32 sfxVolume = (f32)configSfxVolume / 127.0f * gLuaVolumeSfx; ma_sound_set_volume(sound, masterVolume * sfxVolume * volume * intensity); ma_sound_set_pan(sound, pan); + audio->baseVolume = volume; ma_sound_start(sound); } +void audio_custom_update_volume(void) { + struct DynamicPoolNode* node = sModAudio->tail; + while (node) { + struct DynamicPoolNode* prev = node->prev; + struct ModAudio* audio = node->ptr; + f32 masterVolume = (f32)configMasterVolume / 127.0f; + f32 musicVolume = (f32)configMusicVolume / 127.0f; + if (audio->isStream) { + ma_sound_set_volume(&audio->sound, masterVolume * musicVolume * audio->baseVolume); + } + node = prev; + } +} + void audio_custom_shutdown(void) { if (!sModAudio) { return; } struct DynamicPoolNode* node = sModAudio->tail; diff --git a/src/pc/lua/utils/smlua_audio_utils.h b/src/pc/lua/utils/smlua_audio_utils.h index 831f0fa36..c33f294f3 100644 --- a/src/pc/lua/utils/smlua_audio_utils.h +++ b/src/pc/lua/utils/smlua_audio_utils.h @@ -23,6 +23,7 @@ struct ModAudio { ma_sound sound; struct ModAudioSampleCopies* sampleCopiesTail; bool isStream; + f32 baseVolume; }; struct ModAudio* audio_stream_load(const char* filename); @@ -45,6 +46,8 @@ void audio_sample_destroy(struct ModAudio* audio); void audio_sample_stop(struct ModAudio* audio); void audio_sample_play(struct ModAudio* audio, Vec3f position, f32 volume); +void audio_custom_update_volume(void); + void audio_custom_shutdown(void); void smlua_audio_custom_init(void); diff --git a/src/pc/lua/utils/smlua_misc_utils.c b/src/pc/lua/utils/smlua_misc_utils.c index 470176cbc..ebd295e06 100644 --- a/src/pc/lua/utils/smlua_misc_utils.c +++ b/src/pc/lua/utils/smlua_misc_utils.c @@ -25,6 +25,7 @@ #include "game/level_geo.h" #include "game/first_person_cam.h" #include "pc/lua/utils/smlua_math_utils.h" +#include "pc/lua/utils/smlua_audio_utils.h" #ifdef DISCORD_SDK #include "pc/discord/discord.h" @@ -674,18 +675,22 @@ f32 get_volume_env(void) { void set_volume_master(f32 volume) { gLuaVolumeMaster = clampf(volume, 0.0f, 1.0f); + audio_custom_update_volume(); } void set_volume_level(f32 volume) { gLuaVolumeLevel = clampf(volume, 0.0f, 1.0f); + audio_custom_update_volume(); } void set_volume_sfx(f32 volume) { gLuaVolumeSfx = clampf(volume, 0.0f, 1.0f); + audio_custom_update_volume(); } void set_volume_env(f32 volume) { gLuaVolumeEnv = clampf(volume, 0.0f, 1.0f); + audio_custom_update_volume(); } ///