Skip to content

Commit

Permalink
Rename to Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
SirBob01 committed May 17, 2023
1 parent 5c15a60 commit 70cd5ca
Show file tree
Hide file tree
Showing 15 changed files with 229 additions and 205 deletions.
6 changes: 3 additions & 3 deletions src/Sound/Chunk.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "../Types.hpp"
#include "./EffectNode.hpp"
#include "./Filter.hpp"
#include "./Sound.hpp"

namespace Dynamo::Sound {
Expand Down Expand Up @@ -30,10 +30,10 @@ namespace Dynamo::Sound {
std::reference_wrapper<Sound> sound;

/**
* @brief Reference to the audio processing effect graph.
* @brief Reference to the audio processing filter graph.
*
*/
std::reference_wrapper<EffectNode> effect;
std::reference_wrapper<Filter> filter;

/**
* @brief Frame offset of the chunk.
Expand Down
54 changes: 0 additions & 54 deletions src/Sound/EffectNode.cpp

This file was deleted.

79 changes: 0 additions & 79 deletions src/Sound/EffectNode.hpp

This file was deleted.

61 changes: 61 additions & 0 deletions src/Sound/Filter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "./Filter.hpp"

namespace Dynamo::Sound {
b8 Filter::has_cycles() {
std::unordered_set<Filter *> visited;
std::vector<std::reference_wrapper<Filter>> stack;

stack.push_back(*this);
while (!stack.empty()) {
Filter &node = stack.back();
stack.pop_back();
if (visited.count(&node) > 0) {
return true;
}
for (Filter &child : node._dependencies) {
stack.push_back(child);
}
visited.insert(&node);
}
return false;
}

void Filter::connect(Filter &destination) {
_dependencies.push_back(destination);
if (has_cycles()) {
Log::error("Jukebox Filter graph disallows cycles.");
}
}

void Filter::disconnect(Filter &node) {
auto it = _dependencies.begin();
while (it != _dependencies.end()) {
Filter &curr = *it;
if (&curr == &node) {
it = _dependencies.erase(it);
return;
} else {
it++;
}
}
Log::error("Jukebox Filter graph attempted to disconnect a "
"non-existent node.");
}

void Filter::disconnect() { _dependencies.clear(); }

const std::vector<std::reference_wrapper<Filter>> &
Filter::get_dependencies() const {
return _dependencies;
}

Sound &Filter::get_output() { return _output; }

Sound &run_filter(Filter &filter,
Sound &src,
u32 offset,
u32 length,
ListenerSet &listeners) {
return src;
}
} // namespace Dynamo::Sound
100 changes: 100 additions & 0 deletions src/Sound/Filter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#pragma once

#include <functional>
#include <unordered_set>
#include <vector>

#include "../Types.hpp"
#include "./Listener.hpp"
#include "./Sound.hpp"

namespace Dynamo::Sound {
/**
* @brief Filter node in a signal processing graph.
*
*/
class Filter {
Sound _output;
std::vector<std::reference_wrapper<Filter>> _dependencies;

private:
/**
* @brief Test if the graph has cycles.
*
* @return b8
*/
b8 has_cycles();

public:
/**
* @brief Destroy the Filter object.
*
*/
virtual ~Filter() = default;

/**
* @brief Connect a dependency.
*
* The resulting graph must not result in a cycle.
*
* @param node
*/
void connect(Filter &node);

/**
* @brief Disconnect all dependencies.
*
*/
void disconnect();

/**
* @brief Disconnect a dependency.
*
* @param node
*/
void disconnect(Filter &node);

/**
* @brief Get the immutable list of dependencies.
*
* @return const std::vector<std::reference_wrapper<Filter>>&
*/
const std::vector<std::reference_wrapper<Filter>> &
get_dependencies() const;

/**
* @brief Get the output sound buffer.
*
* @return Sound&
*/
Sound &get_output();

/**
* @brief Transform a Sound.
*
* @param src
* @param length
* @param listeners
*/
virtual void transform(Sound &src,
u32 offset,
u32 length,
ListenerSet &listeners) = 0;
};

/**
* @brief Traverse the filter graph and process the sound.
*
* @param filter
* @param src
* @param offset
* @param length
* @param listeners
* @return Sound&
*/
Sound &run_filter(Filter &root,
Sound &src,
u32 offset,
u32 length,
ListenerSet &listeners);
} // namespace Dynamo::Sound
18 changes: 9 additions & 9 deletions src/Sound/Filters/Attenuation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ namespace Dynamo::Sound {
return (_cutoff_radius - distance) / (_cutoff_radius - _inner_radius);
}

Sound &Attenuation::transform(Sound &src,
u32 offset,
u32 length,
ListenerSet &listeners) {
void Attenuation::transform(Sound &src,
u32 offset,
u32 length,
ListenerSet &listeners) {
ListenerProperties &listener = listeners.find_closest(position);
f32 distance = (position - listener.position).length();
f32 gain = linear(distance);

_output.resize(length, src.channels());
for (u32 c = 0; c < _output.channels(); c++) {
for (u32 f = 0; f < _output.frames(); f++) {
_output[c][f] = src[c][f + offset] * gain;
Sound &output = get_output();
output.resize(length, src.channels());
for (u32 c = 0; c < output.channels(); c++) {
for (u32 f = 0; f < output.frames(); f++) {
output[c][f] = src[c][f + offset] * gain;
}
}
return _output;
}
} // namespace Dynamo::Sound
13 changes: 6 additions & 7 deletions src/Sound/Filters/Attenuation.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "../../Types.hpp"
#include "../EffectNode.hpp"
#include "../Filter.hpp"
#include "../Sound.hpp"

namespace Dynamo::Sound {
Expand All @@ -10,8 +10,7 @@ namespace Dynamo::Sound {
* as it moves further away from the listener.
*
*/
class Attenuation : public EffectNode {
Sound _output;
class Attenuation : public Filter {
f32 _inner_radius;
f32 _cutoff_radius;

Expand All @@ -38,9 +37,9 @@ namespace Dynamo::Sound {
*/
Attenuation(f32 inner_radius, f32 cutoff_radius);

Sound &transform(Sound &src,
u32 offset,
u32 length,
ListenerSet &listeners) override;
void transform(Sound &src,
u32 offset,
u32 length,
ListenerSet &listeners) override;
};
} // namespace Dynamo::Sound
Loading

0 comments on commit 70cd5ca

Please sign in to comment.