Klang C++ 0.7.15
Language Reference (draft)
Loading...
Searching...
No Matches
klang::Modifiers::Modal

Language Guide / Signal Modifiers

Modal

klang::Modifiers::Modal

A resonator tuned to a single mode.

Modal synthesis models the sound of a resonating object as a set of modes, each with its own frequency and decay. Excite several resonators together and sum their outputs to model a struck or plucked object.

Set the mode’s frequency in Hz and its decay time in seconds. Feed it an impulse to start a ringing response. Reading the resonator advances the decay, so read it on every sample while the mode is sounding.

Example · Excite a 440 Hz mode

Call the sound’s trigger() method when an event should excite the resonator.

struct Ring : Sound {
    Modal mode;

    Ring() {
        mode.set(440, 0.8);
    }

    void trigger() {
        1 >> mode;
    }

    void process() {
        mode >> out;
    }
};

Modal resonator. More...

#include <klang.h>

+ Inheritance diagram for klang::Modifiers::Modal:

Public Member Functions

void set (param f, param decay, param gain)
 
void set (param f, param decay)
 
void input ()
 
void process ()
 
 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

float a1 = 0
 
float a2 = 0
 
float y1 = 0
 
float y2 = 0
 
float gain = 0.05f
 
signal in
 
signal out
 

Detailed Description

Definition at line 7753 of file klang.h.

Member Function Documentation

◆ input() [1/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() [2/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

◆ input() [3/3]

void klang::Modifiers::Modal::input ( )
inlinevirtual

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

Definition at line 7783 of file klang.h.

7783 {
7784 in *= gain;
7785 }

◆ 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()()

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()

void klang::Modifiers::Modal::process ( )
inlinevirtual

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

Definition at line 7787 of file klang.h.

7787 {
7788 out = in + a1 * y1 + a2 * y2;
7789 y2 = y1;
7790 y1 = out;
7791 in = 0;
7792 }

◆ reset()

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

Definition at line 2981 of file klang.h.

2981{ out = 0; }

◆ set() [1/2]

void klang::Modifiers::Modal::set ( param f,
param decay )
inline

Definition at line 7765 of file klang.h.

7765 {
7766 gain = 0.05f;
7767
7768 constexpr float min_d = 1e-6f; // Prevents underflow
7769 constexpr float max_d = 0.9999f; // Ensures stability
7770
7771 const float w = f.value * fs.w;
7772 const float d = std::clamp(std::exp(-pi / (decay * fs)), min_d, max_d);
7773
7774 a1 = 2.f * d * std::clamp(std::cos(w), -0.9999f, 0.9999f);
7775 a2 = -d * d;
7776
7777 y2 = 0;
7778 y1 = 0;
7779 in = 0;
7780 out = 0;
7781 }
constexpr constant pi
The mathematical constant, pi (and it's inverse).
Definition klang.h:287
klang::dB fs

References klang::fs, and klang::pi.

◆ set() [2/2]

void klang::Modifiers::Modal::set ( param f,
param decay,
param gain )
inline

Definition at line 7760 of file klang.h.

7760 {
7761 set(f, decay);
7762 this->gain = std::clamp(gain.value * 0.05f, -0.05f, 0.05f);
7763 }
void set(param f, param decay, param gain)
Definition klang.h:7760

References klang::signal::value.

Member Data Documentation

◆ a1

float klang::Modifiers::Modal::a1 = 0

Definition at line 7754 of file klang.h.

◆ a2

float klang::Modifiers::Modal::a2 = 0

Definition at line 7754 of file klang.h.

◆ gain

float klang::Modifiers::Modal::gain = 0.05f

Definition at line 7757 of file klang.h.

◆ in

◆ out

Definition at line 2961 of file klang.h.

2961{ 0.f };

◆ y1

float klang::Modifiers::Modal::y1 = 0

Definition at line 7755 of file klang.h.

◆ y2

float klang::Modifiers::Modal::y2 = 0

Definition at line 7755 of file klang.h.