simplify and optimize SoundMixBuffer
This commit is contained in:
@@ -276,7 +276,7 @@ SoundMixBuffer::~SoundMixBuffer()
|
||||
|
||||
void SoundMixBuffer::SetVolume( float f )
|
||||
{
|
||||
vol = f;
|
||||
vol = int(256*f);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
Sint32 samp = buf[pos] * 32768;
|
||||
|
||||
/* 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;
|
||||
/* Scale volume and add. */
|
||||
mixbuf[pos] += buf[pos] * vol;
|
||||
}
|
||||
}
|
||||
|
||||
void SoundMixBuffer::read(Sint16 *buf)
|
||||
{
|
||||
int clip=0;
|
||||
for(unsigned pos = 0; pos < used; ++pos)
|
||||
{
|
||||
/* XXX: dither */
|
||||
Sint32 out = (mixbuf[pos]) / 32768;
|
||||
Sint32 out = (mixbuf[pos]) / 256;
|
||||
out = clamp(out, -32768, 32767);
|
||||
buf[pos] = Sint16(out);
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ class SoundMixBuffer
|
||||
Sint32 *mixbuf;
|
||||
unsigned bufsize; /* actual allocated samples */
|
||||
unsigned used; /* used samples */
|
||||
float vol;
|
||||
int vol; /* vol * 256 */
|
||||
|
||||
public:
|
||||
void write(const Sint16 *buf, unsigned size);
|
||||
|
||||
Reference in New Issue
Block a user