Skip to content

Commit

Permalink
fix(engine): Unloading sound banks.
Browse files Browse the repository at this point in the history
Signed-off-by: Axel Nana <[email protected]>
  • Loading branch information
na2axl committed Oct 31, 2024
1 parent 5accf4a commit e97fc2a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 41 deletions.
25 changes: 25 additions & 0 deletions include/SparkyStudios/Audio/Amplitude/Core/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,31 @@ namespace SparkyStudios::Audio::Amplitude
*/
virtual void UnloadSoundBanks() = 0;

/**
* @brief Checks if a sound bank with the given filename has been loaded.
*
* @param[in] filename The file to check.
*
* @return `true` if the sound bank has been loaded, `false` otherwise.
*/
[[nodiscard]] virtual bool HasLoadedSoundBank(const AmOsString& filename) const = 0;

/**
* @brief Checks if a sound bank with the given ID has been loaded.
*
* @param[in] id The sound bank id to check.
*
* @return `true` if the sound bank has been loaded, `false` otherwise.
*/
[[nodiscard]] virtual bool HasLoadedSoundBank(AmBankID id) const = 0;

/**
* @brief Checks if any sound banks have been loaded.
*
* @return `true` if any sound banks have been loaded, `false` otherwise.
*/
[[nodiscard]] virtual bool HasLoadedSoundBanks() const = 0;

/**
* @brief Starts the loading of sound files referenced in loaded sound banks.
*
Expand Down
37 changes: 37 additions & 0 deletions src/Core/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,10 @@ namespace SparkyStudios::Audio::Amplitude
_state->sound_bank_map[id] = std::move(soundBank);
outID = id;
}
else
{
_state->sound_bank_id_map.erase(findIt);
}
}
else
{
Expand Down Expand Up @@ -944,6 +948,10 @@ namespace SparkyStudios::Audio::Amplitude
_state->sound_bank_map[id] = std::move(soundBank);
outID = id;
}
else
{
_state->sound_bank_id_map.erase(findIt);
}
}
else
{
Expand Down Expand Up @@ -1004,9 +1012,38 @@ namespace SparkyStudios::Audio::Amplitude

void EngineImpl::UnloadSoundBanks()
{
std::vector<AmBankID> idsToDelete;
idsToDelete.reserve(_state->sound_bank_map.size());

for (const auto& item : _state->sound_bank_map | std::ranges::views::values)
{
if (RefCounter* ref = item->GetRefCounter(); ref->GetCount() > 0 && ref->Decrement() == 0)
{
item->Deinitialize(this);
idsToDelete.push_back(item->GetId());
}
}

for (const auto id : idsToDelete)
_state->sound_bank_map.erase(id);
}

bool EngineImpl::HasLoadedSoundBank(const AmOsString& filename) const
{
if (const auto findIt = _state->sound_bank_id_map.find(filename); findIt != _state->sound_bank_id_map.end())
return _state->sound_bank_map.contains(findIt->second);

return false;
}

bool EngineImpl::HasLoadedSoundBank(AmBankID id) const
{
return _state->sound_bank_map.contains(id);
}

bool EngineImpl::HasLoadedSoundBanks() const
{
return !_state->sound_bank_map.empty();
}

void EngineImpl::StartOpenFileSystem()
Expand Down
3 changes: 3 additions & 0 deletions src/Core/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ namespace SparkyStudios::Audio::Amplitude
void UnloadSoundBank(const AmOsString& filename) override;
void UnloadSoundBank(AmBankID id) override;
void UnloadSoundBanks() override;
[[nodiscard]] bool HasLoadedSoundBank(const AmOsString& filename) const override;
[[nodiscard]] bool HasLoadedSoundBank(AmBankID id) const override;
[[nodiscard]] bool HasLoadedSoundBanks() const override;
void StartLoadSoundFiles() override;
bool TryFinalizeLoadSoundFiles() override;
[[nodiscard]] SwitchContainerHandle GetSwitchContainerHandle(const AmString& name) const override;
Expand Down
40 changes: 0 additions & 40 deletions tests/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,6 @@ TEST_CASE("Engine Tests", "[engine][core][amplitude]")
REQUIRE_FALSE(e3.Valid());
REQUIRE_FALSE(e4.Valid());
REQUIRE_FALSE(e5.Valid());

amEngine->UnloadSoundBank(AM_OS_STRING("tests.init.ambank"));
}

THEN("it can register listeners")
Expand Down Expand Up @@ -288,8 +286,6 @@ TEST_CASE("Engine Tests", "[engine][core][amplitude]")
REQUIRE_FALSE(l3.Valid());
REQUIRE_FALSE(l4.Valid());
REQUIRE_FALSE(l5.Valid());

amEngine->UnloadSoundBank(AM_OS_STRING("tests.init.ambank"));
}

THEN("it can register environments")
Expand Down Expand Up @@ -320,8 +316,6 @@ TEST_CASE("Engine Tests", "[engine][core][amplitude]")
REQUIRE_FALSE(e3.Valid());
REQUIRE_FALSE(e4.Valid());
REQUIRE_FALSE(e5.Valid());

amEngine->UnloadSoundBank(AM_OS_STRING("tests.init.ambank"));
}

THEN("it can register rooms")
Expand Down Expand Up @@ -352,26 +346,20 @@ TEST_CASE("Engine Tests", "[engine][core][amplitude]")
REQUIRE_FALSE(r3.Valid());
REQUIRE_FALSE(r4.Valid());
REQUIRE_FALSE(r5.Valid());

amEngine->UnloadSoundBank(AM_OS_STRING("tests.init.ambank"));
}

THEN("it can access sound assets by names")
{
REQUIRE(amEngine->GetSoundHandle("symphony") != nullptr);
REQUIRE(amEngine->GetSoundHandle("AMB_Forest") != nullptr);
REQUIRE(amEngine->GetSoundHandle("throw_01") != nullptr);

amEngine->UnloadSoundBank(AM_OS_STRING("tests.init.ambank"));
}

THEN("it can access sound assets by IDs")
{
REQUIRE(amEngine->GetSoundHandle(101) != nullptr);
REQUIRE(amEngine->GetSoundHandle(100) != nullptr);
REQUIRE(amEngine->GetSoundHandle(1) != nullptr);

amEngine->UnloadSoundBank(AM_OS_STRING("tests.init.ambank"));
}

THEN("it accesses the same sound assets when fetching by name or ID")
Expand All @@ -383,16 +371,12 @@ TEST_CASE("Engine Tests", "[engine][core][amplitude]")

REQUIRE(amEngine->GetSoundHandle(name) == amEngine->GetSoundHandle(id));
}

amEngine->UnloadSoundBank(AM_OS_STRING("tests.init.ambank"));
}

THEN("it can load the same sound bank again")
{
REQUIRE(amEngine->LoadSoundBank(AM_OS_STRING("tests.init.ambank")));
amEngine->UnloadSoundBank(AM_OS_STRING("tests.init.ambank"));

amEngine->UnloadSoundBank(AM_OS_STRING("tests.init.ambank"));
}

THEN("it can load other sound banks")
Expand All @@ -415,8 +399,6 @@ TEST_CASE("Engine Tests", "[engine][core][amplitude]")

Thread::Sleep(2000); // wait for the sound to finish playing
REQUIRE_FALSE(channel.Playing());

amEngine->UnloadSoundBank(AM_OS_STRING("tests.init.ambank"));
}

THEN("engine can play a sound using its ID")
Expand All @@ -429,8 +411,6 @@ TEST_CASE("Engine Tests", "[engine][core][amplitude]")

Thread::Sleep(2000); // wait for the sound to finish playing
REQUIRE_FALSE(channel.Playing());

amEngine->UnloadSoundBank(AM_OS_STRING("tests.init.ambank"));
}

THEN("engine can play a sound using its name")
Expand All @@ -443,8 +423,6 @@ TEST_CASE("Engine Tests", "[engine][core][amplitude]")

Thread::Sleep(1000); // wait for the sound to finish playing
REQUIRE_FALSE(channel.Playing());

amEngine->UnloadSoundBank(AM_OS_STRING("tests.init.ambank"));
}

THEN("engine can play a collection using its handle")
Expand All @@ -459,8 +437,6 @@ TEST_CASE("Engine Tests", "[engine][core][amplitude]")

Thread::Sleep(kAmSecond * 5); // wait for the sound to finish playing
REQUIRE_FALSE(channel.Playing());

amEngine->UnloadSoundBank(AM_OS_STRING("tests.init.ambank"));
}

THEN("engine can play a collection using its ID")
Expand All @@ -473,8 +449,6 @@ TEST_CASE("Engine Tests", "[engine][core][amplitude]")

Thread::Sleep(kAmSecond * 3); // wait for the sound to finish playing
REQUIRE_FALSE(channel.Playing());

amEngine->UnloadSoundBank(AM_OS_STRING("tests.init.ambank"));
}

THEN("engine can play a collection using its name")
Expand All @@ -487,8 +461,6 @@ TEST_CASE("Engine Tests", "[engine][core][amplitude]")

Thread::Sleep(kAmSecond * 3); // wait for the sound to finish playing
REQUIRE_FALSE(channel.Playing());

amEngine->UnloadSoundBank(AM_OS_STRING("tests.init.ambank"));
}

THEN("engine can play a switch container using its handle")
Expand All @@ -507,8 +479,6 @@ TEST_CASE("Engine Tests", "[engine][core][amplitude]")

Thread::Sleep(1000); // wait for the sound to finish playing
REQUIRE_FALSE(channel.Playing());

amEngine->UnloadSoundBank(AM_OS_STRING("tests.init.ambank"));
}

THEN("engine can play a switch container using its ID")
Expand All @@ -526,8 +496,6 @@ TEST_CASE("Engine Tests", "[engine][core][amplitude]")

Thread::Sleep(1000); // wait for the sound to finish playing
REQUIRE_FALSE(channel.Playing());

amEngine->UnloadSoundBank(AM_OS_STRING("tests.init.ambank"));
}

THEN("engine can play a switch container using its name")
Expand All @@ -545,8 +513,6 @@ TEST_CASE("Engine Tests", "[engine][core][amplitude]")

Thread::Sleep(1000); // wait for the sound to finish playing
REQUIRE_FALSE(channel.Playing());

amEngine->UnloadSoundBank(AM_OS_STRING("tests.init.ambank"));
}

GIVEN("a playing channel")
Expand Down Expand Up @@ -681,8 +647,6 @@ TEST_CASE("Engine Tests", "[engine][core][amplitude]")

if (channel.Valid())
channel.Stop(0);

amEngine->UnloadSoundBank(AM_OS_STRING("tests.init.ambank"));
}

GIVEN("a registered bus")
Expand Down Expand Up @@ -749,8 +713,6 @@ TEST_CASE("Engine Tests", "[engine][core][amplitude]")
bus.Clear();
REQUIRE_FALSE(bus.Valid());
}

amEngine->UnloadSoundBank(AM_OS_STRING("tests.init.ambank"));
}

GIVEN("an environment")
Expand Down Expand Up @@ -796,8 +758,6 @@ TEST_CASE("Engine Tests", "[engine][core][amplitude]")
}
}
}

amEngine->UnloadSoundBank(AM_OS_STRING("tests.init.ambank"));
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ struct AmTestListener : Catch::EventListenerBase

if (amEngine->IsInitialized())
{
amEngine->UnloadSoundBanks();
while (amEngine->HasLoadedSoundBanks())
amEngine->UnloadSoundBanks();

amEngine->Deinitialize();

Expand Down

0 comments on commit e97fc2a

Please sign in to comment.