From f72dea3852d461452482b1eefe9f25cf75e0cc2d Mon Sep 17 00:00:00 2001 From: stiffly Date: Wed, 24 Feb 2016 11:51:06 +0100 Subject: [PATCH] Added a system that will handle BGM. Created specific logic for sounds to loop like Petter wants. --- include/Engine/Sound/SoundManager.h | 37 +++++++++--------- include/Game/Systems/BackgroundMusicSystem.h | 21 ++++++++++ src/Engine/Sound/SoundManager.cpp | 40 +++++++++++++++++--- src/Game/Systems/BackgroundMusicSystem.cpp | 23 +++++++++++ 4 files changed, 98 insertions(+), 23 deletions(-) create mode 100644 include/Game/Systems/BackgroundMusicSystem.h create mode 100644 src/Game/Systems/BackgroundMusicSystem.cpp diff --git a/include/Engine/Sound/SoundManager.h b/include/Engine/Sound/SoundManager.h index 5b027c62..ec034510 100644 --- a/include/Engine/Sound/SoundManager.h +++ b/include/Engine/Sound/SoundManager.h @@ -9,25 +9,23 @@ #include "OpenAL/al.h" #include "OpenAL/alc.h" -#include "imgui/imgui.h" - -#include "Core/World.h" -#include "Core/EventBroker.h" +#include "../Engine/Core/World.h" +#include "../Engine/Core/EventBroker.h" #include "../Engine/Core/ResourceManager.h" #include "../Engine/Core/ConfigFile.h" -#include "Core/Transform.h" // Absolute transform -#include "Sound/Sound.h" +#include "../Engine/Core/Transform.h" +#include "../Engine/Sound/Sound.h" #include "../Engine/Sound/EPlayQueueOnEntity.h" -#include "Sound/EPlaySoundOnEntity.h" -#include "Sound/EPlaySoundOnPosition.h" -#include "Sound/EPlayBackgroundMusic.h" -#include "Sound/EPauseSound.h" -#include "Sound/EContinueSound.h" -#include "Sound/EStopSound.h" -#include "Sound/ESetBGMGain.h" -#include "Sound/ESetSFXGain.h" -#include "Core/EPause.h" -#include "Core/EComponentAttached.h" +#include "../Engine/Sound/EPlaySoundOnEntity.h" +#include "../Engine/Sound/EPlaySoundOnPosition.h" +#include "../Engine/Sound/EPlayBackgroundMusic.h" +#include "../Engine/Sound/EPauseSound.h" +#include "../Engine/Sound/EContinueSound.h" +#include "../Engine/Sound/EStopSound.h" +#include "../Engine/Sound/ESetBGMGain.h" +#include "../Engine/Sound/ESetSFXGain.h" +#include "../Engine/Core/EPause.h" +#include "../Engine/Core/EComponentAttached.h" #include "../Core/EPlayerSpawned.h" typedef std::pair> QueuedBuffers; @@ -43,6 +41,7 @@ struct Source Sound* SoundResource = nullptr; ALuint ALsource; SoundType Type; + float Duration; }; class SoundManager @@ -74,14 +73,18 @@ private: ALenum getSourceState(ALuint source); void setGain(Source* source, float gain); void setSoundProperties(Source* source, ComponentWrapper* soundComponent); + float getDurationSeconds(Source* source); + float getTimeOffsetSeconds(Source* source); // Specific logic void playSound(Source* source); - // Need to be the same format (sample rate etc) + // Needs to be the same format (sample rate etc) void playQueue(QueuedBuffers qb); void stopSound(Source* source); Source* createSource(std::string filePath); std::unordered_map m_Sources; + void mainMenuLoop(); + Source* m_CurrentBGM; // Logic World* m_World = nullptr; diff --git a/include/Game/Systems/BackgroundMusicSystem.h b/include/Game/Systems/BackgroundMusicSystem.h new file mode 100644 index 00000000..243b0f61 --- /dev/null +++ b/include/Game/Systems/BackgroundMusicSystem.h @@ -0,0 +1,21 @@ +#ifndef Systems_BackgroundMusicSystem_h__ +#define Systems_BackgroundMusicSystem_h__ + +#include "../Engine/Core/System.h" +#include "../Engine/Core/EventBroker.h" + +// Handles transitions between BGM's depending on the current state of the game. +class BackgroundMusicSystem : public PureSystem +{ +public: + BackgroundMusicSystem(SystemParams params); + ~BackgroundMusicSystem(); + void UpdateComponent(EntityWrapper& entity, ComponentWrapper& cEmitter, double dt) override; + +private: + void mainMenuLoop(); + + const std::string menu = "Audio/crosscounter.wav"; +}; + +#endif // ifndef diff --git a/src/Engine/Sound/SoundManager.cpp b/src/Engine/Sound/SoundManager.cpp index 8e103d5c..872017fe 100644 --- a/src/Engine/Sound/SoundManager.cpp +++ b/src/Engine/Sound/SoundManager.cpp @@ -56,13 +56,10 @@ void SoundManager::stopEmitters() void SoundManager::Update(double dt) { m_EventBroker->Process(); - deleteInactiveEmitters(); // can be optimized with "EEntityDeleted" + deleteInactiveEmitters(); updateEmitters(dt); updateListener(dt); - - // Editor debug info - ImGui::SliderFloat("BGM", &m_BGMVolumeChannel, 0.0f, 1.0f, "%.3f", 1.0f); - ImGui::SliderFloat("SFX", &m_SFXVolumeChannel, 0.0f, 1.0f, "%.3f", 1.0f); + mainMenuLoop(); } void SoundManager::deleteInactiveEmitters() @@ -166,9 +163,20 @@ Source* SoundManager::createSource(std::string filePath) Source* source = new Source(); source->ALsource = alSource; source->SoundResource = ResourceManager::Load(filePath); + source->Duration = getDurationSeconds(source); return source; } + +void SoundManager::mainMenuLoop() +{ + float time = getTimeOffsetSeconds(m_CurrentBGM); + if (m_CurrentBGM->Duration - time <= 5) { // 5 is hard coded value that Petter recommended + m_CurrentBGM = createSource("Audio/crosscounter.wav"); + playSound(m_CurrentBGM); + } +} + void SoundManager::playSound(Source* source) { alSourcei(source->ALsource, AL_BUFFER, source->SoundResource->Buffer()); @@ -338,12 +346,32 @@ void SoundManager::setSoundProperties(Source* source, ComponentWrapper* soundCom float gain = (source->Type == SoundType::SFX) ? m_SFXVolumeChannel : m_BGMVolumeChannel; alSourcef(source->ALsource, AL_GAIN, (float)(double)(*soundComponent)["Gain"] * gain); alSourcef(source->ALsource, AL_PITCH, (float)(double)(*soundComponent)["Pitch"]); - alSourcei(source->ALsource, AL_LOOPING, (int)(bool)(*soundComponent)["Loop"]); // YOLO + alSourcei(source->ALsource, AL_LOOPING, (int)(bool)(*soundComponent)["Loop"]); alSourcef(source->ALsource, AL_MAX_DISTANCE, (float)(double)(*soundComponent)["MaxDistance"]); alSourcef(source->ALsource, AL_ROLLOFF_FACTOR, (float)(double)(*soundComponent)["RollOffFactor"]); alSourcef(source->ALsource, AL_REFERENCE_DISTANCE, (float)(double)(*soundComponent)["ReferenceDistance"]); } +float SoundManager::getDurationSeconds(Source* source) +{ + ALuint buffer = source->SoundResource->Buffer(); + ALint sizeBytes, channels, bits, frequenzy; + alGetBufferi(buffer, AL_SIZE, &sizeBytes); + alGetBufferi(buffer, AL_CHANNELS, &channels); + alGetBufferi(buffer, AL_BITS, &bits); + alGetBufferi(buffer, AL_FREQUENCY, &frequenzy); + float sampleLength = (float)sizeBytes * 8 / (channels * bits); + return (sampleLength / frequenzy); +} + + +float SoundManager::getTimeOffsetSeconds(Source* source) +{ + float time; + alGetSourcef(source->ALsource, AL_SEC_OFFSET, &time); + return time; +} + void SoundManager::initOpenAL() { // Initialize OpenAL diff --git a/src/Game/Systems/BackgroundMusicSystem.cpp b/src/Game/Systems/BackgroundMusicSystem.cpp new file mode 100644 index 00000000..d37427b9 --- /dev/null +++ b/src/Game/Systems/BackgroundMusicSystem.cpp @@ -0,0 +1,23 @@ +#include "../Game/Systems/BackgroundMusicSystem.h" + +BackgroundMusicSystem::BackgroundMusicSystem(SystemParams params) + : System(params) + , PureSystem("SoundEmitter") +{ + +} + +BackgroundMusicSystem::~BackgroundMusicSystem() +{ + +} + +void BackgroundMusicSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& cEmitter, double dt) +{ + +} + +void BackgroundMusicSystem::mainMenuLoop() +{ + +}