| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- // ----------------------------------------------------------------
- // From Game Programming in C++ by Sanjay Madhav
- // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
- //
- // Released under the BSD License
- // See LICENSE in root directory for full details.
- // ----------------------------------------------------------------
- #include "SoundEvent.h"
- #include "AudioSystem.h"
- #include <fmod_studio.hpp>
- SoundEvent::SoundEvent(class AudioSystem* system, unsigned int id)
- :mSystem(system)
- ,mID(id)
- {
- }
- SoundEvent::SoundEvent()
- :mSystem(nullptr)
- ,mID(0)
- {
- }
- bool SoundEvent::IsValid()
- {
- return (mSystem && mSystem->GetEventInstance(mID) != nullptr);
- }
- void SoundEvent::Restart()
- {
- auto event = mSystem ? mSystem->GetEventInstance(mID) : nullptr;
- if (event)
- {
- event->start();
- }
- }
- void SoundEvent::Stop(bool allowFadeOut /* true */)
- {
- auto event = mSystem ? mSystem->GetEventInstance(mID) : nullptr;
- if (event)
- {
- FMOD_STUDIO_STOP_MODE mode = allowFadeOut ?
- FMOD_STUDIO_STOP_ALLOWFADEOUT :
- FMOD_STUDIO_STOP_IMMEDIATE;
- event->stop(mode);
- }
- }
- void SoundEvent::SetPaused(bool pause)
- {
- auto event = mSystem ? mSystem->GetEventInstance(mID) : nullptr;
- if (event)
- {
- event->setPaused(pause);
- }
- }
- void SoundEvent::SetVolume(float value)
- {
- auto event = mSystem ? mSystem->GetEventInstance(mID) : nullptr;
- if (event)
- {
- event->setVolume(value);
- }
- }
- void SoundEvent::SetPitch(float value)
- {
- auto event = mSystem ? mSystem->GetEventInstance(mID) : nullptr;
- if (event)
- {
- event->setPitch(value);
- }
- }
- void SoundEvent::SetParameter(const std::string& name, float value)
- {
- auto event = mSystem ? mSystem->GetEventInstance(mID) : nullptr;
- if (event)
- {
- event->setParameterValue(name.c_str(), value);
- }
- }
- bool SoundEvent::GetPaused() const
- {
- bool retVal = false;
- auto event = mSystem ? mSystem->GetEventInstance(mID) : nullptr;
- if (event)
- {
- event->getPaused(&retVal);
- }
- return retVal;
- }
- float SoundEvent::GetVolume() const
- {
- float retVal = 0.0f;
- auto event = mSystem ? mSystem->GetEventInstance(mID) : nullptr;
- if (event)
- {
- event->getVolume(&retVal);
- }
- return retVal;
- }
- float SoundEvent::GetPitch() const
- {
- float retVal = 0.0f;
- auto event = mSystem ? mSystem->GetEventInstance(mID) : nullptr;
- if (event)
- {
- event->getPitch(&retVal);
- }
- return retVal;
- }
- float SoundEvent::GetParameter(const std::string& name)
- {
- float retVal = 0.0f;
- auto event = mSystem ? mSystem->GetEventInstance(mID) : nullptr;
- if (event)
- {
- event->getParameterValue(name.c_str(), &retVal);
- }
- return retVal;
- }
- bool SoundEvent::Is3D() const
- {
- bool retVal = false;
- auto event = mSystem ? mSystem->GetEventInstance(mID) : nullptr;
- if (event)
- {
- // Get the event description
- FMOD::Studio::EventDescription* ed = nullptr;
- event->getDescription(&ed);
- if (ed)
- {
- ed->is3D(&retVal);
- }
- }
- return retVal;
- }
- namespace
- {
- FMOD_VECTOR VecToFMOD(const Vector3& in)
- {
- // Convert from our coordinates (+x forward, +y right, +z up)
- // to FMOD (+z forward, +x right, +y up)
- FMOD_VECTOR v;
- v.x = in.y;
- v.y = in.z;
- v.z = in.x;
- return v;
- }
- }
- void SoundEvent::Set3DAttributes(const Matrix4& worldTrans)
- {
- auto event = mSystem ? mSystem->GetEventInstance(mID) : nullptr;
- if (event)
- {
- FMOD_3D_ATTRIBUTES attr;
- // Set position, forward, up
- attr.position = VecToFMOD(worldTrans.GetTranslation());
- // In world transform, first row is forward
- attr.forward = VecToFMOD(worldTrans.GetXAxis());
- // Third row is up
- attr.up = VecToFMOD(worldTrans.GetZAxis());
- // Set velocity to zero (fix if using Doppler effect)
- attr.velocity = { 0.0f, 0.0f, 0.0f };
- event->set3DAttributes(&attr);
- }
- }
|