Merge pull request #401 from SHADE-DP/SP3-129-AudioSystem

Fixed 3d audio
This commit is contained in:
XiaoQiDigipen 2023-03-07 20:11:02 +08:00 committed by GitHub
commit 55932628b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 8 deletions

View File

@ -54,12 +54,41 @@ namespace SHADE
denseListener = &SHComponentManager::GetDense<SHAudioListenerComponent>();
fmodStudioSystem->getCoreSystem(&fmodSystem);
fmodSystem->setSoftwareFormat(0, FMOD_SPEAKERMODE_5POINT1, 0);
result = fmodStudioSystem->initialize(AUDIO_SYS_MAX_CHANNELS, AUDIO_SYS_MAX_CHANNELS, FMOD_STUDIO_INIT_NORMAL | FMOD_INIT_NORMAL, extraDriverData);
ErrorCheck();
fmodSystem->setSoftwareFormat(0, speakerMode, 0);
result = fmodSystem->setSoftwareFormat(0, FMOD_SPEAKERMODE_5POINT1, 0);
ErrorCheck();
result = fmodSystem->setOutput(FMOD_OUTPUTTYPE_AUDIO3D);
if(result != FMOD_OK)
{
result = fmodSystem->setOutput(FMOD_OUTPUTTYPE_WINSONIC);
if (result == FMOD_OK)
{
result = fmodSystem->setSoftwareFormat(0, FMOD_SPEAKERMODE_7POINT1POINT4, 0);
ErrorCheck();
}
}
int numDrivers{};
result = fmodSystem->getNumDrivers(&numDrivers);
if(numDrivers == 0)
{
fmodSystem->setDSPBufferSize(512, 4);
fmodSystem->setOutput(FMOD_OUTPUTTYPE_AUTODETECT);
}
FMOD_OUTPUTTYPE outputType{};
fmodSystem->getSoftwareFormat(0, &speakerMode, 0);
fmodSystem->getOutput(&outputType);
FMOD_STUDIO_INITFLAGS studioInitFlags = FMOD_STUDIO_INIT_NORMAL;
FMOD_INITFLAGS coreInitFlags = FMOD_INIT_NORMAL | FMOD_INIT_3D_RIGHTHANDED;
#ifdef _DEBUG
studioInitFlags |= FMOD_STUDIO_INIT_LIVEUPDATE;
coreInitFlags |= FMOD_INIT_PROFILE_ENABLE;
#endif
result = fmodStudioSystem->initialize(AUDIO_SYS_MAX_CHANNELS, studioInitFlags, coreInitFlags, extraDriverData);
ErrorCheck();
result = fmodSystem->createChannelGroup("SFX", &sfxChannelGroup);
ErrorCheck();
@ -104,6 +133,10 @@ namespace SHADE
const ReceiverPtr ON_PAUSE_RECEIVER_PTR = std::dynamic_pointer_cast<SHEventReceiver>(ON_PAUSE_RECEIVER);
SHEventManager::SubscribeTo(SH_EDITOR_ON_PAUSE_EVENT, ON_PAUSE_RECEIVER_PTR);
const std::shared_ptr ON_SCENE_EXIT_RECEIVER{ std::make_shared<SHEventReceiverSpec<SHAudioSystem>>(this, &SHAudioSystem::onSceneExit) };
const ReceiverPtr ON_SCENE_EXIT_RECEIVER_PTR = std::dynamic_pointer_cast<SHEventReceiver>(ON_SCENE_EXIT_RECEIVER);
SHEventManager::SubscribeTo(SH_SCENE_EXIT_POST, ON_SCENE_EXIT_RECEIVER_PTR);
#endif
}
@ -145,7 +178,9 @@ namespace SHADE
FMOD_3D_ATTRIBUTES attribs{ { 0 } };
SHVec3 pos = camComponent->GetPosition();
SHVec3 forward, up, right;
camSystem->GetCameraAxis(*camComponent, forward, right, up);
up *= -1.0f;
attribs.position = { pos.x, pos.y, pos.z };
attribs.forward = { forward.x, forward.y, forward.z };
attribs.up = { up.x, up.y, up.z };
@ -156,7 +191,7 @@ namespace SHADE
auto [begin, end] = audioClipLibrary.GetDenseAccess();
for (auto& it = begin; it != end; ++it)
{
if(it->instance && (it->transformRef != MAX_EID))
if(it->instance && it->instance->isValid() && (it->transformRef != MAX_EID))
{
if(SHTransformComponent* transformComponent = SHComponentManager::GetComponent_s<SHTransformComponent>(it->transformRef))
{
@ -165,11 +200,13 @@ namespace SHADE
SHQuaternion worldOrientation = transformComponent->GetWorldOrientation();
SHVec3 orientatedFoward = (worldOrientation * SHQuaternion::FromEuler(SHVec3::Forward) * SHQuaternion::Conjugate(worldOrientation)).ToEuler();
SHVec3 orientatedUp = (worldOrientation * SHQuaternion::FromEuler(SHVec3::Up) * SHQuaternion::Conjugate(worldOrientation)).ToEuler();
orientatedFoward = SHVec3::Normalise(orientatedFoward);
orientatedUp = SHVec3::Normalise(orientatedUp);
attribs.position = {pos.x, pos.y, pos.z};
attribs.forward = {orientatedFoward.x, orientatedFoward.y, orientatedFoward.z};
attribs.up = {orientatedUp.x, orientatedUp.y, orientatedUp.z};
it->instance->set3DAttributes(&attribs);
result = it->instance->set3DAttributes(&attribs);
ErrorCheck();
}
}
}
@ -796,6 +833,18 @@ namespace SHADE
return onStopEvent->handle;
}
SHEventHandle SHAudioSystem::onSceneExit(SHEventPtr onSceneExitEvent)
{
auto [begin, end] = audioClipLibrary.GetDenseAccess();
for (auto& it = begin; it != end; ++it)
{
it->instance->release();
}
return onSceneExitEvent->handle;
}
SHEventHandle SHAudioSystem::onPlay(SHEventPtr onStopEvent)
{
if(GetPaused())

View File

@ -123,6 +123,7 @@ namespace SHADE
SHEventHandle onPlay(SHEventPtr onStopEvent);
SHEventHandle onStop(SHEventPtr onStopEvent);
SHEventHandle onPause(SHEventPtr onStopEvent);
SHEventHandle onSceneExit(SHEventPtr onSceneExitEvent);
};
}