From e867722d9a99d62c703fc3bcc598b0951fa6926f Mon Sep 17 00:00:00 2001 From: Stiffly Date: Thu, 24 Sep 2015 22:55:24 +0200 Subject: [PATCH] Added logic for mastervolume. And a coresponding event. (sources need to be updated for it to be fully working) --- include/Sound/EMasterVolume.h | 27 ++++++++++++++++ include/Sound/EPlaySound.h | 2 +- include/Sound/SoundSystem.h | 5 ++- src/game/Sound/SoundSystem.cpp | 56 +++++++++++++++++++++++----------- 4 files changed, 71 insertions(+), 19 deletions(-) create mode 100644 include/Sound/EMasterVolume.h diff --git a/include/Sound/EMasterVolume.h b/include/Sound/EMasterVolume.h new file mode 100644 index 0000000..3f95e6d --- /dev/null +++ b/include/Sound/EMasterVolume.h @@ -0,0 +1,27 @@ +// +// Created by Adam on 2015-09-24. +// + +#ifndef EVENTS_EMASTERVOLUME_H__ +#define EVENTS_EMASTERVOLUME_H__ + +#include "Core/EventBroker.h" + +namespace dd +{ + +namespace Events +{ + +struct MasterVolume : public Event +{ + float gain = 1; + //To determine what channel group to apply the change to. + bool isAmbient = false; +}; + +} + +} + +#endif diff --git a/include/Sound/EPlaySound.h b/include/Sound/EPlaySound.h index ee51df0..a66300d 100644 --- a/include/Sound/EPlaySound.h +++ b/include/Sound/EPlaySound.h @@ -14,7 +14,7 @@ struct PlaySound : Event std::string path; float volume = 1.f; float pitch = 1.f; - bool loop = false; + bool isAmbient = false; }; } diff --git a/include/Sound/SoundSystem.h b/include/Sound/SoundSystem.h index af70ed6..3e56cef 100644 --- a/include/Sound/SoundSystem.h +++ b/include/Sound/SoundSystem.h @@ -10,6 +10,7 @@ #include "Sound.h" #include "Sound/EPlaySound.h" #include "Sound/EStopSound.h" +#include "Sound/EMasterVolume.h" #include "Physics/EContact.h" #include "Game/CBall.h" #include "Game/CBrick.h" @@ -38,9 +39,11 @@ private: dd::EventRelay m_EPlaySFX; dd::EventRelay m_EContact; dd::EventRelay m_EStopSound; + dd::EventRelay m_EMasterVolume; bool OnPlaySound(const dd::Events::PlaySound &event); bool OnContact(const dd::Events::Contact &event); bool OnStopSound(const dd::Events::StopSound &event); + bool OnMasterVolume(const dd::Events::MasterVolume &event); ALuint CreateSource(); @@ -48,7 +51,7 @@ private: ALCdevice* m_Device; - //Temp + float m_BGMMasterVolume, m_SFXMasterVolume; }; } diff --git a/src/game/Sound/SoundSystem.cpp b/src/game/Sound/SoundSystem.cpp index a003a19..b91d3dd 100644 --- a/src/game/Sound/SoundSystem.cpp +++ b/src/game/Sound/SoundSystem.cpp @@ -22,34 +22,45 @@ void dd::Systems::SoundSystem::Initialize() alGetError(); + //Probably unnecessary. vec3(0) probably default. const ALfloat pos[3] = {0, 0, 0}; - alListenerfv(AL_POSITION, pos); + //alListenerfv(AL_POSITION, pos); + + m_SFXMasterVolume = 1.f; + m_BGMMasterVolume = 1.f; //Subscribe to events EVENT_SUBSCRIBE_MEMBER(m_EContact, &SoundSystem::OnContact); EVENT_SUBSCRIBE_MEMBER(m_EPlaySFX, &SoundSystem::OnPlaySound); EVENT_SUBSCRIBE_MEMBER(m_EStopSound, &SoundSystem::OnStopSound); + EVENT_SUBSCRIBE_MEMBER(m_EMasterVolume, &SoundSystem::OnMasterVolume); + //Todo: Move this { dd::Events::PlaySound e; - e.path = "Sounds/BGM/soft-guitar.wav"; - e.loop = true; + e.path = "Sounds/BGM/under-the-sea-instrumental.wav"; + e.isAmbient = true; EventBroker->Publish(e); } { dd::Events::PlaySound e; e.path = "Sounds/BGM/water-flowing.wav"; e.volume = 0.3f; - e.loop = true; + e.isAmbient = true; + EventBroker->Publish(e); + } + { + dd::Events::MasterVolume e; + e.isAmbient = true; + e.gain = 0.001f; EventBroker->Publish(e); } } void dd::Systems::SoundSystem::Update(double dt) { - //LOG_INFO("Sources : %i", m_SourcesToBuffers.size()); - + //Clean up none-active sources std::vector deleteList; for (auto item : m_SourcesToBuffers) { ALint sourceState; @@ -64,7 +75,6 @@ void dd::Systems::SoundSystem::Update(double dt) //alDeleteBuffers(1, &m_SourcesToBuffers[deleteList[i]]); m_SourcesToBuffers.erase(deleteList[i]); } - } ALuint dd::Systems::SoundSystem::CreateSource() @@ -88,16 +98,20 @@ bool dd::Systems::SoundSystem::OnPlaySound(const dd::Events::PlaySound &event) alSourcei(source, AL_BUFFER, buffer); //Sound settings - alSourcef(source, AL_GAIN, event.volume); - alSourcef(source, AL_PITCH, event.pitch); - if (event.loop) { + float relativeVolume = 1.f; + if (event.isAmbient) { alSourcei(source, AL_LOOPING, AL_TRUE); + relativeVolume = m_BGMMasterVolume; } - else if (!event.loop) + else if (!event.isAmbient) { alSourcei(source, AL_LOOPING, AL_FALSE); + relativeVolume = m_SFXMasterVolume; } + alSourcef(source, AL_GAIN, (event.volume * relativeVolume)); + alSourcef(source, AL_PITCH, event.pitch); + //Play alSourcePlay(source); @@ -115,6 +129,18 @@ bool dd::Systems::SoundSystem::OnStopSound(const dd::Events::StopSound &event) return false; } +bool dd::Systems::SoundSystem::OnMasterVolume(const dd::Events::MasterVolume &event) +{ + if (event.isAmbient) { + m_BGMMasterVolume = event.gain; + } + else if (!event.isAmbient) { + m_SFXMasterVolume = event.gain; + } + m_World->GetEntities(); +} + +//On contact: play the sound given in the CCOllisionSound. bool dd::Systems::SoundSystem::OnContact(const dd::Events::Contact &event) { //Check which entity has the collisionSound component. @@ -127,16 +153,12 @@ bool dd::Systems::SoundSystem::OnContact(const dd::Events::Contact &event) } } - { - //dd::Events::StopSound e; - //e.path = "Sounds/BGM/soft-guitar.wav"; - //EventBroker->Publish(e); - } //Send play-sound event dd::Events::PlaySound e; e.path = collisionSound->filePath; + e.isAmbient = false; EventBroker->Publish(e); - return true; + //return true; }