Skip to content

Commit

Permalink
Replace different instances of SoundData with SoundDataBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
Laguna1989 committed Dec 18, 2023
1 parent 7327522 commit d9b87b1
Show file tree
Hide file tree
Showing 26 changed files with 338 additions and 553 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test_verification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ jobs:
Import-Certificate -FilePath Scream\Install\driver\x64\Scream.cat -CertStoreLocation Cert:\LocalMachine\TrustedPublisher
Scream\Install\helpers\devcon-x64.exe install Scream\Install\driver\x64\Scream.inf *Scream
- name: Copy assets
run: cp -r assets ${{github.workspace}}/build/test/unit_tests/Debug

- name: Test
working-directory: ${{github.workspace}}/build
run: test/unit_tests/Debug/OpenALpp_UnitTests.exe --order rand
Expand Down
4 changes: 2 additions & 2 deletions impl/oalpp/sound_data.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef OPENALPP_SOUND_DATA_INCLUDE_HPP
#define OPENALPP_SOUND_DATA_INCLUDE_HPP

#include "oalpp/sound_data/sound_data.hpp"
#include "oalpp/sound_data/sound_data_with_effect.hpp"
#include <oalpp/sound_data/sound_data_builder.hpp>
#include <oalpp/sound_data/sound_data_interface.hpp>

#endif // OPENALPP_SOUND_DATA_INCLUDE_HPP
25 changes: 18 additions & 7 deletions impl/oalpp/sound_data/sound_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,26 @@

namespace oalpp {

SoundData::SoundData(std::string const& fileName)
SoundData::SoundData(std::vector<float> const& data, int sampleRate, int numberOfChannels)
: m_samples { data }
, m_sampleRate { sampleRate }
, m_numberOfChannels { numberOfChannels }
{
auto fileData = std::make_unique<nqr::AudioData>();
nqr::NyquistIO loader;
loader.Load(fileData.get(), fileName);
}

m_numberOfChannels = fileData->channelCount;
m_sampleRate = fileData->sampleRate;
m_samples = std::move(fileData->samples);
SoundData::SoundData(SoundData&& data) noexcept
: m_samples { std::move(data.m_samples) }
, m_sampleRate { std::exchange(data.m_sampleRate, 0) }
, m_numberOfChannels { std::exchange(data.m_numberOfChannels, 0) }
{
}

SoundData& SoundData::operator=(SoundData&& other) noexcept
{
m_samples = std::move(other.m_samples);
m_sampleRate = std::exchange(other.m_sampleRate, 0);
m_numberOfChannels = std::exchange(other.m_numberOfChannels, 0);
return *this;
}

int SoundData::getNumberOfChannels() const { return m_numberOfChannels; }
Expand Down
12 changes: 10 additions & 2 deletions impl/oalpp/sound_data/sound_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@
#define OPENALPP_SOUND_DATA_HPP

#include "sound_data_interface.hpp"
#include <string>

namespace oalpp {

class SoundData : public SoundDataInterface {
public:
explicit SoundData(std::string const& fileName);
SoundData(std::vector<float> const& data, int sampleRate, int numberOfChannels);
SoundData() = delete;
~SoundData() override = default;

SoundData(SoundData const& data) = delete;
SoundData(SoundData&& data) noexcept;

SoundData& operator=(SoundData const&) = delete;
SoundData& operator=(SoundData&&) noexcept;


int getNumberOfChannels() const override;
int getSampleRate() const override;
Expand Down
120 changes: 120 additions & 0 deletions impl/oalpp/sound_data/sound_data_builder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#include "sound_data_builder.hpp"
#include <libnyquist/Decoders.h>

oalpp::SoundDataBuilder& oalpp::SoundDataBuilder::fromFile(std::string const& file)
{
nqr::NyquistIO loader;
auto const audioData = std::make_unique<nqr::AudioData>();
loader.Load(audioData.get(), file);
m_data = std::move(audioData->samples);
m_sampleRate = audioData->sampleRate;
m_numberOfChannels = audioData->channelCount;
return *this;
}

oalpp::SoundData oalpp::SoundDataBuilder::create()
{
return oalpp::SoundData { m_data, m_sampleRate, m_numberOfChannels };
}

oalpp::SoundDataBuilder& oalpp::SoundDataBuilder::toString(std::string& str)
{
str = "# Sound Data\n";
str += "# sampleRate: " + std::to_string(m_sampleRate) + "\n";
str += "# numberOfChannels: " + std::to_string(m_numberOfChannels) + "\n";

for (auto index = 0U; index != m_data.size(); ++index) {
str += std::to_string(index) + " " + std::to_string(m_data[index]) + "\n";
}
str += "\n";

return *this;
}

oalpp::SoundDataBuilder& oalpp::SoundDataBuilder::withEffect(
oalpp::effects::MonoEffectInterface& effect)
{
if (m_numberOfChannels == 1) {
m_data = effect.process(m_data);
} else {
std::vector<float> lefts;
std::vector<float> rights;
auto const halfSize = m_data.size() / 2;
lefts.resize(halfSize);
rights.resize(halfSize);

bool toggle = false;
std::partition_copy(
m_data.begin(), m_data.end(), lefts.begin(), rights.begin(), [&toggle](float) {
toggle = !toggle;
return toggle;
});

std::vector<float> const leftsProcessed = effect.process(lefts);
std::vector<float> const rightsProcessed = effect.process(rights);

m_data.resize(leftsProcessed.size() * 2U);
for (auto i = 0U; i != leftsProcessed.size(); ++i) {
m_data[i * 2U + 0U] = leftsProcessed[i];
m_data[i * 2U + 1U] = rightsProcessed[i];
}
}

return *this;
}

oalpp::SoundDataBuilder& oalpp::SoundDataBuilder::onlyLeftChannel()
{
if (m_numberOfChannels != 2) {
throw std::invalid_argument { "Can not use left channel from mono sound data." };
}

std::vector<float> samples;
samples.resize(m_data.size() / 2);

for (auto index = 0U; index != samples.size(); ++index) {
samples.at(index) = m_data.at(index * 2);
}

m_numberOfChannels = 1;
m_data = samples;
return *this;
}

oalpp::SoundDataBuilder& oalpp::SoundDataBuilder::onlyRightChannel()
{
if (m_numberOfChannels != 2) {
throw std::invalid_argument { "Can not use right channel from mono sound data." };
}

std::vector<float> samples;
samples.resize(m_data.size() / 2);

for (auto index = 0U; index != samples.size(); ++index) {
samples.at(index) = m_data.at(index * 2 + 1);
}

m_numberOfChannels = 1;
m_data = samples;
return *this;
}

oalpp::SoundDataBuilder& oalpp::SoundDataBuilder::toStereo()
{
if (m_numberOfChannels != 1) {
throw std::invalid_argument { "Can not make stereo sound data from non-mono file." };
}

std::vector<float> samples;
samples.resize(m_data.size() * 2);

for (auto index = 0U; index != m_data.size(); ++index) {
samples.at(index * 2 + 0) = m_data.at(index);
samples.at(index * 2 + 1) = m_data.at(index);
}

m_data = samples;
m_numberOfChannels = 2;

return *this;
}
30 changes: 30 additions & 0 deletions impl/oalpp/sound_data/sound_data_builder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef SOUND_DATA_BUILDER_HPP
#define SOUND_DATA_BUILDER_HPP

#include "oalpp/effects/utility/gain.hpp"
#include <oalpp/sound_data/sound_data.hpp>
#include <string>
#include <vector>

namespace oalpp {

class SoundDataBuilder {
public:
[[nodiscard]] SoundData create();

[[nodiscard]] SoundDataBuilder& fromFile(std::string const& file);
[[nodiscard]] SoundDataBuilder& toString(std::string& str);
[[nodiscard]] SoundDataBuilder& withEffect(oalpp::effects::MonoEffectInterface& effect);
[[nodiscard]] SoundDataBuilder& onlyLeftChannel();
[[nodiscard]] SoundDataBuilder& onlyRightChannel();
[[nodiscard]] SoundDataBuilder& toStereo();

private:
std::vector<float> m_data {};
int m_sampleRate { 0 };
int m_numberOfChannels { 0 };
};

} // namespace oalpp

#endif // SOUND_DATA_BUILDER_HPP
26 changes: 0 additions & 26 deletions impl/oalpp/sound_data/sound_data_left_to_mono.cpp

This file was deleted.

23 changes: 0 additions & 23 deletions impl/oalpp/sound_data/sound_data_left_to_mono.hpp

This file was deleted.

29 changes: 0 additions & 29 deletions impl/oalpp/sound_data/sound_data_mid_to_mono.cpp

This file was deleted.

22 changes: 0 additions & 22 deletions impl/oalpp/sound_data/sound_data_mid_to_mono.hpp

This file was deleted.

26 changes: 0 additions & 26 deletions impl/oalpp/sound_data/sound_data_mono_to_stereo.cpp

This file was deleted.

23 changes: 0 additions & 23 deletions impl/oalpp/sound_data/sound_data_mono_to_stereo.hpp

This file was deleted.

Loading

0 comments on commit d9b87b1

Please sign in to comment.