Feature: Allow base sounds set to be changed mid-game. (#12399)

This commit is contained in:
Peter Nelson
2024-04-01 17:54:42 +01:00
committed by GitHub
parent 3d2a8fb60c
commit d68e5159e1
5 changed files with 51 additions and 8 deletions

View File

@@ -35,6 +35,7 @@ struct MixerChannel {
};
static std::atomic<uint8_t> _active_channels;
static std::atomic<uint8_t> _stop_channels;
static MixerChannel _channels[8];
static uint32_t _play_rate = 11025;
static uint32_t _max_size = UINT_MAX;
@@ -108,6 +109,16 @@ static void MxCloseChannel(uint8_t channel_index)
_active_channels.fetch_and(~(1 << channel_index), std::memory_order_release);
}
/**
* Close all mixer channels.
* This signals to the mixer that each channel should be closed even if it has not played all remaining samples.
* This is safe (and designed) to be called from the main thread.
*/
void MxCloseAllChannels()
{
_stop_channels.fetch_or(~0, std::memory_order_release);
}
void MxMixSamples(void *buffer, uint samples)
{
PerformanceMeasurer framerate(PFE_SOUND);
@@ -126,6 +137,12 @@ void MxMixSamples(void *buffer, uint samples)
if (_music_stream) _music_stream((int16_t*)buffer, samples);
}
/* Check if any channels should be stopped. */
uint8_t stop = _stop_channels.load(std::memory_order_acquire);
for (uint8_t idx : SetBitIterator(stop)) {
MxCloseChannel(idx);
}
/* Apply simple x^3 scaling to master effect volume. This increases the
* perceived difference in loudness to better match expectations. effect_vol
* is expected to be in the range 0-127 hence the division by 127 * 127 to
@@ -200,6 +217,7 @@ 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);
}