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

Language Guide / Plugin Architecture

Note

klang::Note

Base class for a mono synth voice.

A note owns the DSP state for a single voice. Initialise that state in on(Pitch, Amplitude) when a note starts. Use off(Amplitude) to begin the release stage, and call stop() when the voice is finished so the synth can reuse it.

Example · Play a sine voice

Set the oscillator’s pitch when the note starts and write one sample to out each time it processes. This voice uses the inherited note-off behaviour, which stops it immediately.

struct SineVoice : Note {
    Sine sine;

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

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

Example · Release a voice with an envelope

These methods belong in a Note subclass with a configured Sine sine; and ADSR adsr;.

event off(Amplitude velocity) {
    adsr.release();
}

void process() {
    sine * adsr >> out;
    if (adsr.finished())
        stop();
}

Synthesiser note (mono) More...

#include <klang.h>

+ Inheritance diagram for klang::Note:

Public Types

enum  Stage
 

Public Member Functions

virtual void prepare ()
 
virtual void process () override=0
 
virtual bool process (buffer buffer)
 
virtual bool process (buffer *buffer)
 
void attach (Synth *synth)
 
virtual void init ()
 
virtual void start (Pitch p, Velocity v)
 
virtual bool release (Velocity v=0)
 
virtual bool stop (Velocity v=0)
 
bool finished () const
 
virtual void controlChange (int controller, int value)
 
virtual void onControl (int index, float value)
 
virtual void onPreset (int index)
 
virtual void onMIDI (int status, int byte1, int byte2)
 
Output< signal > & operator() (params &&... p)
 
 operator const signal & () override
 
 operator const signal & () const override
 
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

Pitch pitch
 
Velocity velocity
 
Controls controls
 
enum klang::NoteBase::Stage stage
 
signal out
 

Detailed Description

Definition at line 5633 of file klang.h.

Member Enumeration Documentation

◆ Stage

enum klang::NoteBase::Stage
inherited

Member Function Documentation

◆ attach()

void klang::NoteBase< Synth >::attach ( Synth * synth)
inlineinherited

Definition at line 5590 of file klang.h.

5590 {
5591 NoteBase::synth = synth;
5592 controls = synth->controls;
5593 init();
5594 }
virtual void init()
Definition klang.h:5596
Controls controls
Definition klang.h:5530

◆ control()

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

Definition at line 5509 of file klang.h.

5509{};

◆ controlChange()

virtual void klang::NoteBase< Synth >::controlChange ( int controller,
int value )
inlinevirtualinherited

Definition at line 5627 of file klang.h.

5627{ midi(0xB0, controller, value); };
virtual event midi(int status, int byte1, int byte2)
Definition klang.h:5511

◆ finished()

bool klang::NoteBase< Synth >::finished ( ) const
inlineinherited

Definition at line 5623 of file klang.h.

5623{ return stage == Off; }

◆ getSynth()

auto klang::NoteBase< Synth >::getSynth ( )
inlineprotectedinherited

Definition at line 5581 of file klang.h.

5581{ return static_cast<std::remove_pointer_t<decltype(synth)>*>(synth); }

◆ init()

virtual void klang::NoteBase< Synth >::init ( )
inlinevirtualinherited

Definition at line 5596 of file klang.h.

5596{}

◆ midi()

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

Definition at line 5511 of file klang.h.

5511{};

◆ off()

virtual event klang::NoteBase< Synth >::off ( Velocity v = 0)
inlineprotectedvirtualinherited

Definition at line 5578 of file klang.h.

5578{ stage = Off; }

◆ on()

virtual event klang::NoteBase< Synth >::on ( Pitch p,
Velocity v )
inlineprotectedvirtualinherited

Definition at line 5577 of file klang.h.

5577{}

◆ onControl()

virtual void klang::Controller::onControl ( int index,
float value )
inlinevirtualinherited

Reimplemented in klang::Stereo::Synth, and klang::Synth.

Definition at line 5513 of file klang.h.

5513{ this->control(index, value); };
virtual event control(int index, float value)
Definition klang.h:5509

◆ onMIDI()

virtual void klang::Controller::onMIDI ( int status,
int byte1,
int byte2 )
inlinevirtualinherited

Reimplemented in klang::Stereo::Synth, and klang::Synth.

Definition at line 5515 of file klang.h.

5515{ this->midi(status, byte1, byte2); }

◆ onPreset()

virtual void klang::Controller::onPreset ( int index)
inlinevirtualinherited

Reimplemented in klang::Stereo::Synth, and klang::Synth.

Definition at line 5514 of file klang.h.

5514{ this->preset(index); };
virtual event preset(int index)
Definition klang.h:5510

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

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

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

Definition at line 3057 of file klang.h.

3057{ return out; } // return last output

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

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

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

Definition at line 3056 of file klang.h.

3056{ this->process(); return out; } // return processed output

◆ operator()()

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

Definition at line 3050 of file klang.h.

3050 {
3051 set(std::forward<params>(p)...); return *this;
3052 }

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

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

◆ prepare()

virtual void klang::Note::prepare ( )
inlinevirtual

Definition at line 5634 of file klang.h.

5634{}

◆ preset()

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

Definition at line 5510 of file klang.h.

5510{};

◆ process() [1/3]

virtual void klang::Note::process ( )
overridepure virtual

◆ process() [2/3]

virtual bool klang::Note::process ( buffer * buffer)
inlinevirtual

Definition at line 5647 of file klang.h.

5647 {
5648 return this->process(buffer[0]);
5649 }
virtual void process() override=0

◆ process() [3/3]

virtual bool klang::Note::process ( buffer buffer)
inlinevirtual

Definition at line 5636 of file klang.h.

5636 {
5637 this->prepare();
5638 while (!buffer.finished()) {
5639 this->process();
5640 buffer++ += out;
5641#if HAS_KLANG_DEBUG
5642 debug.buffer++;
5643#endif
5644 }
5645 return !finished();
5646 }
bool finished() const
Definition klang.h:5623
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:5634

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

◆ release()

virtual bool klang::NoteBase< Synth >::release ( Velocity v = 0)
inlinevirtualinherited

Definition at line 5606 of file klang.h.

5606 {
5607 if (stage == Off)
5608 return true;
5609
5610 if (stage != Release) {
5611 stage = Release;
5612 off(v);
5613 }
5614
5615 return stage == Off;
5616 }
virtual event off(Velocity v=0)
Definition klang.h:5578

◆ reset()

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

Definition at line 2981 of file klang.h.

2981{ out = 0; }

◆ start()

virtual void klang::NoteBase< Synth >::start ( Pitch p,
Velocity v )
inlinevirtualinherited

Definition at line 5598 of file klang.h.

5598 {
5599 stage = Onset;
5600 pitch = p;
5601 velocity = v;
5602 on(pitch, velocity);
5603 stage = Sustain;
5604 }
virtual event on(Pitch p, Velocity v)
Definition klang.h:5577

◆ stop()

virtual bool klang::NoteBase< Synth >::stop ( Velocity v = 0)
inlinevirtualinherited

Definition at line 5618 of file klang.h.

5618 {
5619 stage = Off;
5620 return true;
5621 }

Member Data Documentation

◆ controls

Controls klang::NoteBase< Synth >::controls
inherited

Definition at line 5585 of file klang.h.

◆ out

Definition at line 2961 of file klang.h.

2961{ 0.f };

◆ pitch

Pitch klang::NoteBase< Synth >::pitch
inherited

Definition at line 5583 of file klang.h.

◆ stage

enum klang::NoteBase::Stage klang::NoteBase< Synth >::stage
inherited

◆ velocity

Velocity klang::NoteBase< Synth >::velocity
inherited

Definition at line 5584 of file klang.h.