audiosystem base level done along with its components

This commit is contained in:
Glence 2022-09-24 19:13:23 +08:00
parent c4105510b8
commit d7b5ae9f02
8 changed files with 92 additions and 65 deletions

View File

@ -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"

View File

@ -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::SHTransformSystem>();
SHADE::SHSystemManager::CreateSystem<SHADE::SHGraphicsSystem>();
SHADE::SHGraphicsSystem* graphicsSystem = static_cast<SHADE::SHGraphicsSystem*>(SHADE::SHSystemManager::GetSystem<SHADE::SHGraphicsSystem>());
SHADE::SHSystemManager::CreateSystem<SHADE::SHAudioSystem>();
// Create Routines
SHADE::SHSystemManager::RegisterRoutine<SHADE::SHScriptEngine, SHADE::SHScriptEngine::FrameSetUpRoutine>();
@ -69,6 +73,10 @@ namespace Sandbox
SHADE::SHSystemManager::RegisterRoutine<SHADE::SHGraphicsSystem, SHADE::SHGraphicsSystem::EndRoutine>();
SHADE::SHComponentManager::CreateComponentSparseSet<SHADE::SHRenderable>();
SHADE::SHSystemManager::RegisterRoutine<SHADE::SHAudioSystem, SHADE::SHAudioSystem::AudioRoutine>();
SHADE::SHComponentManager::CreateComponentSparseSet<SHADE::SHAudioSourceComponent>();
SHADE::SHComponentManager::CreateComponentSparseSet<SHADE::SHAudioListenerComponent>();
// Set up graphics system and windows
graphicsSystem->SetWindow(&window);
sdlWindow = SDL_CreateWindowFrom(window.GetHWND());

View File

@ -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;
}

View File

@ -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

View File

@ -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<SHAudioSystem>()->PlaySFX(id, GetEID(),loop,spatial);
SHSystemManager::GetSystem<SHAudioSystem>()->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<SHAudioSystem>()->PlayBGM(id, GetEID(), loop, spatial);
SHSystemManager::GetSystem<SHAudioSystem>()->PlayBGM(id, GetEID(), loop, spatial, min, max);
}
/**

View File

@ -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

View File

@ -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 <iostream>
//#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 <FMOD/fmod_errors.h>
#include <FMOD/fmod.hpp>
#include <FMOD/fmod_studio.hpp>
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<SHAudioSourceComponent>();
//SHComponentManager::CreateComponentSparseSet<SHAudioListenerComponent>();
SHADE::SHComponentManager::CreateComponentSparseSet<SHADE::SHAudioSourceComponent>();
SHADE::SHComponentManager::CreateComponentSparseSet<SHADE::SHAudioListenerComponent>();
denseListener = &SHComponentManager::GetDense<SHAudioListenerComponent>();
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<void>(dt);
system->update();
fmodStudioSystem->update();
if (!denseListener->empty())
{
SHAudioListenerComponent& listener = denseListener->at(0);
SHTransformComponent* listenerTransform = SHComponentManager::GetComponent_s<SHTransformComponent>(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<SHAudioSystem*>(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<SHTransformComponent>(eid))
{
SHTransformComponent* audioTransform = SHComponentManager::GetComponent_s<SHTransformComponent>(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<SHTransformComponent>(eid))
{
SHTransformComponent* audioTransform = SHComponentManager::GetComponent_s<SHTransformComponent>(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<SHAudioSystem*>(SHSystemManager::GetSystem("Audio"));
auto audioSystem = SHSystemManager::GetSystem<SHADE::SHAudioSystem>();
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<SHAudioSystem*>(SHSystemManager::GetSystem("Audio"))->GetListenerPosition();
auto audioSystem = SHSystemManager::GetSystem<SHADE::SHAudioSystem>();
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<SHAudioSystem*>(SHSystemManager::GetSystem("Audio"));
instance->setVolume(audioSystem->GetMasterVolume() * (isSfx ? audioSystem->GetSfxVolume() : audioSystem->GetBgmVolume()));
}

View File

@ -4,11 +4,13 @@
#include <vector>
#include <set>
#include <unordered_map>
#include <FMOD/fmod_studio.hpp>
#include "ECS_Base/System/SHSystem.h"
#include "ECS_Base/System/SHSystemRoutine.h"
#include "ECS_Base/SHECSMacros.h"
#include "Math/SHMath.h"
#include <optional>
#include <FMOD/fmod_studio.hpp>
#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<EntityID, SHSound> soundList;