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>
|
| template<typename DERIVED , typename... ARGS> |
| static DERIVED * | Create (ARGS &&... args) |
| |
Definition at line 5719 of file klang.h.
◆ Note
◆ Synth()
Definition at line 5724 of file klang.h.
Notes< Synth, Note > notes
◆ ~Synth()
| virtual klang::Synth::~Synth |
( |
| ) |
|
|
inlinevirtual |
◆ allNotesOff()
| virtual event klang::Synth::allNotesOff |
( |
float | velocity = 0.f | ) |
|
|
inlinevirtual |
◆ control()
| virtual event klang::Controller::control |
( |
int | index, |
|
|
float | value ) |
|
inlineprotectedvirtualinherited |
◆ 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 |
◆ input() [1/3]
|
|
inlineprotectedvirtualinherited |
◆ input() [2/3]
◆ input() [3/3]
◆ midi()
| virtual event klang::Controller::midi |
( |
int | status, |
|
|
int | byte1, |
|
|
int | byte2 ) |
|
inlineprotectedvirtualinherited |
◆ noteOff()
| virtual event klang::Synth::noteOff |
( |
int | pitch, |
|
|
float | velocity ) |
|
inlinevirtual |
◆ noteOn()
| virtual event klang::Synth::noteOn |
( |
int | pitch, |
|
|
float | velocity ) |
|
inlinevirtual |
◆ onControl()
| virtual event klang::Synth::onControl |
( |
int | index, |
|
|
float | value ) |
|
inlineoverridevirtual |
◆ onMIDI()
| virtual event klang::Synth::onMIDI |
( |
int | status, |
|
|
int | byte1, |
|
|
int | byte2 ) |
|
inlineoverridevirtual |
◆ onPreset()
| virtual event klang::Synth::onPreset |
( |
int | index | ) |
|
|
inlineoverridevirtual |
◆ operator const signal &() [1/2]
|
|
inlineoverridevirtualinherited |
◆ operator const signal &() [2/2]
|
|
inlineoverridevirtualinherited |
◆ operator()()
Definition at line 3098 of file klang.h.
3098 {
3099 set(std::forward<params>(p)...); return *this;
3100 }
◆ operator*()
◆ operator+()
◆ operator-()
◆ operator/()
◆ operator<<()
◆ operator>>()
◆ output()
◆ playing()
| virtual bool klang::Synth::playing |
( |
| ) |
const |
|
inlinevirtual |
◆ prepare()
| virtual void klang::Effect::prepare |
( |
| ) |
|
|
inlinevirtualinherited |
◆ preset()
| virtual event klang::Controller::preset |
( |
int | index | ) |
|
|
inlineprotectedvirtualinherited |
◆ process() [1/3]
| virtual void klang::Synth::process |
( |
| ) |
|
|
inlineoverridevirtual |
◆ process() [2/3]
| virtual void klang::Synth::process |
( |
buffer | buffer | ) |
|
|
inlineoverridevirtual |
◆ 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 {
5798
5799
5800 if (parameters) {
5803 }
5804
5805
5806 for (
unsigned int n = 0; n <
notes.
count; n++) {
5809 if (!note->process(mono))
5810 note->stop();
5811 }
5812 }
5813
5814
5816
5817
5818 if (parameters) {
5821 }
5822 }
unsigned int size() const
The current number of items in the array.
virtual void process() override
References klang::Array< TYPE, CAPACITY >::count.
◆ reset()
◆ controls
◆ in
◆ notes
◆ out
◆ presets