Skip to content

Commit

Permalink
Merge pull request #91 from Laguna1989/feaature/BuilderFromSoundData
Browse files Browse the repository at this point in the history
Add functionality to SoundDataBuilder to copy existing SoundData
  • Loading branch information
Laguna1989 authored Dec 25, 2023
2 parents 8f154f6 + 6923af1 commit 5da288a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
9 changes: 9 additions & 0 deletions impl/oalpp/sound_data/sound_data_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
5 changes: 5 additions & 0 deletions impl/oalpp/sound_data/sound_data_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions test/unit_tests/sound_data_builder_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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 };
Expand Down

0 comments on commit 5da288a

Please sign in to comment.