From 6f65580ee025dc473a88d4d1dd0b5148b785ffd7 Mon Sep 17 00:00:00 2001 From: Alex St-Onge Date: Wed, 1 Nov 2023 08:57:45 -0700 Subject: [PATCH] waveguide gates --- include/delayline.h | 3 +++ include/waveguide_gates.h | 32 +++++++++++++++++++++++++ scripts/waveguide_gates.py | 2 -- src/CMakeLists.txt | 3 ++- src/delayline.cpp | 12 ++++++++++ src/waveguide_gates.cpp | 49 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 include/waveguide_gates.h create mode 100644 src/waveguide_gates.cpp diff --git a/include/delayline.h b/include/delayline.h index da08209..1948ad2 100644 --- a/include/delayline.h +++ b/include/delayline.h @@ -69,6 +69,9 @@ class Delayline /// @param input Input sample void SetIn(float delay, float input); + float& operator[](size_t index) const; + float& operator[](size_t index); + protected: const size_t max_size_ = 0; const bool reverse_ = false; diff --git a/include/waveguide_gates.h b/include/waveguide_gates.h new file mode 100644 index 0000000..ddf5f73 --- /dev/null +++ b/include/waveguide_gates.h @@ -0,0 +1,32 @@ +#pragma once + +#include "dsp_base.h" + +#include "delayline.h" + +namespace dsp +{ + +class WaveguideGates +{ + public: + WaveguideGates(); + ~WaveguideGates() = default; + + void SetDelay(float delay); + float GetDelay() const; + + void SetCoeff(float c); + + void Process(Delayline& left_traveling_line, Delayline& right_traveling_line); + + private: + float coeff_ = 0.f; + float delay_ = 0.f; + size_t delay_integer_ = 0; + float delay_fractional_ = 0.f; + float inv_delay_fractional_ = 0.f; + Delayline delay_left_; + Delayline delay_right_; +}; +} // namespace dsp \ No newline at end of file diff --git a/scripts/waveguide_gates.py b/scripts/waveguide_gates.py index b9466c0..549b77f 100644 --- a/scripts/waveguide_gates.py +++ b/scripts/waveguide_gates.py @@ -24,8 +24,6 @@ def tick(self, input): return a + (b - a) * self.delay_frac - - Fs = 48000 # Waveguide Length diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bc07038..d33113a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -14,7 +14,8 @@ set(LIB_SOURCES sinc_resampler.cpp string_ensemble.cpp termination.cpp - waveguide.cpp) + waveguide.cpp + waveguide_gates.cpp) add_library(dsp STATIC ${LIB_SOURCES}) diff --git a/src/delayline.cpp b/src/delayline.cpp index 4482927..9996f68 100644 --- a/src/delayline.cpp +++ b/src/delayline.cpp @@ -124,4 +124,16 @@ void Delayline::SetIn(float delay, float input) } } +float& Delayline::operator[](size_t index) const +{ + size_t read_ptr = (write_ptr_ + index) % max_size_; + return line_[read_ptr]; +} + +float& Delayline::operator[](size_t index) +{ + size_t read_ptr = (write_ptr_ + index) % max_size_; + return line_[read_ptr]; +} + } // namespace dsp \ No newline at end of file diff --git a/src/waveguide_gates.cpp b/src/waveguide_gates.cpp new file mode 100644 index 0000000..676d600 --- /dev/null +++ b/src/waveguide_gates.cpp @@ -0,0 +1,49 @@ +#include "waveguide_gates.h" + +namespace dsp +{ + +WaveguideGates::WaveguideGates() : delay_left_(8), delay_right_(8) +{ +} + +void WaveguideGates::SetDelay(float delay) +{ + delay_ = delay; + delay_integer_ = static_cast(delay); + delay_fractional_ = delay - delay_integer_; + inv_delay_fractional_ = 1.f - delay_fractional_; + + delay_left_.SetDelay(delay_fractional_ * 2.f); + delay_right_.SetDelay(inv_delay_fractional_ * 2.f); +} + +float WaveguideGates::GetDelay() const +{ + return delay_; +} + +void WaveguideGates::SetCoeff(float c) +{ + coeff_ = c; +} + +void WaveguideGates::Process(Delayline& left_traveling_line, Delayline& right_traveling_line) +{ + const float kLeftGate = delay_integer_ - 1; + const float kRightGate = delay_integer_ + 1; + + float left_input = right_traveling_line.TapOut(kLeftGate); + float left_interpolated = delay_left_.Tick(left_input); + float left_gate_sample = left_traveling_line.TapOut(delay_integer_); + float new_sample = left_gate_sample * (1 - coeff_) + left_interpolated * (coeff_); + left_traveling_line.SetIn(delay_integer_, left_interpolated); + + float right_input = left_traveling_line.TapOut(kRightGate); + float right_interpolated = delay_right_.Tick(right_input); + float right_gate_sample = right_traveling_line.TapOut(delay_integer_); + new_sample += right_gate_sample * (1 - coeff_) + right_interpolated * (coeff_); + right_traveling_line.SetIn(delay_integer_, right_interpolated); +} + +} // namespace dsp \ No newline at end of file