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 void SHAudioSystem::ErrorCheck() const
{ {
if (result != FMOD_OK) 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) 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) if (channel->isPlaying(&isPlaying) == FMOD_OK && isPlaying)
channel->stop(); 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(); masterGroup->stop();
} }
@ -349,6 +360,10 @@ namespace SHADE
audioClipHandle = audioClipLibrary.Create(); audioClipHandle = audioClipLibrary.Create();
it->second->createInstance(&audioClipHandle->instance); it->second->createInstance(&audioClipHandle->instance);
} }
else
{
SHLOG_ERROR("FMOD: Failed to find event path: {}", path)
}
return audioClipHandle; return audioClipHandle;
} }
@ -487,10 +502,12 @@ namespace SHADE
} }
void SHAudioSystem::SetBgmVolume(float const bgmvol) void SHAudioSystem::SetBgmVolume(float const bgmvol)
{ {
bgmVolume = bgmvol;
bgmChannelGroup->setVolume(bgmvol); bgmChannelGroup->setVolume(bgmvol);
} }
void SHAudioSystem::SetSfxVolume(float const sfxvol) void SHAudioSystem::SetSfxVolume(float const sfxvol)
{ {
sfxVolume = sfxvol;
sfxChannelGroup->setVolume(sfxvol); sfxChannelGroup->setVolume(sfxvol);
} }
void SHAudioSystem::SetMasterVolume(float const mastervol) void SHAudioSystem::SetMasterVolume(float const mastervol)
@ -506,15 +523,12 @@ namespace SHADE
{ {
channel->setPaused(paused); channel->setPaused(paused);
} }
for (auto const& event : eventMap) auto [begin, end] = audioClipLibrary.GetDenseAccess();
for (auto it = begin; it != end; ++it)
{ {
int instanceCount = 0; if (it->instance)
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)
{ {
instance->setPaused(pause); it->SetPause(pause);
} }
} }
} }
@ -546,6 +560,7 @@ namespace SHADE
bank->loadSampleData(); bank->loadSampleData();
int numOfEvents; int numOfEvents;
bank->getEventCount(&numOfEvents); bank->getEventCount(&numOfEvents);
if (numOfEvents > 0) if (numOfEvents > 0)
{ {
std::vector<FMOD::Studio::EventDescription*> events(numOfEvents); 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() void AudioClip::Play()
{ {
if(!instance) if(!instance)
return; 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) //void AudioClip::Play(bool isSfx)
@ -598,7 +673,11 @@ namespace SHADE
{ {
if (!instance) if (!instance)
return; 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) void AudioClip::SetPause(bool pause)
@ -623,7 +702,11 @@ namespace SHADE
{ {
if (!instance) if (!instance)
return; 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) //void AudioClip::SetParameterLabel(const char* paramName, const char* label)
@ -635,13 +718,33 @@ namespace SHADE
float AudioClip::GetParameterValue(const char* paramName) float AudioClip::GetParameterValue(const char* paramName)
{ {
if (!instance)
return {};
float value{}; 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; 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) SHEventHandle SHAudioSystem::onStop(SHEventPtr onStopEvent)
{ {
StopAllSounds(); StopAllSounds();

View File

@ -35,6 +35,8 @@ namespace SHADE
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);
float GetVolume();
void SetVolume(float volume);
friend class SHAudioSystem; friend class SHAudioSystem;
private: private:
FMOD::Studio::EventInstance* instance = nullptr; FMOD::Studio::EventInstance* instance = nullptr;
@ -85,7 +87,13 @@ namespace SHADE
void SetPaused(bool pause); void SetPaused(bool pause);
bool GetPaused() const; bool GetPaused() const;
SHVec3 GetListenerPosition(); SHVec3 GetListenerPosition();
void LoadBank(const char* path); 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: private:
FMOD::Studio::System* fmodStudioSystem; FMOD::Studio::System* fmodStudioSystem;

View File

@ -99,6 +99,31 @@ namespace SHADE
audioSys->StopAllSounds(); 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) AudioClipHandler Audio::CreateAudioClip(System::String^ path)
{ {
auto audioSys = SHSystemManager::GetSystem<SHAudioSystem>(); auto audioSys = SHSystemManager::GetSystem<SHAudioSystem>();

View File

@ -101,6 +101,10 @@ namespace SHADE
/// </summary> /// </summary>
static void StopAllSounds(); 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 //to comment ltr
static AudioClipHandler CreateAudioClip(System::String^ path); static AudioClipHandler CreateAudioClip(System::String^ path);

View File

@ -84,6 +84,14 @@ namespace SHADE
return NativeObject->GetParameterValue(Convert::ToNative(paramName).data()); 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(); bool IsPaused();
void SetParameter(System::String^ paramName, float value); void SetParameter(System::String^ paramName, float value);
float GetParameterValue(System::String^ paramName); float GetParameterValue(System::String^ paramName);
float GetVolume();
void SetVolume(float volume);
protected: protected:
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/