1
0
Fork 0

(svn r3552) Remove the global variable _mixer

release/0.5
tron 2006-02-05 09:56:04 +00:00
parent cf414c1864
commit 867e1acf08
6 changed files with 15 additions and 25 deletions

25
mixer.c
View File

@ -5,8 +5,6 @@
#include "mixer.h" #include "mixer.h"
struct MixerChannel { struct MixerChannel {
// Mixer
Mixer *mx;
bool active; bool active;
// pointer to allocated buffer memory // pointer to allocated buffer memory
@ -25,10 +23,8 @@ struct MixerChannel {
uint flags; uint flags;
}; };
struct Mixer { static MixerChannel _channels[8];
uint32 play_rate; static uint32 _play_rate;
MixerChannel channels[8];
};
static void mix_int8_to_int16(MixerChannel *sc, int16 *buffer, uint samples) static void mix_int8_to_int16(MixerChannel *sc, int16 *buffer, uint samples)
@ -79,7 +75,7 @@ static void MxCloseChannel(MixerChannel *mc)
mc->memory = NULL; mc->memory = NULL;
} }
void MxMixSamples(Mixer *mx, void *buffer, uint samples) void MxMixSamples(void *buffer, uint samples)
{ {
MixerChannel *mc; MixerChannel *mc;
@ -87,7 +83,7 @@ void MxMixSamples(Mixer *mx, void *buffer, uint samples)
memset(buffer, 0, sizeof(int16) * 2 * samples); memset(buffer, 0, sizeof(int16) * 2 * samples);
// Mix each channel // Mix each channel
for (mc = mx->channels; mc != endof(mx->channels); mc++) { for (mc = _channels; mc != endof(_channels); mc++) {
if (mc->active) { if (mc->active) {
mix_int8_to_int16(mc, buffer, samples); mix_int8_to_int16(mc, buffer, samples);
if (mc->samples_left == 0) MxCloseChannel(mc); if (mc->samples_left == 0) MxCloseChannel(mc);
@ -104,13 +100,12 @@ void MxMixSamples(Mixer *mx, void *buffer, uint samples)
#endif #endif
} }
MixerChannel *MxAllocateChannel(Mixer *mx) MixerChannel *MxAllocateChannel(void)
{ {
MixerChannel *mc; MixerChannel *mc;
for (mc = mx->channels; mc != endof(mx->channels); mc++) for (mc = _channels; mc != endof(_channels); mc++)
if (mc->memory == NULL) { if (mc->memory == NULL) {
mc->active = false; mc->active = false;
mc->mx = mx;
return mc; return mc;
} }
return NULL; return NULL;
@ -123,7 +118,7 @@ void MxSetChannelRawSrc(MixerChannel *mc, int8 *mem, uint size, uint rate, uint
mc->frac_pos = 0; mc->frac_pos = 0;
mc->pos = 0; mc->pos = 0;
mc->frac_speed = (rate << 16) / mc->mx->play_rate; mc->frac_speed = (rate << 16) / _play_rate;
// adjust the magnitude to prevent overflow // adjust the magnitude to prevent overflow
while (size & 0xFFFF0000) { while (size & 0xFFFF0000) {
@ -131,7 +126,7 @@ void MxSetChannelRawSrc(MixerChannel *mc, int8 *mem, uint size, uint rate, uint
rate = (rate >> 1) + 1; rate = (rate >> 1) + 1;
} }
mc->samples_left = size * mc->mx->play_rate / rate; mc->samples_left = size * _play_rate / rate;
} }
void MxSetChannelVolume(MixerChannel *mc, uint left, uint right) void MxSetChannelVolume(MixerChannel *mc, uint left, uint right)
@ -149,8 +144,6 @@ void MxActivateChannel(MixerChannel* mc)
bool MxInitialize(uint rate) bool MxInitialize(uint rate)
{ {
static Mixer mx; _play_rate = rate;
_mixer = &mx;
mx.play_rate = rate;
return true; return true;
} }

View File

@ -3,7 +3,6 @@
#ifndef MIXER_H #ifndef MIXER_H
#define MIXER_H #define MIXER_H
typedef struct Mixer Mixer;
typedef struct MixerChannel MixerChannel; typedef struct MixerChannel MixerChannel;
enum { enum {
@ -13,12 +12,10 @@ enum {
// MX_UNSIGNED = 8, // MX_UNSIGNED = 8,
}; };
VARDEF Mixer *_mixer;
bool MxInitialize(uint rate); bool MxInitialize(uint rate);
void MxMixSamples(Mixer *mx, void *buffer, uint samples); void MxMixSamples(void* buffer, uint samples);
MixerChannel *MxAllocateChannel(Mixer *mx); MixerChannel* MxAllocateChannel(void);
void MxSetChannelRawSrc(MixerChannel *mc, int8 *mem, uint size, uint rate, uint flags); void MxSetChannelRawSrc(MixerChannel *mc, int8 *mem, uint size, uint rate, uint flags);
void MxSetChannelVolume(MixerChannel *mc, uint left, uint right); void MxSetChannelVolume(MixerChannel *mc, uint left, uint right);
void MxActivateChannel(MixerChannel*); void MxActivateChannel(MixerChannel*);

View File

@ -140,7 +140,7 @@ static void StartSound(uint sound, int panning, uint volume)
uint left_vol, right_vol; uint left_vol, right_vol;
if (volume == 0) return; if (volume == 0) return;
mc = MxAllocateChannel(_mixer); mc = MxAllocateChannel();
if (mc == NULL) return; if (mc == NULL) return;
if (!SetBankSource(mc, sound)) return; if (!SetBankSource(mc, sound)) return;

View File

@ -42,7 +42,7 @@ static AudioUnit _outputAudioUnit;
/* The CoreAudio callback */ /* The CoreAudio callback */
static OSStatus audioCallback(void *inRefCon, AudioUnitRenderActionFlags inActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, AudioBuffer *ioData) static OSStatus audioCallback(void *inRefCon, AudioUnitRenderActionFlags inActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, AudioBuffer *ioData)
{ {
MxMixSamples(_mixer, ioData->mData, ioData->mDataByteSize / 4); MxMixSamples(ioData->mData, ioData->mDataByteSize / 4);
return noErr; return noErr;
} }

View File

@ -13,7 +13,7 @@
static void CDECL fill_sound_buffer(void *userdata, Uint8 *stream, int len) static void CDECL fill_sound_buffer(void *userdata, Uint8 *stream, int len)
{ {
MxMixSamples(_mixer, stream, len / 4); MxMixSamples(stream, len / 4);
} }
static const char *SdlSoundStart(const char * const *parm) static const char *SdlSoundStart(const char * const *parm)

View File

@ -29,7 +29,7 @@ static void FillHeaders(void)
for (hdr = _wave_hdr; hdr != endof(_wave_hdr); hdr++) { for (hdr = _wave_hdr; hdr != endof(_wave_hdr); hdr++) {
if (!(hdr->dwFlags & WHDR_INQUEUE)) { if (!(hdr->dwFlags & WHDR_INQUEUE)) {
MxMixSamples(_mixer, hdr->lpData, hdr->dwBufferLength / 4); MxMixSamples(hdr->lpData, hdr->dwBufferLength / 4);
if (waveOutWrite(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) if (waveOutWrite(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
error("waveOutWrite failed"); error("waveOutWrite failed");
} }