From d6c06de5ad8f36c74eacba46e48a482c5c5c69b7 Mon Sep 17 00:00:00 2001 From: Niels Martin Hansen Date: Sun, 4 Nov 2018 12:58:42 +0100 Subject: Add: Mixer feature for streaming sampled music --- src/mixer.cpp | 17 +++++++++++++++++ src/mixer.h | 10 ++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/mixer.cpp b/src/mixer.cpp index 6aaa8204d..6014f6082 100644 --- a/src/mixer.cpp +++ b/src/mixer.cpp @@ -15,6 +15,7 @@ #include "framerate_type.h" #include "safeguards.h" +#include "mixer.h" struct MixerChannel { bool active; @@ -38,6 +39,7 @@ struct MixerChannel { static MixerChannel _channels[8]; static uint32 _play_rate = 11025; static uint32 _max_size = UINT_MAX; +static MxStreamCallback _music_stream = NULL; /** * The theoretical maximum volume for a single sound sample. Multiple sound @@ -151,6 +153,9 @@ void MxMixSamples(void *buffer, uint samples) /* Clear the buffer */ memset(buffer, 0, sizeof(int16) * 2 * samples); + /* Fetch music if a sampled stream is available */ + if (_music_stream) _music_stream((int16*)buffer, samples); + /* Mix each channel */ for (mc = _channels; mc != endof(_channels); mc++) { if (mc->active) { @@ -215,6 +220,17 @@ void MxSetChannelVolume(MixerChannel *mc, uint volume, float pan) void MxActivateChannel(MixerChannel *mc) { mc->active = true; +} + +/** + * Set source of PCM music + * @param music_callback Function that will be called to fill sample buffers with music data. + * @return Sample rate of mixer, which the buffers supplied to the callback must be rendered at. + */ +uint32 MxSetMusicSource(MxStreamCallback music_callback) +{ + _music_stream = music_callback; + return _play_rate; } @@ -222,5 +238,6 @@ bool MxInitialize(uint rate) { _play_rate = rate; _max_size = UINT_MAX / _play_rate; + _music_stream = NULL; /* rate may have changed, any music source is now invalid */ return true; } diff --git a/src/mixer.h b/src/mixer.h index 0ccee6109..9766682d6 100644 --- a/src/mixer.h +++ b/src/mixer.h @@ -14,6 +14,14 @@ struct MixerChannel; +/** + * Type of callback functions for supplying PCM music. + * A music decoder/renderer implements this function and installs it with MxSetMusicSource, which also returns the sample rate used. + * @param buffer Pointer to interleaved 2-channel signed 16 bit PCM data buffer, guaranteed to be 0-initialized. + * @param samples number of samples that must be filled into \c buffer. + */ +typedef void(*MxStreamCallback)(int16 *buffer, size_t samples); + bool MxInitialize(uint rate); void MxMixSamples(void *buffer, uint samples); @@ -22,4 +30,6 @@ void MxSetChannelRawSrc(MixerChannel *mc, int8 *mem, size_t size, uint rate, boo void MxSetChannelVolume(MixerChannel *mc, uint volume, float pan); void MxActivateChannel(MixerChannel*); +uint32 MxSetMusicSource(MxStreamCallback music_callback); + #endif /* MIXER_H */ -- cgit v1.2.3-54-g00ecf