Changeset - r23101:10b102b8ce3d
[Not reviewed]
master
0 2 0
Niels Martin Hansen - 6 years ago 2018-11-04 11:58:42
nielsm@indvikleren.dk
Add: Mixer feature for streaming sampled music
2 files changed with 27 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/mixer.cpp
Show inline comments
 
@@ -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 sam
 
	/* 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) {
 
@@ -217,10 +222,22 @@ 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;
 
}
 

	
 

	
 
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;
 
}
src/mixer.h
Show inline comments
 
@@ -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
 
void MxSetChannelVolume(MixerChannel *mc, uint volume, float pan);
 
void MxActivateChannel(MixerChannel*);
 

	
 
uint32 MxSetMusicSource(MxStreamCallback music_callback);
 

	
 
#endif /* MIXER_H */
0 comments (0 inline, 0 general)