Skip to content

Commit

Permalink
fix: Stability fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
na2axl committed Nov 18, 2024
1 parent 156bdcc commit 6e0a1a4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 37 deletions.
8 changes: 4 additions & 4 deletions include/SparkyStudios/Audio/Amplitude/Sound/Switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace SparkyStudios::Audio::Amplitude
*
* For example, you can have a switch named `SurfaceType` which have `wood`, `grass`, `metal` and `water` as states. A
* `SwitchContainer` using this switch can group sounds per switch states, so when a state is active, all the sounds of
* that state are played. Changing the state of a `Switch` will updated ALL the `SwitchContainer` objects that use this `Switch`.
* that state are played. Changing the state of a `Switch` will update ALL the `SwitchContainer` objects that use this `Switch`.
*
* The `Switch` is a shared object between sound sources. They are used only by `SwitchContainer` objects.
*
Expand All @@ -100,7 +100,7 @@ namespace SparkyStudios::Audio::Amplitude
/**
* @brief Sets the current state of the switch.
*
* @note Changing the state of a `Switch` will updated ALL the `SwitchContainer` objects that use this `Switch`.
* @note Changing the state of a `Switch` will update ALL the `SwitchContainer` objects that use this `Switch`.
*
* @param[in] state The state to apply to the switch.
*/
Expand All @@ -109,7 +109,7 @@ namespace SparkyStudios::Audio::Amplitude
/**
* @brief Sets the current state of the switch using the state ID.
*
* @note Changing the state of a `Switch` will updated ALL the `SwitchContainer` objects that use this `Switch`.
* @note Changing the state of a `Switch` will update ALL the `SwitchContainer` objects that use this `Switch`.
*
* @param[in] id The ID of the state to apply. This ID should exist in the list
* of registered switch states.
Expand All @@ -119,7 +119,7 @@ namespace SparkyStudios::Audio::Amplitude
/**
* @brief Sets the current state of the switch using the state name.
*
* @note Changing the state of a `Switch` will updated ALL the `SwitchContainer` objects that use this `Switch`.
* @note Changing the state of a `Switch` will update ALL the `SwitchContainer` objects that use this `Switch`.
*
* @param[in] name The name of the state to apply. This name should exist in the
* list of registered switch states.
Expand Down
2 changes: 1 addition & 1 deletion samples/sample_01/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ static void run(AmVoidPtr param)
Engine::AddPluginSearchPath(AM_OS_STRING("./assets/plugins"));
Engine::AddPluginSearchPath(sdkPath / AM_OS_STRING("lib/" AM_SDK_PLATFORM "/plugins"));

#if defined(_DEBUG) || defined(DEBUG) || (defined(__GNUC__) && !defined(__OPTIMIZE__))
#ifdef AM_DEBUG
Engine::LoadPlugin(AM_OS_STRING("AmplitudeVorbisCodecPlugin_d"));
Engine::LoadPlugin(AM_OS_STRING("AmplitudeFlacCodecPlugin_d"));
#else
Expand Down
4 changes: 2 additions & 2 deletions src/DSP/Filters/BiquadResonantFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ namespace SparkyStudios::Audio::Amplitude

void BiquadResonantFilterInstance::ComputeBiquadResonantParams()
{
if (m_parameters[BiquadResonantFilter::ATTRIBUTE_TYPE] == BiquadResonantFilter::TYPE_DUAL_BAND_HIGH_PASS ||
m_parameters[BiquadResonantFilter::ATTRIBUTE_TYPE] == BiquadResonantFilter::TYPE_DUAL_BAND_LOW_PASS)
if (const auto type = static_cast<BiquadResonantFilter::TYPE>(m_parameters[BiquadResonantFilter::ATTRIBUTE_TYPE]);
type == BiquadResonantFilter::TYPE_DUAL_BAND_HIGH_PASS || type == BiquadResonantFilter::TYPE_DUAL_BAND_LOW_PASS)
{
const AmReal32 k =
std::tan(M_PI * m_parameters[BiquadResonantFilter::ATTRIBUTE_FREQUENCY] / static_cast<AmReal32>(_sampleRate));
Expand Down
54 changes: 26 additions & 28 deletions src/Mixer/Nodes/AttenuationNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,28 @@ namespace SparkyStudios::Audio::Amplitude
{
constexpr AmReal32 kQ = 0.707107f; // sqrt(0.5)

void AirAbsorptionEQFilter::Normalize(std::array<AmReal32, kAmAirAbsorptionBandCount>& gains, AmReal32& overallGain)
{
if (const auto maxGain = std::max({ gains[0], gains[1], gains[2] }); maxGain < kEpsilon)
{
overallGain = 0.0f;
for (auto i = 0; i < kAmAirAbsorptionBandCount; ++i)
gains[i] = 1.0f;
}
else
{
for (auto i = 0; i < kAmAirAbsorptionBandCount; ++i)
{
constexpr AmReal32 kMaxEQGain = 0.0625f;

gains[i] /= maxGain;
gains[i] = std::max(gains[i], kMaxEQGain);
}

overallGain *= maxGain;
}
}

AirAbsorptionEQFilter::AirAbsorptionEQFilter()
: _eqFilterFactory()
, _lowShelfFilter{ nullptr, nullptr }
Expand Down Expand Up @@ -140,30 +162,6 @@ namespace SparkyStudios::Audio::Amplitude
}
}

void AirAbsorptionEQFilter::Normalize(std::array<AmReal32, kAmAirAbsorptionBandCount>& gains, AmReal32& overallGain)
{
constexpr AmReal32 kMaxEQGain = 0.0625f;

auto maxGain = std::max({ gains[0], gains[1], gains[2] });

if (maxGain < kEpsilon)
{
overallGain = 0.0f;
for (auto i = 0; i < kAmAirAbsorptionBandCount; ++i)
gains[i] = 1.0f;
}
else
{
for (auto i = 0; i < kAmAirAbsorptionBandCount; ++i)
{
gains[i] /= maxGain;
gains[i] = std::max(gains[i], kMaxEQGain);
}

overallGain *= maxGain;
}
}

void AirAbsorptionEQFilter::EnsureFilters()
{
for (AmUInt32 i = 0; i < 2; ++i)
Expand Down Expand Up @@ -248,6 +246,9 @@ namespace SparkyStudios::Audio::Amplitude
}
}

if (Gain::IsZero(targetGain))
return nullptr;

// Set and normalize gains
if (attenuation->IsAirAbsorptionEnabled() && listener.Valid())
{
Expand All @@ -257,13 +258,10 @@ namespace SparkyStudios::Audio::Amplitude
for (AmUInt32 i = 0; i < kAmAirAbsorptionBandCount; ++i)
_gains[i] = attenuation->EvaluateAirAbsorption(soundLocation, listenerLocation, i);

_eqFilter.Normalize(_gains, targetGain);
AirAbsorptionEQFilter::Normalize(_gains, targetGain);
_eqFilter.SetGains(_gains[0], _gains[1], _gains[2]);
}

if (Gain::IsZero(targetGain))
return nullptr;

_output = *input;

// Apply gain attenuation
Expand Down
4 changes: 2 additions & 2 deletions src/Mixer/Nodes/AttenuationNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ namespace SparkyStudios::Audio::Amplitude
class AirAbsorptionEQFilter final
{
public:
static void Normalize(std::array<AmReal32, kAmAirAbsorptionBandCount>& gains, AmReal32& overallGain);

AirAbsorptionEQFilter();
~AirAbsorptionEQFilter();

void SetGains(AmReal32 gainLow, AmReal32 gainMid, AmReal32 gainHigh);

void Process(const AudioBuffer& input, AudioBuffer& output, AmReal32 sampleRate);

void Normalize(std::array<AmReal32, kAmAirAbsorptionBandCount>& gains, AmReal32& overallGain);

private:
void EnsureFilters();
void ApplyFilters(AmUInt32 set, const AudioBuffer& input, AudioBuffer& output, AmReal32 sampleRate);
Expand Down

0 comments on commit 6e0a1a4

Please sign in to comment.