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

Language Guide / Signal Generators

Oscillator

klang::Oscillator

Base class for mono oscillators.

Oscillators generate repeating signals. Use them as sound sources at audio frequencies or to modulate controls at lower frequencies. Derive from this class to build a waveform using Klang's frequency and phase handling.

Set frequency and read a sample

Call osc.set(hz) before generating the first sample. Assign the oscillator to a signal to generate one sample, or use osc(hz) to set the frequency inline before reading or routing the sample. Store the result if several expressions need the same sample.

Example ยท Blend a sine and a saw

This subclass passes its frequency to Sine and Saw and reads one sample from each. Set blend between 0 (sine) and 1 (saw). Each source advances its own phase, so reset() resets both sources.

struct OscillatorBlend : Oscillator {
    Sine sine;
    Saw saw;
    float blend = 0.5; // 0 = sine, 1 = saw

    void reset() override {
        sine.reset();
        saw.reset();
    }

    void process() override {
        out = sine(frequency) * (1.0 - blend) + saw(frequency) * blend;
    }
};

Audio oscillator object (mono output) More...

#include <klang.h>

+ Inheritance diagram for klang::Oscillator:

Public Member Functions

virtual void reset ()
 
virtual void set (param frequency)
 
virtual void set (param frequency, param phase)
 
virtual void set (param frequency, relative phase)
 
virtual void set (relative phase)
 
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)
 

Public Attributes

Frequency frequency
 
Phase offset
 
signal out
 

Protected Attributes

Phase increment
 
Phase position
 

Detailed Description

Definition at line 3684 of file klang.h.

Member Function Documentation

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

◆ process()

◆ reset()

virtual void klang::Generic::Oscillator< signal >::reset ( )
inlinevirtualinherited

Reimplemented in klang::Generators::Fast::Phasor, and klang::Generators::Fast::Sine.

Definition at line 3651 of file klang.h.

◆ set() [1/4]

virtual void klang::Generic::Oscillator< signal >::set ( param frequency)
inlinevirtualinherited

Reimplemented in klang::Generators::Fast::Phasor, klang::Generators::Fast::Sine, and klang::Wavetable.

Definition at line 3654 of file klang.h.

3654 {
3656 increment = frequency * 2.f * pi.f / fs;
3657 }
constexpr constant pi
The mathematical constant, pi (and it's inverse).
Definition klang.h:287
klang::dB fs
const float f
Constant as float.
Definition klang.h:162

◆ set() [2/4]

virtual void klang::Generic::Oscillator< signal >::set ( param frequency,
param phase )
inlinevirtualinherited

Reimplemented in klang::Generators::Fast::Phasor, klang::Generators::Fast::Sine, and klang::Wavetable.

Definition at line 3659 of file klang.h.

3659 {
3660 position = phase;
3661 set(frequency);
3662 }

◆ set() [3/4]

virtual void klang::Generic::Oscillator< signal >::set ( param frequency,
relative phase )
inlinevirtualinherited

Reimplemented in klang::Generators::Fast::Phasor, klang::Generators::Fast::Sine, and klang::Wavetable.

Definition at line 3664 of file klang.h.

3664 {
3665 set(frequency);
3666 set(phase);
3667 }

◆ set() [4/4]

virtual void klang::Generic::Oscillator< signal >::set ( relative phase)
inlinevirtualinherited

Reimplemented in klang::Generators::Fast::Phasor, klang::Generators::Fast::Sine, and klang::Wavetable.

Definition at line 3669 of file klang.h.

3669 {
3670 offset = phase * (2 * pi);
3671 }

Member Data Documentation

◆ frequency

Frequency klang::Generic::Oscillator< signal >::frequency
inherited

Definition at line 3647 of file klang.h.

◆ increment

Phase klang::Generic::Oscillator< signal >::increment
protectedinherited

Definition at line 3644 of file klang.h.

◆ offset

Phase klang::Generic::Oscillator< signal >::offset
inherited

Definition at line 3648 of file klang.h.

◆ out

Definition at line 2961 of file klang.h.

2961{ 0.f };

◆ position

Phase klang::Generic::Oscillator< signal >::position
protectedinherited

Definition at line 3645 of file klang.h.