Skip to content

Commit

Permalink
fix: Unhandled cases in Attenuation Node fixed.
Browse files Browse the repository at this point in the history
Signed-off-by: Axel Nana <[email protected]>
  • Loading branch information
na2axl committed Oct 6, 2024
1 parent 4e3580e commit c27706f
Showing 1 changed file with 24 additions and 33 deletions.
57 changes: 24 additions & 33 deletions src/Mixer/Nodes/AttenuationNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,36 +209,36 @@ namespace SparkyStudios::Audio::Amplitude
const auto* layer = GetLayer();

const Attenuation* attenuation = layer->GetAttenuation();
if (attenuation == nullptr)
return input;

const Listener& listener = layer->GetListener();

AmReal32 targetGain = 1.0f;

if (attenuation != nullptr)
// Compute attenuated gain based on spatialization
{
const eSpatialization spatialization = layer->GetSpatialization();

if (listener.Valid())
{
if (spatialization != eSpatialization_None)
const Entity& entity = layer->GetEntity();

if (spatialization == eSpatialization_PositionOrientation)
{
AMPLITUDE_ASSERT(entity.Valid());
targetGain *= attenuation->GetGain(entity, listener);
}
else if (spatialization == eSpatialization_HRTF && entity.Valid())
{
targetGain *= attenuation->GetGain(entity, listener);
}
else if (spatialization == eSpatialization_Position)
{
const Entity& entity = layer->GetEntity();

if (spatialization == eSpatialization_PositionOrientation)
{
AMPLITUDE_ASSERT(entity.Valid());
targetGain *= attenuation->GetGain(entity, listener);
}
else if (spatialization == eSpatialization_HRTF && entity.Valid())
{
targetGain *= attenuation->GetGain(entity, listener);
}
else
{
const AmVec3& location = layer->GetLocation();

// Position-based spatialization, or HRTF-based spatialization without entity
targetGain *= attenuation->GetGain(location, listener);
}
const AmVec3& location = layer->GetLocation();

// Position-based spatialization, or HRTF-based spatialization without entity
targetGain *= attenuation->GetGain(location, listener);
}
}
else
Expand All @@ -264,25 +264,16 @@ namespace SparkyStudios::Audio::Amplitude
if (Gain::IsZero(targetGain))
return nullptr;

if (Gain::IsOne(targetGain))
return input;

_output = *input;

// Apply gain attenuation
for (AmSize c = 0; c < _output.GetChannelCount(); ++c)
Gain::ApplyReplaceConstantGain(targetGain, input->GetChannel(c), 0, _output[c], 0, input->GetFrameCount());
if (!Gain::IsOne(targetGain))
for (AmSize c = 0; c < _output.GetChannelCount(); ++c)
Gain::ApplyReplaceConstantGain(targetGain, input->GetChannel(c), 0, _output[c], 0, input->GetFrameCount());

// Apply air absorption EQ filter
if (attenuation->IsAirAbsorptionEnabled() && listener.Valid())
{
const AmVec3& soundLocation = layer->GetLocation();
const AmVec3& listenerLocation = listener.GetLocation();

auto sampleRate = layer->GetSampleRate();

_eqFilter.Process(_output, _output, sampleRate);
}
_eqFilter.Process(_output, _output, layer->GetSampleRate());

return &_output;
}
Expand Down

0 comments on commit c27706f

Please sign in to comment.