From e7d73e8493b3c66c561cace94b43e21e75c405fe Mon Sep 17 00:00:00 2001 From: Kp Date: Sat, 13 May 2023 15:02:55 +0000 Subject: [PATCH] Use RAII to manage SDL_LockAudio state --- common/main/d_sdl_audio.h | 26 ++++++++++++++++++++++++++ d2x-rebirth/libmve/mveplay.cpp | 9 ++++----- similar/arch/sdl/digi_audio.cpp | 9 +++------ similar/arch/sdl/digi_mixer.cpp | 1 - 4 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 common/main/d_sdl_audio.h diff --git a/common/main/d_sdl_audio.h b/common/main/d_sdl_audio.h new file mode 100644 index 000000000..fd1d070f3 --- /dev/null +++ b/common/main/d_sdl_audio.h @@ -0,0 +1,26 @@ +/* + * Portions of this file are copyright Rebirth contributors and licensed as + * described in COPYING.txt. + * See COPYING.txt for license details. + */ + +#pragma once +#include + +namespace dcx { + +struct RAII_SDL_LockAudio +{ + RAII_SDL_LockAudio() + { + SDL_LockAudio(); + } + ~RAII_SDL_LockAudio() + { + SDL_UnlockAudio(); + } + RAII_SDL_LockAudio(const RAII_SDL_LockAudio &) = delete; + RAII_SDL_LockAudio &operator=(const RAII_SDL_LockAudio &) = delete; +}; + +} diff --git a/d2x-rebirth/libmve/mveplay.cpp b/d2x-rebirth/libmve/mveplay.cpp index f7ee76875..f43c927bd 100644 --- a/d2x-rebirth/libmve/mveplay.cpp +++ b/d2x-rebirth/libmve/mveplay.cpp @@ -33,6 +33,7 @@ #endif #include "config.h" +#include "d_sdl_audio.h" #include "mvelib.h" #include "mve_audio.h" #include "byteutil.h" @@ -409,8 +410,9 @@ int MVESTREAM::handle_mve_segment_audioframedata(const mve_opcode major, const u static const int selected_chan=1; if (mve_audio_spec) { - if (mve_audio_playing) - SDL_LockAudio(); + std::optional lock_audio{ + mve_audio_playing ? std::optional(std::in_place) : std::nullopt + }; const auto chan = get_ushort(data + 2); unsigned nsamp = get_ushort(data + 4); @@ -500,9 +502,6 @@ int MVESTREAM::handle_mve_segment_audioframedata(const mve_opcode major, const u if (mve_audio_buftail == mve_audio_bufhead) con_printf(CON_CRITICAL, "d'oh! buffer ring overrun (%d)", mve_audio_bufhead); } - - if (mve_audio_playing) - SDL_UnlockAudio(); } return 1; diff --git a/similar/arch/sdl/digi_audio.cpp b/similar/arch/sdl/digi_audio.cpp index 29f46e034..1ad23d491 100644 --- a/similar/arch/sdl/digi_audio.cpp +++ b/similar/arch/sdl/digi_audio.cpp @@ -26,6 +26,7 @@ #include "piggy.h" #include "compiler-range_for.h" +#include "d_sdl_audio.h" #include "d_underlying_value.h" namespace dcx { @@ -147,7 +148,7 @@ static void audio_mixcallback(void *, Uint8 *stream, int len) memset(stream, 0x80, len); // fix "static" sound bug on Mac OS X - SDL_LockAudio(); + RAII_SDL_LockAudio lock_audio{}; range_for (auto &sl, SoundSlots) { @@ -184,8 +185,6 @@ static void audio_mixcallback(void *, Uint8 *stream, int len) sl.position = std::distance(sl.samples.begin(), sldata); } } - - SDL_UnlockAudio(); } } @@ -256,7 +255,7 @@ sound_channel digi_audio_start_sound(short soundnum, fix volume, sound_pan pan, if (soundnum < 0) return sound_channel::None; - SDL_LockAudio(); + RAII_SDL_LockAudio lock_audio{}; const auto starting_channel = next_channel; @@ -271,7 +270,6 @@ sound_channel digi_audio_start_sound(short soundnum, fix volume, sound_pan pan, next_channel = next(next_channel); if (next_channel == starting_channel) { - SDL_UnlockAudio(); return sound_channel::None; } } @@ -304,7 +302,6 @@ sound_channel digi_audio_start_sound(short soundnum, fix volume, sound_pan pan, const auto i = next_channel; next_channel = next(next_channel); - SDL_UnlockAudio(); return i; } diff --git a/similar/arch/sdl/digi_mixer.cpp b/similar/arch/sdl/digi_mixer.cpp index bfb4337a6..fb4e024c4 100644 --- a/similar/arch/sdl/digi_mixer.cpp +++ b/similar/arch/sdl/digi_mixer.cpp @@ -21,7 +21,6 @@ #include #include -#include #include #include "pstypes.h"