From d7b5ae9f024254dd2bcaaca29cb4bd15f2c44439 Mon Sep 17 00:00:00 2001 From: Glence Date: Sat, 24 Sep 2022 19:13:23 +0800 Subject: [PATCH] audiosystem base level done along with its components --- SHADE_Application/premake5.lua | 3 +- .../src/Application/SBApplication.cpp | 8 ++ .../AudioSystem/SHAudioListenerComponent.cpp | 18 ++--- .../AudioSystem/SHAudioListenerComponent.h | 20 ++--- .../AudioSystem/SHAudioSourceComponent.cpp | 8 +- .../src/AudioSystem/SHAudioSourceComponent.h | 4 +- .../src/AudioSystem/SHAudioSystem.cpp | 76 ++++++++++--------- SHADE_Engine/src/AudioSystem/SHAudioSystem.h | 20 +++-- 8 files changed, 92 insertions(+), 65 deletions(-) diff --git a/SHADE_Application/premake5.lua b/SHADE_Application/premake5.lua index 50bd41b2..7e3675aa 100644 --- a/SHADE_Application/premake5.lua +++ b/SHADE_Application/premake5.lua @@ -33,7 +33,8 @@ project "SHADE_Application" "%{IncludeDir.spdlog}/include", "%{IncludeDir.VULKAN}/include", "%{IncludeDir.VMA}/include", - "%{IncludeDir.VULKAN}/Source/SPIRV-Reflect" + "%{IncludeDir.VULKAN}/Source/SPIRV-Reflect", + "%{IncludeDir.fmod}/include" } externalwarnings "Off" diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index 938b0b57..e689cabd 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -23,6 +23,9 @@ #include "Graphics/MiddleEnd/Interface/SHRenderable.h" #include "Scene/SHSceneManager.h" #include "Math/Transform/SHTransformSystem.h" +#include "AudioSystem/SHAudioSystem.h" +#include "AudioSystem/SHAudioSourceComponent.h" +#include "AudioSystem/SHAudioListenerComponent.h" #include "Scenes/SBTestScene.h" @@ -51,6 +54,7 @@ namespace Sandbox SHADE::SHSystemManager::CreateSystem(); SHADE::SHSystemManager::CreateSystem(); SHADE::SHGraphicsSystem* graphicsSystem = static_cast(SHADE::SHSystemManager::GetSystem()); + SHADE::SHSystemManager::CreateSystem(); // Create Routines SHADE::SHSystemManager::RegisterRoutine(); @@ -69,6 +73,10 @@ namespace Sandbox SHADE::SHSystemManager::RegisterRoutine(); SHADE::SHComponentManager::CreateComponentSparseSet(); + SHADE::SHSystemManager::RegisterRoutine(); + SHADE::SHComponentManager::CreateComponentSparseSet(); + SHADE::SHComponentManager::CreateComponentSparseSet(); + // Set up graphics system and windows graphicsSystem->SetWindow(&window); sdlWindow = SDL_CreateWindowFrom(window.GetHWND()); diff --git a/SHADE_Engine/src/AudioSystem/SHAudioListenerComponent.cpp b/SHADE_Engine/src/AudioSystem/SHAudioListenerComponent.cpp index c496ff07..342d4702 100644 --- a/SHADE_Engine/src/AudioSystem/SHAudioListenerComponent.cpp +++ b/SHADE_Engine/src/AudioSystem/SHAudioListenerComponent.cpp @@ -10,46 +10,46 @@ #include "SHpch.h" #include "SHAudioListenerComponent.h" -#include "ECS_Base/System/SHSystemManager.h" +//#include "ECS_Base/System/SHSystemManager.h" namespace SHADE { - const SHMathVec3f SHAudioListenerComponent::GetPos() const + const SHVec3 SHAudioListenerComponent::GetPos() const { return pos; } - const SHMathVec3f SHAudioListenerComponent::GetVel() const + const SHVec3 SHAudioListenerComponent::GetVel() const { return vel; } - const SHMathVec3f SHAudioListenerComponent::GetForward() const + const SHVec3 SHAudioListenerComponent::GetForward() const { return forward; } - const SHMathVec3f SHAudioListenerComponent::GetUp() const + const SHVec3 SHAudioListenerComponent::GetUp() const { return up; } - void SHAudioListenerComponent::SetPos(SHMathVec3f p) + void SHAudioListenerComponent::SetPos(SHVec3 p) { pos = p; } - void SHAudioListenerComponent::SetVel(SHMathVec3f v) + void SHAudioListenerComponent::SetVel(SHVec3 v) { vel = v; } - void SHAudioListenerComponent::SetForward(SHMathVec3f f) + void SHAudioListenerComponent::SetForward(SHVec3 f) { forward = f; } - void SHAudioListenerComponent::SetUp(SHMathVec3f u) + void SHAudioListenerComponent::SetUp(SHVec3 u) { up = u; } diff --git a/SHADE_Engine/src/AudioSystem/SHAudioListenerComponent.h b/SHADE_Engine/src/AudioSystem/SHAudioListenerComponent.h index 4b024e79..d0e39bdc 100644 --- a/SHADE_Engine/src/AudioSystem/SHAudioListenerComponent.h +++ b/SHADE_Engine/src/AudioSystem/SHAudioListenerComponent.h @@ -11,7 +11,7 @@ #pragma once #include "ECS_Base/Components/SHComponent.h" -#include "Math/SHMathVec.h" +#include "Math/SHMath.h" namespace SHADE { @@ -23,20 +23,20 @@ namespace SHADE SHAudioListenerComponent() = default; ~SHAudioListenerComponent() = default; - const SHMathVec3f GetPos() const; - const SHMathVec3f GetVel() const; - const SHMathVec3f GetForward() const; - const SHMathVec3f GetUp() const; + const SHVec3 GetPos() const; + const SHVec3 GetVel() const; + const SHVec3 GetForward() const; + const SHVec3 GetUp() const; - void SetPos(SHMathVec3f p); - void SetVel(SHMathVec3f v); - void SetForward(SHMathVec3f f); - void SetUp(SHMathVec3f u); + void SetPos(SHVec3 p); + void SetVel(SHVec3 v); + void SetForward(SHVec3 f); + void SetUp(SHVec3 u); private: - SHMathVec3f pos{}, vel{}, forward{}, up{ 0.f,1.f,0.f }; + SHVec3 pos{}, vel{}, forward{}, up{ 0.f,1.f,0.f }; }; }//namespace SHADE diff --git a/SHADE_Engine/src/AudioSystem/SHAudioSourceComponent.cpp b/SHADE_Engine/src/AudioSystem/SHAudioSourceComponent.cpp index 9d216439..891ea1e1 100644 --- a/SHADE_Engine/src/AudioSystem/SHAudioSourceComponent.cpp +++ b/SHADE_Engine/src/AudioSystem/SHAudioSourceComponent.cpp @@ -20,9 +20,9 @@ namespace SHADE * * @param index where the sound is in the index */ - void SHAudioSourceComponent::PlaySoundSFX(EntityID id, bool loop, bool spatial) + void SHAudioSourceComponent::PlaySoundSFX(EntityID id, bool loop, bool spatial, float min , float max ) { - SHSystemManager::GetSystem()->PlaySFX(id, GetEID(),loop,spatial); + SHSystemManager::GetSystem()->PlaySFX(id, GetEID(),loop,spatial, min, max); } //void SHAudioSourceComponent::PlayOneShot(const char* path, bool isSFX, bool spatial) @@ -46,9 +46,9 @@ namespace SHADE // } //} - void SHAudioSourceComponent::PlaySoundBGM(EntityID id, bool loop, bool spatial) + void SHAudioSourceComponent::PlaySoundBGM(EntityID id, bool loop, bool spatial, float min, float max) { - SHSystemManager::GetSystem()->PlayBGM(id, GetEID(), loop, spatial); + SHSystemManager::GetSystem()->PlayBGM(id, GetEID(), loop, spatial, min, max); } /** diff --git a/SHADE_Engine/src/AudioSystem/SHAudioSourceComponent.h b/SHADE_Engine/src/AudioSystem/SHAudioSourceComponent.h index 98459b7e..987dc3d4 100644 --- a/SHADE_Engine/src/AudioSystem/SHAudioSourceComponent.h +++ b/SHADE_Engine/src/AudioSystem/SHAudioSourceComponent.h @@ -35,9 +35,9 @@ namespace SHADE * * @param index where the sound is in the index */ - void PlaySoundSFX(EntityID id, bool loop = false, bool spatial = false); + void PlaySoundSFX(EntityID id, bool loop = false, bool spatial = false, float min = 5.0f, float max = 1000.f); - void PlaySoundBGM(EntityID id, bool loop = false, bool spatial = false); + void PlaySoundBGM(EntityID id, bool loop = false, bool spatial = false, float min = 5.0f, float max = 1000.f); /** * @brief Stop the sound diff --git a/SHADE_Engine/src/AudioSystem/SHAudioSystem.cpp b/SHADE_Engine/src/AudioSystem/SHAudioSystem.cpp index 43301457..c0ad46cd 100644 --- a/SHADE_Engine/src/AudioSystem/SHAudioSystem.cpp +++ b/SHADE_Engine/src/AudioSystem/SHAudioSystem.cpp @@ -3,27 +3,25 @@ #include "SHAudioSystem.h" #include "Scene/SHSceneManager.h" #include "ECS_Base/Managers/SHComponentManager.h" +#include "ECS_Base/Managers/SHSystemManager.h" #include "ECS_Base/Managers/SHEntityManager.h" #include -//#include "Components/SHAudioListenerComponent.h" -//#include "Components/SHAudioSourceComponent.h" +#include "AudioSystem/SHAudioListenerComponent.h" +#include "AudioSystem/SHAudioSourceComponent.h" //#include "ECS_Base/System/SHComponentManager.h" //#include "Resource Manager/SHResourceManager.h" #include "Math/Transform/SHTransformComponent.h" #pragma warning(push) #pragma warning(disable:26812) //disable warning about preference of enum class over enum as ImGuizmo uses enums -//#include "fmod_errors.h" -//#include "fmod.hpp" -//#include "fmod_studio.hpp" #include #include #include namespace SHADE { SHAudioSystem::SHAudioSystem() - : system(nullptr) + : fmodStudioSystem(nullptr) , extraDriverData(nullptr) , soundList() , bgmChannelGroup(nullptr) @@ -38,7 +36,7 @@ namespace SHADE , speakerMode(FMOD_SPEAKERMODE_5POINT1) , paused(false) { - result = FMOD::Studio::System::create(&system); + result = FMOD::Studio::System::create(&fmodStudioSystem); ErrorCheck(); } @@ -49,11 +47,11 @@ namespace SHADE void SHADE::SHAudioSystem::Init() { - //SHComponentManager::CreateComponentSparseSet(); - //SHComponentManager::CreateComponentSparseSet(); + SHADE::SHComponentManager::CreateComponentSparseSet(); + SHADE::SHComponentManager::CreateComponentSparseSet(); denseListener = &SHComponentManager::GetDense(); - system->getCoreSystem(&coreSystem); + fmodStudioSystem->getCoreSystem(&fmodSystem); //result = coreSystem->getVersion(&version); //ErrorCheck(); //if (version < FMOD_VERSION) @@ -62,18 +60,18 @@ namespace SHADE // std::cerr << "FMOD version mismatch." << std::endl; //} - result = system->initialize(AUDIO_SYS_MAX_CHANNELS, AUDIO_SYS_MAX_CHANNELS, FMOD_STUDIO_INIT_NORMAL, extraDriverData); + result = fmodStudioSystem->initialize(AUDIO_SYS_MAX_CHANNELS, AUDIO_SYS_MAX_CHANNELS, FMOD_STUDIO_INIT_NORMAL, extraDriverData); ErrorCheck(); - coreSystem->setSoftwareFormat(0, speakerMode, 0); + fmodSystem->setSoftwareFormat(0, speakerMode, 0); - result = coreSystem->createChannelGroup("SFX", &sfxChannelGroup); + result = fmodSystem->createChannelGroup("SFX", &sfxChannelGroup); ErrorCheck(); - result = coreSystem->createChannelGroup("BGM", &bgmChannelGroup); + result = fmodSystem->createChannelGroup("BGM", &bgmChannelGroup); ErrorCheck(); - result = coreSystem->getMasterChannelGroup(&masterGroup); + result = fmodSystem->getMasterChannelGroup(&masterGroup); ErrorCheck(); result = masterGroup->addGroup(bgmChannelGroup); @@ -87,7 +85,7 @@ namespace SHADE sfxChannelGroup->setVolume(sfxVolume); masterGroup->setVolume(masterVolume); - SHResourceManager::LoadAllAudio(system, soundList); + //SHResourceManager::LoadAllAudio(system, soundList); //TODO: Move to SHResourceManager (LoadAllAudio to load all .bank files and store SHBanks in bankMap) //result = system->loadBankFile("resources/audio/banks/Master.bank", FMOD_STUDIO_LOAD_BANK_NORMAL, &masterBank); @@ -110,23 +108,33 @@ namespace SHADE void SHADE::SHAudioSystem::Run(float dt) { static_cast(dt); - system->update(); + fmodStudioSystem->update(); if (!denseListener->empty()) { SHAudioListenerComponent& listener = denseListener->at(0); SHTransformComponent* listenerTransform = SHComponentManager::GetComponent_s(listener.GetEID()); - listener.SetPos(listenerTransform->GetLocalPosition()); - listener.SetForward({ (listenerTransform->GetScale()[0] > 0.f) ? 1.f : -1.f, 0.f, 0.f }); - FMOD_VECTOR pos = { listener.pos[0] ,listener.pos[1] ,0.f }; - FMOD_VECTOR forward = { listener.forward[0] ,listener.forward[1] ,listener.forward[2] }; - FMOD_VECTOR up = { listener.up[0] ,listener.up[1] ,listener.up[2] }; - coreSystem->set3DListenerAttributes(0, &pos, nullptr, &forward, &up); + if (listenerTransform) + { + listener.SetPos(listenerTransform->GetLocalPosition()); + listener.SetForward({ (listenerTransform->GetLocalScale()[0] > 0.f) ? 1.f : -1.f, 0.f, 0.f }); + FMOD_VECTOR pos = { listener.pos[0] ,listener.pos[1] ,0.f }; + FMOD_VECTOR forward = { listener.forward[0] ,listener.forward[1] ,listener.forward[2] }; + FMOD_VECTOR up = { listener.up[0] ,listener.up[1] ,listener.up[2] }; + fmodSystem->set3DListenerAttributes(0, &pos, nullptr, &forward, &up); + } } //int channels; //system->getChannelsPlaying(&channels); //std::cout << "ACTIVE CHANNELS: " << channels << std::endl; } + SHAudioSystem::AudioRoutine::AudioRoutine() + : SHSystemRoutine("Audio Routine", false) {} + void SHAudioSystem::AudioRoutine::Execute(double dt) noexcept + { + reinterpret_cast(system)->Run(dt); + } + void SHADE::SHAudioSystem::Exit() { for (auto& event : eventMap) @@ -161,7 +169,7 @@ namespace SHADE if (system) { - result = system->release(); + result = fmodStudioSystem->release(); ErrorCheck(); } } @@ -209,7 +217,7 @@ namespace SHADE } } - void SHAudioSystem::PlaySFX(ResourceID id, EntityID eid, const bool& loop, const bool& spatial, float min = 5.0f, float max = 1000.0f) + void SHAudioSystem::PlaySFX(EntityID id, EntityID eid, const bool& loop, const bool& spatial, float min, float max) { SHSound sound = soundList[id]; int index = GetAvailableChannelIndex(); @@ -219,7 +227,7 @@ namespace SHADE mode |= (loop ? FMOD_LOOP_NORMAL : FMOD_LOOP_OFF); mode |= (spatial ? FMOD_3D : FMOD_2D); sound->setMode(mode); - result = coreSystem->playSound(sound, sfxChannelGroup, false, &audioChannels[index]); + result = fmodSystem->playSound(sound, sfxChannelGroup, false, &audioChannels[index]); if (spatial && SHComponentManager::HasComponent(eid)) { SHTransformComponent* audioTransform = SHComponentManager::GetComponent_s(eid); @@ -232,7 +240,7 @@ namespace SHADE } } - void SHAudioSystem::PlayBGM(ResourceID id, EntityID eid, const bool& loop, const bool& spatial, float min = 5.0f, float max = 1000.0f) + void SHAudioSystem::PlayBGM(EntityID id, EntityID eid, const bool& loop, const bool& spatial, float min, float max) { SHSound sound = soundList[id]; int index = GetAvailableChannelIndex(); @@ -242,7 +250,7 @@ namespace SHADE mode |= (loop ? FMOD_LOOP_NORMAL : FMOD_LOOP_OFF); mode |= (spatial ? FMOD_3D : FMOD_2D); sound->setMode(mode); - result = coreSystem->playSound(sound, bgmChannelGroup, false, &audioChannels[index]); + result = fmodSystem->playSound(sound, bgmChannelGroup, false, &audioChannels[index]); if (spatial && SHComponentManager::HasComponent(eid)) { SHTransformComponent* audioTransform = SHComponentManager::GetComponent_s(eid); @@ -255,7 +263,7 @@ namespace SHADE } } - void SHAudioSystem::SetMute(ResourceID id, bool mute) + void SHAudioSystem::SetMute(EntityID id, bool mute) { SHSound sound; for (auto& channel : audioChannels) @@ -268,7 +276,7 @@ namespace SHADE } } - void SHAudioSystem::StopSound(ResourceID id) + void SHAudioSystem::StopSound(EntityID id) { SHSound sound; for (auto& channel : audioChannels) @@ -295,7 +303,7 @@ namespace SHADE { FMOD_GUID guid; FMOD::Studio::EventDescription* event; - result = system->getEvent(path, &event); + result = fmodStudioSystem->getEvent(path, &event); ErrorCheck(); if (result == FMOD_OK) { @@ -486,7 +494,7 @@ namespace SHADE if (!instance) return; instance->start(); - auto audioSystem = dynamic_cast(SHSystemManager::GetSystem("Audio")); + auto audioSystem = SHSystemManager::GetSystem(); instance->setVolume(audioSystem->GetMasterVolume() * (isSfx ? audioSystem->GetSfxVolume() : audioSystem->GetBgmVolume())); } @@ -499,12 +507,12 @@ namespace SHADE attributes.forward.z = 1.0f; attributes.up.y = 1.0f; - SHVec3 listenerPos = dynamic_cast(SHSystemManager::GetSystem("Audio"))->GetListenerPosition(); + auto audioSystem = SHSystemManager::GetSystem(); + SHVec3 listenerPos = audioSystem->GetListenerPosition(); attributes.position.x = position[0]; attributes.position.y = position[1]; attributes.position.z = listenerPos[2]; instance->set3DAttributes(&attributes); - auto audioSystem = dynamic_cast(SHSystemManager::GetSystem("Audio")); instance->setVolume(audioSystem->GetMasterVolume() * (isSfx ? audioSystem->GetSfxVolume() : audioSystem->GetBgmVolume())); } diff --git a/SHADE_Engine/src/AudioSystem/SHAudioSystem.h b/SHADE_Engine/src/AudioSystem/SHAudioSystem.h index f459d57c..5fc29b7c 100644 --- a/SHADE_Engine/src/AudioSystem/SHAudioSystem.h +++ b/SHADE_Engine/src/AudioSystem/SHAudioSystem.h @@ -4,11 +4,13 @@ #include #include #include -#include #include "ECS_Base/System/SHSystem.h" +#include "ECS_Base/System/SHSystemRoutine.h" #include "ECS_Base/SHECSMacros.h" #include "Math/SHMath.h" #include +#include +#include "SH_API.h" #define AUDIO_SYS_MAX_CHANNELS 1024 namespace SHADE @@ -17,7 +19,9 @@ namespace SHADE typedef FMOD::Studio::Bank* SHBank; class SHAudioListenerComponent; + typedef uint64_t AudioClipID; + class AudioClip { public: @@ -39,7 +43,7 @@ namespace SHADE AudioClipID id; }; - class SHAudioSystem : public SHSystem + class SH_API SHAudioSystem : public SHSystem { public: SHAudioSystem(); @@ -47,6 +51,12 @@ namespace SHADE void Init(); void Run(float dt); + class SH_API AudioRoutine final : public SHSystemRoutine + { + public: + AudioRoutine(); + void Execute(double dt) noexcept override final; + }; void Exit(); int GetAvailableChannelIndex(); @@ -71,12 +81,12 @@ namespace SHADE void SetPaused(bool pause); bool GetPaused() const; SHVec3 GetListenerPosition(); - void LoadBank(const char* path); + //void LoadBank(const char* path); private: + FMOD::Studio::System* fmodStudioSystem; + FMOD::System* fmodSystem; bool paused; void ErrorCheck() const; - FMOD::Studio::System* system; - FMOD::System* coreSystem; void* extraDriverData; std::unordered_map soundList;