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

Language Guide / Plugin Architecture

Stereo::Synth

klang::Stereo::Synth

Base class for polyphonic stereo synthesisers.

Use Stereo::Note for voices that render two channels. Register them in the synth’s note pool just as you would for a mono synth. Inside a Stereo::Synth subclass, use Mono::Note for a mono voice whose output should feed both channels.

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

Example · Stereo synth with mono voices

Inside the synth class, Mono::Note selects the mono-voice adapter. Each voice generates one sample, and the adapter adds that sample to both channels of the stereo mix. The inherited note-off behaviour stops the voice.

struct StereoOrgan : Stereo::Synth {
    struct Voice : Mono::Note {
        Sine sine;

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

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

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

Example · Stereo synth with stereo voices

Inside the synth class, Note selects its stereo note type. Each voice writes its left and right outputs separately. This example detunes the right oscillator slightly to give the voice stereo width.

struct WideOrgan : Stereo::Synth {
    struct Voice : Note {
        Sine left, right;

        event on(Pitch pitch, Amplitude velocity) {
            const param hz = pitch -> Frequency;
            left.set(hz);
            right.set(hz * 1.003);
        }

        void process() {
            left * 0.1 >> out.l;
            right * 0.1 >> out.r;
        }
    };

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

Process the stereo voice mix

After summing the voices, the synth feeds the combined left and right signals to its own effect stage. Its process() reads the mix through in.l and in.r and writes the final output to out.l and out.r. This uses exactly the same form as a Stereo::Effect.

Here the synth filters the complete stereo mix with independent history for each channel. The stereo note’s internals are abbreviated from the earlier example.

struct FilteredStereoOrgan : Stereo::Synth {
    struct Voice : Note { /* ... */ };

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

    LPF leftFilter, rightFilter;

    void process() {
        in.l >> leftFilter(2000, 0.707) >> out.l;
        in.r >> rightFilter(2000, 0.707) >> out.r;
    }
};

See also Synth.

Synthesier mini-plugin (stereo) More...

#include <klang.h>

+ Inheritance diagram for klang::Stereo::Synth:

Classes

struct  Mono
 
struct  Notes
 Synthesiser note array (stereo) More...
 

Public Types

typedef Stereo::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 **buffers, int length, float *parameters=nullptr)
 
virtual void prepare ()
 
virtual void process (interleaved::buffer buffer)
 Process interleaved host frames; inherited by the current Stereo::Sound.
 
 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

klang::Stereo::Synth::Notes notes
 
Controls controls
 
Presets presets
 
signal in
 
signal out
 

Detailed Description

Definition at line 6143 of file klang.h.


Class Documentation

◆ klang::Stereo::Synth::Mono

struct klang::Stereo::Synth::Mono

Definition at line 6146 of file klang.h.

Class Members
typedef Note Note

Member Typedef Documentation

◆ Note

Definition at line 6144 of file klang.h.

Constructor & Destructor Documentation

◆ Synth()

klang::Stereo::Synth::Synth ( )
inline

Definition at line 6153 of file klang.h.

6153: notes(this) {}
klang::Stereo::Synth::Notes notes

◆ ~Synth()

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

Definition at line 6154 of file klang.h.

6154{}

Member Function Documentation

◆ allNotesOff()

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

Definition at line 6208 of file klang.h.

6208 {
6209 for (unsigned int n = 0; n < notes.count; n++)
6210 if (notes[n]->stage != Note::Off)
6211 notes[n]->release(velocity);
6212 }
unsigned int count
Definition klang.h:308

◆ 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::Stereo::Synth::indexOf ( Note * note) const
inline

Definition at line 6160 of file klang.h.

6160 {
6161 int index = 0;
6162 for (const auto* n : notes.items) {
6163 if (note == n)
6164 return index;
6165 index++;
6166 }
6167 return -1; // not found
6168 }
TYPE items[CAPACITY]
Definition klang.h:309

◆ 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::Stereo::Synth::noteOff ( int pitch,
float velocity )
inlinevirtual

Definition at line 6202 of file klang.h.

6202 {
6203 for (unsigned int n = 0; n < notes.count; n++)
6204 if (notes[n]->pitch == pitch && notes[n]->stage == Note::Sustain)
6205 notes[n]->release(velocity);
6206 }

◆ noteOn()

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

Definition at line 6195 of file klang.h.

6195 {
6196 const int n = notes.assign();
6197 if (n != -1)
6198 notes[n]->start((float)pitch, velocity);
6199 }
int assign()
Definition klang.h:5679

◆ onControl()

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

Reimplemented from klang::Controller.

Definition at line 6171 of file klang.h.

6171 {
6172 this->control(index, value);
6173 for (unsigned int n = 0; n < notes.count; n++)
6174 if (notes[n]->stage != Note::Off)
6175 notes[n]->onControl(index, value);
6176 };
virtual event control(int index, float value)
Definition klang.h:5509

◆ onMIDI()

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

Reimplemented from klang::Controller.

Definition at line 6179 of file klang.h.

6179 {
6180 this->midi(status, byte1, byte2);
6181 for (unsigned int n = 0; n < notes.count; n++)
6182 if (notes[n]->stage != Note::Off)
6183 notes[n]->onMIDI(status, byte1, byte2);
6184 }
virtual event midi(int status, int byte1, int byte2)
Definition klang.h:5511

◆ onPreset()

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

Reimplemented from klang::Controller.

Definition at line 6187 of file klang.h.

6187 {
6188 this->preset(index);
6189 for (unsigned int n = 0; n < notes.count; n++)
6190 if (notes[n]->stage != Note::Off)
6191 notes[n]->onPreset(index);
6192 };
virtual event preset(int index)
Definition klang.h:5510

◆ 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::Stereo::Synth::playing ( ) const
inlinevirtual

Definition at line 6218 of file klang.h.

6218 {
6219 for (unsigned int n = 0; n < notes.count; n++)
6220 if (notes[n]->stage != Note::Off)
6221 return true;
6222 return false;
6223 }

◆ prepare()

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

Definition at line 6066 of file klang.h.

6066{};

◆ preset()

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

Definition at line 5510 of file klang.h.

5510{};

◆ process() [1/4]

virtual void klang::Stereo::Effect::process ( interleaved::buffer buffer)
inlinevirtualinherited

Reimplemented in klang::Stereo::Sound.

Definition at line 6080 of file klang.h.

6080 {
6081 this->prepare();
6082 while (!buffer.finished()) {
6083 input(buffer);
6084 this->process();
6085 buffer++ = out;
6086#if HAS_KLANG_DEBUG
6087 debug.buffer++;
6088#endif
6089 }
6090 }
klang::interleaved::buffer< 2 > buffer
Definition klang.h:5999
static Debug debug
Definition klang.h:4518
static THREAD_LOCAL Buffer buffer
Definition klang.h:4361
virtual void prepare()
Definition klang.h:6066
virtual void process()
Definition klang.h:6067

References klang::Debug::buffer, klang::debug, and klang::Stereo::buffer::finished().

◆ process() [2/4]

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

Reimplemented from klang::Stereo::Effect.

Definition at line 6215 of file klang.h.

6215{ out = in; }

◆ process() [3/4]

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

Reimplemented from klang::Stereo::Effect.

Definition at line 6216 of file klang.h.

6216{ Effect::process(buffer); }

◆ process() [4/4]

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

Definition at line 6225 of file klang.h.

6225 {
6226 klang::buffer left(buffers[0], length);
6227 klang::buffer right(buffers[1], length);
6228 klang::Stereo::buffer buffer(left, right);
6229
6230 // sync parameters
6231 if (parameters) {
6232 for (unsigned int c = 0; c < controls.size(); c++)
6233 controls[c].set(parameters[c]);
6234 }
6235
6236 // generate note audio
6237 for (unsigned int n = 0; n < notes.count; n++) {
6238 Note* note = notes[n];
6239 if (note->stage != Note::Off) {
6240 if (!note->process(buffer))
6241 note->stop();
6242 }
6243 }
6244
6245 // apply post processing
6246 this->process(buffer);
6247
6248 // sync update (changed by process())
6249 if (parameters) {
6250 for (unsigned int c = 0; c < controls.size(); c++)
6251 parameters[c] = controls[c].value;
6252 }
6253 }
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
Stereo::Note Note
Definition klang.h:6144
virtual void process() override
Definition klang.h:6215
Stereo audio buffer.
Definition klang.h:5934

References klang::Stereo::Note::process(), klang::NoteBase< SYNTH >::stage, and klang::NoteBase< SYNTH >::stop().

◆ 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

klang::Stereo::Synth::Notes klang::Stereo::Synth::notes

◆ 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.