diff --git a/stepmania/src/RageSound.cpp b/stepmania/src/RageSound.cpp index 7a2dc5e0c7..9d3b8e4745 100644 --- a/stepmania/src/RageSound.cpp +++ b/stepmania/src/RageSound.cpp @@ -28,6 +28,7 @@ #include "RageException.h" #include "PrefsManager.h" #include "arch/ArchHooks/ArchHooks.h" +#include "RageSoundUtil.h" #include "RageSoundReader_Preload.h" #include "RageSoundReader_Resample.h" @@ -280,22 +281,6 @@ void RageSound::RateChange(char *buf, int &cnt, cnt = (cnt * speed_output_samples) / speed_input_samples; } -/* Dupe mono to stereo in-place; do it in reverse, to handle overlap. */ -void ConvertMonoToStereoInPlace( int16_t *data, int iFrames ) -{ - int16_t *input = data; - int16_t *output = input; - input += iFrames; - output += iFrames * 2; - while( iFrames-- ) - { - input -= 1; - output -= 2; - output[0] = input[0]; - output[1] = input[0]; - } -} - /* Fill the buffer by about "bytes" worth of data. (We might go a little * over, and we won't overflow our buffer.) Return the number of bytes * actually read; 0 = EOF. Convert mono input to stereo. */ @@ -338,7 +323,7 @@ int RageSound::FillBuf( int frames ) if( Sample->GetNumChannels() == 1 ) { - ConvertMonoToStereoInPlace( (int16_t *) inbuf, cnt / sizeof(int16_t) ); + RageSoundUtil::ConvertMonoToStereoInPlace( (int16_t *) inbuf, cnt / sizeof(int16_t) ); cnt *= 2; } @@ -396,59 +381,6 @@ int RageSound::GetData( char *buffer, int frames ) return got; } -/* Pan buffer left or right; fPos is -1...+1. */ -void PanSound( int16_t *buffer, int frames, float fPos ) -{ - if( fPos == 0 ) - return; /* no change */ - - bool bSwap = fPos < 0; - if( bSwap ) - fPos = -fPos; - - float fLeftFactors[2] ={ 1-fPos, 0 }; - float fRightFactors[2] = - { - SCALE( fPos, 0, 1, 0.5f, 0 ), - SCALE( fPos, 0, 1, 0.5f, 1 ) - }; - - if( bSwap ) - { - swap( fLeftFactors[0], fRightFactors[0] ); - swap( fLeftFactors[1], fRightFactors[1] ); - } - - ASSERT_M( channels == 2, ssprintf("%i", channels) ); - for( int samp = 0; samp < frames; ++samp ) - { - int16_t l = int16_t(buffer[0]*fLeftFactors[0] + buffer[1]*fLeftFactors[1]); - int16_t r = int16_t(buffer[0]*fRightFactors[0] + buffer[1]*fRightFactors[1]); - buffer[0] = l; - buffer[1] = r; - buffer += 2; - } -} - -void FadeSound( int16_t *buffer, int frames, float fStartVolume, float fEndVolume ) -{ - /* If the whole buffer is full volume, skip. */ - if( fStartVolume > .9999f && fEndVolume > .9999f ) - return; - - for( int samp = 0; samp < frames; ++samp ) - { - float fVolPercent = SCALE( samp, 0, frames, fStartVolume, fEndVolume ); - - fVolPercent = clamp( fVolPercent, 0.f, 1.f ); - for(int i = 0; i < channels; ++i) - { - *buffer = short(*buffer * fVolPercent); - buffer++; - } - } -} - /* RageSound::GetDataToPlay and RageSound::FillBuf are the main threaded API. These * need to execute without blocking other threads from calling eg. GetPositionSeconds, * since they may take some time to run. @@ -569,10 +501,10 @@ bool RageSound::GetDataToPlay( int16_t *buffer, int size, int &sound_frame, int const float fEndSecond = float(decode_position+got_frames) / samplerate(); const float fStartVolume = SCALE( fStartSecond, fStartFadingOutAt, fFinishFadingOutAt, 1.0f, 0.0f ); const float fEndVolume = SCALE( fEndSecond, fStartFadingOutAt, fFinishFadingOutAt, 1.0f, 0.0f ); - FadeSound( buffer, got_frames, fStartVolume, fEndVolume ); + RageSoundUtil::Fade( buffer, got_frames, fStartVolume, fEndVolume ); } - PanSound( buffer, got_frames, m_Param.m_Balance ); + RageSoundUtil::Pan( buffer, got_frames, m_Param.m_Balance ); sound_frame = decode_position; diff --git a/stepmania/src/RageSoundReader_Chain.cpp b/stepmania/src/RageSoundReader_Chain.cpp index b0bcadfa2a..537ce9872f 100644 --- a/stepmania/src/RageSoundReader_Chain.cpp +++ b/stepmania/src/RageSoundReader_Chain.cpp @@ -6,6 +6,7 @@ #include "RageLog.h" #include "RageUtil.h" #include "RageSoundMixBuffer.h" +#include "RageSoundUtil.h" /* * Keyed sounds should pass this object to SoundReader_Preload, to preprocess it. @@ -264,10 +265,6 @@ unsigned RageSoundReader_Chain::GetNextSoundIndex() const return iNextSound; } -// XXX move these out of RageSound.cpp -void ConvertMonoToStereoInPlace( int16_t *data, int iFrames ); -void PanSound( int16_t *buffer, int frames, float fPos ); - int RageSoundReader_Chain::Read( char *pBuffer, unsigned iLength ) { RageSoundMixBuffer mix; @@ -319,12 +316,12 @@ int RageSoundReader_Chain::Read( char *pBuffer, unsigned iLength ) if( m_iChannels == 2 && pSound->GetNumChannels() == 1 ) { - ConvertMonoToStereoInPlace( Buffer, iSamplesRead ); + RageSoundUtil::ConvertMonoToStereoInPlace( Buffer, iSamplesRead ); iSamplesRead *= 2; } if( fabs(s.fPan) > 0.0001f ) - PanSound( Buffer, iSamplesRead, s.fPan ); + RageSoundUtil::Pan( Buffer, iSamplesRead, s.fPan ); mix.write( Buffer, iSamplesRead ); ++i; diff --git a/stepmania/src/RageSoundUtil.cpp b/stepmania/src/RageSoundUtil.cpp new file mode 100644 index 0000000000..ac293648ff --- /dev/null +++ b/stepmania/src/RageSoundUtil.cpp @@ -0,0 +1,101 @@ +#include "global.h" +#include "RageSoundUtil.h" +#include "RageUtil.h" + +/* Some of these functions assume a channel count: */ +static const int channels = 2; + +/* Pan buffer left or right; fPos is -1...+1. Buffer is assumed to be stereo. */ +void RageSoundUtil::Pan( int16_t *buffer, int frames, float fPos ) +{ + if( fPos == 0 ) + return; /* no change */ + + bool bSwap = fPos < 0; + if( bSwap ) + fPos = -fPos; + + float fLeftFactors[2] ={ 1-fPos, 0 }; + float fRightFactors[2] = + { + SCALE( fPos, 0, 1, 0.5f, 0 ), + SCALE( fPos, 0, 1, 0.5f, 1 ) + }; + + if( bSwap ) + { + swap( fLeftFactors[0], fRightFactors[0] ); + swap( fLeftFactors[1], fRightFactors[1] ); + } + + ASSERT_M( channels == 2, ssprintf("%i", channels) ); + for( int samp = 0; samp < frames; ++samp ) + { + int16_t l = int16_t(buffer[0]*fLeftFactors[0] + buffer[1]*fLeftFactors[1]); + int16_t r = int16_t(buffer[0]*fRightFactors[0] + buffer[1]*fRightFactors[1]); + buffer[0] = l; + buffer[1] = r; + buffer += 2; + } +} + +void RageSoundUtil::Fade( int16_t *buffer, int frames, float fStartVolume, float fEndVolume ) +{ + /* If the whole buffer is full volume, skip. */ + if( fStartVolume > .9999f && fEndVolume > .9999f ) + return; + + for( int samp = 0; samp < frames; ++samp ) + { + float fVolPercent = SCALE( samp, 0, frames, fStartVolume, fEndVolume ); + + fVolPercent = clamp( fVolPercent, 0.f, 1.f ); + for(int i = 0; i < channels; ++i) + { + *buffer = short(*buffer * fVolPercent); + buffer++; + } + } +} + +/* Dupe mono to stereo in-place; do it in reverse, to handle overlap. */ +void RageSoundUtil::ConvertMonoToStereoInPlace( int16_t *data, int iFrames ) +{ + int16_t *input = data; + int16_t *output = input; + input += iFrames; + output += iFrames * 2; + while( iFrames-- ) + { + input -= 1; + output -= 2; + output[0] = input[0]; + output[1] = input[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/RageSoundUtil.h b/stepmania/src/RageSoundUtil.h new file mode 100644 index 0000000000..c4281912d4 --- /dev/null +++ b/stepmania/src/RageSoundUtil.h @@ -0,0 +1,39 @@ +/* RageSoundUtil - simple utilities that operate on sound buffers */ + +#ifndef RAGE_SOUND_UTIL_H +#define RAGE_SOUND_UTIL_H + +namespace RageSoundUtil +{ + void Pan( int16_t *pBuffer, int iFrames, float fPos ); + void Fade( int16_t *pBuffer, int iFrames, float fStartVolume, float fEndVolume ); + void ConvertMonoToStereoInPlace( int16_t *pBuffer, int iFrames ); +}; + +#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. + */ +