added volume parameter to AV::Sound objects, fixed playCallback mixing code

git-svn-id: svn://anubis/anaglym/trunk@293 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
Josh Holtrop 2010-07-27 14:55:27 +00:00
parent 8b8a7eee1f
commit 854f14bf82
2 changed files with 22 additions and 3 deletions

22
AV.cc
View File

@ -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<Sound *>::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;
}

3
AV.h
View File

@ -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();