From 3cce9f80751d5bc2da692d9570e511e0f92f5967 Mon Sep 17 00:00:00 2001 From: stiffly Date: Fri, 8 Jan 2016 15:29:20 +0100 Subject: [PATCH] Using absolute transform --- include/Engine/Sound/SoundSystem.h | 28 ++--- resources/Schema/Components/SoundEmitter.xml | 1 + resources/Schema/Components/SoundEmitter.xsd | 1 + src/Engine/Sound/SoundSystem.cpp | 116 ++++++++++++++++--- 4 files changed, 111 insertions(+), 35 deletions(-) diff --git a/include/Engine/Sound/SoundSystem.h b/include/Engine/Sound/SoundSystem.h index de31e216..5a8225ef 100644 --- a/include/Engine/Sound/SoundSystem.h +++ b/include/Engine/Sound/SoundSystem.h @@ -10,6 +10,7 @@ #include "Core/World.h" #include "Core/EventBroker.h" +#include "Rendering/RenderQueueFactory.h" // Absolute transform #include "Sound/Sound.h" #include "Sound/EPlaySound.h" @@ -27,35 +28,27 @@ public: ~SoundSystem(); void Update(); // Update emitters private: - // Private setters and getters for working with glm + // Help functions for working with OpenaAL void setListenerPos(glm::vec3 pos) { alListener3f(AL_POSITION, pos.x, pos.y, pos.z); }; glm::vec3 listnerPos() { glm::vec3 pos; alGetListener3f(AL_POSITION, &pos.x, &pos.y, &pos.z); return pos; }; void setListenerVel(glm::vec3 vel) { alListener3f(AL_VELOCITY, vel.x, vel.y, vel.z); }; glm::vec3 listenerVel() { glm::vec3 vel; alGetListener3f(AL_VELOCITY, &vel.x, &vel.y, &vel.z); return vel; }; - void setListenerOri(glm::vec3 ori) - { - glm::vec3 forward = glm::vec3(0.0, 0.0, -1.0); - forward = glm::rotateX(forward, ori.x); - forward = glm::rotateY(forward, ori.y); - forward = glm::rotateZ(forward, ori.z); - glm::normalize(forward); - glm::vec3 up = glm::vec3(0.0, 1.0, 0.0); - up = glm::rotateX(up, ori.x); - up = glm::rotateY(up, ori.y); - up = glm::rotateZ(up, ori.z); - glm::normalize(up); - ALfloat lori[6] = { forward.x, forward.y, forward.z , up.x, up.y, up.z }; - alListenerfv(AL_ORIENTATION, lori); - }; + void setListenerOri(glm::vec3 ori); glm::vec3 listenerOri() { glm::vec3 ori; alGetListener3f(AL_ORIENTATION, &ori.x, &ori.y, &ori.z); return ori; }; void setSourcePos(ALuint source, glm::vec3 pos) { ALfloat spos[3] = { pos.x, pos.y, pos.z }; alSourcefv(source, AL_POSITION, spos); }; void setSourceVel(ALuint source, glm::vec3 vel) { ALfloat svel[3] = { vel.x, vel.y, vel.z }; alSourcefv(source, AL_VELOCITY, svel); }; - void setSourceOri(ALuint source, glm::vec3 ori) { ALfloat sori[3] = { ori.x, ori.y, ori.z }; alSourcefv(source, AL_ORIENTATION, sori); }; + + bool isPlaying(ALuint source); // Logic World* m_World; EventBroker* m_EventBroker; + void initOpenAL(); + void updateEmitters(); + void updateListener(); + void deleteInactiveEmitters(); + void addNewEmitters(); ALuint createSource(); void playSound(Source source); void stopSound(Source source); @@ -71,7 +64,6 @@ private: // Events EventRelay m_EPlaySound; bool OnPlaySound(const Events::PlaySound &e); - }; #endif \ No newline at end of file diff --git a/resources/Schema/Components/SoundEmitter.xml b/resources/Schema/Components/SoundEmitter.xml index a032dfbb..cbcfc440 100644 --- a/resources/Schema/Components/SoundEmitter.xml +++ b/resources/Schema/Components/SoundEmitter.xml @@ -1,4 +1,5 @@ + Audio/crosscounter.wav 1.0 1.0 20.0 diff --git a/resources/Schema/Components/SoundEmitter.xsd b/resources/Schema/Components/SoundEmitter.xsd index 6bebcac5..31c18ce9 100644 --- a/resources/Schema/Components/SoundEmitter.xsd +++ b/resources/Schema/Components/SoundEmitter.xsd @@ -6,6 +6,7 @@ + The "volume" of the emitter. A value betweeen 0-1 diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index 64312fa8..fd379d50 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -4,6 +4,17 @@ SoundSystem::SoundSystem(World* world, EventBroker* eventBroker) { m_EventBroker = eventBroker; m_World = world; + + initOpenAL(); + + alSpeedOfSound(340.29f); // Speed of sound + alDistanceModel(AL_INVERSE_DISTANCE); + + EVENT_SUBSCRIBE_MEMBER(m_EPlaySound, &SoundSystem::OnPlaySound); +} + +void SoundSystem::initOpenAL() +{ // Initialize OpenAL m_ALCdevice = alcOpenDevice(nullptr); if (m_ALCdevice != nullptr) { @@ -12,11 +23,6 @@ SoundSystem::SoundSystem(World* world, EventBroker* eventBroker) } else { LOG_ERROR("OpenAL failed to initialize."); } - - alSpeedOfSound(340.29f); // Speed of sound - alDistanceModel(AL_INVERSE_DISTANCE); - - EVENT_SUBSCRIBE_MEMBER(m_EPlaySound, &SoundSystem::OnPlaySound); } SoundSystem::~SoundSystem() @@ -28,14 +34,67 @@ SoundSystem::~SoundSystem() } void SoundSystem::Update() +{ + //deleteInactiveEmitters(); // Not tested + addNewEmitters(); + updateEmitters(); + updateListener(); +} + +void SoundSystem::deleteInactiveEmitters() +{ + auto emitterComponents = m_World->GetComponents("SoundEmitter"); + for (auto it = emitterComponents->begin(); it != emitterComponents->end(); it++) { + EntityID emitter = (*it).EntityID; + if (isPlaying(m_Sources[emitter].ALsource)) { // The sound is still playing, do not remove + continue; + } + alDeleteBuffers(1, &m_Sources[emitter].ALsource); + delete m_Sources[emitter].SoundResource; + m_Sources.erase(emitter); + } +} + +void SoundSystem::addNewEmitters() +{ + auto emitterComponents = m_World->GetComponents("SoundEmitter"); + for (auto it = emitterComponents->begin(); it != emitterComponents->end(); it++) { + EntityID emitter = (*it).EntityID; + std::unordered_map::iterator i; + i = m_Sources.find(emitter); + if (i == m_Sources.end()) { // Did not exist, add it + Source source; + source.ALsource = createSource(); + source.SoundResource = ResourceManager::Load((std::string)(*it)["FilePath"]); + m_Sources[emitter] = source; + } + } +} + +void SoundSystem::updateEmitters() +{ + auto emitterComponents = m_World->GetComponents("SoundEmitter"); + for (auto it = emitterComponents->begin(); it != emitterComponents->end(); it++) { + EntityID emitter = (*it).EntityID; + std::unordered_map::iterator i; + i = m_Sources.find(emitter); + if (i != m_Sources.end()) { + setSourcePos(m_Sources[emitter].ALsource, RenderQueueFactory::AbsolutePosition(m_World, emitter)); + } + // No orientation, emitts in all directions + // Velocity for doppler effect + } +} + +void SoundSystem::updateListener() { // Should only be one listener. auto listenerComponents = m_World->GetComponents("Listener"); for (auto it = listenerComponents->begin(); it != listenerComponents->end(); it++) { EntityID listener = (*it).EntityID; - auto transform = m_World->GetComponent(listener, "Transform"); - setListenerPos(transform["Position"]); - setListenerOri(transform["Orientation"]); + setListenerPos(RenderQueueFactory::AbsolutePosition(m_World, listener)); + setListenerOri(glm::eulerAngles(RenderQueueFactory::AbsoluteOrientation(m_World, listener))); + // Velocity for doppler effect } } @@ -62,15 +121,38 @@ void SoundSystem::stopEmitter(EntityID emitter) bool SoundSystem::OnPlaySound(const Events::PlaySound & e) { - Sound *sound = ResourceManager::Load(e.FilePath); - if (sound == nullptr) { - return false; - } - ALuint source = createSource(); - Source sauce; - sauce.ALsource = source; - sauce.SoundResource = sound; - playSound(sauce); + //Sound *sound = ResourceManager::Load(e.FilePath); + //if (sound == nullptr) { + // return false; + //} + //ALuint source = createSource(); + //Source sauce; + //sauce.ALsource = source; + //sauce.SoundResource = sound; + //playSound(sauce); return false; } +void SoundSystem::setListenerOri(glm::vec3 ori) +{ + glm::vec3 forward = glm::vec3(0.0, 0.0, -1.0); + forward = glm::rotateX(forward, ori.x); + forward = glm::rotateY(forward, ori.y); + forward = glm::rotateZ(forward, ori.z); + glm::normalize(forward); + glm::vec3 up = glm::vec3(0.0, 1.0, 0.0); + up = glm::rotateX(up, ori.x); + up = glm::rotateY(up, ori.y); + up = glm::rotateZ(up, ori.z); + glm::normalize(up); + ALfloat lOri[6] = { forward.x, forward.y, forward.z, up.x, up.y, up.z }; + alListenerfv(AL_ORIENTATION, lOri); +} + +bool SoundSystem::isPlaying(ALuint source) +{ + ALenum state; + alGetSourcei(source, AL_SOURCE_STATE, &state); + return (state == AL_PLAYING); +} +