Skip to content

Commit

Permalink
fix: Stability fixes.
Browse files Browse the repository at this point in the history
Signed-off-by: Axel Nana <[email protected]>
  • Loading branch information
na2axl committed Nov 1, 2024
1 parent ffc6b69 commit e372e2d
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 13 deletions.
4 changes: 2 additions & 2 deletions sample_project/rtpc/rtpc_player_height.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
"enabled": false,
"fade_attack": {
"duration": 1,
"fader": "Exponential"
"fader": "Linear"
},
"fade_release": {
"duration": 1,
"fader": "Exponential"
"fader": "Linear"
}
}
}
17 changes: 17 additions & 0 deletions sample_project/rtpc/wind_force.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"id": 2,
"name": "wind_force",
"min_value": 0,
"max_value": 343000,
"fade_settings": {
"enabled": true,
"fade_attack": {
"duration": 1000,
"fader": "Linear"
},
"fade_release": {
"duration": 500,
"fader": "Linear"
}
}
}
3 changes: 2 additions & 1 deletion sample_project/soundbanks/init.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"footsteps.amswitchcontainer"
],
"rtpc": [
"rtpc_player_height.amrtpc"
"rtpc_player_height.amrtpc",
"wind_force.amrtpc"
]
}
3 changes: 2 additions & 1 deletion sample_project/soundbanks/tests.init.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"footsteps.amswitchcontainer"
],
"rtpc": [
"rtpc_player_height.amrtpc"
"rtpc_player_height.amrtpc",
"wind_force.amrtpc"
]
}
5 changes: 4 additions & 1 deletion src/Core/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1840,7 +1840,10 @@ namespace SparkyStudios::Audio::Amplitude

void EngineImpl::SetDefaultListener(const Listener* listener)
{
if (listener->Valid())
if (listener == nullptr)
_defaultListener = nullptr;

else if (listener->Valid())
_defaultListener = listener->GetState();
}

Expand Down
10 changes: 4 additions & 6 deletions src/Sound/Rtpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,21 @@ namespace SparkyStudios::Audio::Amplitude
void RtpcImpl::Update(AmTime deltaTime)
{
if (_faderRelease && _currentValue > _targetValue)
{
_currentValue = _faderRelease->GetFromTime(Engine::GetInstance()->GetTotalTime());
}

if (_faderAttack && _currentValue < _targetValue)
{
_currentValue = _faderAttack->GetFromTime(Engine::GetInstance()->GetTotalTime());
}

_currentValue = std::clamp(_currentValue, _minValue, _maxValue);
}

void RtpcImpl::SetValue(AmReal64 value)
{
_targetValue = AM_CLAMP(value, _minValue, _maxValue);
_targetValue = std::clamp(value, _minValue, _maxValue);

if (_faderAttack == nullptr || _faderRelease == nullptr)
{
_currentValue = value;
_currentValue = _targetValue;
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ add_executable(${PROJECT_NAME}
target_link_libraries(${PROJECT_NAME} PRIVATE Catch2::Catch2 Static)

add_custom_target(ss_amplitude_audio_test_package
COMMAND $<TARGET_FILE:ampk> -v -c 0 ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/samples/assets ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/samples/assets.ampk
COMMAND $<TARGET_FILE:ampk> -q -c 0 "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/samples/assets" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/samples/assets.ampk"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)

Expand Down
156 changes: 156 additions & 0 deletions tests/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,33 @@ TEST_CASE("Engine Tests", "[engine][core][amplitude]")
amEngine->SetDefaultListener(1);
REQUIRE(amEngine->GetDefaultListener().GetState() == listener.GetState());

THEN("it can set its master gain")
{
amEngine->SetMasterGain(0.1f);
REQUIRE(amEngine->GetMasterGain() == 0.1f);

amEngine->SetMasterGain(1.0f);
REQUIRE(amEngine->GetMasterGain() == 1.0f);
}

THEN("it can be muted")
{
amEngine->SetMute(true);
REQUIRE(amEngine->IsMuted());

amEngine->SetMute(false);
REQUIRE_FALSE(amEngine->IsMuted());
}

THEN("it can be paused")
{
amEngine->Pause(true);
REQUIRE(amEngine->IsPaused());

amEngine->Pause(false);
REQUIRE_FALSE(amEngine->IsPaused());
}

THEN("it can load sound files")
{
REQUIRE(amEngine->TryFinalizeLoadSoundFiles());
Expand Down Expand Up @@ -286,6 +313,15 @@ TEST_CASE("Engine Tests", "[engine][core][amplitude]")
REQUIRE_FALSE(l3.Valid());
REQUIRE_FALSE(l4.Valid());
REQUIRE_FALSE(l5.Valid());

AND_THEN("it ca set the default listener")
{
amEngine->SetDefaultListener(&l1);
REQUIRE(amEngine->GetDefaultListener().GetState() == l1.GetState());

amEngine->SetDefaultListener(nullptr);
REQUIRE_FALSE(amEngine->GetDefaultListener().Valid());
}
}

THEN("it can register environments")
Expand Down Expand Up @@ -541,6 +577,22 @@ TEST_CASE("Engine Tests", "[engine][core][amplitude]")
SwitchHandle invalidSwitch2 = amEngine->GetSwitchHandle(99999);
REQUIRE(invalidSwitch2 == nullptr);

AND_THEN("engine cannot set switch states of invalid switch IDs")
{
// This is to increase coverage, the methods will effectively do nothing with invalid handles
amEngine->SetSwitchState(99999, 1);
amEngine->SetSwitchState(99999, "unknown");
amEngine->SetSwitchState(99999, SwitchState());
}

AND_THEN("engine cannot set switch states of invalid switch names")
{
// This is to increase coverage, the methods will effectively do nothing with invalid handles
amEngine->SetSwitchState("invalid_switch", 1);
amEngine->SetSwitchState("invalid_switch", "unknown");
amEngine->SetSwitchState("invalid_switch", SwitchState());
}

AND_THEN("engine cannot set switch states of invalid switch handles")
{
// This is to increase coverage, the methods will effectively do nothing with invalid handles
Expand All @@ -550,6 +602,45 @@ TEST_CASE("Engine Tests", "[engine][core][amplitude]")
}
}

THEN("engine can load rtpc handles by name")
{
RtpcHandle rtpc1 = amEngine->GetRtpcHandle("rtpc_player_height");
REQUIRE(rtpc1 != nullptr);
}

THEN("engine can load rtpc handles by ID")
{
RtpcHandle rtpc1 = amEngine->GetRtpcHandle(1);
REQUIRE(rtpc1 != nullptr);
}

THEN("engine cannot load rtpc handles with invalid names or IDs")
{
RtpcHandle invalidRtpc1 = amEngine->GetRtpcHandle("invalid_rtpc");
REQUIRE(invalidRtpc1 == nullptr);

RtpcHandle invalidRtpc2 = amEngine->GetRtpcHandle(99999);
REQUIRE(invalidRtpc2 == nullptr);

AND_THEN("engine cannot set rtpc values of invalid rtpc IDs")
{
// This is to increase coverage, the methods will effectively do nothing with invalid handles
amEngine->SetRtpcValue(99999, 1);
}

AND_THEN("engine cannot set rtpc values of invalid rtpc names")
{
// This is to increase coverage, the methods will effectively do nothing with invalid handles
amEngine->SetRtpcValue("invalid_rtpc", 1);
}

AND_THEN("engine cannot set rtpc values of invalid rtpc handles")
{
// This is to increase coverage, the methods will effectively do nothing with invalid handles
amEngine->SetRtpcValue(invalidRtpc1, 1);
}
}

GIVEN("a playing channel")
{
AmVec3 location = { 10.0f, 20.0f, 30.0f };
Expand Down Expand Up @@ -896,6 +987,71 @@ TEST_CASE("Engine Tests", "[engine][core][amplitude]")
}
}
}

GIVEN("a rtpc")
{
RtpcHandle rtpc1 = amEngine->GetRtpcHandle(1);
REQUIRE(rtpc1 != nullptr);

RtpcHandle rtpc2 = amEngine->GetRtpcHandle("wind_force");
REQUIRE(rtpc2 != nullptr);

THEN("it can change its value")
{
rtpc1->SetValue(50);
REQUIRE(rtpc1->GetValue() == 50.0);

rtpc2->SetValue(1000);
REQUIRE_FALSE(rtpc2->GetValue() == 1000);
amEngine->WaitUntilFrames(65);
REQUIRE(std::abs(rtpc2->GetValue() - 1000.0) < kEpsilon);

AND_THEN("engine can change its value by ID")
{
amEngine->SetRtpcValue(rtpc1->GetId(), 75);
REQUIRE(rtpc1->GetValue() == 75.0);

amEngine->SetRtpcValue(rtpc2->GetId(), 75);
REQUIRE_FALSE(rtpc2->GetValue() == 75.0);
amEngine->WaitUntilFrames(35);
REQUIRE(std::abs(rtpc2->GetValue() - 75.0) < kEpsilon);
}

AND_THEN("engine can change its value by name")
{
amEngine->SetRtpcValue(rtpc1->GetName(), 80);
REQUIRE(rtpc1->GetValue() == 80.0);

amEngine->SetRtpcValue(rtpc2->GetId(), 75000);
REQUIRE_FALSE(rtpc2->GetValue() == 75000.0);
amEngine->WaitUntilFrames(65);
REQUIRE(std::abs(rtpc2->GetValue() - 75000.0) < kEpsilon);
}

AND_THEN("engine can change its value by handle")
{
amEngine->SetRtpcValue(rtpc1, 90);
REQUIRE(rtpc1->GetValue() == 90.0);

amEngine->SetRtpcValue(rtpc2->GetId(), 90);
REQUIRE_FALSE(rtpc2->GetValue() == 90.0);
amEngine->WaitUntilFrames(35);
REQUIRE(std::abs(rtpc2->GetValue() - 90.0) < kEpsilon);
}
}

THEN("it cannot set values higher than max")
{
rtpc1->SetValue(rtpc1->GetMaxValue() * 2);
REQUIRE(rtpc1->GetValue() == rtpc1->GetMaxValue());
}

THEN("it cannot set values lower than min")
{
rtpc1->SetValue(rtpc1->GetMinValue() * -2);
REQUIRE(rtpc1->GetValue() == rtpc1->GetMinValue());
}
}
}
}
}
2 changes: 1 addition & 1 deletion tests/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ TEST_CASE("Thread Tests", "[thread][amplitude]")
SECTION("can correctly sleep a thread for a duration")
{
const auto start = Thread::GetTimeMillis();
Thread::Sleep(100);
Thread::Sleep(105);
const auto end = Thread::GetTimeMillis();

REQUIRE((end - start) >= 100); // Should at least run for 100ms
Expand Down

0 comments on commit e372e2d

Please sign in to comment.