summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiels Martin Hansen <nielsm@indvikleren.dk>2018-11-04 12:58:42 +0100
committerNiels Martin Hansen <nielsm@indvikleren.dk>2019-01-05 17:17:10 +0100
commitd6c06de5ad8f36c74eacba46e48a482c5c5c69b7 (patch)
treeb5b857b33ac537162eb023b108b8ee651dfb3787
parent12ba56c5a3173054ac25899bef481f4801d5b6e8 (diff)
downloadopenttd-d6c06de5ad8f36c74eacba46e48a482c5c5c69b7.tar.xz
Add: Mixer feature for streaming sampled music
-rw-r--r--src/mixer.cpp17
-rw-r--r--src/mixer.h10
2 files changed, 27 insertions, 0 deletions
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 */