1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-02-19 19:09:49 +00:00

Mute sounds that go outside the max distance

This commit is contained in:
Chris Robinson 2012-03-31 00:57:29 -07:00
parent fc167dbc83
commit 5adeee20fd

View file

@ -77,6 +77,7 @@ class OpenAL_SoundStream : public Sound
ALuint mBufferSize;
DecoderPtr mDecoder;
bool mIs3D;
volatile bool mIsFinished;
@ -84,7 +85,7 @@ class OpenAL_SoundStream : public Sound
OpenAL_SoundStream& operator=(const OpenAL_SoundStream &rhs);
public:
OpenAL_SoundStream(OpenAL_Output &output, ALuint src, DecoderPtr decoder);
OpenAL_SoundStream(OpenAL_Output &output, ALuint src, DecoderPtr decoder, bool is3D);
virtual ~OpenAL_SoundStream();
virtual void stop();
@ -164,8 +165,8 @@ private:
};
OpenAL_SoundStream::OpenAL_SoundStream(OpenAL_Output &output, ALuint src, DecoderPtr decoder)
: mOutput(output), mSource(src), mDecoder(decoder), mIsFinished(true)
OpenAL_SoundStream::OpenAL_SoundStream(OpenAL_Output &output, ALuint src, DecoderPtr decoder, bool is3D)
: mOutput(output), mSource(src), mDecoder(decoder), mIs3D(is3D), mIsFinished(true)
{
throwALerror();
@ -256,7 +257,10 @@ bool OpenAL_SoundStream::isPlaying()
void OpenAL_SoundStream::update()
{
alSourcef(mSource, AL_GAIN, mVolume*mBaseVolume);
if(mIs3D && mPos.squaredDistance(mOutput.mPos) > mMaxDistance*mMaxDistance)
alSourcef(mSource, AL_GAIN, 0.0f);
else
alSourcef(mSource, AL_GAIN, mVolume*mBaseVolume);
alSource3f(mSource, AL_POSITION, mPos[0], mPos[2], -mPos[1]);
alSource3f(mSource, AL_DIRECTION, 0.0f, 0.0f, 0.0f);
alSource3f(mSource, AL_VELOCITY, 0.0f, 0.0f, 0.0f);
@ -323,11 +327,13 @@ class OpenAL_Sound : public Sound
ALuint mSource;
ALuint mBuffer;
bool mIs3D;
OpenAL_Sound(const OpenAL_Sound &rhs);
OpenAL_Sound& operator=(const OpenAL_Sound &rhs);
public:
OpenAL_Sound(OpenAL_Output &output, ALuint src, ALuint buf);
OpenAL_Sound(OpenAL_Output &output, ALuint src, ALuint buf, bool is3D);
virtual ~OpenAL_Sound();
virtual void stop();
@ -335,8 +341,8 @@ public:
virtual void update();
};
OpenAL_Sound::OpenAL_Sound(OpenAL_Output &output, ALuint src, ALuint buf)
: mOutput(output), mSource(src), mBuffer(buf)
OpenAL_Sound::OpenAL_Sound(OpenAL_Output &output, ALuint src, ALuint buf, bool is3D)
: mOutput(output), mSource(src), mBuffer(buf), mIs3D(is3D)
{
}
OpenAL_Sound::~OpenAL_Sound()
@ -366,7 +372,10 @@ bool OpenAL_Sound::isPlaying()
void OpenAL_Sound::update()
{
alSourcef(mSource, AL_GAIN, mVolume*mBaseVolume);
if(mIs3D && mPos.squaredDistance(mOutput.mPos) > mMaxDistance*mMaxDistance)
alSourcef(mSource, AL_GAIN, 0.0f);
else
alSourcef(mSource, AL_GAIN, mVolume*mBaseVolume);
alSource3f(mSource, AL_POSITION, mPos[0], mPos[2], -mPos[1]);
alSource3f(mSource, AL_DIRECTION, 0.0f, 0.0f, 0.0f);
alSource3f(mSource, AL_VELOCITY, 0.0f, 0.0f, 0.0f);
@ -583,7 +592,7 @@ SoundPtr OpenAL_Output::playSound(const std::string &fname, float volume, float
try
{
buf = getBuffer(fname);
sound.reset(new OpenAL_Sound(*this, src, buf));
sound.reset(new OpenAL_Sound(*this, src, buf, false));
}
catch(std::exception &e)
{
@ -632,7 +641,7 @@ SoundPtr OpenAL_Output::playSound3D(const std::string &fname, const Ogre::Vector
try
{
buf = getBuffer(fname);
sound.reset(new OpenAL_Sound(*this, src, buf));
sound.reset(new OpenAL_Sound(*this, src, buf, true));
}
catch(std::exception &e)
{
@ -651,7 +660,8 @@ SoundPtr OpenAL_Output::playSound3D(const std::string &fname, const Ogre::Vector
alSourcef(src, AL_MAX_DISTANCE, max);
alSourcef(src, AL_ROLLOFF_FACTOR, 1.0f);
alSourcef(src, AL_GAIN, volume);
alSourcef(src, AL_GAIN, (pos.squaredDistance(mPos) > max*max) ?
0.0f : volume);
alSourcef(src, AL_PITCH, pitch);
alSourcei(src, AL_SOURCE_RELATIVE, AL_FALSE);
@ -682,7 +692,7 @@ SoundPtr OpenAL_Output::streamSound(const std::string &fname, float volume, floa
{
DecoderPtr decoder = mManager.getDecoder();
decoder->open(fname);
sound.reset(new OpenAL_SoundStream(*this, src, decoder));
sound.reset(new OpenAL_SoundStream(*this, src, decoder, false));
}
catch(std::exception &e)
{