From 2ff50ed116ea7203a2b7b055645faf50d19079f7 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Thu, 22 Jan 2004 06:55:18 +0000 Subject: [PATCH] support per-sample volume --- stepmania/src/arch/Sound/DSoundHelpers.cpp | 12 +++++++++--- stepmania/src/arch/Sound/DSoundHelpers.h | 1 + stepmania/src/arch/Sound/RageSoundDriver_DSound.cpp | 10 ++-------- stepmania/src/arch/Sound/RageSoundDriver_DSound.h | 1 - .../arch/Sound/RageSoundDriver_DSound_Software.cpp | 5 ++--- stepmania/src/arch/Sound/RageSoundDriver_WaveOut.cpp | 3 +-- 6 files changed, 15 insertions(+), 17 deletions(-) diff --git a/stepmania/src/arch/Sound/DSoundHelpers.cpp b/stepmania/src/arch/Sound/DSoundHelpers.cpp index 5d3d653445..eb8e0ebbe1 100644 --- a/stepmania/src/arch/Sound/DSoundHelpers.cpp +++ b/stepmania/src/arch/Sound/DSoundHelpers.cpp @@ -135,6 +135,7 @@ DSoundBuf::DSoundBuf(DSound &ds, DSoundBuf::hw hardware, samplerate = samplerate_; samplebits = samplebits_; writeahead = writeahead_; + volume = 0; buffer_locked = false; last_cursor_pos = write_cursor = buffer_bytes_filled = 0; LastPosition = 0; @@ -254,9 +255,14 @@ void DSoundBuf::SetVolume(float vol) vol = 0.001f; // fix log10f(0) == -INF float vl2 = log10f(vol) / log10f(2); /* vol log 2 */ - /* Volume is a multiplier; SetVolume wants attenuation in thousands of a - * decibel. */ - buf->SetVolume(max(int(1000 * vl2), DSBVOLUME_MIN)); + /* Volume is a multiplier; SetVolume wants attenuation in thousands of a decibel. */ + const int new_volume = max( int(1000 * vl2), DSBVOLUME_MIN ); + + if( volume == new_volume ) + return; + volume = new_volume; + + buf->SetVolume( volume ); } /* Determine if "pos" is between "start" and "end", for a circular buffer. */ diff --git a/stepmania/src/arch/Sound/DSoundHelpers.h b/stepmania/src/arch/Sound/DSoundHelpers.h index e05d7a07af..05d9865927 100644 --- a/stepmania/src/arch/Sound/DSoundHelpers.h +++ b/stepmania/src/arch/Sound/DSoundHelpers.h @@ -30,6 +30,7 @@ class DSoundBuf IDirectSoundBuffer *buf; int channels, samplerate, samplebits, writeahead; + int volume; int buffersize; diff --git a/stepmania/src/arch/Sound/RageSoundDriver_DSound.cpp b/stepmania/src/arch/Sound/RageSoundDriver_DSound.cpp index fae1d924b2..eaa5c79792 100644 --- a/stepmania/src/arch/Sound/RageSoundDriver_DSound.cpp +++ b/stepmania/src/arch/Sound/RageSoundDriver_DSound.cpp @@ -123,6 +123,8 @@ bool RageSound_DSound::stream::GetData(bool init) * fill anything in STOPPING; in that case, we just clear the audio buffer. */ if(state != STOPPING) { + pcm->SetVolume( snd->GetVolume() ); + int bytes_read = 0; int bytes_left = len; @@ -236,14 +238,6 @@ RageSound_DSound::~RageSound_DSound() delete stream_pool[i]; } -void RageSound_DSound::VolumeChanged() -{ - for(unsigned i = 0; i < stream_pool.size(); ++i) - { - stream_pool[i]->pcm->SetVolume(SOUNDMAN->GetMixVolume()); - } -} - void RageSound_DSound::StartMixing( RageSoundBase *snd ) { LockMutex L(SOUNDMAN->lock); diff --git a/stepmania/src/arch/Sound/RageSoundDriver_DSound.h b/stepmania/src/arch/Sound/RageSoundDriver_DSound.h index 28b35dc718..06258924b3 100644 --- a/stepmania/src/arch/Sound/RageSoundDriver_DSound.h +++ b/stepmania/src/arch/Sound/RageSoundDriver_DSound.h @@ -49,7 +49,6 @@ class RageSound_DSound: public RageSoundDriver void StopMixing( RageSoundBase *snd ); /* used by RageSound */ int64_t GetPosition( const RageSoundBase *snd ) const; void Update(float delta); - void VolumeChanged(); int GetSampleRate( int rate ) const { return rate; } diff --git a/stepmania/src/arch/Sound/RageSoundDriver_DSound_Software.cpp b/stepmania/src/arch/Sound/RageSoundDriver_DSound_Software.cpp index add5aea25e..1ea17c48e1 100644 --- a/stepmania/src/arch/Sound/RageSoundDriver_DSound_Software.cpp +++ b/stepmania/src/arch/Sound/RageSoundDriver_DSound_Software.cpp @@ -14,7 +14,7 @@ const int channels = 2; const int bytes_per_frame = channels*2; /* 16-bit */ const int samplerate = 44100; -const int buffersize_frames = 1024*4; /* in frames */ +const int buffersize_frames = 1024*16; /* in frames */ const int buffersize = buffersize_frames * bytes_per_frame; /* in bytes */ /* We'll fill the buffer in chunks this big. This should evenly divide the @@ -80,7 +80,6 @@ bool RageSound_DSound_Software::GetData() memset(buf, 0, bufsize*sizeof(Uint16)); static SoundMixBuffer mix; - mix.SetVolume( SOUNDMAN->GetMixVolume() ); for(unsigned i = 0; i < sounds.size(); ++i) { @@ -114,7 +113,7 @@ bool RageSound_DSound_Software::GetData() bytes_read += got; bytes_left -= got; - mix.write( (Sint16 *) buf, bytes_read / sizeof(Sint16) ); + mix.write( (Sint16 *) buf, bytes_read / sizeof(Sint16), sounds[i]->snd->GetVolume() ); if( bytes_left > 0 ) { diff --git a/stepmania/src/arch/Sound/RageSoundDriver_WaveOut.cpp b/stepmania/src/arch/Sound/RageSoundDriver_WaveOut.cpp index 4cda343aec..c7671b8044 100644 --- a/stepmania/src/arch/Sound/RageSoundDriver_WaveOut.cpp +++ b/stepmania/src/arch/Sound/RageSoundDriver_WaveOut.cpp @@ -78,7 +78,6 @@ bool RageSound_WaveOut::GetData() memset(buf, 0, bufsize*sizeof(Uint16)); memset(buffers[b].lpData, 0, bufsize*sizeof(Uint16)); static SoundMixBuffer mix; - mix.SetVolume( SOUNDMAN->GetMixVolume() ); const int64_t play_pos = last_cursor_pos; const int64_t cur_play_pos = GetPosition( NULL ); @@ -113,7 +112,7 @@ bool RageSound_WaveOut::GetData() bytes_read += got; bytes_left -= got; - mix.write( (Sint16 *) buf, bytes_read / sizeof(Sint16) ); + mix.write( (Sint16 *) buf, bytes_read / sizeof(Sint16), sounds[i]->snd->GetVolume() ); if( bytes_left > 0 ) {