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

Language Guide / Plugin Architecture

Synth

klang::Synth

Base class for polyphonic mono synthesisers.

A Synth creates and mixes voices. Define a Note subclass to hold per-voice state and register a pool with notes.add<Voice>(count) during construction. The synth assigns incoming notes to the pool.

Synth inherits the Plugin interface through Effect. Use controls to expose patch parameters such as filter cutoff or attack time, and presets to provide named patches.

Example · Register and render voices

Each voice sets its oscillator when the note starts and frees the voice when it receives note-off. Add a release envelope when the note needs to decay smoothly.

struct Organ : Synth {
    struct Voice : Note {
        Sine sine;

        event on(Pitch pitch, Amplitude velocity) {
            sine.set(pitch -> Frequency);
        }

        event off(Amplitude velocity) {
            stop();
        }

        void process() {
            sine * 0.1 >> out;
        }
    };

    Organ() { notes.add<Voice>(8); }
};

Process the voice mix

After rendering and summing the active voices, the synth passes their combined signal into its own effect stage. The synth’s process() receives that mix in in and writes the final sample to out. This uses exactly the same form as an Effect.

Here the synth filters the whole voice mix. The note’s internals are abbreviated from the earlier example. The filter keeps its history as notes start and stop.

struct FilteredOrgan : Synth {
    struct Voice : Note { /* ... */ };

    FilteredOrgan() {
        notes.add<Voice>(8);
    }

    LPF filter;

    void process() {
        in >> filter(2000, 0.707) >> out;
    }
};

See also Stereo::Synth.

Synthesiser object (mono) More...

#include <klang.h>

+ Inheritance diagram for klang::Synth:

Public Types

typedef Note Note
 

Public Member Functions

 Synth ()
 
virtual ~Synth ()
 
int indexOf (Note *note) const
 
virtual event onControl (int index, float value) override
 
virtual event onMIDI (int status, int byte1, int byte2) override
 
virtual event onPreset (int index) override
 
virtual event noteOn (int pitch, float velocity)
 
virtual event noteOff (int pitch, float velocity)
 
virtual event allNotesOff (float velocity=0.f)
 
virtual void process () override
 
virtual void process (buffer buffer) override
 
virtual bool playing () const
 
virtual void process (float *buffer, int length, float *parameters=nullptr)
 
virtual void prepare ()
 
 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 ()
 

Static Public Member Functions

template<typename DERIVED , typename... ARGS>
static DERIVED * Create (ARGS &&... args)
 

Public Attributes

Notes< Synth, Note > notes
 
Controls controls
 
Presets presets
 
signal in
 
signal out
 

Detailed Description

Definition at line 5719 of file klang.h.

Member Typedef Documentation

◆ Note

Definition at line 5720 of file klang.h.

Constructor & Destructor Documentation

◆ Synth()

klang::Synth::Synth ( )
inline

Definition at line 5724 of file klang.h.

5724: notes(this) {}
Notes< Synth, Note > notes
Definition klang.h:5722

◆ ~Synth()

virtual klang::Synth::~Synth ( )
inlinevirtual

Definition at line 5725 of file klang.h.

5725{}

Member Function Documentation

◆ allNotesOff()

virtual event klang::Synth::allNotesOff ( float velocity = 0.f)
inlinevirtual

Definition at line 5779 of file klang.h.

5779 {
5780 for (unsigned int n = 0; n < notes.count; n++)
5781 if (notes[n]->stage != Note::Off)
5782 notes[n]->release(velocity);
5783 }
unsigned int count
Definition klang.h:308

References klang::Array< TYPE, CAPACITY >::count.

◆ control()

virtual event klang::Controller::control ( int index,
float value )
inlineprotectedvirtualinherited

Definition at line 5509 of file klang.h.

5509{};

◆ Create()

template<typename DERIVED , typename... ARGS>
static DERIVED * klang::Plugin::Create ( ARGS &&... args)
inlinestaticinherited

Definition at line 5523 of file klang.h.

5523 {
5524 static_assert(std::is_base_of_v<Plugin, DERIVED>);
5525 DERIVED* p = new DERIVED();
5526 p->controls.set(std::forward<ARGS>(args)...);
5527 return p;
5528 }

◆ indexOf()

int klang::Synth::indexOf ( Note * note) const
inline

Definition at line 5731 of file klang.h.

5731 {
5732 int index = 0;
5733 for (const auto* n : notes.items) {
5734 if (note == n)
5735 return index;
5736 index++;
5737 }
5738 return -1; // not found
5739 }
TYPE items[CAPACITY]
Definition klang.h:309

References klang::Array< TYPE, CAPACITY >::items.

◆ input() [1/3]

virtual void klang::Generic::Input< signal >::input ( )
inlineprotectedvirtualinherited

◆ 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

◆ midi()

virtual event klang::Controller::midi ( int status,
int byte1,
int byte2 )
inlineprotectedvirtualinherited

Definition at line 5511 of file klang.h.

5511{};

◆ noteOff()

virtual event klang::Synth::noteOff ( int pitch,
float velocity )
inlinevirtual

Definition at line 5773 of file klang.h.

5773 {
5774 for (unsigned int n = 0; n < notes.count; n++)
5775 if (notes[n]->pitch == pitch && notes[n]->stage == Note::Sustain)
5776 notes[n]->release(velocity);
5777 }

References klang::Array< TYPE, CAPACITY >::count.

◆ noteOn()

virtual event klang::Synth::noteOn ( int pitch,
float velocity )
inlinevirtual

Definition at line 5766 of file klang.h.

5766 {
5767 const int n = notes.assign();
5768 if (n != -1)
5769 notes[n]->start((float)pitch, velocity);
5770 }
int assign()
Definition klang.h:5679

References klang::Notes< SYNTH, NOTE >::assign().

◆ onControl()

virtual event klang::Synth::onControl ( int index,
float value )
inlineoverridevirtual

Reimplemented from klang::Controller.

Definition at line 5742 of file klang.h.

5742 {
5743 this->control(index, value);
5744 for (unsigned int n = 0; n < notes.count; n++)
5745 if (notes[n]->stage != Note::Off)
5746 notes[n]->onControl(index, value);
5747 };
virtual event control(int index, float value)
Definition klang.h:5509

References klang::Array< TYPE, CAPACITY >::count.

◆ onMIDI()

virtual event klang::Synth::onMIDI ( int status,
int byte1,
int byte2 )
inlineoverridevirtual

Reimplemented from klang::Controller.

Definition at line 5750 of file klang.h.

5750 {
5751 this->midi(status, byte1, byte2);
5752 for (unsigned int n = 0; n < notes.count; n++)
5753 if (notes[n]->stage != Note::Off)
5754 notes[n]->onMIDI(status, byte1, byte2);
5755 }
virtual event midi(int status, int byte1, int byte2)
Definition klang.h:5511

References klang::Array< TYPE, CAPACITY >::count.

◆ onPreset()

virtual event klang::Synth::onPreset ( int index)
inlineoverridevirtual

Reimplemented from klang::Controller.

Definition at line 5758 of file klang.h.

5758 {
5759 this->preset(index);
5760 for (unsigned int n = 0; n < notes.count; n++)
5761 if (notes[n]->stage != Note::Off)
5762 notes[n]->onPreset(index);
5763 };
virtual event preset(int index)
Definition klang.h:5510

References klang::Array< TYPE, CAPACITY >::count.

◆ 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; }

◆ playing()

virtual bool klang::Synth::playing ( ) const
inlinevirtual

Definition at line 5789 of file klang.h.

5789 {
5790 for (unsigned int n = 0; n < notes.count; n++)
5791 if (notes[n]->stage != Note::Off)
5792 return true;
5793 return false;
5794 }

References klang::Array< TYPE, CAPACITY >::count.

◆ prepare()

virtual void klang::Effect::prepare ( )
inlinevirtualinherited

Definition at line 5538 of file klang.h.

5538{};

◆ preset()

virtual event klang::Controller::preset ( int index)
inlineprotectedvirtualinherited

Definition at line 5510 of file klang.h.

5510{};

◆ process() [1/3]

virtual void klang::Synth::process ( )
inlineoverridevirtual

Reimplemented from klang::Effect.

Definition at line 5786 of file klang.h.

5786{ out = in; }

◆ process() [2/3]

virtual void klang::Synth::process ( buffer buffer)
inlineoverridevirtual

Reimplemented from klang::Effect.

Definition at line 5787 of file klang.h.

5787{ Effect::process(buffer); }
virtual void process()
Definition klang.h:5539

◆ process() [3/3]

virtual void klang::Synth::process ( float * buffer,
int length,
float * parameters = nullptr )
inlinevirtual

Definition at line 5796 of file klang.h.

5796 {
5797 klang::buffer mono(buffer, length);
5798
5799 // sync parameters
5800 if (parameters) {
5801 for (unsigned int c = 0; c < controls.size(); c++)
5802 controls[c].set(parameters[c]);
5803 }
5804
5805 // generate note audio
5806 for (unsigned int n = 0; n < notes.count; n++) {
5807 Note* note = notes[n];
5808 if (note->stage != Note::Off) {
5809 if (!note->process(mono))
5810 note->stop();
5811 }
5812 }
5813
5814 // apply post processing
5815 this->process(mono);
5816
5817 // sync update (changed by process())
5818 if (parameters) {
5819 for (unsigned int c = 0; c < controls.size(); c++)
5820 parameters[c] = controls[c].value;
5821 }
5822 }
Audio buffer (mono)
Definition klang.h:2610
unsigned int size() const
The current number of items in the array.
Definition klang.h:315
Controls controls
Definition klang.h:5530
Note Note
Definition klang.h:5720
virtual void process() override
Definition klang.h:5786

References klang::Array< TYPE, CAPACITY >::count.

◆ reset()

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

Definition at line 2981 of file klang.h.

2981{ out = 0; }

Member Data Documentation

◆ controls

Controls klang::Plugin::controls
inherited

Definition at line 5530 of file klang.h.

◆ in

◆ notes

Notes<Synth, Note> klang::Synth::notes

Definition at line 5722 of file klang.h.

◆ out

Definition at line 2961 of file klang.h.

2961{ 0.f };

◆ presets

Presets klang::Plugin::presets
inherited

Definition at line 5531 of file klang.h.