From 6df4acb996335c531d59187e2f9931eaa11802ce Mon Sep 17 00:00:00 2001 From: Loobinex Date: Sat, 18 Apr 2026 13:40:53 +0200 Subject: [PATCH 1/3] Do not restart music that is already playing Fixes #4505 --- src/bflib_sndlib.cpp | 1 - src/lua_api.c | 1 + src/lvl_script_commands.c | 11 ++++++++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/bflib_sndlib.cpp b/src/bflib_sndlib.cpp index 1864da8069..a7ceb4fd2c 100644 --- a/src/bflib_sndlib.cpp +++ b/src/bflib_sndlib.cpp @@ -500,7 +500,6 @@ extern "C" void set_music_volume(SoundVolume value) { extern "C" TbBool play_music(const char * fname) { std::lock_guard guard(g_mix_mutex); - game.music_track = -1; snprintf(game.music_fname, sizeof(game.music_fname), "%s", fname); // Mix_PlayMusic will stop anything currently playing and eventually // calls on_music_finished so theres no need to call Mix_FreeMusic first. diff --git a/src/lua_api.c b/src/lua_api.c index ca14051b6a..203a176ede 100644 --- a/src/lua_api.c +++ b/src/lua_api.c @@ -1558,6 +1558,7 @@ static int lua_Set_music(lua_State *L) } } else { const char *track_name = luaL_checkstring(L, 1); + game.music_track = -1; play_music(prepare_file_fmtpath(FGrp_CmpgMedia, "%s", track_name)); } return 0; diff --git a/src/lvl_script_commands.c b/src/lvl_script_commands.c index 4fb043ec04..24066b2114 100644 --- a/src/lvl_script_commands.c +++ b/src/lvl_script_commands.c @@ -4363,13 +4363,22 @@ static void set_music_check(const struct ScriptLine *scline) static void set_music_process(struct ScriptContext *context) { short track = context->value->chars[0]; + if ((track > 0) && (game.music_track == track)) + { + JUSTLOG("doing nothing because %d = %d", game.music_track , track); + return; + } if (track == 0) { SCRPTLOG("Stopping music"); stop_music(); } else if (track < 0) { const char * fname = script_strval(context->value->longs[1]); + const char* full_fname = prepare_file_fmtpath(FGrp_CmpgMedia, "%s", fname); + if (strcmp(game.music_fname, full_fname) == 0) + return; SCRPTLOG("Playing music from %s", fname); - play_music(prepare_file_fmtpath(FGrp_CmpgMedia, "%s", fname)); + game.music_track = -1; + play_music(full_fname); } else { SCRPTLOG("Playing music track %d", track); play_music_track(track); From d88de95fec1b512bddcdbde003f43ec6522b6f4c Mon Sep 17 00:00:00 2001 From: Loobinex Date: Sun, 19 Apr 2026 01:14:50 +0200 Subject: [PATCH 2/3] removed left over log line --- src/lvl_script_commands.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lvl_script_commands.c b/src/lvl_script_commands.c index 24066b2114..13fcfd2b89 100644 --- a/src/lvl_script_commands.c +++ b/src/lvl_script_commands.c @@ -4365,7 +4365,6 @@ static void set_music_process(struct ScriptContext *context) short track = context->value->chars[0]; if ((track > 0) && (game.music_track == track)) { - JUSTLOG("doing nothing because %d = %d", game.music_track , track); return; } if (track == 0) { From 98c544abe16ed7682f65be5b9792f34f376b1c3a Mon Sep 17 00:00:00 2001 From: Loobinex Date: Sun, 19 Apr 2026 15:22:14 +0200 Subject: [PATCH 3/3] Put it all in play_music instead --- src/bflib_sndlib.cpp | 3 +++ src/lua_api.c | 1 - src/lvl_script_commands.c | 8 +------- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/bflib_sndlib.cpp b/src/bflib_sndlib.cpp index a7ceb4fd2c..645a743cce 100644 --- a/src/bflib_sndlib.cpp +++ b/src/bflib_sndlib.cpp @@ -500,6 +500,9 @@ extern "C" void set_music_volume(SoundVolume value) { extern "C" TbBool play_music(const char * fname) { std::lock_guard guard(g_mix_mutex); + if (strcmp(game.music_fname, fname) == 0) + return false; + game.music_track = -1; snprintf(game.music_fname, sizeof(game.music_fname), "%s", fname); // Mix_PlayMusic will stop anything currently playing and eventually // calls on_music_finished so theres no need to call Mix_FreeMusic first. diff --git a/src/lua_api.c b/src/lua_api.c index 203a176ede..ca14051b6a 100644 --- a/src/lua_api.c +++ b/src/lua_api.c @@ -1558,7 +1558,6 @@ static int lua_Set_music(lua_State *L) } } else { const char *track_name = luaL_checkstring(L, 1); - game.music_track = -1; play_music(prepare_file_fmtpath(FGrp_CmpgMedia, "%s", track_name)); } return 0; diff --git a/src/lvl_script_commands.c b/src/lvl_script_commands.c index 13fcfd2b89..99554f89e0 100644 --- a/src/lvl_script_commands.c +++ b/src/lvl_script_commands.c @@ -4372,14 +4372,8 @@ static void set_music_process(struct ScriptContext *context) stop_music(); } else if (track < 0) { const char * fname = script_strval(context->value->longs[1]); - const char* full_fname = prepare_file_fmtpath(FGrp_CmpgMedia, "%s", fname); - if (strcmp(game.music_fname, full_fname) == 0) - return; - SCRPTLOG("Playing music from %s", fname); - game.music_track = -1; - play_music(full_fname); + play_music(prepare_file_fmtpath(FGrp_CmpgMedia, "%s", fname)); } else { - SCRPTLOG("Playing music track %d", track); play_music_track(track); } }