mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-01-26 18:35:20 +00:00
Init sound by a single object
This commit is contained in:
parent
a83b32074f
commit
9240833bda
@ -11,85 +11,65 @@ namespace MWSound
|
|||||||
inline int operator&(int a, PlayMode b) { return a & static_cast<int>(b); }
|
inline int operator&(int a, PlayMode b) { return a & static_cast<int>(b); }
|
||||||
inline int operator&(PlayMode a, PlayMode b) { return static_cast<int>(a) & static_cast<int>(b); }
|
inline int operator&(PlayMode a, PlayMode b) { return static_cast<int>(a) & static_cast<int>(b); }
|
||||||
|
|
||||||
|
struct SoundParams
|
||||||
|
{
|
||||||
|
osg::Vec3f mPos;
|
||||||
|
float mVolume = 1;
|
||||||
|
float mBaseVolume = 1;
|
||||||
|
float mPitch = 1;
|
||||||
|
float mMinDistance = 1;
|
||||||
|
float mMaxDistance = 1000;
|
||||||
|
int mFlags = 0;
|
||||||
|
float mFadeOutTime = 0;
|
||||||
|
};
|
||||||
|
|
||||||
class SoundBase {
|
class SoundBase {
|
||||||
SoundBase& operator=(const SoundBase&) = delete;
|
SoundBase& operator=(const SoundBase&) = delete;
|
||||||
SoundBase(const SoundBase&) = delete;
|
SoundBase(const SoundBase&) = delete;
|
||||||
SoundBase(SoundBase&&) = delete;
|
SoundBase(SoundBase&&) = delete;
|
||||||
|
|
||||||
osg::Vec3f mPos;
|
SoundParams mParams;
|
||||||
float mVolume; /* NOTE: Real volume = mVolume*mBaseVolume */
|
|
||||||
float mBaseVolume;
|
|
||||||
float mPitch;
|
|
||||||
float mMinDistance;
|
|
||||||
float mMaxDistance;
|
|
||||||
int mFlags;
|
|
||||||
|
|
||||||
float mFadeOutTime;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Sound_Instance mHandle;
|
Sound_Instance mHandle = nullptr;
|
||||||
|
|
||||||
friend class OpenAL_Output;
|
friend class OpenAL_Output;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void setPosition(const osg::Vec3f &pos) { mPos = pos; }
|
void setPosition(const osg::Vec3f &pos) { mParams.mPos = pos; }
|
||||||
void setVolume(float volume) { mVolume = volume; }
|
void setVolume(float volume) { mParams.mVolume = volume; }
|
||||||
void setBaseVolume(float volume) { mBaseVolume = volume; }
|
void setBaseVolume(float volume) { mParams.mBaseVolume = volume; }
|
||||||
void setFadeout(float duration) { mFadeOutTime = duration; }
|
void setFadeout(float duration) { mParams.mFadeOutTime = duration; }
|
||||||
void updateFade(float duration)
|
void updateFade(float duration)
|
||||||
{
|
{
|
||||||
if(mFadeOutTime > 0.0f)
|
if (mParams.mFadeOutTime > 0.0f)
|
||||||
{
|
{
|
||||||
float soundDuration = std::min(duration, mFadeOutTime);
|
float soundDuration = std::min(duration, mParams.mFadeOutTime);
|
||||||
mVolume *= (mFadeOutTime-soundDuration) / mFadeOutTime;
|
mParams.mVolume *= (mParams.mFadeOutTime - soundDuration) / mParams.mFadeOutTime;
|
||||||
mFadeOutTime -= soundDuration;
|
mParams.mFadeOutTime -= soundDuration;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const osg::Vec3f &getPosition() const { return mPos; }
|
const osg::Vec3f &getPosition() const { return mParams.mPos; }
|
||||||
float getRealVolume() const { return mVolume * mBaseVolume; }
|
float getRealVolume() const { return mParams.mVolume * mParams.mBaseVolume; }
|
||||||
float getPitch() const { return mPitch; }
|
float getPitch() const { return mParams.mPitch; }
|
||||||
float getMinDistance() const { return mMinDistance; }
|
float getMinDistance() const { return mParams.mMinDistance; }
|
||||||
float getMaxDistance() const { return mMaxDistance; }
|
float getMaxDistance() const { return mParams.mMaxDistance; }
|
||||||
|
|
||||||
MWSound::Type getPlayType() const
|
MWSound::Type getPlayType() const
|
||||||
{ return static_cast<MWSound::Type>(mFlags&MWSound::Type::Mask); }
|
{ return static_cast<MWSound::Type>(mParams.mFlags & MWSound::Type::Mask); }
|
||||||
bool getUseEnv() const { return !(mFlags&MWSound::PlayMode::NoEnv); }
|
bool getUseEnv() const { return !(mParams.mFlags & MWSound::PlayMode::NoEnv); }
|
||||||
bool getIsLooping() const { return mFlags&MWSound::PlayMode::Loop; }
|
bool getIsLooping() const { return mParams.mFlags & MWSound::PlayMode::Loop; }
|
||||||
bool getDistanceCull() const { return mFlags&MWSound::PlayMode::RemoveAtDistance; }
|
bool getDistanceCull() const { return mParams.mFlags & MWSound::PlayMode::RemoveAtDistance; }
|
||||||
bool getIs3D() const { return mFlags&Play_3D; }
|
bool getIs3D() const { return mParams.mFlags & Play_3D; }
|
||||||
|
|
||||||
void init(const osg::Vec3f& pos, float vol, float basevol, float pitch, float mindist, float maxdist, int flags)
|
void init(const SoundParams& params)
|
||||||
{
|
{
|
||||||
mPos = pos;
|
mParams = params;
|
||||||
mVolume = vol;
|
|
||||||
mBaseVolume = basevol;
|
|
||||||
mPitch = pitch;
|
|
||||||
mMinDistance = mindist;
|
|
||||||
mMaxDistance = maxdist;
|
|
||||||
mFlags = flags;
|
|
||||||
mFadeOutTime = 0.0f;
|
|
||||||
mHandle = nullptr;
|
mHandle = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void init(float vol, float basevol, float pitch, int flags)
|
SoundBase() = default;
|
||||||
{
|
|
||||||
mPos = osg::Vec3f(0.0f, 0.0f, 0.0f);
|
|
||||||
mVolume = vol;
|
|
||||||
mBaseVolume = basevol;
|
|
||||||
mPitch = pitch;
|
|
||||||
mMinDistance = 1.0f;
|
|
||||||
mMaxDistance = 1000.0f;
|
|
||||||
mFlags = flags;
|
|
||||||
mFadeOutTime = 0.0f;
|
|
||||||
mHandle = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
SoundBase()
|
|
||||||
: mPos(0.0f, 0.0f, 0.0f), mVolume(1.0f), mBaseVolume(1.0f), mPitch(1.0f)
|
|
||||||
, mMinDistance(1.0f), mMaxDistance(1000.0f), mFlags(0), mFadeOutTime(0.0f)
|
|
||||||
, mHandle(nullptr)
|
|
||||||
{ }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class Sound : public SoundBase {
|
class Sound : public SoundBase {
|
||||||
|
@ -290,13 +290,25 @@ namespace MWSound
|
|||||||
StreamPtr sound = getStreamRef();
|
StreamPtr sound = getStreamRef();
|
||||||
if(playlocal)
|
if(playlocal)
|
||||||
{
|
{
|
||||||
sound->init(1.0f, basevol, 1.0f, PlayMode::NoEnv|Type::Voice|Play_2D);
|
sound->init([&] {
|
||||||
|
SoundParams params;
|
||||||
|
params.mBaseVolume = basevol;
|
||||||
|
params.mFlags = PlayMode::NoEnv | Type::Voice | Play_2D;
|
||||||
|
return params;
|
||||||
|
} ());
|
||||||
played = mOutput->streamSound(decoder, sound.get(), true);
|
played = mOutput->streamSound(decoder, sound.get(), true);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
sound->init(pos, 1.0f, basevol, 1.0f, minDistance, maxDistance,
|
sound->init([&] {
|
||||||
PlayMode::Normal|Type::Voice|Play_3D);
|
SoundParams params;
|
||||||
|
params.mPos = pos;
|
||||||
|
params.mBaseVolume = basevol;
|
||||||
|
params.mMinDistance = minDistance;
|
||||||
|
params.mMaxDistance = maxDistance;
|
||||||
|
params.mFlags = PlayMode::Normal | Type::Voice | Play_3D;
|
||||||
|
return params;
|
||||||
|
} ());
|
||||||
played = mOutput->streamSound3D(decoder, sound.get(), true);
|
played = mOutput->streamSound3D(decoder, sound.get(), true);
|
||||||
}
|
}
|
||||||
if(!played)
|
if(!played)
|
||||||
@ -332,8 +344,12 @@ namespace MWSound
|
|||||||
decoder->open(filename);
|
decoder->open(filename);
|
||||||
|
|
||||||
mMusic = getStreamRef();
|
mMusic = getStreamRef();
|
||||||
mMusic->init(1.0f, volumeFromType(Type::Music), 1.0f,
|
mMusic->init([&] {
|
||||||
PlayMode::NoEnv|Type::Music|Play_2D);
|
SoundParams params;
|
||||||
|
params.mBaseVolume = volumeFromType(Type::Music);
|
||||||
|
params.mFlags = PlayMode::NoEnv | Type::Music | Play_2D;
|
||||||
|
return params;
|
||||||
|
} ());
|
||||||
mOutput->streamSound(decoder, mMusic.get());
|
mOutput->streamSound(decoder, mMusic.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -561,7 +577,12 @@ namespace MWSound
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
StreamPtr track = getStreamRef();
|
StreamPtr track = getStreamRef();
|
||||||
track->init(1.0f, volumeFromType(type), 1.0f, PlayMode::NoEnv|type|Play_2D);
|
track->init([&] {
|
||||||
|
SoundParams params;
|
||||||
|
params.mBaseVolume = volumeFromType(type);
|
||||||
|
params.mFlags = PlayMode::NoEnv | type | Play_2D;
|
||||||
|
return params;
|
||||||
|
} ());
|
||||||
if(!mOutput->streamSound(decoder, track.get()))
|
if(!mOutput->streamSound(decoder, track.get()))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
@ -598,7 +619,14 @@ namespace MWSound
|
|||||||
stopSound(sfx, MWWorld::ConstPtr());
|
stopSound(sfx, MWWorld::ConstPtr());
|
||||||
|
|
||||||
SoundPtr sound = getSoundRef();
|
SoundPtr sound = getSoundRef();
|
||||||
sound->init(volume * sfx->mVolume, volumeFromType(type), pitch, mode|type|Play_2D);
|
sound->init([&] {
|
||||||
|
SoundParams params;
|
||||||
|
params.mVolume = volume * sfx->mVolume;
|
||||||
|
params.mBaseVolume = volumeFromType(type);
|
||||||
|
params.mPitch = pitch;
|
||||||
|
params.mFlags = mode | type | Play_2D;
|
||||||
|
return params;
|
||||||
|
} ());
|
||||||
if(!mOutput->playSound(sound.get(), sfx->mHandle, offset))
|
if(!mOutput->playSound(sound.get(), sfx->mHandle, offset))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
@ -635,13 +663,29 @@ namespace MWSound
|
|||||||
SoundPtr sound = getSoundRef();
|
SoundPtr sound = getSoundRef();
|
||||||
if(!(mode&PlayMode::NoPlayerLocal) && ptr == MWMechanics::getPlayer())
|
if(!(mode&PlayMode::NoPlayerLocal) && ptr == MWMechanics::getPlayer())
|
||||||
{
|
{
|
||||||
sound->init(volume * sfx->mVolume, volumeFromType(type), pitch, mode|type|Play_2D);
|
sound->init([&] {
|
||||||
|
SoundParams params;
|
||||||
|
params.mVolume = volume * sfx->mVolume;
|
||||||
|
params.mBaseVolume = volumeFromType(type);
|
||||||
|
params.mPitch = pitch;
|
||||||
|
params.mFlags = mode | type | Play_2D;
|
||||||
|
return params;
|
||||||
|
} ());
|
||||||
played = mOutput->playSound(sound.get(), sfx->mHandle, offset);
|
played = mOutput->playSound(sound.get(), sfx->mHandle, offset);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
sound->init(objpos, volume * sfx->mVolume, volumeFromType(type), pitch,
|
sound->init([&] {
|
||||||
sfx->mMinDist, sfx->mMaxDist, mode|type|Play_3D);
|
SoundParams params;
|
||||||
|
params.mPos = objpos;
|
||||||
|
params.mVolume = volume * sfx->mVolume;
|
||||||
|
params.mBaseVolume = volumeFromType(type);
|
||||||
|
params.mPitch = pitch;
|
||||||
|
params.mMinDistance = sfx->mMinDist;
|
||||||
|
params.mMaxDistance = sfx->mMaxDist;
|
||||||
|
params.mFlags = mode | type | Play_3D;
|
||||||
|
return params;
|
||||||
|
} ());
|
||||||
played = mOutput->playSound3D(sound.get(), sfx->mHandle, offset);
|
played = mOutput->playSound3D(sound.get(), sfx->mHandle, offset);
|
||||||
}
|
}
|
||||||
if(!played)
|
if(!played)
|
||||||
@ -670,8 +714,17 @@ namespace MWSound
|
|||||||
if(!sfx) return nullptr;
|
if(!sfx) return nullptr;
|
||||||
|
|
||||||
SoundPtr sound = getSoundRef();
|
SoundPtr sound = getSoundRef();
|
||||||
sound->init(initialPos, volume * sfx->mVolume, volumeFromType(type), pitch,
|
sound->init([&] {
|
||||||
sfx->mMinDist, sfx->mMaxDist, mode|type|Play_3D);
|
SoundParams params;
|
||||||
|
params.mPos = initialPos;
|
||||||
|
params.mVolume = volume * sfx->mVolume;
|
||||||
|
params.mBaseVolume = volumeFromType(type);
|
||||||
|
params.mPitch = pitch;
|
||||||
|
params.mMinDistance = sfx->mMinDist;
|
||||||
|
params.mMaxDistance = sfx->mMaxDist;
|
||||||
|
params.mFlags = mode | type | Play_3D;
|
||||||
|
return params;
|
||||||
|
} ());
|
||||||
if(!mOutput->playSound3D(sound.get(), sfx->mHandle, offset))
|
if(!mOutput->playSound3D(sound.get(), sfx->mHandle, offset))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user