From 35bdf452ddc40260007ea49fd3336f78acf827bf Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sun, 24 Oct 2004 03:05:57 +0000 Subject: [PATCH] split RageSoundMixBuffer into its own file --- stepmania/src/RageSoundManager.cpp | 63 ------------------- stepmania/src/RageSoundManager.h | 19 ------ stepmania/src/RageSoundMixBuffer.cpp | 94 ++++++++++++++++++++++++++++ stepmania/src/RageSoundMixBuffer.h | 49 +++++++++++++++ 4 files changed, 143 insertions(+), 82 deletions(-) create mode 100644 stepmania/src/RageSoundMixBuffer.cpp create mode 100644 stepmania/src/RageSoundMixBuffer.h diff --git a/stepmania/src/RageSoundManager.cpp b/stepmania/src/RageSoundManager.cpp index acdbe10196..9b1576ee5f 100644 --- a/stepmania/src/RageSoundManager.cpp +++ b/stepmania/src/RageSoundManager.cpp @@ -364,69 +364,6 @@ void RageSoundManager::AttenuateBuf( int16_t *buf, int samples, float vol ) } } - -SoundMixBuffer::SoundMixBuffer() -{ - bufsize = used = 0; - mixbuf = NULL; - SetVolume( SOUNDMAN->GetMixVolume() ); -} - -SoundMixBuffer::~SoundMixBuffer() -{ - free(mixbuf); -} - -void SoundMixBuffer::SetVolume( float f ) -{ - vol = int(256*f); -} - -void SoundMixBuffer::write( const int16_t *buf, unsigned size, float volume, int offset ) -{ - int factor = vol; - if( volume != -1 ) - factor = int( 256*volume ); - - const unsigned realsize = size+offset; - if( bufsize < realsize ) - { - mixbuf = (int32_t *) realloc( mixbuf, sizeof(int32_t) * realsize ); - bufsize = realsize; - } - - if( used < realsize ) - { - memset( mixbuf + used, 0, (realsize - used) * sizeof(int32_t) ); - used = realsize; - } - - /* Scale volume and add. */ - for(unsigned pos = 0; pos < size; ++pos) - mixbuf[pos+offset] += buf[pos] * factor; -} - -void SoundMixBuffer::read(int16_t *buf) -{ - for( unsigned pos = 0; pos < used; ++pos ) - { - int32_t out = (mixbuf[pos]) / 256; - buf[pos] = (int16_t) clamp( out, -32768, 32767 ); - } - used = 0; -} - -void SoundMixBuffer::read( float *buf ) -{ - const int Minimum = -32768 * 256; - const int Maximum = 32767 * 256; - - for( unsigned pos = 0; pos < used; ++pos ) - buf[pos] = SCALE( (float)mixbuf[pos], Minimum, Maximum, -1.0f, 1.0f ); - - used = 0; -} - /* * Copyright (c) 2002-2004 Glenn Maynard * All rights reserved. diff --git a/stepmania/src/RageSoundManager.h b/stepmania/src/RageSoundManager.h index 4f71057840..865275fc2f 100644 --- a/stepmania/src/RageSoundManager.h +++ b/stepmania/src/RageSoundManager.h @@ -77,25 +77,6 @@ private: RageSound *GetSoundByID( int ID ); }; -/* This inputs and outputs 16-bit 44khz stereo input. */ -class SoundMixBuffer -{ - int32_t *mixbuf; - unsigned bufsize; /* actual allocated samples */ - unsigned used; /* used samples */ - int vol; /* vol * 256 */ - -public: - void write( const int16_t *buf, unsigned size, float volume = -1, int offset = 0 ); - void read(int16_t *buf); - void read( float *buf ); - unsigned size() const { return used; } - void SetVolume(float f); - - SoundMixBuffer(); - ~SoundMixBuffer(); -}; - extern RageSoundManager *SOUNDMAN; #endif diff --git a/stepmania/src/RageSoundMixBuffer.cpp b/stepmania/src/RageSoundMixBuffer.cpp new file mode 100644 index 0000000000..48d8f5eb6e --- /dev/null +++ b/stepmania/src/RageSoundMixBuffer.cpp @@ -0,0 +1,94 @@ +#include "global.h" +#include "RageSoundMixBuffer.h" +#include "RageSoundManager.h" +#include "RageUtil.h" + +RageSoundMixBuffer::RageSoundMixBuffer() +{ + bufsize = used = 0; + mixbuf = NULL; + if( SOUNDMAN != NULL ) + SetVolume( SOUNDMAN->GetMixVolume() ); + else + SetVolume( 1.0f ); +} + +RageSoundMixBuffer::~RageSoundMixBuffer() +{ + free(mixbuf); +} + +void RageSoundMixBuffer::SetVolume( float f ) +{ + vol = int(256*f); +} + +void RageSoundMixBuffer::write( const int16_t *buf, unsigned size, float volume, int offset ) +{ + int factor = vol; + if( volume != -1 ) + factor = int( 256*volume ); + + const unsigned realsize = size+offset; + if( bufsize < realsize ) + { + mixbuf = (int32_t *) realloc( mixbuf, sizeof(int32_t) * realsize ); + bufsize = realsize; + } + + if( used < realsize ) + { + memset( mixbuf + used, 0, (realsize - used) * sizeof(int32_t) ); + used = realsize; + } + + /* Scale volume and add. */ + for(unsigned pos = 0; pos < size; ++pos) + mixbuf[pos+offset] += buf[pos] * factor; +} + +void RageSoundMixBuffer::read(int16_t *buf) +{ + for( unsigned pos = 0; pos < used; ++pos ) + { + int32_t out = (mixbuf[pos]) / 256; + buf[pos] = (int16_t) clamp( out, -32768, 32767 ); + } + used = 0; +} + +void RageSoundMixBuffer::read( float *buf ) +{ + const int Minimum = -32768 * 256; + const int Maximum = 32767 * 256; + + for( unsigned pos = 0; pos < used; ++pos ) + buf[pos] = SCALE( (float)mixbuf[pos], Minimum, Maximum, -1.0f, 1.0f ); + + used = 0; +} + +/* + * Copyright (c) 2002-2004 Glenn Maynard + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, and/or sell copies of the Software, and to permit persons to + * whom the Software is furnished to do so, provided that the above + * copyright notice(s) and this permission notice appear in all copies of + * the Software and that both the above copyright notice(s) and this + * permission notice appear in supporting documentation. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF + * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS + * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT + * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS + * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR + * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */ diff --git a/stepmania/src/RageSoundMixBuffer.h b/stepmania/src/RageSoundMixBuffer.h new file mode 100644 index 0000000000..4ae7a54614 --- /dev/null +++ b/stepmania/src/RageSoundMixBuffer.h @@ -0,0 +1,49 @@ +/* RageSoundMixBuffer - Simple audio mixing */ + +#ifndef RAGE_SOUND_MIX_BUFFER_H +#define RAGE_SOUND_MIX_BUFFER_H + +class RageSoundMixBuffer +{ + int32_t *mixbuf; + unsigned bufsize; /* actual allocated samples */ + unsigned used; /* used samples */ + int vol; /* vol * 256 */ + +public: + void write( const int16_t *buf, unsigned size, float volume = -1, int offset = 0 ); + void read(int16_t *buf); + void read( float *buf ); + unsigned size() const { return used; } + void SetVolume(float f); + + RageSoundMixBuffer(); + ~RageSoundMixBuffer(); +}; + +#endif + +/* + * Copyright (c) 2002-2004 Glenn Maynard + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, and/or sell copies of the Software, and to permit persons to + * whom the Software is furnished to do so, provided that the above + * copyright notice(s) and this permission notice appear in all copies of + * the Software and that both the above copyright notice(s) and this + * permission notice appear in supporting documentation. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF + * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS + * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT + * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS + * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR + * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */