Skip to content

Commit

Permalink
Tmp - ugh more renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
SirBob01 committed May 20, 2023
1 parent 5f73221 commit 5a390b2
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 81 deletions.
2 changes: 1 addition & 1 deletion src/Dynamo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#include "Sound/Convolver.hpp"
#include "Sound/Device.hpp"
#include "Sound/Filter.hpp"
#include "Sound/Filters/Attenuation.hpp"
#include "Sound/Filters/Binaural.hpp"
#include "Sound/Filters/Distance.hpp"
#include "Sound/Filters/Gain.hpp"
#include "Sound/Filters/Stereo.hpp"
#include "Sound/HRTF.hpp"
Expand Down
44 changes: 44 additions & 0 deletions src/Sound/Filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,57 @@ namespace Dynamo::Sound {
return _dependencies;
}

Sound &Filter::get_input() { return _input; }

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

std::vector<std::reference_wrapper<Filter>> topological_sort(Filter &root) {
std::vector<std::reference_wrapper<Filter>> order;
std::vector<std::reference_wrapper<Filter>> stack;
stack.push_back(root);

// Implement topological sorting algorithm
while (!stack.empty()) {
Filter &node = stack.back();
stack.pop_back();

for (Filter &child : node.get_dependencies()) {
stack.push_back(child);
}
order.push_back(node);
}

return order;
}

Sound &run_filter(Filter &filter,
Sound &src,
u32 offset,
u32 length,
ListenerSet &listeners) {
const u32 channels = src.channels();
auto ordered = topological_sort(filter);

// Write input buffers
for (u32 i = 0; i < ordered.size(); i++) {
Filter &node = ordered[i];
if (node.get_dependencies().empty()) {
Sound &input = node.get_input();
input.resize(length, channels);

for (u32 c = 0; c < channels; c++) {
for (u32 f = 0; f < length; f++) {
input[c][f] = src[c][f + offset];
}
}
}
}
for (Filter &node : ordered) {
// for (Filter &dep : node.get_dependencies()) {
// }
// Sound &src = node.get_input();
node.transform({listeners});
}
return src;
}
} // namespace Dynamo::Sound
34 changes: 27 additions & 7 deletions src/Sound/Filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,22 @@
#include "./Sound.hpp"

namespace Dynamo::Sound {
/**
* @brief Filter context for performing transformations.
*
*/
struct FilterContext {
std::reference_wrapper<ListenerSet> listeners;
};

/**
* @brief Filter node in a signal processing graph.
*
*/
class Filter {
Sound _input;
Sound _output;

std::vector<std::reference_wrapper<Filter>> _dependencies;

private:
Expand Down Expand Up @@ -62,6 +72,13 @@ namespace Dynamo::Sound {
const std::vector<std::reference_wrapper<Filter>> &
get_dependencies() const;

/**
* @brief Get the input sound buffer.
*
* @return Sound&
*/
Sound &get_input();

/**
* @brief Get the output sound buffer.
*
Expand All @@ -72,16 +89,19 @@ namespace Dynamo::Sound {
/**
* @brief Transform a Sound.
*
* @param src
* @param length
* @param listeners
* @param context Filter context data.
*/
virtual void transform(Sound &src,
u32 offset,
u32 length,
ListenerSet &listeners) = 0;
virtual void transform(FilterContext context) = 0;
};

/**
* @brief Topologically sort the filter graph.
*
* @param root
* @return std::vector<std::reference_wrapper<Filter>>
*/
std::vector<std::reference_wrapper<Filter>> topological_sort(Filter &root);

/**
* @brief Traverse the filter graph and process the sound.
*
Expand Down
33 changes: 0 additions & 33 deletions src/Sound/Filters/Attenuation.cpp

This file was deleted.

12 changes: 5 additions & 7 deletions src/Sound/Filters/Binaural.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@ namespace Dynamo::Sound {
_impulse_response.resize(HRIR_LENGTH, 2);
}

void Binaural::transform(Sound &src,
u32 offset,
u32 length,
ListenerSet &listeners) {
void Binaural::transform(u32 offset, u32 length, ListenerSet &listeners) {
ListenerProperties &listener = listeners.find_closest(position);
_hrtf.get().calculate_HRIR(listener.position,
listener.rotation,
position,
_impulse_response);

Sound &output = get_output();
output.set_frames(length);
Sound &src = get_input();
Sound &dst = get_output();
dst.set_frames(length);
for (i32 c = 0; c < 2; c++) {
_convolvers[c].initialize(_impulse_response[c],
_impulse_response.frames());
_convolvers[c].compute(src[c] + offset, output[c], length);
_convolvers[c].compute(src[c] + offset, dst[c], length);
}
}
} // namespace Dynamo::Sound
5 changes: 1 addition & 4 deletions src/Sound/Filters/Binaural.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ namespace Dynamo::Sound {
*/
Binaural(HRTF &hrtf);

void transform(Sound &src,
u32 offset,
u32 length,
ListenerSet &listeners) override;
void transform(u32 offset, u32 length, ListenerSet &listeners) override;
};
} // namespace Dynamo::Sound
31 changes: 31 additions & 0 deletions src/Sound/Filters/Distance.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "./Distance.hpp"

namespace Dynamo::Sound {
Distance::Distance(f32 inner_radius, f32 cutoff_radius) :
_inner_radius(inner_radius), _cutoff_radius(cutoff_radius) {}

f32 Distance::linear(f32 distance) {
if (distance < _inner_radius) {
return 1;
}
if (distance > _cutoff_radius) {
return 0;
}
return (_cutoff_radius - distance) / (_cutoff_radius - _inner_radius);
}

void Distance::transform(ListenerSet &listeners) {
ListenerProperties &listener = listeners.find_closest(position);
f32 distance = (position - listener.position).length();
f32 gain = linear(distance);

Sound &src = get_input();
Sound &dst = get_output();
dst.resize(length, src.channels());
for (u32 c = 0; c < dst.channels(); c++) {
for (u32 f = 0; f < dst.frames(); f++) {
dst[c][f] = src[c][f + offset] * gain;
}
}
}
} // namespace Dynamo::Sound
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Dynamo::Sound {
* as it moves further away from the listener.
*
*/
class Attenuation : public Filter {
class Distance : public Filter {
f32 _inner_radius;
f32 _cutoff_radius;

Expand All @@ -30,16 +30,13 @@ namespace Dynamo::Sound {
Vec3 position;

/**
* @brief Construct a new Attenuation object.
* @brief Construct a new Distance object.
*
* @param inner_radius Minimum distance to start attenuation.
* @param cutoff_radius Maximum distance to cutoff the sound.
*/
Attenuation(f32 inner_radius, f32 cutoff_radius);
Distance(f32 inner_radius, f32 cutoff_radius);

void transform(Sound &src,
u32 offset,
u32 length,
ListenerSet &listeners) override;
void transform(u32 offset, u32 length, ListenerSet &listeners) override;
};
} // namespace Dynamo::Sound
12 changes: 5 additions & 7 deletions src/Sound/Filters/Gain.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#include "./Gain.hpp"

namespace Dynamo::Sound {
void Gain::transform(Sound &src,
u32 offset,
u32 length,
ListenerSet &listeners) {
Sound &output = get_output();
output.resize(length, src.channels());
void Gain::transform(u32 offset, u32 length, ListenerSet &listeners) {
Sound &src = get_input();
Sound &dst = get_output();
dst.resize(length, src.channels());
for (u32 c = 0; c < src.channels(); c++) {
for (u32 f = 0; f < length; f++) {
output[c][f] = src[c][f + offset] * gain;
dst[c][f] = src[c][f + offset] * gain;
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/Sound/Filters/Gain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ namespace Dynamo::Sound {
*/
f32 gain = 1.0;

void transform(Sound &src,
u32 offset,
u32 length,
ListenerSet &listeners) override;
void transform(u32 offset, u32 length, ListenerSet &listeners) override;
};
} // namespace Dynamo::Sound
12 changes: 5 additions & 7 deletions src/Sound/Filters/Stereo.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#include "./Stereo.hpp"

namespace Dynamo::Sound {
void Stereo::transform(Sound &src,
u32 offset,
u32 length,
ListenerSet &listeners) {
void Stereo::transform(u32 offset, u32 length, ListenerSet &listeners) {
ListenerProperties &listener = listeners.find_closest(position);
Vec3 delta = position - listener.position;
Vec3 up = listener.rotation.up();
Expand All @@ -23,12 +20,13 @@ namespace Dynamo::Sound {
f32 l_gain = std::sqrt(1 - pan);
f32 r_gain = std::sqrt(pan);

Sound &output = get_output();
output.set_frames(length);
Sound &src = get_input();
Sound &dst = get_output();
dst.set_frames(length);
for (u32 c = 0; c < 2; c++) {
f32 gain = c == 0 ? l_gain : r_gain;
for (u32 f = 0; f < length; f++) {
output[c][f] = src[c][f + offset] * gain;
dst[c][f] = src[c][f + offset] * gain;
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/Sound/Filters/Stereo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ namespace Dynamo::Sound {
*/
Vec3 position;

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

0 comments on commit 5a390b2

Please sign in to comment.