SoundMixBuffer work

This commit is contained in:
Glenn Maynard
2004-01-21 10:32:01 +00:00
parent a0c555a971
commit 8ff6ad9bf5
2 changed files with 14 additions and 32 deletions
+8 -26
View File
@@ -182,14 +182,14 @@ SoundMixBuffer::~SoundMixBuffer()
void SoundMixBuffer::SetVolume( float f ) void SoundMixBuffer::SetVolume( float f )
{ {
vol = int(256*f); vol = int(256*f);
/* Optimize full volume. */
if( f > 0.99f )
vol = 256;
} }
void SoundMixBuffer::write(const Sint16 *buf, unsigned size) void SoundMixBuffer::write( const Sint16 *buf, unsigned size, float volume )
{ {
int factor = vol;
if( volume != -1 )
factor = int( 256*volume );
if(bufsize < size) if(bufsize < size)
{ {
mixbuf = (Sint32 *) realloc(mixbuf, sizeof(Sint32) * size); mixbuf = (Sint32 *) realloc(mixbuf, sizeof(Sint32) * size);
@@ -202,43 +202,25 @@ void SoundMixBuffer::write(const Sint16 *buf, unsigned size)
used = size; used = size;
} }
if( vol != 256 )
{
/* Scale volume and add. */ /* Scale volume and add. */
for(unsigned pos = 0; pos < size; ++pos) for(unsigned pos = 0; pos < size; ++pos)
mixbuf[pos] += buf[pos] * vol; mixbuf[pos] += buf[pos] * factor;
} else {
/* Just add. */
for(unsigned pos = 0; pos < size; ++pos)
mixbuf[pos] += buf[pos];
}
} }
void SoundMixBuffer::read(Sint16 *buf) void SoundMixBuffer::read(Sint16 *buf)
{
if( vol != 256 )
{ {
for( unsigned pos = 0; pos < used; ++pos ) for( unsigned pos = 0; pos < used; ++pos )
{ {
Sint32 out = (mixbuf[pos]) / 256; Sint32 out = (mixbuf[pos]) / 256;
buf[pos] = (Sint16) clamp( out, -32768, 32767 ); buf[pos] = (Sint16) clamp( out, -32768, 32767 );
} }
} else {
for( unsigned pos = 0; pos < used; ++pos )
buf[pos] = (Sint16) clamp( mixbuf[pos], -32768, 32767 );
}
used = 0; used = 0;
} }
void SoundMixBuffer::read( float *buf ) void SoundMixBuffer::read( float *buf )
{ {
int Minimum = -32768; const int Minimum = -32768 * 256;
int Maximum = 32767; const int Maximum = 32767 * 256;
if( vol != 256 )
{
Minimum *= 256;
Maximum *= 256;
}
for( unsigned pos = 0; pos < used; ++pos ) for( unsigned pos = 0; pos < used; ++pos )
buf[pos] = SCALE( (float)mixbuf[pos], Minimum, Maximum, -1.0f, 1.0f ); buf[pos] = SCALE( (float)mixbuf[pos], Minimum, Maximum, -1.0f, 1.0f );
+1 -1
View File
@@ -66,7 +66,7 @@ class SoundMixBuffer
int vol; /* vol * 256 */ int vol; /* vol * 256 */
public: public:
void write(const Sint16 *buf, unsigned size); void write( const Sint16 *buf, unsigned size, float volume = -1 );
void read(Sint16 *buf); void read(Sint16 *buf);
void read( float *buf ); void read( float *buf );
unsigned size() const { return used; } unsigned size() const { return used; }