From 6923af145d07299cfaeacc2a9b2d3fb8890f4a41 Mon Sep 17 00:00:00 2001 From: Simon Weis Date: Mon, 25 Dec 2023 15:50:50 +0100 Subject: [PATCH] Add functionality to SoundDataBuilder to copy existing SoundData --- impl/oalpp/sound_data/sound_data_builder.cpp | 9 ++++++ impl/oalpp/sound_data/sound_data_builder.hpp | 5 +++ test/unit_tests/sound_data_builder_test.cpp | 32 ++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/impl/oalpp/sound_data/sound_data_builder.cpp b/impl/oalpp/sound_data/sound_data_builder.cpp index eb4da36..ce1c176 100644 --- a/impl/oalpp/sound_data/sound_data_builder.cpp +++ b/impl/oalpp/sound_data/sound_data_builder.cpp @@ -17,6 +17,15 @@ oalpp::SoundDataBuilder& oalpp::SoundDataBuilder::fromFile(std::string const& fi return *this; } +oalpp::SoundDataBuilder& oalpp::SoundDataBuilder::fromExistingSoundData(SoundData const& data) +{ + m_data = data.getSamples(); + m_sampleRate = data.getSampleRate(); + m_numberOfChannels = data.getNumberOfChannels(); + + return *this; +} + oalpp::SoundDataBuilder& oalpp::SoundDataBuilder::withEffect( oalpp::effects::MonoEffectInterface& effect) { diff --git a/impl/oalpp/sound_data/sound_data_builder.hpp b/impl/oalpp/sound_data/sound_data_builder.hpp index 5b9f44a..01d0cf8 100644 --- a/impl/oalpp/sound_data/sound_data_builder.hpp +++ b/impl/oalpp/sound_data/sound_data_builder.hpp @@ -19,6 +19,11 @@ class SoundDataBuilder { /// \return SoundDataBuilder object (fluent interface) [[nodiscard]] SoundDataBuilder& fromFile(std::string const& file); + /// Load SoundData content from an already existing SoundData instance + /// \param data SoundData to copy from + /// \return SoundDataBuilder object (fluent interface) + [[nodiscard]] SoundDataBuilder& fromExistingSoundData(SoundData const& data); + /// Apply an effect to the SoundData /// \param effect effect to be applied /// \return SoundDataBuilder object (fluent interface) diff --git a/test/unit_tests/sound_data_builder_test.cpp b/test/unit_tests/sound_data_builder_test.cpp index 4b13244..f26dab5 100644 --- a/test/unit_tests/sound_data_builder_test.cpp +++ b/test/unit_tests/sound_data_builder_test.cpp @@ -41,6 +41,22 @@ TEST_CASE("SoundDataBuilder", "[SoundDataBuilder]") REQUIRE(samples_loaded == samples_expected); } + SECTION("From Existing Sound Data") + { + oalpp::SoundData const initial_data = builder.fromFile("assets/test.mp3").create(); + + oalpp::SoundData const data = builder.fromExistingSoundData(initial_data).create(); + + REQUIRE(data.getNumberOfChannels() == 2); + REQUIRE(data.getSampleRate() == 44100); + + auto const& samples_loaded = data.getSamples(); + REQUIRE(samples_loaded == samples_expected); + + auto const& samples_loaded_initial = initial_data.getSamples(); + REQUIRE(samples_loaded_initial == samples_expected); + } + SECTION("With Gain Effect") { oalpp::effects::utility::Gain gain { 0.5f }; @@ -115,6 +131,22 @@ TEST_CASE("SoundDataBuilder", "[SoundDataBuilder]") REQUIRE(samples_loaded == samples_expected); } + SECTION("From Existing Sound Data") + { + oalpp::SoundData const initial_data = builder.fromFile("assets/test_mono.mp3").create(); + + oalpp::SoundData const data = builder.fromExistingSoundData(initial_data).create(); + + REQUIRE(data.getNumberOfChannels() == 1); + REQUIRE(data.getSampleRate() == 44100); + + auto const& samples_loaded = data.getSamples(); + REQUIRE(samples_loaded == samples_expected); + + auto const& samples_loaded_initial = initial_data.getSamples(); + REQUIRE(samples_loaded_initial == samples_expected); + } + SECTION("With Gain Effect") { oalpp::effects::utility::Gain gain { 0.5f };