Added global parameter control and VCA volume control #334

Merged
srishamharan merged 2 commits from SP3-129-AudioSystem into main 2023-02-03 20:21:28 +08:00
6 changed files with 164 additions and 14 deletions

View File

@ -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<FMOD::Studio::EventInstance*> instances(instanceCount);
event.second->getInstanceList(instances.data(), static_cast<int>(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<FMOD::Studio::EventDescription*> 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,11 +718,31 @@ 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)

View File

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

View File

@ -99,6 +99,31 @@ namespace SHADE
audioSys->StopAllSounds();
}
float Audio::GetVCAVolume(System::String^ path)
{
auto audioSys = SHSystemManager::GetSystem<SHAudioSystem>();
return audioSys->GetVCAVolume(Convert::ToNative(path).data());
}
void Audio::SetVCAVolume(System::String^ path, float volume)
{
auto audioSys = SHSystemManager::GetSystem<SHAudioSystem>();
audioSys->SetVCAVolume(Convert::ToNative(path).data(), volume);
}
float Audio::GetParameterValue(System::String^ path)
{
auto audioSys = SHSystemManager::GetSystem<SHAudioSystem>();
return audioSys->GetParameterValue(Convert::ToNative(path).data());
}
void Audio::SetParameter(System::String^ path, float value)
{
auto audioSys = SHSystemManager::GetSystem<SHAudioSystem>();
audioSys->SetParameter(Convert::ToNative(path).data(), value);
}
AudioClipHandler Audio::CreateAudioClip(System::String^ path)
{
auto audioSys = SHSystemManager::GetSystem<SHAudioSystem>();

View File

@ -101,6 +101,10 @@ namespace SHADE
/// </summary>
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);

View File

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

View File

@ -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:
/*-----------------------------------------------------------------------------*/