From 50d7ad80a2563a0058bfb6d1e65465a2f550ea42 Mon Sep 17 00:00:00 2001 From: SHAM-DP Date: Mon, 27 Feb 2023 22:02:48 +0800 Subject: [PATCH 1/3] Added Spatial Audio --- .../src/AudioSystem/SHAudioSystem.cpp | 60 +++++++++++++++---- SHADE_Engine/src/AudioSystem/SHAudioSystem.h | 2 +- SHADE_Engine/src/Common/SHAllComponents.h | 3 +- .../Inspector/SHEditorInspector.cpp | 9 ++- .../src/Serialization/SHSerialization.cpp | 3 + 5 files changed, 60 insertions(+), 17 deletions(-) diff --git a/SHADE_Engine/src/AudioSystem/SHAudioSystem.cpp b/SHADE_Engine/src/AudioSystem/SHAudioSystem.cpp index a7245123..e5e062a2 100644 --- a/SHADE_Engine/src/AudioSystem/SHAudioSystem.cpp +++ b/SHADE_Engine/src/AudioSystem/SHAudioSystem.cpp @@ -16,6 +16,7 @@ #include #include #include +#include "Camera/SHCameraSystem.h" const std::string AUDIO_FOLDER_PATH{ std::string(ASSET_ROOT)+ "/Audio/" }; @@ -53,8 +54,9 @@ namespace SHADE denseListener = &SHComponentManager::GetDense(); fmodStudioSystem->getCoreSystem(&fmodSystem); - - result = fmodStudioSystem->initialize(AUDIO_SYS_MAX_CHANNELS, AUDIO_SYS_MAX_CHANNELS, FMOD_STUDIO_INIT_NORMAL, extraDriverData); + fmodSystem->setSoftwareFormat(0, FMOD_SPEAKERMODE_5POINT1, 0); + result = fmodStudioSystem->initialize(AUDIO_SYS_MAX_CHANNELS, AUDIO_SYS_MAX_CHANNELS, FMOD_STUDIO_INIT_NORMAL | FMOD_INIT_NORMAL, extraDriverData); + ErrorCheck(); fmodSystem->setSoftwareFormat(0, speakerMode, 0); @@ -119,18 +121,44 @@ namespace SHADE // PlayEventOnce("event:/Characters/sfx_footsteps_raccoon"); fmodStudioSystem->update(); - if (!denseListener->empty()) + //int listenerID = 0; + //for(auto& listener : *denseListener) + //{ + // if(!listener.isActive) + // continue; + // //SHAudioListenerComponent& listener = denseListener->at(0); //Loop through dense + // SHTransformComponent* listenerTransform = SHComponentManager::GetComponent_s(listener.GetEID()); + // if (listenerTransform) + // { + // listener.SetPos(listenerTransform->GetWorldPosition()); + // SHQuaternion worldOrientation = listenerTransform->GetWorldOrientation(); + // SHVec3 orientatedFoward = (worldOrientation * SHQuaternion::FromEuler(SHVec3::Forward) * SHQuaternion::Conjugate(worldOrientation)).ToEuler(); + // SHVec3 orientatedUp = (worldOrientation * SHQuaternion::FromEuler(SHVec3::Up) * SHQuaternion::Conjugate(worldOrientation)).ToEuler(); + // listener.SetForward(orientatedFoward); + // listener.SetUp(orientatedUp); + + // FMOD_3D_ATTRIBUTES attribs{ { 0 } }; + // attribs.position = { listener.pos.x, listener.pos.y, listener.pos.z }; + // attribs.forward = { orientatedFoward.x, orientatedFoward.y, orientatedFoward.z }; + // attribs.up = { orientatedUp.x, orientatedUp.y, orientatedUp.z }; + // + // fmodStudioSystem->setListenerAttributes(listenerID++, &attribs); + // } + //} + + if(auto camSystem = SHSystemManager::GetSystem()) { - SHAudioListenerComponent& listener = denseListener->at(0); - SHTransformComponent* listenerTransform = SHComponentManager::GetComponent_s(listener.GetEID()); - if (listenerTransform) + auto mainCamEID = camSystem->GetMainCameraEID(); + if(auto camComponent = SHComponentManager::GetComponent_s(mainCamEID)) { - listener.SetPos(listenerTransform->GetWorldPosition()); // TODO: Clean up listener - listener.SetForward({ (listenerTransform->GetLocalScale()[0] > 0.f) ? 1.f : -1.f, 0.f, 0.f }); //TODO: USE CORRECT FORWARD - 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); + FMOD_3D_ATTRIBUTES attribs{ { 0 } }; + SHVec3 pos = camComponent->GetPosition(); + SHVec3 forward, up, right; + camSystem->GetCameraAxis(*camComponent, forward, right, up); + attribs.position = { pos.x, pos.y, pos.z }; + attribs.forward = { forward.x, forward.y, forward.z }; + attribs.up = { up.x, up.y, up.z }; + fmodStudioSystem->setListenerAttributes(0, &attribs); } } @@ -141,9 +169,15 @@ namespace SHADE { if(SHTransformComponent* transformComponent = SHComponentManager::GetComponent_s(it->transformRef)) { - FMOD_3D_ATTRIBUTES attribs{}; //TODO: Set other attribs + FMOD_3D_ATTRIBUTES attribs{ { 0 } }; //TODO: Set other attribs auto pos = transformComponent->GetWorldPosition(); + SHQuaternion worldOrientation = transformComponent->GetWorldOrientation(); + SHVec3 orientatedFoward = (worldOrientation * SHQuaternion::FromEuler(SHVec3::Forward) * SHQuaternion::Conjugate(worldOrientation)).ToEuler(); + SHVec3 orientatedUp = (worldOrientation * SHQuaternion::FromEuler(SHVec3::Up) * SHQuaternion::Conjugate(worldOrientation)).ToEuler(); + attribs.position = {pos.x, pos.y, pos.z}; + attribs.forward = {orientatedFoward.x, orientatedFoward.y, orientatedFoward.z}; + attribs.up = {orientatedUp.x, orientatedUp.y, orientatedUp.z}; it->instance->set3DAttributes(&attribs); } } diff --git a/SHADE_Engine/src/AudioSystem/SHAudioSystem.h b/SHADE_Engine/src/AudioSystem/SHAudioSystem.h index 5714a618..68942a8e 100644 --- a/SHADE_Engine/src/AudioSystem/SHAudioSystem.h +++ b/SHADE_Engine/src/AudioSystem/SHAudioSystem.h @@ -22,7 +22,7 @@ namespace SHADE typedef FMOD::Studio::Bank* SHBank; class SHAudioListenerComponent; - + class SH_API AudioClip { public: diff --git a/SHADE_Engine/src/Common/SHAllComponents.h b/SHADE_Engine/src/Common/SHAllComponents.h index 9d280677..36b74853 100644 --- a/SHADE_Engine/src/Common/SHAllComponents.h +++ b/SHADE_Engine/src/Common/SHAllComponents.h @@ -12,4 +12,5 @@ #include "Graphics/MiddleEnd/Lights/SHLightComponent.h" #include "Graphics/MiddleEnd/Interface/SHRenderable.h" #include "Physics/Interface/SHColliderComponent.h" -#include "Graphics/MiddleEnd/TextRendering/SHTextRenderableComponent.h" \ No newline at end of file +#include "Graphics/MiddleEnd/TextRendering/SHTextRenderableComponent.h" +#include "AudioSystem/SHAudioListenerComponent.h" \ No newline at end of file diff --git a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorInspector.cpp b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorInspector.cpp index 391e0be2..c95221ec 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorInspector.cpp +++ b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorInspector.cpp @@ -171,10 +171,15 @@ namespace SHADE if (auto toggleButton = SHComponentManager::GetComponent_s(eid)) { DrawComponent(toggleButton); - }if (auto slider = SHComponentManager::GetComponent_s(eid)) + } + if (auto slider = SHComponentManager::GetComponent_s(eid)) { DrawComponent(slider); } + if(auto listenerComponent = SHComponentManager::GetComponent_s(eid)) + { + DrawComponent(listenerComponent); + } ImGui::Separator(); // Render Scripts SHScriptEngine* scriptEngine = static_cast(SHSystemManager::GetSystem()); @@ -198,7 +203,7 @@ namespace SHADE DrawAddComponentWithEnforcedComponentButton(eid); DrawAddComponentWithEnforcedComponentButton(eid); DrawAddComponentWithEnforcedComponentButton(eid); - + DrawAddComponentWithEnforcedComponentButton(eid); ImGui::EndMenu(); } diff --git a/SHADE_Engine/src/Serialization/SHSerialization.cpp b/SHADE_Engine/src/Serialization/SHSerialization.cpp index 29fb8cd7..13f5e36e 100644 --- a/SHADE_Engine/src/Serialization/SHSerialization.cpp +++ b/SHADE_Engine/src/Serialization/SHSerialization.cpp @@ -246,6 +246,7 @@ namespace SHADE AddComponentToComponentNode(components, eid); AddComponentToComponentNode(components, eid); AddComponentToComponentNode(components, eid); + AddComponentToComponentNode(components, eid); node[ComponentsNode] = components; @@ -306,6 +307,7 @@ namespace SHADE AddComponentID(componentIDList, componentsNode); AddComponentID(componentIDList, componentsNode); AddComponentID(componentIDList, componentsNode); + AddComponentID(componentIDList, componentsNode); return componentIDList; } @@ -392,5 +394,6 @@ namespace SHADE SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); + SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); } } -- 2.40.1 From 417f086dfaff863a2c55ee41c4635af9b0147b51 Mon Sep 17 00:00:00 2001 From: SHAM-DP Date: Tue, 28 Feb 2023 09:36:28 +0800 Subject: [PATCH 2/3] Clean up and verified VCA Volume editing is working fine --- SHADE_Engine/src/AudioSystem/SHAudioSystem.cpp | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/SHADE_Engine/src/AudioSystem/SHAudioSystem.cpp b/SHADE_Engine/src/AudioSystem/SHAudioSystem.cpp index e5e062a2..99c0c9a1 100644 --- a/SHADE_Engine/src/AudioSystem/SHAudioSystem.cpp +++ b/SHADE_Engine/src/AudioSystem/SHAudioSystem.cpp @@ -91,11 +91,6 @@ namespace SHADE LoadBank((AUDIO_FOLDER_PATH + "SFX.bank").data()); LoadBank((AUDIO_FOLDER_PATH + "UI.bank").data()); - //auto clip = CreateAudioClip("event:/Characters/sfx_footsteps_human"); - //clip->Play(); - //PlayEventOnce("event:/Characters/sfx_footsteps_raccoon"); - //PlayEventOnce("event:/SFX/Dawn/Dawn_Attack"); - #ifdef SHEDITOR // Subscribe to Editor State Change Events @@ -163,13 +158,13 @@ namespace SHADE } auto [begin, end] = audioClipLibrary.GetDenseAccess(); - for(auto it = begin; it != end; ++it) + for (auto& it = begin; it != end; ++it) { if(it->instance && (it->transformRef != MAX_EID)) { if(SHTransformComponent* transformComponent = SHComponentManager::GetComponent_s(it->transformRef)) { - FMOD_3D_ATTRIBUTES attribs{ { 0 } }; //TODO: Set other attribs + FMOD_3D_ATTRIBUTES attribs{ { 0 } }; auto pos = transformComponent->GetWorldPosition(); SHQuaternion worldOrientation = transformComponent->GetWorldOrientation(); SHVec3 orientatedFoward = (worldOrientation * SHQuaternion::FromEuler(SHVec3::Forward) * SHQuaternion::Conjugate(worldOrientation)).ToEuler(); -- 2.40.1 From a6659f7cbc4d54291039fb270f8645d936bef3c0 Mon Sep 17 00:00:00 2001 From: SHAM-DP Date: Tue, 28 Feb 2023 13:11:21 +0800 Subject: [PATCH 3/3] Remove some redundant lines --- SHADE_Engine/src/AudioSystem/SHAudioSystem.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/SHADE_Engine/src/AudioSystem/SHAudioSystem.cpp b/SHADE_Engine/src/AudioSystem/SHAudioSystem.cpp index 99c0c9a1..e02eac50 100644 --- a/SHADE_Engine/src/AudioSystem/SHAudioSystem.cpp +++ b/SHADE_Engine/src/AudioSystem/SHAudioSystem.cpp @@ -85,8 +85,6 @@ namespace SHADE LoadBank((AUDIO_FOLDER_PATH + "Master.bank").data()); LoadBank((AUDIO_FOLDER_PATH + "Master.strings.bank").data()); - //LoadBank((AUDIO_FOLDER_PATH + "Music.bank").data()); - //LoadBank((AUDIO_FOLDER_PATH + "footsteps.bank").data()); LoadBank((AUDIO_FOLDER_PATH + "Music.bank").data()); LoadBank((AUDIO_FOLDER_PATH + "SFX.bank").data()); LoadBank((AUDIO_FOLDER_PATH + "UI.bank").data()); @@ -112,8 +110,6 @@ namespace SHADE void SHADE::SHAudioSystem::Run(double dt) { static_cast(dt); - //if (GetKeyState(VK_SPACE) & 0x8000) - // PlayEventOnce("event:/Characters/sfx_footsteps_raccoon"); fmodStudioSystem->update(); //int listenerID = 0; -- 2.40.1