Remove a few redundancies.

Audio System now creates handles to audio clips
This commit is contained in:
SHAM-DP 2022-12-27 16:23:53 +08:00
parent 35c8321e98
commit f49ecdbb14
2 changed files with 107 additions and 66 deletions

View File

@ -325,26 +325,69 @@ namespace SHADE
return std::nullopt; return std::nullopt;
} }
AudioClip* SHAudioSystem::CreateAudioClip(const char* path) Handle<AudioClip> SHAudioSystem::CreateAudioClip(const char* path)
{ {
AudioClipID newID{}; Handle<AudioClip> audioClipHandle{};
AudioClip* clip = nullptr;
auto it = eventMap.find(path); if(auto it = eventMap.find(path); it != eventMap.end())
if (it != eventMap.end())
{ {
FMOD::Studio::EventInstance* event = nullptr; audioClipHandle = audioClipLibrary.Create();
it->second->createInstance(&event); it->second->createInstance(&audioClipHandle->instance);
if (event) }
return audioClipHandle;
}
void SHAudioSystem::AddAudioClipToBGMChannelGroup(Handle<AudioClip> handle)
{ {
//event->start(); if(!handle->instance)
newID = clipID; return;
clipID++; FMOD::ChannelGroup* channelGroup;
eventInstances.emplace(newID, AudioClip(newID, event)); handle->instance->getChannelGroup(&channelGroup);
clip = &eventInstances[newID];
if(!channelGroup)
{
SHLOG_ERROR("Event instance has no channel group")
return;
} }
bgmChannelGroup->addGroup(channelGroup);
} }
return clip;
void SHAudioSystem::AddAudioClipToSFXChannelGroup(Handle<AudioClip> handle)
{
if (!handle->instance)
return;
FMOD::ChannelGroup* channelGroup;
handle->instance->getChannelGroup(&channelGroup);
if (!channelGroup)
{
SHLOG_ERROR("Event instance has no channel group")
return;
} }
sfxChannelGroup->addGroup(channelGroup);
}
//AudioClip* SHAudioSystem::CreateAudioClip(const char* path)
//{
// AudioClipID newID{};
// AudioClip* clip = nullptr;
// auto it = eventMap.find(path);
// if (it != eventMap.end())
// {
// FMOD::Studio::EventInstance* event = nullptr;
// it->second->createInstance(&event);
// if (event)
// {
// //event->start();
// newID = clipID;
// clipID++;
// eventInstances.emplace(newID, AudioClip(newID, event));
// clip = &eventInstances[newID];
// }
// }
// return clip;
//}
//std::vector<const char*> SHAudioSystem::GetAllEvents() //std::vector<const char*> SHAudioSystem::GetAllEvents()
//{ //{
@ -489,41 +532,39 @@ namespace SHADE
} }
} }
AudioClip::AudioClip(AudioClipID clipID, FMOD::Studio::EventInstance* inst) void AudioClip::Play()
:instance(inst), id(clipID)
{ {
} if(!instance)
AudioClip::~AudioClip()
{
}
void AudioClip::Play(bool isSfx)
{
if (!instance)
return; return;
instance->start(); instance->start();
auto audioSystem = SHSystemManager::GetSystem<SHADE::SHAudioSystem>();
instance->setVolume(audioSystem->GetMasterVolume() * (isSfx ? audioSystem->GetSfxVolume() : audioSystem->GetBgmVolume()));
} }
void AudioClip::Play(SHVec3 position, bool isSfx) //void AudioClip::Play(bool isSfx)
{ //{
if (!instance) // if (!instance)
return; // return;
instance->start(); // instance->start();
FMOD_3D_ATTRIBUTES attributes{ {} }; // auto audioSystem = SHSystemManager::GetSystem<SHADE::SHAudioSystem>();
attributes.forward.z = 1.0f; // instance->setVolume(audioSystem->GetMasterVolume() * (isSfx ? audioSystem->GetSfxVolume() : audioSystem->GetBgmVolume()));
attributes.up.y = 1.0f; //}
auto audioSystem = SHSystemManager::GetSystem<SHADE::SHAudioSystem>(); //void AudioClip::Play(SHVec3 position, bool isSfx)
SHVec3 listenerPos = audioSystem->GetListenerPosition(); //{
attributes.position.x = position[0]; // if (!instance)
attributes.position.y = position[1]; // return;
attributes.position.z = listenerPos[2]; // instance->start();
instance->set3DAttributes(&attributes); // FMOD_3D_ATTRIBUTES attributes{ {} };
instance->setVolume(audioSystem->GetMasterVolume() * (isSfx ? audioSystem->GetSfxVolume() : audioSystem->GetBgmVolume())); // attributes.forward.z = 1.0f;
} // attributes.up.y = 1.0f;
// 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);
// instance->setVolume(audioSystem->GetMasterVolume() * (isSfx ? audioSystem->GetSfxVolume() : audioSystem->GetBgmVolume()));
//}
void AudioClip::Stop(bool fadeOut) void AudioClip::Stop(bool fadeOut)
{ {
@ -557,12 +598,12 @@ namespace SHADE
instance->setParameterByName(paramName, value); instance->setParameterByName(paramName, value);
} }
void AudioClip::SetParameterLabel(const char* paramName, const char* label) //void AudioClip::SetParameterLabel(const char* paramName, const char* label)
{ //{
if (!instance) // if (!instance)
return; // return;
instance->setParameterByNameWithLabel(paramName, label); // instance->setParameterByNameWithLabel(paramName, label);
} //}
float AudioClip::GetParameterValue(const char* paramName) float AudioClip::GetParameterValue(const char* paramName)
{ {

View File

@ -13,6 +13,7 @@
#include "Events/SHEvent.h" #include "Events/SHEvent.h"
#include "SH_API.h" #include "SH_API.h"
#include <Resource/SHResourceLibrary.h>
#define AUDIO_SYS_MAX_CHANNELS 1024 #define AUDIO_SYS_MAX_CHANNELS 1024
namespace SHADE namespace SHADE
@ -22,27 +23,22 @@ namespace SHADE
class SHAudioListenerComponent; class SHAudioListenerComponent;
typedef uint64_t AudioClipID;
class AudioClip class AudioClip
{ {
public: public:
AudioClip() = default; void Play();
AudioClip(AudioClipID clipID, FMOD::Studio::EventInstance* inst); //void Play(SHVec3 position);
~AudioClip();
void Play(bool isSfx = true);
void Play(SHVec3 position, bool isSfx = true);
void Stop(bool fadeOut = true); void Stop(bool fadeOut = true);
void SetPause(bool pause); void SetPause(bool pause);
bool IsPaused(); bool IsPaused();
void SetParameter(const char* paramName, float value); void SetParameter(const char* paramName, float value);
void SetParameterLabel(const char* paramName, const char* label); //void SetParameterLabel(const char* paramName, const char* label);
float GetParameterValue(const char* paramName); float GetParameterValue(const char* paramName);
friend class SHAudioSystem; friend class SHAudioSystem;
private: private:
FMOD::Studio::EventInstance* instance; FMOD::Studio::EventInstance* instance = nullptr;
AudioClipID id; //SHTransformComponent* transformRef;
}; };
class SH_API SHAudioSystem : public SHSystem class SH_API SHAudioSystem : public SHSystem
@ -62,7 +58,7 @@ namespace SHADE
void Exit(); void Exit();
int GetAvailableChannelIndex(); int GetAvailableChannelIndex();
/*std::vector<SHSound>::size_type CreateSound(const char* filepath, bool loop = false);*/
void PlaySFX(EntityID id, EntityID eid, const bool& loop, const bool& spatial, float min = 5.0f, float max = 1000.0f); void PlaySFX(EntityID id, EntityID eid, const bool& loop, const bool& spatial, float min = 5.0f, float max = 1000.0f);
void PlayBGM(EntityID id, EntityID eid, const bool& loop, const bool& spatial, float min = 5.0f, float max = 1000.0f); void PlayBGM(EntityID id, EntityID eid, const bool& loop, const bool& spatial, float min = 5.0f, float max = 1000.0f);
void PlayEventOnce(const char* path, bool isSFX = true, EntityID eid = MAX_EID, bool spatial = false); void PlayEventOnce(const char* path, bool isSFX = true, EntityID eid = MAX_EID, bool spatial = false);
@ -71,8 +67,10 @@ namespace SHADE
void StopAllSounds(); void StopAllSounds();
std::optional<FMOD_GUID> GetEventGUID(const char* path); std::optional<FMOD_GUID> GetEventGUID(const char* path);
AudioClip* CreateAudioClip(const char* path); //AudioClip* CreateAudioClip(const char* path);
//std::vector<const char*> GetAllEvents(); Handle<AudioClip> CreateAudioClip(const char* path);
void AddAudioClipToBGMChannelGroup(Handle<AudioClip> handle);
void AddAudioClipToSFXChannelGroup(Handle<AudioClip> handle);
float GetBgmVolume(); float GetBgmVolume();
float GetSfxVolume(); float GetSfxVolume();
@ -84,6 +82,7 @@ namespace SHADE
bool GetPaused() const; bool GetPaused() const;
SHVec3 GetListenerPosition(); SHVec3 GetListenerPosition();
void LoadBank(const char* path); void LoadBank(const char* path);
private: private:
FMOD::Studio::System* fmodStudioSystem; FMOD::Studio::System* fmodStudioSystem;
FMOD::System* fmodSystem; FMOD::System* fmodSystem;
@ -95,7 +94,9 @@ namespace SHADE
//std::unordered_map<ResourceID, SHBank> bankMap; //std::unordered_map<ResourceID, SHBank> bankMap;
std::unordered_map<std::string, SHBank> bankMap; std::unordered_map<std::string, SHBank> bankMap;
std::unordered_map<std::string, FMOD::Studio::EventDescription*> eventMap; std::unordered_map<std::string, FMOD::Studio::EventDescription*> eventMap;
std::unordered_map<AudioClipID, AudioClip> eventInstances; //std::unordered_map<AudioClipID, AudioClip> eventInstances;
SHResourceLibrary<AudioClip> audioClipLibrary{};
FMOD::ChannelGroup* bgmChannelGroup, * sfxChannelGroup, * masterGroup; FMOD::ChannelGroup* bgmChannelGroup, * sfxChannelGroup, * masterGroup;
FMOD::Channel* audioChannels[AUDIO_SYS_MAX_CHANNELS]; FMOD::Channel* audioChannels[AUDIO_SYS_MAX_CHANNELS];
FMOD_RESULT result; FMOD_RESULT result;
@ -105,7 +106,6 @@ namespace SHADE
SHBank masterBank, stringsBank, musicBank, sfxBank; //To do: change to map of banks loaded by resource manager SHBank masterBank, stringsBank, musicBank, sfxBank; //To do: change to map of banks loaded by resource manager
std::vector<SHAudioListenerComponent>* denseListener; std::vector<SHAudioListenerComponent>* denseListener;
AudioClipID clipID = 0;
SHEventHandle onPlay(SHEventPtr onStopEvent); SHEventHandle onPlay(SHEventPtr onStopEvent);
SHEventHandle onStop(SHEventPtr onStopEvent); SHEventHandle onStop(SHEventPtr onStopEvent);