Skip to content

Commit

Permalink
waveguide gates
Browse files Browse the repository at this point in the history
  • Loading branch information
Segfault1602 committed Nov 1, 2023
1 parent 64b79a1 commit 6f65580
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 3 deletions.
3 changes: 3 additions & 0 deletions include/delayline.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
32 changes: 32 additions & 0 deletions include/waveguide_gates.h
Original file line number Diff line number Diff line change
@@ -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
2 changes: 0 additions & 2 deletions scripts/waveguide_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ def tick(self, input):

return a + (b - a) * self.delay_frac



Fs = 48000

# Waveguide Length
Expand Down
3 changes: 2 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down
12 changes: 12 additions & 0 deletions src/delayline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
49 changes: 49 additions & 0 deletions src/waveguide_gates.cpp
Original file line number Diff line number Diff line change
@@ -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<int>(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

0 comments on commit 6f65580

Please sign in to comment.