simplify and optimize SoundMixBuffer

This commit is contained in:
Glenn Maynard
2003-10-21 21:35:58 +00:00
parent f4ff423457
commit a47708b0b5
2 changed files with 6 additions and 17 deletions
+5 -16
View File
@@ -276,7 +276,7 @@ SoundMixBuffer::~SoundMixBuffer()
void SoundMixBuffer::SetVolume( float f ) void SoundMixBuffer::SetVolume( float f )
{ {
vol = f; vol = int(256*f);
} }
void SoundMixBuffer::write(const Sint16 *buf, unsigned size) void SoundMixBuffer::write(const Sint16 *buf, unsigned size)
@@ -295,28 +295,17 @@ void SoundMixBuffer::write(const Sint16 *buf, unsigned size)
for(unsigned pos = 0; pos < size; ++pos) for(unsigned pos = 0; pos < size; ++pos)
{ {
Sint32 samp = buf[pos] * 32768; /* Scale volume and add. */
mixbuf[pos] += buf[pos] * vol;
/* Scale volume: */
samp = Sint32(samp * vol);
/* Add and clip. Can't use clamp() here--we're clamping to
* the min and max of Sint32. */
if(samp > 0 && mixbuf[pos] + samp < mixbuf[pos])
mixbuf[pos] = INT_MAX;
else if(samp < 0 && mixbuf[pos] + samp > mixbuf[pos])
mixbuf[pos] = INT_MIN;
else
mixbuf[pos] = mixbuf[pos] + samp;
} }
} }
void SoundMixBuffer::read(Sint16 *buf) void SoundMixBuffer::read(Sint16 *buf)
{ {
int clip=0;
for(unsigned pos = 0; pos < used; ++pos) for(unsigned pos = 0; pos < used; ++pos)
{ {
/* XXX: dither */ Sint32 out = (mixbuf[pos]) / 256;
Sint32 out = (mixbuf[pos]) / 32768;
out = clamp(out, -32768, 32767); out = clamp(out, -32768, 32767);
buf[pos] = Sint16(out); buf[pos] = Sint16(out);
} }
+1 -1
View File
@@ -70,7 +70,7 @@ class SoundMixBuffer
Sint32 *mixbuf; Sint32 *mixbuf;
unsigned bufsize; /* actual allocated samples */ unsigned bufsize; /* actual allocated samples */
unsigned used; /* used samples */ unsigned used; /* used samples */
float vol; int vol; /* vol * 256 */
public: public:
void write(const Sint16 *buf, unsigned size); void write(const Sint16 *buf, unsigned size);