1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-27 12:35:46 +00:00

Merge remote branch 'chris/sound'

This commit is contained in:
Marc Zinnschlag 2012-03-31 17:45:26 +02:00
commit a96599d96c
21 changed files with 166 additions and 175 deletions

View File

@ -56,7 +56,7 @@ namespace MWClass
boost::shared_ptr<MWWorld::Action> Apparatus::activate (const MWWorld::Ptr& ptr, boost::shared_ptr<MWWorld::Action> Apparatus::activate (const MWWorld::Ptr& ptr,
const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const
{ {
environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr), 1.0, 1.0, false, true); environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr), 1.0, 1.0, MWSound::Play_NoTrack);
return boost::shared_ptr<MWWorld::Action> ( return boost::shared_ptr<MWWorld::Action> (
new MWWorld::ActionTake (ptr)); new MWWorld::ActionTake (ptr));

View File

@ -60,7 +60,7 @@ namespace MWClass
boost::shared_ptr<MWWorld::Action> Armor::activate (const MWWorld::Ptr& ptr, boost::shared_ptr<MWWorld::Action> Armor::activate (const MWWorld::Ptr& ptr,
const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const
{ {
environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, false, true); environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, MWSound::Play_NoTrack);
return boost::shared_ptr<MWWorld::Action> ( return boost::shared_ptr<MWWorld::Action> (
new MWWorld::ActionTake (ptr)); new MWWorld::ActionTake (ptr));

View File

@ -58,7 +58,7 @@ namespace MWClass
{ {
// TODO implement reading // TODO implement reading
environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, false, true); environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, MWSound::Play_NoTrack);
return boost::shared_ptr<MWWorld::Action> ( return boost::shared_ptr<MWWorld::Action> (
new MWWorld::ActionTake (ptr)); new MWWorld::ActionTake (ptr));

View File

@ -57,7 +57,7 @@ namespace MWClass
boost::shared_ptr<MWWorld::Action> Clothing::activate (const MWWorld::Ptr& ptr, boost::shared_ptr<MWWorld::Action> Clothing::activate (const MWWorld::Ptr& ptr,
const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const
{ {
environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, false, true); environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, MWSound::Play_NoTrack);
return boost::shared_ptr<MWWorld::Action> ( return boost::shared_ptr<MWWorld::Action> (
new MWWorld::ActionTake (ptr)); new MWWorld::ActionTake (ptr));

View File

@ -85,7 +85,7 @@ namespace MWClass
{ {
// TODO check for key // TODO check for key
std::cout << "Locked container" << std::endl; std::cout << "Locked container" << std::endl;
environment.mSoundManager->playSound3D (ptr, lockedSound, 1.0, 1.0, false); environment.mSoundManager->playSound3D (ptr, lockedSound, 1.0, 1.0);
return boost::shared_ptr<MWWorld::Action> (new MWWorld::NullAction); return boost::shared_ptr<MWWorld::Action> (new MWWorld::NullAction);
} }
else else
@ -100,7 +100,7 @@ namespace MWClass
{ {
// Trap activation goes here // Trap activation goes here
std::cout << "Activated trap: " << ptr.getCellRef().trap << std::endl; std::cout << "Activated trap: " << ptr.getCellRef().trap << std::endl;
environment.mSoundManager->playSound3D (ptr, trapActivationSound, 1.0, 1.0, false); environment.mSoundManager->playSound3D (ptr, trapActivationSound, 1.0, 1.0);
ptr.getCellRef().trap = ""; ptr.getCellRef().trap = "";
return boost::shared_ptr<MWWorld::Action> (new MWWorld::NullAction); return boost::shared_ptr<MWWorld::Action> (new MWWorld::NullAction);
} }

View File

@ -73,7 +73,7 @@ namespace MWClass
// TODO check for key // TODO check for key
// TODO report failure to player (message, sound?). Look up behaviour of original MW. // TODO report failure to player (message, sound?). Look up behaviour of original MW.
std::cout << "Locked!" << std::endl; std::cout << "Locked!" << std::endl;
environment.mSoundManager->playSound3D (ptr, lockedSound, 1.0, 1.0, false); environment.mSoundManager->playSound3D (ptr, lockedSound, 1.0, 1.0);
return boost::shared_ptr<MWWorld::Action> (new MWWorld::NullAction); return boost::shared_ptr<MWWorld::Action> (new MWWorld::NullAction);
} }
@ -81,7 +81,7 @@ namespace MWClass
{ {
// Trap activation // Trap activation
std::cout << "Activated trap: " << ptr.getCellRef().trap << std::endl; std::cout << "Activated trap: " << ptr.getCellRef().trap << std::endl;
environment.mSoundManager->playSound3D(ptr, trapActivationSound, 1.0, 1.0, false); environment.mSoundManager->playSound3D(ptr, trapActivationSound, 1.0, 1.0);
ptr.getCellRef().trap = ""; ptr.getCellRef().trap = "";
return boost::shared_ptr<MWWorld::Action> (new MWWorld::NullAction); return boost::shared_ptr<MWWorld::Action> (new MWWorld::NullAction);
} }
@ -110,7 +110,7 @@ namespace MWClass
// TODO return action for rotating the door // TODO return action for rotating the door
// This is a little pointless, but helps with testing // This is a little pointless, but helps with testing
environment.mSoundManager->playSound3D (ptr, openSound, 1.0, 1.0, false); environment.mSoundManager->playSound3D (ptr, openSound, 1.0, 1.0);
return boost::shared_ptr<MWWorld::Action> (new MWWorld::NullAction); return boost::shared_ptr<MWWorld::Action> (new MWWorld::NullAction);
} }
} }

View File

@ -54,7 +54,7 @@ namespace MWClass
boost::shared_ptr<MWWorld::Action> Ingredient::activate (const MWWorld::Ptr& ptr, boost::shared_ptr<MWWorld::Action> Ingredient::activate (const MWWorld::Ptr& ptr,
const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const
{ {
environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, false, true); environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, MWSound::Play_NoTrack);
return boost::shared_ptr<MWWorld::Action> ( return boost::shared_ptr<MWWorld::Action> (
new MWWorld::ActionTake (ptr)); new MWWorld::ActionTake (ptr));

View File

@ -59,7 +59,7 @@ namespace MWClass
if (!ref->base->sound.empty()) if (!ref->base->sound.empty())
{ {
environment.mSoundManager->playSound3D (ptr, ref->base->sound, 1.0, 1.0, true); environment.mSoundManager->playSound3D (ptr, ref->base->sound, 1.0, 1.0, MWSound::Play_Loop);
} }
} }
@ -83,7 +83,7 @@ namespace MWClass
if (!(ref->base->data.flags & ESM::Light::Carry)) if (!(ref->base->data.flags & ESM::Light::Carry))
return boost::shared_ptr<MWWorld::Action> (new MWWorld::NullAction); return boost::shared_ptr<MWWorld::Action> (new MWWorld::NullAction);
environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, false, true); environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, MWSound::Play_NoTrack);
return boost::shared_ptr<MWWorld::Action> ( return boost::shared_ptr<MWWorld::Action> (
new MWWorld::ActionTake (ptr)); new MWWorld::ActionTake (ptr));

View File

@ -58,7 +58,7 @@ namespace MWClass
boost::shared_ptr<MWWorld::Action> Lockpick::activate (const MWWorld::Ptr& ptr, boost::shared_ptr<MWWorld::Action> Lockpick::activate (const MWWorld::Ptr& ptr,
const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const
{ {
environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, false, true); environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, MWSound::Play_NoTrack);
return boost::shared_ptr<MWWorld::Action> ( return boost::shared_ptr<MWWorld::Action> (
new MWWorld::ActionTake (ptr)); new MWWorld::ActionTake (ptr));

View File

@ -56,7 +56,7 @@ namespace MWClass
boost::shared_ptr<MWWorld::Action> Miscellaneous::activate (const MWWorld::Ptr& ptr, boost::shared_ptr<MWWorld::Action> Miscellaneous::activate (const MWWorld::Ptr& ptr,
const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const
{ {
environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, false, true); environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, MWSound::Play_NoTrack);
return boost::shared_ptr<MWWorld::Action> ( return boost::shared_ptr<MWWorld::Action> (
new MWWorld::ActionTake (ptr)); new MWWorld::ActionTake (ptr));

View File

@ -56,7 +56,7 @@ namespace MWClass
boost::shared_ptr<MWWorld::Action> Potion::activate (const MWWorld::Ptr& ptr, boost::shared_ptr<MWWorld::Action> Potion::activate (const MWWorld::Ptr& ptr,
const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const
{ {
environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, false, true); environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, MWSound::Play_NoTrack);
return boost::shared_ptr<MWWorld::Action> ( return boost::shared_ptr<MWWorld::Action> (
new MWWorld::ActionTake (ptr)); new MWWorld::ActionTake (ptr));

View File

@ -57,7 +57,7 @@ namespace MWClass
boost::shared_ptr<MWWorld::Action> Probe::activate (const MWWorld::Ptr& ptr, boost::shared_ptr<MWWorld::Action> Probe::activate (const MWWorld::Ptr& ptr,
const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const
{ {
environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, false, true); environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, MWSound::Play_NoTrack);
return boost::shared_ptr<MWWorld::Action> ( return boost::shared_ptr<MWWorld::Action> (
new MWWorld::ActionTake (ptr)); new MWWorld::ActionTake (ptr));

View File

@ -56,7 +56,7 @@ namespace MWClass
boost::shared_ptr<MWWorld::Action> Repair::activate (const MWWorld::Ptr& ptr, boost::shared_ptr<MWWorld::Action> Repair::activate (const MWWorld::Ptr& ptr,
const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const
{ {
environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, false, true); environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, MWSound::Play_NoTrack);
return boost::shared_ptr<MWWorld::Action> ( return boost::shared_ptr<MWWorld::Action> (
new MWWorld::ActionTake (ptr)); new MWWorld::ActionTake (ptr));

View File

@ -57,7 +57,7 @@ namespace MWClass
boost::shared_ptr<MWWorld::Action> Weapon::activate (const MWWorld::Ptr& ptr, boost::shared_ptr<MWWorld::Action> Weapon::activate (const MWWorld::Ptr& ptr,
const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const
{ {
environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, false, true); environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, MWSound::Play_NoTrack);
return boost::shared_ptr<MWWorld::Action> ( return boost::shared_ptr<MWWorld::Action> (
new MWWorld::ActionTake (ptr)); new MWWorld::ActionTake (ptr));

View File

@ -130,7 +130,7 @@ namespace MWScript
std::string sound = runtime.getStringLiteral (runtime[0].mInteger); std::string sound = runtime.getStringLiteral (runtime[0].mInteger);
runtime.pop(); runtime.pop();
context.getSoundManager().playSound3D (ptr, sound, 1.0, 1.0, mLoop); context.getSoundManager().playSound3D (ptr, sound, 1.0, 1.0, mLoop ? MWSound::Play_Loop : 0);
} }
}; };
@ -159,7 +159,7 @@ namespace MWScript
Interpreter::Type_Float pitch = runtime[0].mFloat; Interpreter::Type_Float pitch = runtime[0].mFloat;
runtime.pop(); runtime.pop();
context.getSoundManager().playSound3D (ptr, sound, volume, pitch, mLoop); context.getSoundManager().playSound3D (ptr, sound, volume, pitch, mLoop ? MWSound::Play_Loop : 0);
} }
}; };

View File

@ -25,14 +25,20 @@ static void throwALCerror(ALCdevice *device)
{ {
ALCenum err = alcGetError(device); ALCenum err = alcGetError(device);
if(err != ALC_NO_ERROR) if(err != ALC_NO_ERROR)
fail(alcGetString(device, err)); {
const ALCchar *errstring = alcGetString(device, err);
fail(errstring ? errstring : "");
}
} }
static void throwALerror() static void throwALerror()
{ {
ALenum err = alGetError(); ALenum err = alGetError();
if(err != AL_NO_ERROR) if(err != AL_NO_ERROR)
fail(alGetString(err)); {
const ALchar *errstring = alGetString(err);
fail(errstring ? errstring : "");
}
} }
@ -65,7 +71,7 @@ static ALenum getALFormat(ChannelConfig chans, SampleType type)
class OpenAL_SoundStream : public Sound class OpenAL_SoundStream : public Sound
{ {
static const ALuint sNumBuffers = 6; static const ALuint sNumBuffers = 6;
static const ALfloat sBufferLength; static const ALfloat sBufferLength = 0.125f;
OpenAL_Output &mOutput; OpenAL_Output &mOutput;
@ -89,14 +95,12 @@ public:
virtual void stop(); virtual void stop();
virtual bool isPlaying(); virtual bool isPlaying();
virtual void setVolume(float volume); virtual void update();
virtual void update(const float *pos);
void play(); void play();
bool process(); bool process();
}; };
const ALfloat OpenAL_SoundStream::sBufferLength = 0.125f;
// //
// A background streaming thread (keeps active streams processed) // A background streaming thread (keeps active streams processed)
@ -187,7 +191,6 @@ OpenAL_SoundStream::OpenAL_SoundStream(OpenAL_Output &output, ALuint src, Decode
} }
catch(std::exception &e) catch(std::exception &e)
{ {
mOutput.mFreeSources.push_back(mSource);
alDeleteBuffers(sNumBuffers, mBuffers); alDeleteBuffers(sNumBuffers, mBuffers);
alGetError(); alGetError();
throw; throw;
@ -255,16 +258,10 @@ bool OpenAL_SoundStream::isPlaying()
return !mIsFinished; return !mIsFinished;
} }
void OpenAL_SoundStream::setVolume(float volume) void OpenAL_SoundStream::update()
{ {
alSourcef(mSource, AL_GAIN, volume*mBaseVolume); alSourcef(mSource, AL_GAIN, mVolume*mBaseVolume);
throwALerror(); alSource3f(mSource, AL_POSITION, mPos[0], mPos[2], -mPos[1]);
mVolume = volume;
}
void OpenAL_SoundStream::update(const float *pos)
{
alSource3f(mSource, AL_POSITION, pos[0], pos[2], -pos[1]);
alSource3f(mSource, AL_DIRECTION, 0.0f, 0.0f, 0.0f); alSource3f(mSource, AL_DIRECTION, 0.0f, 0.0f, 0.0f);
alSource3f(mSource, AL_VELOCITY, 0.0f, 0.0f, 0.0f); alSource3f(mSource, AL_VELOCITY, 0.0f, 0.0f, 0.0f);
throwALerror(); throwALerror();
@ -321,15 +318,17 @@ bool OpenAL_SoundStream::process()
} }
// //
// A regular OpenAL sound // A regular 2D OpenAL sound
// //
class OpenAL_Sound : public Sound class OpenAL_Sound : public Sound
{ {
protected:
OpenAL_Output &mOutput; OpenAL_Output &mOutput;
ALuint mSource; ALuint mSource;
ALuint mBuffer; ALuint mBuffer;
private:
OpenAL_Sound(const OpenAL_Sound &rhs); OpenAL_Sound(const OpenAL_Sound &rhs);
OpenAL_Sound& operator=(const OpenAL_Sound &rhs); OpenAL_Sound& operator=(const OpenAL_Sound &rhs);
@ -339,8 +338,23 @@ public:
virtual void stop(); virtual void stop();
virtual bool isPlaying(); virtual bool isPlaying();
virtual void setVolume(float volume); virtual void update();
virtual void update(const float *pos); };
//
// A regular 3D OpenAL sound
//
class OpenAL_Sound3D : public OpenAL_Sound
{
OpenAL_Sound3D(const OpenAL_Sound &rhs);
OpenAL_Sound3D& operator=(const OpenAL_Sound &rhs);
public:
OpenAL_Sound3D(OpenAL_Output &output, ALuint src, ALuint buf)
: OpenAL_Sound(output, src, buf)
{ }
virtual void update();
}; };
OpenAL_Sound::OpenAL_Sound(OpenAL_Output &output, ALuint src, ALuint buf) OpenAL_Sound::OpenAL_Sound(OpenAL_Output &output, ALuint src, ALuint buf)
@ -372,16 +386,22 @@ bool OpenAL_Sound::isPlaying()
return state==AL_PLAYING; return state==AL_PLAYING;
} }
void OpenAL_Sound::setVolume(float volume) void OpenAL_Sound::update()
{ {
alSourcef(mSource, AL_GAIN, volume*mBaseVolume); 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);
throwALerror(); throwALerror();
mVolume = volume;
} }
void OpenAL_Sound::update(const float *pos) void OpenAL_Sound3D::update()
{ {
alSource3f(mSource, AL_POSITION, pos[0], pos[2], -pos[1]); if(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_DIRECTION, 0.0f, 0.0f, 0.0f);
alSource3f(mSource, AL_VELOCITY, 0.0f, 0.0f, 0.0f); alSource3f(mSource, AL_VELOCITY, 0.0f, 0.0f, 0.0f);
throwALerror(); throwALerror();
@ -410,8 +430,7 @@ std::vector<std::string> OpenAL_Output::enumerate()
void OpenAL_Output::init(const std::string &devname) void OpenAL_Output::init(const std::string &devname)
{ {
if(mDevice || mContext) deinit();
fail("Device already open");
mDevice = alcOpenDevice(devname.c_str()); mDevice = alcOpenDevice(devname.c_str());
if(!mDevice) if(!mDevice)
@ -428,7 +447,12 @@ void OpenAL_Output::init(const std::string &devname)
mContext = alcCreateContext(mDevice, NULL); mContext = alcCreateContext(mDevice, NULL);
if(!mContext || alcMakeContextCurrent(mContext) == ALC_FALSE) if(!mContext || alcMakeContextCurrent(mContext) == ALC_FALSE)
{
if(mContext)
alcDestroyContext(mContext);
mContext = 0;
fail(std::string("Failed to setup context: ")+alcGetString(mDevice, alcGetError(mDevice))); fail(std::string("Failed to setup context: ")+alcGetString(mDevice, alcGetError(mDevice)));
}
alDistanceModel(AL_LINEAR_DISTANCE_CLAMPED); alDistanceModel(AL_LINEAR_DISTANCE_CLAMPED);
throwALerror(); throwALerror();
@ -442,33 +466,13 @@ void OpenAL_Output::init(const std::string &devname)
{ {
ALCuint maxtotal = std::min<ALCuint>(maxmono+maxstereo, 256); ALCuint maxtotal = std::min<ALCuint>(maxmono+maxstereo, 256);
if (maxtotal == 0) // workaround for broken implementations if (maxtotal == 0) // workaround for broken implementations
{
maxtotal = 256; maxtotal = 256;
bool stop = false; for(size_t i = 0;i < maxtotal;i++)
for(size_t i = 0;i < maxtotal && !stop;i++) // generate source until error returned
{
ALuint src = 0;
alGenSources(1, &src);
ALenum err = alGetError();
if(err != AL_NO_ERROR)
{
stop = true;
}
else
{
mFreeSources.push_back(src);
}
}
}
else // normal case
{ {
for(size_t i = 0;i < maxtotal;i++) ALuint src = 0;
{ alGenSources(1, &src);
ALuint src = 0; throwALerror();
alGenSources(1, &src); mFreeSources.push_back(src);
throwALerror();
mFreeSources.push_back(src);
}
} }
} }
catch(std::exception &e) catch(std::exception &e)
@ -602,10 +606,8 @@ void OpenAL_Output::bufferFinished(ALuint buf)
} }
SoundPtr OpenAL_Output::playSound(const std::string &fname, float volume, float pitch, bool loop) SoundPtr OpenAL_Output::playSound(const std::string &fname, float volume, float pitch, int flags)
{ {
throwALerror();
boost::shared_ptr<OpenAL_Sound> sound; boost::shared_ptr<OpenAL_Sound> sound;
ALuint src=0, buf=0; ALuint src=0, buf=0;
@ -640,7 +642,7 @@ SoundPtr OpenAL_Output::playSound(const std::string &fname, float volume, float
alSourcef(src, AL_PITCH, pitch); alSourcef(src, AL_PITCH, pitch);
alSourcei(src, AL_SOURCE_RELATIVE, AL_TRUE); alSourcei(src, AL_SOURCE_RELATIVE, AL_TRUE);
alSourcei(src, AL_LOOPING, (loop?AL_TRUE:AL_FALSE)); alSourcei(src, AL_LOOPING, (flags&Play_Loop) ? AL_TRUE : AL_FALSE);
throwALerror(); throwALerror();
alSourcei(src, AL_BUFFER, buf); alSourcei(src, AL_BUFFER, buf);
@ -650,11 +652,9 @@ SoundPtr OpenAL_Output::playSound(const std::string &fname, float volume, float
return sound; return sound;
} }
SoundPtr OpenAL_Output::playSound3D(const std::string &fname, const float *pos, float volume, float pitch, SoundPtr OpenAL_Output::playSound3D(const std::string &fname, const Ogre::Vector3 &pos, float volume, float pitch,
float min, float max, bool loop) float min, float max, int flags)
{ {
throwALerror();
boost::shared_ptr<OpenAL_Sound> sound; boost::shared_ptr<OpenAL_Sound> sound;
ALuint src=0, buf=0; ALuint src=0, buf=0;
@ -666,7 +666,7 @@ SoundPtr OpenAL_Output::playSound3D(const std::string &fname, const float *pos,
try try
{ {
buf = getBuffer(fname); buf = getBuffer(fname);
sound.reset(new OpenAL_Sound(*this, src, buf)); sound.reset(new OpenAL_Sound3D(*this, src, buf));
} }
catch(std::exception &e) catch(std::exception &e)
{ {
@ -677,7 +677,7 @@ SoundPtr OpenAL_Output::playSound3D(const std::string &fname, const float *pos,
throw; throw;
} }
alSource3f(src, AL_POSITION, pos[0], pos[2], -pos[1]); alSource3f(src, AL_POSITION, pos.x, pos.z, -pos.y);
alSource3f(src, AL_DIRECTION, 0.0f, 0.0f, 0.0f); alSource3f(src, AL_DIRECTION, 0.0f, 0.0f, 0.0f);
alSource3f(src, AL_VELOCITY, 0.0f, 0.0f, 0.0f); alSource3f(src, AL_VELOCITY, 0.0f, 0.0f, 0.0f);
@ -685,11 +685,12 @@ SoundPtr OpenAL_Output::playSound3D(const std::string &fname, const float *pos,
alSourcef(src, AL_MAX_DISTANCE, max); alSourcef(src, AL_MAX_DISTANCE, max);
alSourcef(src, AL_ROLLOFF_FACTOR, 1.0f); 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); alSourcef(src, AL_PITCH, pitch);
alSourcei(src, AL_SOURCE_RELATIVE, AL_FALSE); alSourcei(src, AL_SOURCE_RELATIVE, AL_FALSE);
alSourcei(src, AL_LOOPING, (loop?AL_TRUE:AL_FALSE)); alSourcei(src, AL_LOOPING, (flags&Play_Loop) ? AL_TRUE : AL_FALSE);
throwALerror(); throwALerror();
alSourcei(src, AL_BUFFER, buf); alSourcei(src, AL_BUFFER, buf);
@ -702,8 +703,6 @@ SoundPtr OpenAL_Output::playSound3D(const std::string &fname, const float *pos,
SoundPtr OpenAL_Output::streamSound(const std::string &fname, float volume, float pitch) SoundPtr OpenAL_Output::streamSound(const std::string &fname, float volume, float pitch)
{ {
throwALerror();
boost::shared_ptr<OpenAL_SoundStream> sound; boost::shared_ptr<OpenAL_SoundStream> sound;
ALuint src; ALuint src;
@ -743,61 +742,21 @@ SoundPtr OpenAL_Output::streamSound(const std::string &fname, float volume, floa
return sound; return sound;
} }
SoundPtr OpenAL_Output::streamSound3D(const std::string &fname, const float *pos, float volume, float pitch,
float min, float max) void OpenAL_Output::updateListener(const Ogre::Vector3 &pos, const Ogre::Vector3 &atdir, const Ogre::Vector3 &updir)
{ {
throwALerror(); mPos = pos;
boost::shared_ptr<OpenAL_SoundStream> sound; if(mContext)
ALuint src;
if(mFreeSources.empty())
fail("No free sources");
src = mFreeSources.front();
mFreeSources.pop_front();
try
{ {
DecoderPtr decoder = mManager.getDecoder(); ALfloat orient[6] = {
decoder->open(fname); atdir.x, atdir.z, -atdir.y,
sound.reset(new OpenAL_SoundStream(*this, src, decoder)); updir.x, updir.z, -updir.y
};
alListener3f(AL_POSITION, mPos.x, mPos.z, -mPos.y);
alListenerfv(AL_ORIENTATION, orient);
throwALerror();
} }
catch(std::exception &e)
{
mFreeSources.push_back(src);
throw;
}
alSource3f(src, AL_POSITION, pos[0], pos[2], -pos[1]);
alSource3f(src, AL_DIRECTION, 0.0f, 0.0f, 0.0f);
alSource3f(src, AL_VELOCITY, 0.0f, 0.0f, 0.0f);
alSourcef(src, AL_REFERENCE_DISTANCE, min);
alSourcef(src, AL_MAX_DISTANCE, max);
alSourcef(src, AL_ROLLOFF_FACTOR, 1.0f);
alSourcef(src, AL_GAIN, volume);
alSourcef(src, AL_PITCH, pitch);
alSourcei(src, AL_SOURCE_RELATIVE, AL_FALSE);
alSourcei(src, AL_LOOPING, AL_FALSE);
throwALerror();
sound->play();
return sound;
}
void OpenAL_Output::updateListener(const float *pos, const float *atdir, const float *updir)
{
float orient[6] = {
atdir[0], atdir[2], -atdir[1],
updir[0], updir[2], -updir[1]
};
alListener3f(AL_POSITION, pos[0], pos[2], -pos[1]);
alListenerfv(AL_ORIENTATION, orient);
throwALerror();
} }

View File

@ -40,15 +40,12 @@ namespace MWSound
virtual void init(const std::string &devname=""); virtual void init(const std::string &devname="");
virtual void deinit(); virtual void deinit();
virtual SoundPtr playSound(const std::string &fname, float volume, float pitch, bool loop); virtual SoundPtr playSound(const std::string &fname, float volume, float pitch, int flags);
virtual SoundPtr playSound3D(const std::string &fname, const float *pos, float volume, float pitch, virtual SoundPtr playSound3D(const std::string &fname, const Ogre::Vector3 &pos,
float min, float max, bool loop); float volume, float pitch, float min, float max, int flags);
virtual SoundPtr streamSound(const std::string &fname, float volume, float pitch); virtual SoundPtr streamSound(const std::string &fname, float volume, float pitch);
virtual SoundPtr streamSound3D(const std::string &fname, const float *pos, float volume, float pitch,
float min, float max);
virtual void updateListener(const float *pos, const float *atdir, const float *updir); virtual void updateListener(const Ogre::Vector3 &pos, const Ogre::Vector3 &atdir, const Ogre::Vector3 &updir);
OpenAL_Output& operator=(const OpenAL_Output &rhs); OpenAL_Output& operator=(const OpenAL_Output &rhs);
OpenAL_Output(const OpenAL_Output &rhs); OpenAL_Output(const OpenAL_Output &rhs);
@ -60,6 +57,7 @@ namespace MWSound
std::auto_ptr<StreamThread> mStreamThread; std::auto_ptr<StreamThread> mStreamThread;
friend class OpenAL_Sound; friend class OpenAL_Sound;
friend class OpenAL_Sound3D;
friend class OpenAL_SoundStream; friend class OpenAL_SoundStream;
friend class SoundManager; friend class SoundManager;
}; };

View File

@ -1,30 +1,37 @@
#ifndef GAME_SOUND_SOUND_H #ifndef GAME_SOUND_SOUND_H
#define GAME_SOUND_SOUND_H #define GAME_SOUND_SOUND_H
#include <OgreVector3.h>
namespace MWSound namespace MWSound
{ {
class Sound class Sound
{ {
virtual void update(const float *pos) = 0; virtual void update() = 0;
Sound& operator=(const Sound &rhs); Sound& operator=(const Sound &rhs);
Sound(const Sound &rhs); Sound(const Sound &rhs);
protected: protected:
Ogre::Vector3 mPos;
float mVolume; /* NOTE: Real volume = mVolume*mBaseVolume */ float mVolume; /* NOTE: Real volume = mVolume*mBaseVolume */
float mBaseVolume; float mBaseVolume;
float mMinDistance; float mMinDistance;
float mMaxDistance; float mMaxDistance;
int mFlags;
public: public:
virtual void stop() = 0; virtual void stop() = 0;
virtual bool isPlaying() = 0; virtual bool isPlaying() = 0;
virtual void setVolume(float volume) = 0; void setPosition(const Ogre::Vector3 &pos) { mPos = pos; }
void setVolume(float volume) { mVolume = volume; }
Sound() : mVolume(1.0f) Sound() : mPos(0.0f, 0.0f, 0.0f)
, mVolume(1.0f)
, mBaseVolume(1.0f) , mBaseVolume(1.0f)
, mMinDistance(20.0f) /* 1 * min_range_scale */ , mMinDistance(20.0f) /* 1 * min_range_scale */
, mMaxDistance(12750.0f) /* 255 * max_range_scale */ , mMaxDistance(12750.0f) /* 255 * max_range_scale */
, mFlags(Play_Normal)
{ } { }
virtual ~Sound() { } virtual ~Sound() { }

View File

@ -4,6 +4,8 @@
#include <string> #include <string>
#include <memory> #include <memory>
#include <OgreVector3.h>
#include "soundmanager.hpp" #include "soundmanager.hpp"
#include "../mwworld/ptr.hpp" #include "../mwworld/ptr.hpp"
@ -22,19 +24,23 @@ namespace MWSound
virtual void init(const std::string &devname="") = 0; virtual void init(const std::string &devname="") = 0;
virtual void deinit() = 0; virtual void deinit() = 0;
virtual SoundPtr playSound(const std::string &fname, float volume, float pitch, bool loop) = 0; virtual SoundPtr playSound(const std::string &fname, float volume, float pitch, int flags) = 0;
virtual SoundPtr playSound3D(const std::string &fname, const float *pos, float volume, float pitch, virtual SoundPtr playSound3D(const std::string &fname, const Ogre::Vector3 &pos,
float min, float max, bool loop) = 0; float volume, float pitch, float min, float max, int flags) = 0;
virtual SoundPtr streamSound(const std::string &fname, float volume, float pitch) = 0; virtual SoundPtr streamSound(const std::string &fname, float volume, float pitch) = 0;
virtual SoundPtr streamSound3D(const std::string &fname, const float *pos, float volume, float pitch,
float min, float max) = 0;
virtual void updateListener(const float *pos, const float *atdir, const float *updir) = 0; virtual void updateListener(const Ogre::Vector3 &pos, const Ogre::Vector3 &atdir, const Ogre::Vector3 &updir) = 0;
Sound_Output& operator=(const Sound_Output &rhs); Sound_Output& operator=(const Sound_Output &rhs);
Sound_Output(const Sound_Output &rhs); Sound_Output(const Sound_Output &rhs);
Sound_Output(SoundManager &mgr) : mManager(mgr) { } protected:
Ogre::Vector3 mPos;
Sound_Output(SoundManager &mgr)
: mManager(mgr)
, mPos(0.0f, 0.0f, 0.0f)
{ }
public: public:
virtual ~Sound_Output() { } virtual ~Sound_Output() { }

View File

@ -41,6 +41,8 @@ namespace MWSound
SoundManager::SoundManager(bool useSound, MWWorld::Environment& environment) SoundManager::SoundManager(bool useSound, MWWorld::Environment& environment)
: mResourceMgr(Ogre::ResourceGroupManager::getSingleton()) : mResourceMgr(Ogre::ResourceGroupManager::getSingleton())
, mEnvironment(environment) , mEnvironment(environment)
, mOutput(new DEFAULT_OUTPUT(*this))
{ {
if(!useSound) if(!useSound)
return; return;
@ -50,8 +52,6 @@ namespace MWSound
try try
{ {
mOutput.reset(new DEFAULT_OUTPUT(*this));
std::vector<std::string> names = mOutput->enumerate(); std::vector<std::string> names = mOutput->enumerate();
std::cout <<"Enumerated output devices:"<< std::endl; std::cout <<"Enumerated output devices:"<< std::endl;
for(size_t i = 0;i < names.size();i++) for(size_t i = 0;i < names.size();i++)
@ -62,8 +62,6 @@ namespace MWSound
catch(std::exception &e) catch(std::exception &e)
{ {
std::cout <<"Sound init failed: "<<e.what()<< std::endl; std::cout <<"Sound init failed: "<<e.what()<< std::endl;
mOutput.reset();
return;
} }
} }
@ -108,7 +106,7 @@ namespace MWSound
max = std::max(min, max); max = std::max(min, max);
} }
return std::string("Sound/")+snd->sound; return "Sound/"+snd->sound;
} }
@ -182,11 +180,13 @@ namespace MWSound
{ {
// The range values are not tested // The range values are not tested
float basevol = 1.0f; /* TODO: volume settings */ float basevol = 1.0f; /* TODO: volume settings */
std::string filePath = std::string("Sound/")+filename; std::string filePath = "Sound/"+filename;
const ESM::Position &pos = ptr.getCellRef().pos; const ESM::Position &pos = ptr.getCellRef().pos;
const Ogre::Vector3 objpos(pos.pos[0], pos.pos[1], pos.pos[2]);
SoundPtr sound = mOutput->playSound3D(filePath, pos.pos, basevol, 1.0f, SoundPtr sound = mOutput->playSound3D(filePath, objpos, basevol, 1.0f,
20.0f, 12750.0f, false); 20.0f, 12750.0f, Play_Normal);
sound->mPos = objpos;
sound->mBaseVolume = basevol; sound->mBaseVolume = basevol;
mActiveSounds[sound] = std::make_pair(ptr, std::string("_say_sound")); mActiveSounds[sound] = std::make_pair(ptr, std::string("_say_sound"));
@ -203,7 +203,7 @@ namespace MWSound
} }
SoundPtr SoundManager::playSound(const std::string& soundId, float volume, float pitch, bool loop) SoundPtr SoundManager::playSound(const std::string& soundId, float volume, float pitch, int mode)
{ {
SoundPtr sound; SoundPtr sound;
try try
@ -212,11 +212,12 @@ namespace MWSound
float min, max; float min, max;
std::string file = lookup(soundId, basevol, min, max); std::string file = lookup(soundId, basevol, min, max);
sound = mOutput->playSound(file, volume*basevol, pitch, loop); sound = mOutput->playSound(file, volume*basevol, pitch, mode);
sound->mVolume = volume; sound->mVolume = volume;
sound->mBaseVolume = basevol; sound->mBaseVolume = basevol;
sound->mMinDistance = min; sound->mMinDistance = min;
sound->mMaxDistance = max; sound->mMaxDistance = max;
sound->mFlags = mode;
mActiveSounds[sound] = std::make_pair(MWWorld::Ptr(), soundId); mActiveSounds[sound] = std::make_pair(MWWorld::Ptr(), soundId);
} }
@ -228,8 +229,7 @@ namespace MWSound
} }
SoundPtr SoundManager::playSound3D(MWWorld::Ptr ptr, const std::string& soundId, SoundPtr SoundManager::playSound3D(MWWorld::Ptr ptr, const std::string& soundId,
float volume, float pitch, bool loop, float volume, float pitch, int mode)
bool untracked)
{ {
SoundPtr sound; SoundPtr sound;
try try
@ -239,15 +239,20 @@ namespace MWSound
float min, max; float min, max;
std::string file = lookup(soundId, basevol, min, max); std::string file = lookup(soundId, basevol, min, max);
const ESM::Position &pos = ptr.getCellRef().pos; const ESM::Position &pos = ptr.getCellRef().pos;
const Ogre::Vector3 objpos(pos.pos[0], pos.pos[1], pos.pos[2]);
sound = mOutput->playSound3D(file, pos.pos, volume*basevol, pitch, min, max, loop); sound = mOutput->playSound3D(file, objpos, volume*basevol, pitch, min, max, mode);
sound->mPos = objpos;
sound->mVolume = volume; sound->mVolume = volume;
sound->mBaseVolume = basevol; sound->mBaseVolume = basevol;
sound->mMinDistance = min; sound->mMinDistance = min;
sound->mMaxDistance = max; sound->mMaxDistance = max;
sound->mFlags = mode;
mActiveSounds[sound] = (!untracked ? std::make_pair(ptr, soundId) : if((mode&Play_NoTrack))
std::make_pair(MWWorld::Ptr(), soundId)); mActiveSounds[sound] = std::make_pair(MWWorld::Ptr(), soundId);
else
mActiveSounds[sound] = std::make_pair(ptr, soundId);
} }
catch(std::exception &e) catch(std::exception &e)
{ {
@ -326,11 +331,12 @@ namespace MWSound
void SoundManager::updateObject(MWWorld::Ptr ptr) void SoundManager::updateObject(MWWorld::Ptr ptr)
{ {
const ESM::Position &pos = ptr.getCellRef().pos; const ESM::Position &pos = ptr.getCellRef().pos;
const Ogre::Vector3 objpos(pos.pos[0], pos.pos[1], pos.pos[2]);
SoundMap::iterator snditer = mActiveSounds.begin(); SoundMap::iterator snditer = mActiveSounds.begin();
while(snditer != mActiveSounds.end()) while(snditer != mActiveSounds.end())
{ {
if(snditer->second.first == ptr) if(snditer->second.first == ptr)
snditer->first->update(pos.pos); snditer->first->setPosition(objpos);
snditer++; snditer++;
} }
} }
@ -411,9 +417,9 @@ namespace MWSound
// The output handler is expecting vectors oriented like the game // The output handler is expecting vectors oriented like the game
// (that is, -Z goes down, +Y goes forward), but that's not what we // (that is, -Z goes down, +Y goes forward), but that's not what we
// get from Ogre's camera, so we have to convert. // get from Ogre's camera, so we have to convert.
float pos[3] = { nPos[0], -nPos[2], nPos[1] }; const Ogre::Vector3 pos(nPos[0], -nPos[2], nPos[1]);
float at[3] = { nDir[0], -nDir[2], nDir[1] }; const Ogre::Vector3 at(nDir[0], -nDir[2], nDir[1]);
float up[3] = { nUp[0], -nUp[2], nUp[1] }; const Ogre::Vector3 up(nUp[0], -nUp[2], nUp[1]);
mOutput->updateListener(pos, at, up); mOutput->updateListener(pos, at, up);
// Check if any sounds are finished playing, and trash them // Check if any sounds are finished playing, and trash them
@ -423,7 +429,10 @@ namespace MWSound
if(!snditer->first->isPlaying()) if(!snditer->first->isPlaying())
mActiveSounds.erase(snditer++); mActiveSounds.erase(snditer++);
else else
{
snditer->first->update();
snditer++; snditer++;
}
} }
} }

View File

@ -30,6 +30,19 @@ namespace MWSound
typedef boost::shared_ptr<Sound_Decoder> DecoderPtr; typedef boost::shared_ptr<Sound_Decoder> DecoderPtr;
typedef boost::shared_ptr<Sound> SoundPtr; typedef boost::shared_ptr<Sound> SoundPtr;
enum PlayMode {
Play_Normal = 0, /* tracked, non-looping, multi-instance, environment */
Play_Loop = 1<<0, /* Sound will continually loop until explicitly stopped */
Play_NoEnv = 1<<1, /* Do not apply environment effects (eg, underwater filters) */
Play_NoTrack = 1<<2, /* (3D only) Play the sound at the given object's position
* but do not keep it updated (the sound will not move with
* the object and will not stop when the object is deleted. */
};
static inline int operator|(const PlayMode &a, const PlayMode &b)
{ return (int)a | (int)b; }
static inline int operator&(const PlayMode &a, const PlayMode &b)
{ return (int)a & (int)b; }
class SoundManager class SoundManager
{ {
Ogre::ResourceGroupManager& mResourceMgr; Ogre::ResourceGroupManager& mResourceMgr;
@ -87,12 +100,11 @@ namespace MWSound
bool sayDone(MWWorld::Ptr reference) const; bool sayDone(MWWorld::Ptr reference) const;
///< Is actor not speaking? ///< Is actor not speaking?
SoundPtr playSound(const std::string& soundId, float volume, float pitch, bool loop=false); SoundPtr playSound(const std::string& soundId, float volume, float pitch, int mode=Play_Normal);
///< Play a sound, independently of 3D-position ///< Play a sound, independently of 3D-position
SoundPtr playSound3D(MWWorld::Ptr reference, const std::string& soundId, SoundPtr playSound3D(MWWorld::Ptr reference, const std::string& soundId,
float volume, float pitch, bool loop, float volume, float pitch, int mode=Play_Normal);
bool untracked=false);
///< Play a sound from an object ///< Play a sound from an object
void stopSound3D(MWWorld::Ptr reference, const std::string& soundId); void stopSound3D(MWWorld::Ptr reference, const std::string& soundId);