From bccf5343f427ff7e161cdf9b655178d4d0df9130 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sun, 13 Oct 2024 12:50:22 +0100 Subject: [PATCH] Codechange: Use a type alias for mixer channel masking. --- src/mixer.cpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/mixer.cpp b/src/mixer.cpp index bcfde64a01..bba0c4d2af 100644 --- a/src/mixer.cpp +++ b/src/mixer.cpp @@ -34,8 +34,10 @@ struct MixerChannel { bool is16bit; }; -static std::atomic _active_channels; -static std::atomic _stop_channels; +using MixerChannelMask = uint8_t; ///< Type representing a bitmask of mixer channels. + +static std::atomic _active_channels; +static std::atomic _stop_channels; static MixerChannel _channels[8]; static uint32_t _play_rate = 11025; static uint32_t _max_size = UINT_MAX; @@ -106,7 +108,7 @@ static void mix_int16(MixerChannel *sc, int16_t *buffer, uint samples, uint8_t e static void MxCloseChannel(uint8_t channel_index) { - _active_channels.fetch_and(~(1 << channel_index), std::memory_order_release); + _active_channels.fetch_and(~(1U << channel_index), std::memory_order_release); } /** @@ -138,7 +140,7 @@ void MxMixSamples(void *buffer, uint samples) } /* Check if any channels should be stopped. */ - uint8_t stop = _stop_channels.load(std::memory_order_acquire); + MixerChannelMask stop = _stop_channels.load(std::memory_order_acquire); for (uint8_t idx : SetBitIterator(stop)) { MxCloseChannel(idx); } @@ -153,7 +155,7 @@ void MxMixSamples(void *buffer, uint samples) effect_vol_setting) / (127 * 127); /* Mix each channel */ - uint8_t active = _active_channels.load(std::memory_order_acquire); + MixerChannelMask active = _active_channels.load(std::memory_order_acquire); for (uint8_t idx : SetBitIterator(active)) { MixerChannel *mc = &_channels[idx]; if (mc->is16bit) { @@ -167,8 +169,8 @@ void MxMixSamples(void *buffer, uint samples) MixerChannel *MxAllocateChannel() { - uint8_t currently_active = _active_channels.load(std::memory_order_acquire); - uint8_t available = ~currently_active; + MixerChannelMask currently_active = _active_channels.load(std::memory_order_acquire); + MixerChannelMask available = ~currently_active; if (available == 0) return nullptr; uint8_t channel_index = FindFirstBit(available); @@ -217,8 +219,8 @@ void MxSetChannelVolume(MixerChannel *mc, uint volume, float pan) void MxActivateChannel(MixerChannel *mc) { uint8_t channel_index = mc - _channels; - _stop_channels.fetch_and(~(1 << channel_index), std::memory_order_release); - _active_channels.fetch_or((1 << channel_index), std::memory_order_release); + _stop_channels.fetch_and(~(1U << channel_index), std::memory_order_release); + _active_channels.fetch_or((1U << channel_index), std::memory_order_release); } /**