From 5b2f42a920e7b3e55f9dedf4a4541c2f0a6e6ebe Mon Sep 17 00:00:00 2001 From: SHAM-DP Date: Fri, 3 Feb 2023 19:16:12 +0800 Subject: [PATCH] Added controls for global parameters Added controls for VCA volume --- .../src/AudioSystem/SHAudioSystem.cpp | 131 ++++++++++++++++-- SHADE_Engine/src/AudioSystem/SHAudioSystem.h | 8 ++ SHADE_Managed/src/Audio/Audio.cxx | 25 ++++ SHADE_Managed/src/Audio/Audio.hxx | 4 + SHADE_Managed/src/Audio/AudioClip.cxx | 8 ++ SHADE_Managed/src/Audio/AudioClip.hxx | 2 + 6 files changed, 164 insertions(+), 14 deletions(-) diff --git a/SHADE_Engine/src/AudioSystem/SHAudioSystem.cpp b/SHADE_Engine/src/AudioSystem/SHAudioSystem.cpp index b4330979..a7245123 100644 --- a/SHADE_Engine/src/AudioSystem/SHAudioSystem.cpp +++ b/SHADE_Engine/src/AudioSystem/SHAudioSystem.cpp @@ -200,7 +200,9 @@ namespace SHADE void SHAudioSystem::ErrorCheck() const { if (result != FMOD_OK) - std::cerr << "Audio system error: " << FMOD_ErrorString(result) << std::endl; + { + SHLOG_ERROR( "Audio system error: {}", FMOD_ErrorString(result)) + } } void SHAudioSystem::PlayEventOnce(const char* path, bool isSFX, EntityID eid, bool spatial) @@ -319,6 +321,15 @@ namespace SHADE if (channel->isPlaying(&isPlaying) == FMOD_OK && isPlaying) channel->stop(); } + auto [begin, end] = audioClipLibrary.GetDenseAccess(); + for (auto it = begin; it != end; ++it) + { + if (it->instance) + { + it->instance->stop(FMOD_STUDIO_STOP_MODE::FMOD_STUDIO_STOP_IMMEDIATE); + it->instance->release(); + } + } masterGroup->stop(); } @@ -349,6 +360,10 @@ namespace SHADE audioClipHandle = audioClipLibrary.Create(); it->second->createInstance(&audioClipHandle->instance); } + else + { + SHLOG_ERROR("FMOD: Failed to find event path: {}", path) + } return audioClipHandle; } @@ -487,10 +502,12 @@ namespace SHADE } void SHAudioSystem::SetBgmVolume(float const bgmvol) { + bgmVolume = bgmvol; bgmChannelGroup->setVolume(bgmvol); } void SHAudioSystem::SetSfxVolume(float const sfxvol) { + sfxVolume = sfxvol; sfxChannelGroup->setVolume(sfxvol); } void SHAudioSystem::SetMasterVolume(float const mastervol) @@ -506,15 +523,12 @@ namespace SHADE { channel->setPaused(paused); } - for (auto const& event : eventMap) + auto [begin, end] = audioClipLibrary.GetDenseAccess(); + for (auto it = begin; it != end; ++it) { - int instanceCount = 0; - event.second->getInstanceCount(&instanceCount); - std::vector instances(instanceCount); - event.second->getInstanceList(instances.data(), static_cast(instances.size()), &instanceCount); - for (auto const& instance : instances) + if (it->instance) { - instance->setPaused(pause); + it->SetPause(pause); } } } @@ -546,6 +560,7 @@ namespace SHADE bank->loadSampleData(); int numOfEvents; bank->getEventCount(&numOfEvents); + if (numOfEvents > 0) { std::vector events(numOfEvents); @@ -560,11 +575,71 @@ namespace SHADE } } + float SHAudioSystem::GetVCAVolume(const char* path) + { + FMOD::Studio::VCA* vca = nullptr; + result = fmodStudioSystem->getVCA(path, &vca); + float volume = 0.0f; + if (result != FMOD_OK) + { + ErrorCheck(); + return volume; + } + result = vca->getVolume(&volume); + if (result != FMOD_OK) + { + ErrorCheck(); + return volume; + } + return volume; + } + + void SHAudioSystem::SetVCAVolume(const char* path, float value) + { + FMOD::Studio::VCA* vca = nullptr; + result = fmodStudioSystem->getVCA(path, &vca); + if (result != FMOD_OK) + { + ErrorCheck(); + return; + } + result = vca->setVolume(std::clamp(value, 0.0f, 1.0f)); + if (result != FMOD_OK) + { + ErrorCheck(); + return; + } + } + + float SHAudioSystem::GetParameterValue(const char* path) + { + float value = {}; + result = fmodStudioSystem->getParameterByName(path, &value); + if(result != FMOD_OK) + { + ErrorCheck(); + } + return value; + } + + void SHAudioSystem::SetParameter(const char* path, float value) + { + result = fmodStudioSystem->setParameterByName(path, value); + if (result != FMOD_OK) + { + ErrorCheck(); + } + } + void AudioClip::Play() { if(!instance) return; - instance->start(); + FMOD_RESULT result = instance->start(); + if (result != FMOD_OK) + { + SHLOG_ERROR("Audio system error: {}", FMOD_ErrorString(result)) + } } //void AudioClip::Play(bool isSfx) @@ -598,7 +673,11 @@ namespace SHADE { if (!instance) return; - instance->stop(fadeOut ? FMOD_STUDIO_STOP_ALLOWFADEOUT : FMOD_STUDIO_STOP_IMMEDIATE); + FMOD_RESULT result = instance->stop(fadeOut ? FMOD_STUDIO_STOP_ALLOWFADEOUT : FMOD_STUDIO_STOP_IMMEDIATE); + if (result != FMOD_OK) + { + SHLOG_ERROR("Audio system error: {}", FMOD_ErrorString(result)) + } } void AudioClip::SetPause(bool pause) @@ -623,7 +702,11 @@ namespace SHADE { if (!instance) return; - instance->setParameterByName(paramName, value); + FMOD_RESULT result = instance->setParameterByName(paramName, value); + if (result != FMOD_OK) + { + SHLOG_ERROR("Audio system error: {}", FMOD_ErrorString(result)) + } } //void AudioClip::SetParameterLabel(const char* paramName, const char* label) @@ -635,13 +718,33 @@ namespace SHADE float AudioClip::GetParameterValue(const char* paramName) { - if (!instance) - return {}; float value{}; - instance->getParameterByName(paramName, &value); + if (!instance) + return value; + auto result = instance->getParameterByName(paramName, &value); + if (result != FMOD_OK) + { + SHLOG_ERROR("Audio system error: {}", FMOD_ErrorString(result)) + } return value; } + float AudioClip::GetVolume() + { + float volume{}; + if(!instance) + return volume; + instance->getVolume(&volume); + return volume; + } + + void AudioClip::SetVolume(float volume) + { + if(!instance) + return; + instance->setVolume(volume); + } + SHEventHandle SHAudioSystem::onStop(SHEventPtr onStopEvent) { StopAllSounds(); diff --git a/SHADE_Engine/src/AudioSystem/SHAudioSystem.h b/SHADE_Engine/src/AudioSystem/SHAudioSystem.h index 0c12cef4..5714a618 100644 --- a/SHADE_Engine/src/AudioSystem/SHAudioSystem.h +++ b/SHADE_Engine/src/AudioSystem/SHAudioSystem.h @@ -35,6 +35,8 @@ namespace SHADE void SetParameter(const char* paramName, float value); //void SetParameterLabel(const char* paramName, const char* label); float GetParameterValue(const char* paramName); + float GetVolume(); + void SetVolume(float volume); friend class SHAudioSystem; private: FMOD::Studio::EventInstance* instance = nullptr; @@ -85,7 +87,13 @@ namespace SHADE void SetPaused(bool pause); bool GetPaused() const; SHVec3 GetListenerPosition(); + void LoadBank(const char* path); + float GetVCAVolume(const char* path); + void SetVCAVolume(const char* path, float value); + + float GetParameterValue(const char* path); + void SetParameter(const char* path, float value); private: FMOD::Studio::System* fmodStudioSystem; diff --git a/SHADE_Managed/src/Audio/Audio.cxx b/SHADE_Managed/src/Audio/Audio.cxx index 9432886f..c3994cd4 100644 --- a/SHADE_Managed/src/Audio/Audio.cxx +++ b/SHADE_Managed/src/Audio/Audio.cxx @@ -99,6 +99,31 @@ namespace SHADE audioSys->StopAllSounds(); } + float Audio::GetVCAVolume(System::String^ path) + { + auto audioSys = SHSystemManager::GetSystem(); + + return audioSys->GetVCAVolume(Convert::ToNative(path).data()); + } + + void Audio::SetVCAVolume(System::String^ path, float volume) + { + auto audioSys = SHSystemManager::GetSystem(); + audioSys->SetVCAVolume(Convert::ToNative(path).data(), volume); + } + + float Audio::GetParameterValue(System::String^ path) + { + auto audioSys = SHSystemManager::GetSystem(); + return audioSys->GetParameterValue(Convert::ToNative(path).data()); + } + + void Audio::SetParameter(System::String^ path, float value) + { + auto audioSys = SHSystemManager::GetSystem(); + audioSys->SetParameter(Convert::ToNative(path).data(), value); + } + AudioClipHandler Audio::CreateAudioClip(System::String^ path) { auto audioSys = SHSystemManager::GetSystem(); diff --git a/SHADE_Managed/src/Audio/Audio.hxx b/SHADE_Managed/src/Audio/Audio.hxx index a8c94156..179a3eb7 100644 --- a/SHADE_Managed/src/Audio/Audio.hxx +++ b/SHADE_Managed/src/Audio/Audio.hxx @@ -101,6 +101,10 @@ namespace SHADE /// static void StopAllSounds(); + static float GetVCAVolume(System::String^ path); + static void SetVCAVolume(System::String^ path, float volume); + static float GetParameterValue(System::String^ path); + static void SetParameter(System::String^ path, float value); //to comment ltr static AudioClipHandler CreateAudioClip(System::String^ path); diff --git a/SHADE_Managed/src/Audio/AudioClip.cxx b/SHADE_Managed/src/Audio/AudioClip.cxx index 021a3437..7ed58714 100644 --- a/SHADE_Managed/src/Audio/AudioClip.cxx +++ b/SHADE_Managed/src/Audio/AudioClip.cxx @@ -84,6 +84,14 @@ namespace SHADE return NativeObject->GetParameterValue(Convert::ToNative(paramName).data()); } + float AudioClipHandler::GetVolume() + { + return NativeObject->GetVolume(); + } + void AudioClipHandler::SetVolume(float volume) + { + NativeObject->SetVolume(volume); + } } \ No newline at end of file diff --git a/SHADE_Managed/src/Audio/AudioClip.hxx b/SHADE_Managed/src/Audio/AudioClip.hxx index 09f6a2d1..34e9b4a5 100644 --- a/SHADE_Managed/src/Audio/AudioClip.hxx +++ b/SHADE_Managed/src/Audio/AudioClip.hxx @@ -72,6 +72,8 @@ namespace SHADE bool IsPaused(); void SetParameter(System::String^ paramName, float value); float GetParameterValue(System::String^ paramName); + float GetVolume(); + void SetVolume(float volume); protected: /*-----------------------------------------------------------------------------*/