Upgraded to C++ 20 & better audio voice management
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "AudioSystem.h"
|
||||
|
||||
#include "Engine.h"
|
||||
#include "src/Scene/Scene.h"
|
||||
#include "src/Scene/Entities/Entity.h"
|
||||
#include "src/Scene/Components/AudioEmitterComponent.h"
|
||||
@@ -23,6 +24,19 @@ namespace Nuake
|
||||
|
||||
void AudioSystem::Update(Timestep ts)
|
||||
{
|
||||
auto& audioManager = AudioManager::Get();
|
||||
|
||||
// Update 3D listener of the audio system
|
||||
auto currentCamera = m_Scene->GetCurrentCamera();
|
||||
|
||||
Vector3 direction = currentCamera->GetDirection();
|
||||
if (Engine::IsPlayMode())
|
||||
{
|
||||
direction *= Vector3(-1, -1, -1);
|
||||
}
|
||||
|
||||
audioManager.SetListenerPosition(currentCamera->GetTranslation(), std::move(direction), currentCamera->GetUp());
|
||||
|
||||
auto view = m_Scene->m_Registry.view<TransformComponent, AudioEmitterComponent>();
|
||||
for (auto& e : view)
|
||||
{
|
||||
@@ -30,21 +44,50 @@ namespace Nuake
|
||||
|
||||
if (audioEmitterComponent.FilePath.empty())
|
||||
{
|
||||
// Doesn't have a file
|
||||
continue;
|
||||
}
|
||||
|
||||
if (audioEmitterComponent.IsPlaying)
|
||||
const bool isPlaying = audioEmitterComponent.IsPlaying;
|
||||
const std::string absoluteFilePath = FileSystem::RelativeToAbsolute(audioEmitterComponent.FilePath);
|
||||
const bool isVoiceActive = audioManager.IsVoiceActive(absoluteFilePath);
|
||||
|
||||
AudioRequest audioRequest;
|
||||
audioRequest.audioFile = absoluteFilePath;
|
||||
audioRequest.pan = audioEmitterComponent.Pan;
|
||||
audioRequest.volume = audioEmitterComponent.Volume;
|
||||
audioRequest.speed = audioEmitterComponent.PlaybackSpeed;
|
||||
audioRequest.spatialized = audioEmitterComponent.Spatialized;
|
||||
audioRequest.Loop = audioEmitterComponent.Loop;
|
||||
audioRequest.position = transformComponent.GetGlobalTransform()[3];
|
||||
audioRequest.MinDistance = audioEmitterComponent.MinDistance;
|
||||
audioRequest.MaxDistance = audioEmitterComponent.MaxDistance;
|
||||
audioRequest.AttenuationFactor = audioEmitterComponent.AttenuationFactor;
|
||||
|
||||
if (isVoiceActive)
|
||||
{
|
||||
const std::string absoluteFilePath = FileSystem::RelativeToAbsolute(audioEmitterComponent.FilePath);
|
||||
|
||||
AudioRequest audioRequest;
|
||||
audioRequest.audioFile = absoluteFilePath;
|
||||
audioRequest.pan = audioEmitterComponent.Pan;
|
||||
audioRequest.volume = audioEmitterComponent.Volume;
|
||||
audioRequest.speed = audioEmitterComponent.PlaybackSpeed;
|
||||
|
||||
AudioManager::Get().QueueWavAudio(std::move(audioRequest));
|
||||
audioEmitterComponent.IsPlaying = false;
|
||||
if (!isPlaying && audioEmitterComponent.Loop)
|
||||
{
|
||||
audioManager.StopVoice(absoluteFilePath); // Stop audio
|
||||
}
|
||||
else
|
||||
{
|
||||
// Update the active voice with new params
|
||||
audioManager.UpdateVoice(audioRequest);
|
||||
}
|
||||
}
|
||||
else if (isPlaying)
|
||||
{
|
||||
// Reset the play status to false since the audio has been fired
|
||||
if (!audioEmitterComponent.Loop)
|
||||
{
|
||||
audioManager.QueueWavAudio(std::move(audioRequest));
|
||||
audioEmitterComponent.IsPlaying = false;
|
||||
}
|
||||
else if (!isVoiceActive)
|
||||
{
|
||||
audioManager.QueueWavAudio(std::move(audioRequest));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user