Skip to content

Commit

Permalink
Fix bugs in filter node implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
SirBob01 committed May 17, 2023
1 parent 8580edc commit 23e01b8
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 31 deletions.
10 changes: 5 additions & 5 deletions src/Dynamo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
#include "Sound/Chunk.hpp"
#include "Sound/Convolver.hpp"
#include "Sound/Device.hpp"
#include "Sound/Filters/Attenuation.hpp"
#include "Sound/Filters/Binaural.hpp"
#include "Sound/Filters/Filter.hpp"
#include "Sound/Filters/Stereo.hpp"
#include "Sound/EffectNode.hpp"
#include "Sound/Filters/AttenuationEffect.hpp"
#include "Sound/Filters/BinauralEffect.hpp"
#include "Sound/Filters/GainEffect.hpp"
#include "Sound/Filters/StereoEffect.hpp"
#include "Sound/HRTF.hpp"
#include "Sound/Jukebox.hpp"
#include "Sound/Listener.hpp"
#include "Sound/Material.hpp"
#include "Sound/Resample.hpp"
#include "Sound/Sound.hpp"

Expand Down
1 change: 1 addition & 0 deletions src/Sound/EffectNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ namespace Dynamo::Sound {
u32 length,
ListenerSet &listeners) {
// TODO: Implement
return process(src, offset, length, listeners);
}
} // namespace Dynamo::Sound
2 changes: 1 addition & 1 deletion src/Sound/EffectNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Dynamo::Sound {
* @brief Destroy the EffectNode object.
*
*/
virtual ~EffectNode() = 0;
virtual ~EffectNode() = default;

/**
* @brief Connect an outgoing EffectNode.
Expand Down
4 changes: 2 additions & 2 deletions src/Sound/Filters/AttenuationEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ namespace Dynamo::Sound {
u32 offset,
u32 length,
ListenerSet &listeners) {
ListenerProperties &listener = listeners.find_closest(_position);
f32 distance = (_position - listener.position).length();
ListenerProperties &listener = listeners.find_closest(position);
f32 distance = (position - listener.position).length();
f32 gain = linear(distance);

_output.resize(length, src.channels());
Expand Down
10 changes: 6 additions & 4 deletions src/Sound/Filters/AttenuationEffect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ namespace Dynamo::Sound {
f32 _inner_radius;
f32 _cutoff_radius;

Vec3 _position;

/**
* @brief Linear attenuation function.
*
Expand All @@ -26,6 +24,12 @@ namespace Dynamo::Sound {
f32 linear(f32 distance);

public:
/**
* @brief Position of the sound source.
*
*/
Vec3 position;

/**
* @brief Construct a new AttenuationEffect object.
*
Expand All @@ -34,8 +38,6 @@ namespace Dynamo::Sound {
*/
AttenuationEffect(f32 inner_radius, f32 cutoff_radius);

inline void set_position(Vec3 position) { _position = position; }

Sound &process(Sound &src,
u32 offset,
u32 length,
Expand Down
4 changes: 2 additions & 2 deletions src/Sound/Filters/BinauralEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ namespace Dynamo::Sound {
u32 offset,
u32 length,
ListenerSet &listeners) {
ListenerProperties &listener = listeners.find_closest(_position);
ListenerProperties &listener = listeners.find_closest(position);
_hrtf.get().calculate_HRIR(listener.position,
listener.rotation,
_position,
position,
_impulse_response);
_output.set_frames(length);
for (i32 c = 0; c < 2; c++) {
Expand Down
12 changes: 5 additions & 7 deletions src/Sound/Filters/BinauralEffect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace Dynamo::Sound {
class BinauralEffect : public EffectNode {
std::reference_wrapper<HRTF> _hrtf;
std::array<Convolver, 2> _convolvers;
Vec3 _position;

Sound _impulse_response;
Sound _output;
Expand All @@ -30,18 +29,17 @@ namespace Dynamo::Sound {

public:
/**
* @brief Construct a new BinauralEffect object.
* @brief Position of the sound source.
*
* @param hrtf HRTF interpolator
*/
BinauralEffect(HRTF &hrtf);
Vec3 position;

/**
* @brief Set the position of the sound source.
* @brief Construct a new BinauralEffect object.
*
* @param position
* @param hrtf HRTF interpolator
*/
inline void set_position(Vec3 position) { _position = position; }
BinauralEffect(HRTF &hrtf);

Sound &process(Sound &src,
u32 offset,
Expand Down
1 change: 1 addition & 0 deletions src/Sound/Filters/GainEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Dynamo::Sound {
u32 offset,
u32 length,
ListenerSet &listeners) {
_output.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;
Expand Down
4 changes: 2 additions & 2 deletions src/Sound/Filters/GainEffect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ namespace Dynamo::Sound {

public:
/**
* @brief Set the position of the sound source.
* @brief Set the gain multiplier.
*
* @param position
* @param gain
*/
inline void set_gain(f32 gain) { _gain = gain; }

Expand Down
4 changes: 2 additions & 2 deletions src/Sound/Filters/StereoEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace Dynamo::Sound {
u32 offset,
u32 length,
ListenerSet &listeners) {
ListenerProperties &listener = listeners.find_closest(_position);
Vec3 delta = _position - listener.position;
ListenerProperties &listener = listeners.find_closest(position);
Vec3 delta = position - listener.position;
Vec3 up = listener.rotation.up();
Vec3 right = listener.rotation.right();
Vec3 displacement = (delta - up * (delta * up));
Expand Down
6 changes: 2 additions & 4 deletions src/Sound/Filters/StereoEffect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace Dynamo::Sound {
*
*/
class StereoEffect : public EffectNode {
Vec3 _position;
Sound _output;

/**
Expand All @@ -24,11 +23,10 @@ namespace Dynamo::Sound {

public:
/**
* @brief Set the position of the sound source.
* @brief Position of the sound source.
*
* @param position
*/
inline void set_position(Vec3 position) { _position = position; }
Vec3 position;

Sound &process(Sound &src,
u32 offset,
Expand Down
3 changes: 1 addition & 2 deletions src/Sound/Jukebox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ namespace Dynamo::Sound {
}

// Apply the effects graph
transformed =
effect.process(transformed, 0, transformed.frames(), _listeners);
transformed = effect.run(transformed, 0, frames, _listeners);

// Mix the filtered sound onto the composite signal
for (u32 f = 0; f < transformed.frames(); f++) {
Expand Down

0 comments on commit 23e01b8

Please sign in to comment.