21 std::shared_ptr<std::vector<std::byte>>
memory;
27 uint32_t samples_left;
40static std::atomic<MixerChannelMask> _active_channels;
41static std::atomic<MixerChannelMask> _stop_channels;
43static uint32_t _play_rate = 11025;
44static uint32_t _max_size = UINT_MAX;
46static std::mutex _music_stream_mutex;
47static std::atomic<uint8_t> _effect_vol;
67 return ((b[0] * ((1 << 16) - frac_pos)) + (b[1] * frac_pos)) >> 16;
71static void mix_int16(
MixerChannel *sc, int16_t *buffer, uint samples, uint8_t effect_vol)
74 const uint SHIFT =
sizeof(
T) * CHAR_BIT;
76 if (samples > sc->samples_left) samples = sc->samples_left;
77 sc->samples_left -= samples;
80 const T *b =
reinterpret_cast<const T *
>(sc->
memory->data()) + sc->
pos;
81 uint32_t frac_pos = sc->frac_pos;
82 uint32_t frac_speed = sc->frac_speed;
83 int volume_left = sc->
volume_left * effect_vol / 255;
86 if (frac_speed == 0x10000) {
93 }
while (--samples > 0);
100 frac_pos += frac_speed;
103 }
while (--samples > 0);
106 sc->frac_pos = frac_pos;
107 sc->
pos = b -
reinterpret_cast<const T *
>(sc->
memory->data());
110static void MxCloseChannel(uint8_t channel_index)
112 _active_channels.fetch_and(~(1U << channel_index), std::memory_order_release);
122 _stop_channels.fetch_or(~0, std::memory_order_release);
125void MxMixSamples(
void *buffer, uint samples)
128 static uint last_samples = 0;
129 if (samples != last_samples) {
130 framerate.SetExpectedRate((
double)_play_rate / samples);
131 last_samples = samples;
135 std::fill_n(
static_cast<int16_t *
>(buffer), 2 * samples, 0);
138 std::lock_guard<std::mutex>
lock{ _music_stream_mutex };
140 if (_music_stream) _music_stream((int16_t*)buffer, samples);
153 uint8_t effect_vol_setting = _effect_vol.load(std::memory_order_relaxed);
154 uint8_t effect_vol = (effect_vol_setting *
156 effect_vol_setting) / (127 * 127);
163 mix_int16<int16_t>(mc, (int16_t*)buffer, samples, effect_vol);
165 mix_int16<int8_t>(mc, (int16_t*)buffer, samples, effect_vol);
167 if (mc->samples_left == 0) MxCloseChannel(idx);
173 MixerChannelMask currently_active = _active_channels.load(std::memory_order_acquire);
175 if (available == 0)
return nullptr;
184void MxSetChannelRawSrc(
MixerChannel *mc,
const std::shared_ptr<std::vector<std::byte>> &mem, uint rate,
bool is16bit)
190 mc->frac_speed = (rate << 16U) / _play_rate;
192 size_t size = mc->
memory->size();
193 if (is16bit) size /= 2;
198 while (size >= _max_size) {
200 rate = (rate >> 1) + 1;
204 mc->samples_left = (uint)size * _play_rate / rate;
205 mc->is16bit = is16bit;
218 mc->
volume_left = (uint)(sin((1.0 - pan) * M_PI / 2.0) * volume);
219 mc->
volume_right = (uint)(sin(pan * M_PI / 2.0) * volume);
225 uint8_t channel_index = mc - _channels;
226 _stop_channels.fetch_and(~(1U << channel_index), std::memory_order_release);
227 _active_channels.fetch_or((1U << channel_index), std::memory_order_release);
237 std::lock_guard<std::mutex>
lock{ _music_stream_mutex };
238 _music_stream = music_callback;
243bool MxInitialize(uint rate)
245 std::lock_guard<std::mutex>
lock{ _music_stream_mutex };
247 _max_size = UINT_MAX / _play_rate;
248 _music_stream =
nullptr;
261void SetEffectVolume(uint8_t volume)
263 _effect_vol.store(volume, std::memory_order_relaxed);
constexpr uint8_t FindFirstBit(T x)
Search the first set bit in a value.
#define T
Climate temperate.
Types for recording game performance data.
@ Sound
Speed of mixing audio samples.
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
void MxCloseAllChannels()
Close all mixer channels.
static int RateConversion(T *b, int frac_pos)
Perform the rate conversion between the input and output.
uint32_t MxGetRate()
Get the current mixer sampling rate.
uint8_t MixerChannelMask
Type representing a bitmask of mixer channels.
uint32_t MxSetMusicSource(MxStreamCallback music_callback)
Set source of PCM music.
void MxSetChannelVolume(MixerChannel *mc, uint volume, float pan)
Set volume and pan parameters for a sound.
static const int MAX_VOLUME
The theoretical maximum volume for a single sound sample.
Functions to mix sound samples.
void(* MxStreamCallback)(int16_t *buffer, size_t samples)
Type of callback functions for supplying PCM music.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
uint32_t pos
Current position in memory.
std::shared_ptr< std::vector< std::byte > > memory
Pointer to allocated buffer memory.
Iterable ensemble of each set bit in a value.
std::mutex lock
synchronization for playback status fields