Add: Mixer feature for streaming sampled music

pull/78/head
Niels Martin Hansen 6 years ago
parent 12ba56c5a3
commit d6c06de5ad

@ -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;
}

@ -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 */

Loading…
Cancel
Save