Klang C++ 0.7.15
Language Reference (draft)
Loading...
Searching...
No Matches
klang::Delay< SIZE >

Language Guide / Signal Modifiers

Delay

klang::Delay

A delay line with independently readable taps.

For a fixed-capacity Delay<N>, choose the capacity in samples. Writing an input advances the write position. Calling tap() reads the stored history without writing a new sample. Use a tap within the configured capacity.

Example · Read a 100 ms tap at 48 kHz

Declare Delay<48000> delay; and run this once per input sample.

in >> delay;
delay.tap(4800) >> out;

Example · Allocate a variable-size delay

Use Delay<0> when the capacity is known only at runtime. Declare Delay<0> delay; as a member. In setup, call resize() to allocate the delay line in samples. The tap offset is still in samples and must fit within the allocated capacity.

delay.resize(48000);

In process(), use the configured object for each sample.

in >> delay;
delay.tap(4800) >> out;

Example · Feed an echo back into the delay

Read the delayed sample, mix it with the new input, and use << to write the result back into the delay line. A feedback gain below one makes the repeats decay.

struct FeedbackEcho : Effect {
    Delay<48000> delay;

    void process() {
        in + delay(4800) * 0.4 >> out;
        delay << out;
    }
};

Audio delay object (fixed size) More...

#include <klang.h>

+ Inheritance diagram for klang::Delay< SIZE >:

Classes

struct  Tap
 

Public Member Functions

 Delay ()
 
void clear ()
 
void input () override
 
signal tap (int delay) const
 
signal tap (float delay) const
 
signal lagrange (float delay) const
 
signal tap () const
 
virtual void process () override
 
virtual void set (param samples) override
 
template<typename TIME >
signal operator() (TIME &delay)
 
template<typename TIME >
signal operator() (const TIME &delay)
 
unsigned int max () const
 
 operator const signal & () override
 
 operator const signal & () const override
 
Modifier< signal > & operator() (params &&... p)
 
virtual const signal & input () const
 
virtual void input (const signal &source)
 
virtual void operator<< (const signal &source)
 
virtual const signal & output () const
 
TYPE & operator>> (TYPE &destination)
 
signal operator+ (TYPE &other)
 
signal operator* (TYPE &other)
 
signal operator- (TYPE &other)
 
signal operator/ (TYPE &other)
 
void reset ()
 

Public Attributes

buffer buffer
 
float time = 1
 
int position = 0
 
struct klang::Delay::Tap last
 
signal in
 
signal out
 

Detailed Description

template<int SIZE>
struct klang::Delay< SIZE >

Definition at line 4599 of file klang.h.


Class Documentation

◆ klang::Delay::Tap

struct klang::Delay::Tap
template<int SIZE>
struct klang::Delay< SIZE >::Tap

Definition at line 4692 of file klang.h.

Class Members
float fraction
int position

Constructor & Destructor Documentation

◆ Delay()

template<int SIZE>
klang::Delay< SIZE >::Delay ( )
inline

Definition at line 4607 of file klang.h.

4607: buffer(SIZE + 1, 0) { clear(); }
void clear()
Definition klang.h:4609
buffer buffer
Definition klang.h:4603

Member Function Documentation

◆ clear()

template<int SIZE>
void klang::Delay< SIZE >::clear ( )
inline

Definition at line 4609 of file klang.h.

4609 {
4610 buffer.clear();
4611 }
void clear()
Definition klang.h:2673

◆ input() [1/3]

template<int SIZE>
void klang::Delay< SIZE >::input ( )
inlineoverridevirtual

Reimplemented from klang::Generic::Input< signal >.

Definition at line 4613 of file klang.h.

4613 {
4614 buffer++ = in;
4615 position++;
4616 if (position == SIZE) {
4617 buffer.rewind();
4618 position = 0;
4619 }
4620 }
void rewind(int offset=0)
Definition klang.h:2665
int position
Definition klang.h:4605

◆ input() [2/3]

virtual const signal & klang::Generic::Input< signal >::input ( ) const
inlinevirtualinherited

Reimplemented in klang::Generic::Graph< SIZE >::Series.

Definition at line 2947 of file klang.h.

2947{ return in; }

◆ input() [3/3]

virtual void klang::Generic::Input< signal >::input ( const signal & source)
inlinevirtualinherited

Reimplemented in klang::Debug.

Definition at line 2951 of file klang.h.

2951{ in = source; this->input(); }
virtual const signal & input() const
Definition klang.h:2947

◆ lagrange()

template<int SIZE>
signal klang::Delay< SIZE >::lagrange ( float delay) const
inline

Definition at line 4646 of file klang.h.

4646 {
4647 // Calculate the read position
4648 float read = static_cast<float>(position - 1) - delay;
4649 if (read < 0.f)
4650 read += SIZE;
4651
4652 // Separate integer and fractional parts
4653 int i = static_cast<int>(read); // Integer part
4654 float x = read - i; // Fractional part (0 ? x < 1)
4655
4656 // Get four surrounding indices using modulo for circular buffer
4657 int i0 = (i - 1 + SIZE) % SIZE;
4658 int i1 = i; // Main sample
4659 int i2 = (i + 1) % SIZE;
4660 int i3 = (i + 2) % SIZE;
4661
4662 // Read corresponding samples
4663 float y0 = buffer[i0];
4664 float y1 = buffer[i1];
4665 float y2 = buffer[i2];
4666 float y3 = buffer[i3];
4667
4668 // Compute Lagrange interpolation (third-order)
4669 float c0 = (-x * (x - 1) * (x - 2)) / 6.0f;
4670 float c1 = ((x + 1) * (x - 1) * (x - 2)) / 2.0f;
4671 float c2 = (-x * (x + 1) * (x - 2)) / 2.0f;
4672 float c3 = (x * (x + 1) * (x - 1)) / 6.0f;
4673
4674 return c0 * y0 + c1 * y1 + c2 * y2 + c3 * y3;
4675 }

◆ max()

template<int SIZE>
unsigned int klang::Delay< SIZE >::max ( ) const
inline

Definition at line 4728 of file klang.h.

4728{ return SIZE; }

◆ operator const signal &() [1/2]

klang::Generic::Modifier< signal >::operator const signal & ( ) const
inlineoverridevirtualinherited

Reimplemented from klang::Generic::Output< signal >.

Definition at line 3091 of file klang.h.

3091{ return out; } // return last output

◆ operator const signal &() [2/2]

klang::Generic::Modifier< signal >::operator const signal & ( )
inlineoverridevirtualinherited

Reimplemented from klang::Generic::Output< signal >.

Definition at line 3090 of file klang.h.

3090{ this->process(); return out; } // return processed output
virtual void process() override
Definition klang.h:3094

◆ operator()() [1/3]

template<int SIZE>
template<typename TIME >
signal klang::Delay< SIZE >::operator() ( const TIME & delay)
inline

Definition at line 4719 of file klang.h.

4719 {
4720 if constexpr (std::is_integral_v<TIME>)
4721 return tap((int)delay);
4722 else if constexpr (std::is_floating_point_v<TIME>)
4723 return tap((float)delay);
4724 else
4725 return tap((signal)delay);
4726 }
signal tap() const
Definition klang.h:4678

◆ operator()() [2/3]

template<int SIZE>
template<typename TIME >
signal klang::Delay< SIZE >::operator() ( TIME & delay)
inline

Definition at line 4709 of file klang.h.

4709 {
4710 if constexpr (std::is_integral_v<TIME>)
4711 return tap((int)delay);
4712 else if constexpr (std::is_floating_point_v<TIME>)
4713 return tap((float)delay);
4714 else
4715 return tap((signal)delay);
4716 }

◆ operator()() [3/3]

Modifier< signal > & klang::Generic::Modifier< signal >::operator() ( params &&... p)
inlineinherited

Definition at line 3098 of file klang.h.

3098 {
3099 set(std::forward<params>(p)...); return *this;
3100 }

◆ operator*()

signal klang::Generic::Output< signal >::operator* ( TYPE & other)
inlineinherited

Definition at line 2977 of file klang.h.

2977{ this->process(); return out * SIGNAL(other); }

◆ operator+()

signal klang::Generic::Output< signal >::operator+ ( TYPE & other)
inlineinherited

Definition at line 2976 of file klang.h.

2976{ this->process(); return out + SIGNAL(other); }

◆ operator-()

signal klang::Generic::Output< signal >::operator- ( TYPE & other)
inlineinherited

Definition at line 2978 of file klang.h.

2978{ this->process(); return out - SIGNAL(other); }

◆ operator/()

signal klang::Generic::Output< signal >::operator/ ( TYPE & other)
inlineinherited

Definition at line 2979 of file klang.h.

2979{ this->process(); return out / SIGNAL(other); }

◆ operator<<()

virtual void klang::Generic::Input< signal >::operator<< ( const signal & source)
inlinevirtualinherited

Reimplemented in klang::Debug.

Definition at line 2950 of file klang.h.

2950{ in = source; this->input(); }

◆ operator>>()

TYPE & klang::Generic::Output< signal >::operator>> ( TYPE & destination)
inlineinherited

Definition at line 2969 of file klang.h.

2969{ this->process(); return destination = out; }

◆ output()

virtual const signal & klang::Generic::Output< signal >::output ( ) const
inlinevirtualinherited

Definition at line 2964 of file klang.h.

2964{ return out; }

◆ process()

template<int SIZE>
virtual void klang::Delay< SIZE >::process ( )
inlineoverridevirtual

Reimplemented from klang::Generic::Modifier< signal >.

Definition at line 4687 of file klang.h.

4687 {
4688 out = tap();
4689 last.position = (last.position + 1) % SIZE;
4690 }
struct klang::Delay::Tap last

◆ reset()

void klang::Generic::Output< signal >::reset ( )
inlineinherited

Definition at line 2981 of file klang.h.

2981{ out = 0; }

◆ set()

template<int SIZE>
virtual void klang::Delay< SIZE >::set ( param samples)
inlineoverridevirtual

Definition at line 4697 of file klang.h.

4697 {
4698 time = samples < SIZE ? (float)samples : SIZE;
4699
4700 float read = static_cast<float>(position - 1) - time;
4701 if (read < 0.f)
4702 read += SIZE;
4703
4704 last.position = static_cast<int>(read); // Integer part
4705 last.fraction = read - last.position; // Fractional part
4706 }
float time
Definition klang.h:4604

◆ tap() [1/3]

template<int SIZE>
signal klang::Delay< SIZE >::tap ( ) const
inline

Definition at line 4678 of file klang.h.

4678 {
4679 // Use modulo to get the next index without branching
4680 const int i = last.position;
4681 const int j = (i + 1) % SIZE; // Next index in circular buffer
4682
4683 // Linear interpolation: buffer[i] + fraction * (buffer[j] - buffer[i])
4684 return buffer[i] + last.fraction * (buffer[j] - buffer[i]);
4685 }

◆ tap() [2/3]

template<int SIZE>
signal klang::Delay< SIZE >::tap ( float delay) const
inline

Definition at line 4629 of file klang.h.

4629 {
4630 // Calculate the read position
4631 float read = static_cast<float>(position - 1) - delay;
4632 if (read < 0.f)
4633 read += SIZE;
4634
4635 // Separate integer and fractional parts
4636 const int i = static_cast<int>(read); // Integer part
4637 const float fraction = read - i; // Fractional part
4638
4639 // Use modulo to get the next index without branching
4640 const int j = (i + 1) % SIZE; // Next index in circular buffer
4641
4642 // Linear interpolation: buffer[i] + fraction * (buffer[j] - buffer[i])
4643 return buffer[i] + fraction * (buffer[j] - buffer[i]);
4644 }

◆ tap() [3/3]

template<int SIZE>
signal klang::Delay< SIZE >::tap ( int delay) const
inline

Definition at line 4622 of file klang.h.

4622 {
4623 int read = (position - 1) - delay;
4624 if (read < 0)
4625 read += SIZE;
4626 return buffer[read];
4627 }

Member Data Documentation

◆ buffer

template<int SIZE>
buffer klang::Delay< SIZE >::buffer

Definition at line 4603 of file klang.h.

◆ in

◆ last

template<int SIZE>
struct klang::Delay::Tap klang::Delay< SIZE >::last

◆ out

Definition at line 2961 of file klang.h.

2961{ 0.f };

◆ position

template<int SIZE>
int klang::Delay< SIZE >::position = 0

Definition at line 4605 of file klang.h.

◆ time

template<int SIZE>
float klang::Delay< SIZE >::time = 1

Definition at line 4604 of file klang.h.