From 854f14bf82e43fa76f482329e4f676500ba49c3d Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 27 Jul 2010 14:55:27 +0000 Subject: [PATCH] added volume parameter to AV::Sound objects, fixed playCallback mixing code git-svn-id: svn://anubis/anaglym/trunk@293 99a6e188-d820-4881-8870-2d33a10e2619 --- AV.cc | 22 +++++++++++++++++++--- AV.h | 3 +++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/AV.cc b/AV.cc index ea0aeef..5961f75 100644 --- a/AV.cc +++ b/AV.cc @@ -160,8 +160,10 @@ void AV::stopSound(AV::Sound * s) void AV::playCallback(Uint8 * stream, int len) { + uint16_t * stream16 = (uint16_t *) stream; memset(stream, 0, len); SDL_mutexP(m_active_sounds_mutex); +#ifdef DEBUG if (len > CALLBACK_SAMPLES * CHANNELS * BYTES_PER_SAMPLE) { cerr << "anaglym bug: callback buffer length = " << len @@ -169,7 +171,7 @@ void AV::playCallback(Uint8 * stream, int len) << CALLBACK_SAMPLES * CHANNELS * BYTES_PER_SAMPLE << endl; exit(38); } - memset(stream, 0, len); +#endif for (std::set::iterator it = m_active_sounds.begin(); it != m_active_sounds.end(); it++) @@ -179,11 +181,13 @@ void AV::playCallback(Uint8 * stream, int len) { m_active_sounds.erase(it); } - for (int i = 0, samples_decoded = bytes_decoded / 2; + float volume = (*it)->getVolume(); + for (int i = 0, samples_decoded = bytes_decoded / BYTES_PER_SAMPLE; i < samples_decoded; i++) { - stream[i] = min(INT_MAX, stream[i] + m_sound_buffer[i]); + stream16[i] = + min(SHRT_MAX, stream16[i] + (int)(volume * m_sound_buffer[i])); } } if (m_active_sounds.size() == 0) @@ -193,6 +197,8 @@ void AV::playCallback(Uint8 * stream, int len) SDL_mutexV(m_active_sounds_mutex); } +/******************** AV::Sound ********************/ + AV::Sound::Sound(AV & av) : m_av(av), m_buff(1) @@ -200,6 +206,7 @@ AV::Sound::Sound(AV & av) m_rwops = NULL; m_ss = NULL; m_buff_bytes_left = 0; + m_volume = 1.0f; } AV::Sound::~Sound() @@ -314,3 +321,12 @@ int AV::Sound::decode(Uint8 * stream, int len) } return len_decoded; } + +void AV::Sound::setVolume(float v) +{ + m_volume = v; + if (m_volume > 1.0f) + m_volume = 1.0f; + else if (m_volume < 0.0f) + m_volume = 0.0f; +} diff --git a/AV.h b/AV.h index da50ba5..a56039e 100644 --- a/AV.h +++ b/AV.h @@ -27,6 +27,8 @@ class AV void loop(int times); void rewind(); int decode(Uint8 * stream, int len); + float getVolume() { return m_volume; } + void setVolume(float v); protected: AV & m_av; SDL_RWops * m_rwops; @@ -34,6 +36,7 @@ class AV FileLoader::Buffer m_buff; int m_loop_count; int m_buff_bytes_left; + float m_volume; }; AV();