2012-03-17 09:45:18 +00:00
|
|
|
#ifndef GAME_SOUND_SOUND_H
|
|
|
|
#define GAME_SOUND_SOUND_H
|
|
|
|
|
2017-06-13 09:55:22 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2015-12-02 13:18:43 +00:00
|
|
|
#include "sound_output.hpp"
|
2012-07-16 19:54:04 +00:00
|
|
|
|
2012-03-17 09:45:18 +00:00
|
|
|
namespace MWSound
|
|
|
|
{
|
2020-07-18 21:56:14 +00:00
|
|
|
// Extra play flags, not intended for caller use
|
|
|
|
enum PlayModeEx
|
|
|
|
{
|
|
|
|
Play_2D = 0,
|
|
|
|
Play_3D = 1 << 31,
|
|
|
|
};
|
|
|
|
|
2017-09-15 08:03:41 +00:00
|
|
|
// For testing individual PlayMode flags
|
|
|
|
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); }
|
|
|
|
|
2020-07-12 13:25:14 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2017-09-12 08:19:26 +00:00
|
|
|
class SoundBase {
|
|
|
|
SoundBase& operator=(const SoundBase&) = delete;
|
|
|
|
SoundBase(const SoundBase&) = delete;
|
|
|
|
SoundBase(SoundBase&&) = delete;
|
2012-03-24 10:49:03 +00:00
|
|
|
|
2020-07-12 13:25:14 +00:00
|
|
|
SoundParams mParams;
|
2012-03-28 11:58:47 +00:00
|
|
|
|
2015-11-30 13:42:51 +00:00
|
|
|
protected:
|
2020-07-12 13:25:14 +00:00
|
|
|
Sound_Instance mHandle = nullptr;
|
2015-11-30 13:42:51 +00:00
|
|
|
|
|
|
|
friend class OpenAL_Output;
|
|
|
|
|
2012-03-17 09:45:18 +00:00
|
|
|
public:
|
2020-07-12 13:25:14 +00:00
|
|
|
void setPosition(const osg::Vec3f &pos) { mParams.mPos = pos; }
|
|
|
|
void setVolume(float volume) { mParams.mVolume = volume; }
|
|
|
|
void setBaseVolume(float volume) { mParams.mBaseVolume = volume; }
|
|
|
|
void setFadeout(float duration) { mParams.mFadeOutTime = duration; }
|
2015-11-26 17:11:52 +00:00
|
|
|
void updateFade(float duration)
|
|
|
|
{
|
2020-07-12 13:25:14 +00:00
|
|
|
if (mParams.mFadeOutTime > 0.0f)
|
2015-11-26 17:11:52 +00:00
|
|
|
{
|
2020-07-12 13:25:14 +00:00
|
|
|
float soundDuration = std::min(duration, mParams.mFadeOutTime);
|
|
|
|
mParams.mVolume *= (mParams.mFadeOutTime - soundDuration) / mParams.mFadeOutTime;
|
|
|
|
mParams.mFadeOutTime -= soundDuration;
|
2015-11-26 17:11:52 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-28 22:26:26 +00:00
|
|
|
|
2020-07-12 13:25:14 +00:00
|
|
|
const osg::Vec3f &getPosition() const { return mParams.mPos; }
|
|
|
|
float getRealVolume() const { return mParams.mVolume * mParams.mBaseVolume; }
|
|
|
|
float getPitch() const { return mParams.mPitch; }
|
|
|
|
float getMinDistance() const { return mParams.mMinDistance; }
|
|
|
|
float getMaxDistance() const { return mParams.mMaxDistance; }
|
2015-11-30 22:34:14 +00:00
|
|
|
|
2017-09-15 08:03:41 +00:00
|
|
|
MWSound::Type getPlayType() const
|
2020-07-12 13:25:14 +00:00
|
|
|
{ return static_cast<MWSound::Type>(mParams.mFlags & MWSound::Type::Mask); }
|
|
|
|
bool getUseEnv() const { return !(mParams.mFlags & MWSound::PlayMode::NoEnv); }
|
|
|
|
bool getIsLooping() const { return mParams.mFlags & MWSound::PlayMode::Loop; }
|
|
|
|
bool getDistanceCull() const { return mParams.mFlags & MWSound::PlayMode::RemoveAtDistance; }
|
|
|
|
bool getIs3D() const { return mParams.mFlags & Play_3D; }
|
2017-09-12 04:33:18 +00:00
|
|
|
|
2020-07-12 13:25:14 +00:00
|
|
|
void init(const SoundParams& params)
|
2017-09-12 04:33:18 +00:00
|
|
|
{
|
2020-07-12 13:25:14 +00:00
|
|
|
mParams = params;
|
2017-09-12 04:33:18 +00:00
|
|
|
mHandle = nullptr;
|
|
|
|
}
|
|
|
|
|
2020-07-12 13:25:14 +00:00
|
|
|
SoundBase() = default;
|
2015-11-30 15:32:42 +00:00
|
|
|
};
|
|
|
|
|
2017-09-12 08:19:26 +00:00
|
|
|
class Sound : public SoundBase {
|
|
|
|
Sound& operator=(const Sound&) = delete;
|
|
|
|
Sound(const Sound&) = delete;
|
|
|
|
Sound(Sound&&) = delete;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Sound() { }
|
|
|
|
};
|
|
|
|
|
|
|
|
class Stream : public SoundBase {
|
|
|
|
Stream& operator=(const Stream&) = delete;
|
|
|
|
Stream(const Stream&) = delete;
|
|
|
|
Stream(Stream&&) = delete;
|
2015-11-30 15:32:42 +00:00
|
|
|
|
|
|
|
public:
|
2017-09-12 04:33:18 +00:00
|
|
|
Stream() { }
|
2012-03-17 09:45:18 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|