Klang C++ 0.7.15
Language Reference (draft)
Loading...
Searching...
No Matches
Examples

Open an example to see the source from the Klang examples folder. Each listing keeps the model’s own type name. In Studio, use the matching file name for the model.

Effects Processes

Amplitude and balance

These examples change level or stereo balance, including through modulation.

Level and gain

Gain/Gain.k

#include <klang.h>
using namespace klang::optimised;
struct Gain : Effect {
// Initialise plugin (called once at startup)
Gain() {
controls = {
Dial("Gain", 0.0, 1.0, 0.5),
};
}
// Apply processing (called once per sample)
void process() {
param gain = controls[0];
in * gain >> out;
}
};
Effect mini-plugin (mono)
Definition klang.h:5535
A signal used as a control parameter.
Definition klang.h:1486

Stereo pan

Gain/Pan.k

#include <klang.h>
using namespace klang::optimised;
struct Pan : Stereo::Effect {
// Initialise plugin (called once at startup)
Pan() {
controls = {
Dial("Pan", 0.0, 1.0, 0.5),
};
}
// Apply processing (called once per sample)
void process() {
param x = controls[0];
in.l * (1 - x) >> out.l;
in.r * (x) >> out.r;
}
};
Stereo effect mini-plugin.
Definition klang.h:6063

Tremolo

Gain/Tremolo.k

#include <klang.h>
using namespace klang::optimised;
struct Tremolo : Effect {
Sine lfo;
// Initialise plugin (called once at startup)
Tremolo() {
controls = {
Dial("Mod Rate", 1.0, 10.0, 6.0),
Dial("Mod Depth", 0.0, 0.5, 0.5),
};
}
// Prepare for processing (called once per buffer)
void prepare() {
}
// Apply processing (called once per sample)
void process() {
param rate = controls[0];
param depth = controls[1];
signal mod = lfo(rate) * depth + (1 - depth);
mod >> debug;
in * mod >> out;
}
};
auto mod(LEFT left, RIGHT right)
Return the C++ remainder for integers and the floating-point remainder otherwise.
Definition klang.h:3881
Sine wave oscillator (band-limited, optimised)
Definition klang.h:6482
A mono audio signal (equivalent to a float).
Definition klang.h:1123

Ring modulation

Gain/RM.k

#include <klang.h>
using namespace klang::optimised;
struct RM : Effect {
Sine lfo;
// Initialise plugin (called once at startup)
RM() {
controls = {
Dial("Mod Rate", 1.0, 1000.0, 6.0),
Dial("Mod Depth", 0.0, 0.5, 0.5),
};
}
// Apply processing (called once per sample)
void process() {
param rate = controls[0];
param depth = controls[1];
signal mod = lfo(rate);
mod >> debug;
in * mod >> out;
}
};

Distortion

These examples range from hard limiting to waveshaping with a plotted transfer curve.

Hard clipping

Distortion/Clipping.k

#include <klang.h>
using namespace klang::optimised;
struct Clipping : Effect {
// Initialise plugin (called once at startup)
Clipping() {
controls = {
Dial("Overdrive", 1.0, 11.0, 1.0),
};
}
// Apply processing (called once per sample)
void process() {
param gain = controls[0];
in *= gain;
if(in > 1)
in = 1;
else if(in < -1)
in = -1;
in >> out;
}
};

Transfer functions

Distortion/Functions.k

#include <klang.h>
using namespace klang::optimised;
float hardclip(float x){
if(x > 1)
return 1;
else if(x < -1)
return -1;
else
return x;
}
struct Functions : Effect {
// Initialise plugin (called once at startup)
Functions() {
controls = {
Dial("Overdrive", 1.0, 25.0, 1.0),
};
hardclip >> graph(-2,2,-2,2);
}
// Apply processing (called once per sample)
void process() {
param gain = controls[0];
hardclip(in * gain) >> out;
}
};

Soft clipping and shaping

Distortion/Shaping.k

#include <klang.h>
using namespace klang::optimised;
float hardclip(float x){
if(x > 1)
return 1;
else if(x < -1)
return -1;
else
return x;
}
float softclip(float x, float c){
return tanh(c * x) / tanh(c);
}
struct Shaping : Effect {
// Initialise plugin (called once at startup)
Shaping() : f(softclip) {
controls = {
Dial("Distort", 0.001, 5.6, 0.001),
};
}
// Prepare for processing (called once per buffer)
void prepare() {
param distort = controls[0];
hardclip >> graph(-2,2);
f(distort) >> graph;
}
// Apply processing (called once per sample)
void process() {
param distort = controls[0];
in >> f(distort) >> out;
}
};
Applies a function to a signal (input-output)
Definition klang.h:3735

Mute

Distortion/Mute.k

#include <klang.h>
using namespace klang::optimised;
struct Mute : Effect {
// Initialise plugin (called once at startup)
Mute() {
controls = {
Toggle("Mute"),
};
}
// Prepare for processing (called once per buffer)
void prepare() {
}
// Apply processing (called once per sample)
void process() {
param gain;
param mute = controls[0];
if(mute)
gain = 0.f;
else
gain = 1.f;
in * gain >> out;
}
};

Filtering

Compare frequency-selective effects with a custom recursive filter.

Low-, high- and band-pass filters

Filtering/Objects.k

#include <klang.h>
using namespace klang::optimised;
struct Objects : Effect {
LPF filter;
Noise noise;
// Initialise plugin (called once at startup)
Objects() {
controls = {
Dial("Cutoff", 10, 1000, 1000),
Dial("Resonance", 0.1, 10, root2),
};
}
// Prepare for processing (called once per buffer)
void prepare() {
filter(controls[0], controls[1]);
}
// Apply processing (called once per sample)
void process() {
noise >> filter >> out;
}
};
Low-pass filter (LPF)
Definition klang.h:7560
White noise generator (optimised)
Definition klang.h:6568

Custom IIR filter

Filtering/IIR.k

#include <klang.h>
using namespace klang::optimised;
struct IIR : Effect {
signal last = 0;
// Initialise plugin (called once at startup)
IIR() {
controls = {
Dial("Filter", 0.0, 1.0, 0.5),
};
}
signal filter(signal dry){
param f = cube(controls[0]);
signal filtered = (1 - f) * last + f * dry;
last = filtered;
return filtered;
}
// Apply processing (called once per sample)
void process() {
filter(in) >> out;
}
};
TYPE cube(TYPE input)
Definition klang.h:4201
IIR filter for specified order.
Definition klang.h:7306

Wah-wah

Filtering/WahWah.k

#include <klang.h>
using namespace klang::optimised;
struct WahWah : Effect {
LPF lpf;
Sine lfo;
// Initialise plugin (called once at startup)
WahWah() {
controls = {
Dial("f", 10, 10000, 1000),
Dial("Q", 0.1, 10, 1),
Dial("Rate", 4, 10, 6),
};
}
// Apply processing (called once per sample)
void process() {
param f = controls[0];
param Q = controls[1];
param rate = controls[2];
signal mod = lfo(rate) * 0.5 + 0.5f;
in >> lpf(sqr(mod) * f, Q) >> out;
}
};
TYPE sqr(TYPE input)
Stateless square and cube functions preserve Klang signals in audio expressions.
Definition klang.h:4199

Filter bands

Filtering/Bands.k

#include <klang.h>
using namespace klang::optimised;
struct Bands : Effect {
BPF filter1;
BPF filter2;
// Initialise plugin (called once at startup)
Bands() {
controls = {
{ "Band 1",
Dial("f", 10, 1000, 100),
Dial("Q", 0.1, 10, root2),
},
{ "Band 2",
Dial("f", 10, 1000, 500),
Dial("Q", 0.1, 10, root2),
}
};
}
// Prepare for processing (called once per buffer)
void prepare() {
}
// Apply processing (called once per sample)
void process() {
param gain = controls[0];
signal band1 = in >> filter1(controls[0], controls[1]);
signal band2 = in >> filter2(controls[2], controls[3]);
band1 + band2 >> out;
}
};
Band-pass filter (BPF)
Definition klang.h:7594

Three-band EQ

Filtering/EQ.k

#include <klang.h>
using namespace klang::optimised;
struct EQ : Effect {
LPF lpf;
HPF hpf;
// Initialise plugin (called once at startup)
EQ() {
controls = {
Dial("Low", 0.0, 1.0, 0.5),
Dial("Mid", 0.0, 1.0, 0.5),
Dial("High", 0.0, 1.0, 0.5),
};
}
// Prepare for processing (called once per buffer)
void prepare() {
lpf.set(200);
hpf.set(4000);
}
// Apply processing (called once per sample)
void process() {
signal lows = in >> lpf;
signal highs = in >> hpf;
signal mids = in - (lows + highs);
lows * sqr(controls[0]) + mids * sqr(controls[1]) + highs * sqr(controls[2]) >> out;
}
};
void set(param f)
Set the filter cutoff (default Q)
Definition klang.h:7480
High-pass filter (HPF)
Definition klang.h:7577

Delay and reverb

Start with a single echo, then follow how taps and feedback build more complex responses.

Basic echo

Delay/Echo.k

#include <klang.h>
using namespace klang::optimised;
struct Echo : Effect {
// Initialise plugin (called once at startup)
Echo() {
controls = {
Dial("Delay", 0.0, 1.0, 0.5),
Dial("Gain", 0.0, 1.0, 0.5),
};
}
// Apply processing (called once per sample)
void process() {
param time = controls[0] * fs;
param gain = controls[1];
in >> delay;
in + delay(time) * gain >> out;
}
};
klang::dB fs
Audio delay object (fixed size)
Definition klang.h:4599

Feedback echo

Delay/Feedback.k

#include <klang.h>
using namespace klang::optimised;
struct Feedback : Effect {
// Initialise plugin (called once at startup)
Feedback() {
controls = {
Dial("Delay", 0.0, 1.0, 0.5),
Dial("Gain", 0.0, 1.0, 0.5),
};
}
// Apply processing (called once per sample)
void process() {
param time = controls[0] * fs;
param gain = controls[1];
in + delay(time) * gain >> out;
delay << out; // feedback delay
}
};

Patterned delay

Delay/Patterns.k

#include <klang.h>
using namespace klang::optimised;
struct Patterns : Effect {
// Initialise plugin (called once at startup)
Patterns() {
controls = {
Menu("Pattern", "1", "2", "3"),
};
}
// Apply processing (called once per sample)
void process() {
in >> delay;
param times[3][3] = {
{ 0.5f, 1.0f, 1.5f },
{ 0.25f,0.5f, 1.0f },
{ 0.5f, .75f, 1.0f },
};
param gains[3][3] = {
{ .75f, 0.5f, .25f },
{ .25f, 0.5f, .75f },
{ .25f, 0.5f, .25f },
};
const int p = controls[0]; // selected pattern
signal mix = in;
for(int d=0; d<3; d++)
mix += delay(times[p][d] * fs) * gains[p][d];
mix >> out;
}
};

Ping-pong delay

Delay/PingPong.k

#include <klang.h>
using namespace klang::optimised;
struct PingPong : Stereo::Effect {
PingPong() {
controls = {
{ "Left", Dial("Time"),
Dial("Gain", 0, 0.99) },
{ "Right", Dial("Time"),
Dial("Gain", 0, 0.99) }
};
}
void process() {
stereo::signal time = { controls[0], controls[2] };
stereo::signal gain = { controls[1], controls[3] };
// in.l >> delay.l;
in.l + delay.l(time.l * fs) * gain.l >> out.l;
delay.l << out.r;
// in.r >> delay.r;
in.r + delay.r(time.r * fs) * gain.r >> out.r;
delay.r << out.l;
}
};
Audio delay object (stereo)
Definition klang.h:6008
klang::Delay< SIZE > & r
Definition klang.h:6018
klang::Delay< SIZE > & l
Definition klang.h:6018

Reverb

Delay/Reverb.k

#include <klang.h>
using namespace klang::optimised;
struct Reverb : Effect {
Delay<192000> feedforward;
Delay<192000> feedback;
LPF filter;
param times[8] = { 2.078, 5.154, 5.947, 7.544, 8.878, 10.422, 13.938, 17.140 };
param gains[8] = { .609, .262, -.360, -.470, .290, -.423, .100, .200 };
// Initialise plugin (called once at startup)
Reverb() {
controls = {
Dial("Resonance", 0, 0.5, 0.4),
Dial("Room Size", 0, 0.4, 0.1),
Dial("Brightness", 500, 5000, 1500),
};
presets = { { "Bathroom", { 0.276, 0.057, 3835.062 } },
{ "Lively Room", { 0.309, 0.060, 806.557 } },
{ "Air Vent", { 0.496, 0.089, 2807.678 } },
{ "Parking Lot", { 0.216, 0.220, 1367.304 } },
};
}
// Prepare for processing (called once per buffer)
void prepare() {
filter.set(controls[2]);
}
signal early() {
in >> feedforward;
signal mix = in;
for(int d=0; d<8; d++)
mix += feedforward(times[d] * fs/1000) * gains[d];
return mix;
}
signal late() {
param times[3] = { 0.2, 0.324, 0.777 };
param gains[3] = { 1.0, 0.6, 0.2 };
signal mix = 0;
mix += gains[0] * controls[0] * feedback((times[0] * controls[1]) * fs);
mix += gains[1] * controls[0] * feedback((times[1] * controls[1]) * fs);
mix += gains[2] * controls[0] * feedback((times[2] * controls[1]) * fs);
return mix >> filter;
}
// Apply processing (called once per sample)
void process() {
early() + late() >> out >> feedback;
}
};

Alternative reverb

Delay/Reverb2.k

#include <klang.h>
using namespace klang::optimised;
struct Reverb2 : Stereo::Effect {
Delay<192000> feedforward[2];
Delay<192000> feedback[2];
LPF filter[2];
param times[2][8] = { 2.078, 5.154, 5.947, 7.544, 8.878, 10.422, 13.938, 17.140 };
param gains[8] = { .609, .262, -.360, -.470, .290, -.423, .100, .200 };
// Initialise plugin (called once at startup)
Reverb2() {
controls = {
Dial("Resonance", 0, 0.5, 0.4),
Dial("Room Size", 0, 0.4, 0.2),
Dial("Brightness", 500, 5000, 2250),
};
for(int t2=0; t2<8; t2++)
times[1][t2] = times[0][t2] * (1.f - 0.2f * gains[t2]);
}
// Prepare for processing (called once per buffer)
void prepare() {
filter[0].set(controls[2]);
filter[1].set(controls[2]);
}
signal early(int c) {
in[c] >> feedforward[c];
signal mix = in[c];
for(int d=0; d<8; d++){
mix += feedforward[c](times[c][d] * fs/1000) * gains[d];
}
return mix;
}
signal late(int c) {
return controls[0] * feedback[c]((controls[1] + 0.01232 * c) * fs) >> filter[c];
}
// Apply processing (called once per sample)
void process() {
early(0) + late(0) >> out.l >> feedback[0];
early(1) + late(1) >> out.r >> feedback[1];
}
};

Modulated delay

A changing delay time can create vibrato, flanging, or chorus.

Modulated delay

Modulation/ModDelay.k

#include <klang.h>
using namespace klang::optimised;
struct ModDelay : Effect {
Sine lfo;
ModDelay() {
controls = {
{ "Mod", Dial("Rate"),
Dial("Depth"),
Dial("Feedback", 0, 0.9) }
};
}
void process() {
param rate = controls[0] * 9 + 1;
param depth = controls[1] * 0.01;
param gain = controls[2];
signal mod = lfo(rate) * depth + depth; // generate mod signal
signal feedback = gain * delay(mod * fs); // generate feedback signal
in + feedback >> out; // output the modulated delay
delay << out; // feedback output to the dela
}
};

Flanger

Modulation/Flanger.k

#include <klang.h>
using namespace klang::optimised;
struct Flanger : Effect {
Triangle lfo;
// Initialise plugin (called once at startup)
Flanger() {
controls = {
Dial("Mod Rate", 0.1, 1.0, 0.75),
Dial("Mod Depth", 0.1, 5.0, 1.5),
};
}
// Apply processing (called once per sample)
void process() {
param rate = controls[0];
param depth = controls[1] / 1000.f;
signal mod = lfo(rate) * depth + depth;
in + (in >> delay)(mod * fs) >> out;
}
};
Triangle wave with equal rising and falling half-cycles.
Definition klang.h:6873

Chorus study

Modulation/Chorus.k

#include <klang.h>
using namespace klang::optimised;
struct Chorus : Effect {
Sine lfo[3];
// Initialise plugin (called once at startup)
Chorus() {
controls = {
Dial("Mod Rate", 1, 10, 6),
Dial("Mod Depth", 0.0, 1.0, 0.1),
};
}
signal mod(int m){
const param rates[3] = { 2.5,3,3.5 };
const param depths[3] = { 0.45, 0.5, 0.55 };
return (lfo[m](rates[m]) * depths[m] + depths[m]) * fs / 1000.f;
}
// Apply processing (called once per sample)
void process() {
in >> delay;
0.5f * (in + delay(mod(0)) + delay(mod(1)) + delay(mod(2))) >> out;
}
};

Synthesis Techniques

Additive

Build a waveform by summing partials. The studies cover harmonic series, Nyquist limits, reusable voice structures, and resynthesis.

Additive saw

Additive/Saw.k

#include <klang.h>
using namespace klang::optimised;
struct Additive : Oscillator {
Sine osc[32];
void set(param frequency){
for(int o=0; o<32; o++)
osc[o].set(frequency * (o+1));
}
void process() {
out = 0;
for(int o=0; o<32; o++)
out += osc[o] / (o + 1);
}
};
struct Saw : Synth {
struct SawNote : public Note {
Additive osc;
event on(Pitch pitch, Amplitude velocity) {
osc.set(pitch -> Frequency);
}
void process() {
osc >> out;
}
};
Saw() {
notes.add<SawNote>(32);
}
};
Control parameter (linear amplitude)
Definition klang.h:1774
Control parameter (frequency)
Definition klang.h:1723
Descending sawtooth.
Definition klang.h:6868
Synthesiser note (mono)
Definition klang.h:5633
Audio oscillator object (mono output)
Definition klang.h:3684
Control parameter (pitch)
Definition klang.h:1690
Synthesiser object (mono)
Definition klang.h:5719

Additive square

Additive/Square.k

#include <klang.h>
using namespace klang::optimised;
struct Additive : Oscillator {
Sine osc[32];
void set(param frequency){
for(int o=0; o<32; o++)
osc[o].set(frequency * (o+1));
}
void process() {
out = 0;
for(int o=0; o<32; o++){
int h = o + 1;
if(h % 2 && osc[o].frequency < fs.nyquist)
out += osc[o] / h;
}
}
};
struct Square : Synth {
struct SquareNote : public Note {
Additive osc;
event on(Pitch pitch, Amplitude velocity) {
osc.set(pitch -> Frequency);
}
void process() {
osc >> out;
}
};
Square() {
notes.add<SquareNote>(32);
}
};
Square wave with 50% high duty.
Definition klang.h:6878

Nyquist limit

Additive/Nyquist.k

#include <klang.h>
using namespace klang::optimised;
struct Additive : Oscillator {
Sine osc[32];
void set(param frequency){
for(int o=0; o<32; o++)
osc[o].set(frequency * (o+1));
}
void process() {
out = 0;
for(int o=0; o<32; o++){
if(osc[o].frequency < fs.nyquist)
out += osc[o] / (o + 1);
}
}
};
struct Nyquist : Synth {
struct NyquistNote : public Note {
Additive osc;
event on(Pitch pitch, Amplitude velocity) {
osc.set(pitch -> Frequency);
}
void process() {
osc >> out;
}
};
Nyquist() {
notes.add<NyquistNote>(32);
}
};

Partial inheritance

Additive/Inheritance.k

#include <klang.h>
using namespace klang::optimised;
struct Additive : Oscillator {
Sine osc[32];
void set(param frequency){
for(int o=0; o<32; o++)
osc[o].set(frequency * (o+1));
}
virtual void process() = 0;
};
struct AdditiveSaw : Additive {
void process() {
out = 0;
for(int o=0; o<32; o++){
int h = o + 1;
if(osc[o].frequency < fs.nyquist)
out += osc[o] / h;
}
}
};
struct AdditiveSquare : Additive {
void process() {
out = 0;
for(int o=0; o<32; o++){
int h = o + 1;
if(h % 2 && osc[o].frequency < fs.nyquist)
out += osc[o] / h;
}
}
};
struct AdditiveTriangle : Additive {
void process() {
out = 0;
for(int o=0; o<32; o++){
int h = o + 1;
if(osc[o].frequency < fs.nyquist){
if(h % 4 == 1)
out += osc[o] / (h * h);// sqr(h);
else if(h % 4 == 3)
out -= osc[o] / (h * h);//sqr(h);
}
}
}
};
struct Inheritance : Synth {
struct InheritanceNote : public Note {
AdditiveSaw saw;
AdditiveSquare square;
AdditiveTriangle triangle;
Additive* osc = nullptr;
event on(Pitch pitch, Amplitude velocity) {
switch(int(controls[0])){
case 0: osc = &saw; break;
case 1: osc = &square; break;
case 2: osc = &triangle; break;
}
osc->set(pitch -> Frequency);
}
void process() {
*osc >> out;
}
};
Inheritance() {
controls = {
{ "Group",
Menu("Waveform", "Saw", "Square", "Triangle"),
Dial("A dial")
}
};
notes.add<InheritanceNote>(32);
}
};

Resynthesis

Additive/Resynthesis.k

#include <klang.h>
using namespace klang::optimised;
// Measurements from Korg M1 Organ sample (at C2):
// ====================================================
// 65Hz, 131Hz, 196Hz, 263Hz, 327Hz, 393Hz
// -14.5dB, -14.2dB, -14.3dB, -50.0dB, -47.2dB, -24.5dB
const float FREQ[6] = { 65/65.f, 131/65.f, 195/65.f, 263/65.f, 327/65.f, 393/65.f }; // (normalised to f0)
const dB GAIN[6] = { -14.5+12, -14.2+12, -14.3+12, -50.0+12, -47.2+12, -24.5+12 }; // (in dB)
struct Organ : Oscillator {
Sine osc[6];
void set(param frequency){
for(int o=0; o<6; o++)
osc[o].set(frequency * FREQ[o]);
}
void process() {
out = 0;
for(int o=0; o<6; o++)
out += osc[o] * GAIN[o]->Amplitude;
}
};
struct Resynthesis : Synth {
struct ResynthesisNote : public Note {
Organ osc;
ADSR adsr;
event on(Pitch pitch, Amplitude velocity) {
osc.set(pitch -> Frequency);
osc.reset();
adsr(0.001, 0.1, 0.5, 0.1);
}
event off(Amplitude velocity){
adsr.release();
}
void process() {
osc * adsr >> out;
if(adsr.finished())
stop();
}
};
Resynthesis() {
notes.add<ResynthesisNote>(32);
}
};
bool finished() const
Definition klang.h:5287
Attack-Decay-Sustain-Release Envelope.
Definition klang.h:5429
void release(float time=0.f, float level=0.f) override
Definition klang.h:5454
Control parameter (idecibels)
Definition klang.h:1759

Subtractive

Shape an oscillator with filtering, envelopes, and performance controls.

Filter voice

Subtractive/Filter.k

#include <klang.h>
using namespace klang::optimised;
struct Filter : Synth {
struct FilterNote : public Note {
Saw osc;
ADSR adsr;
Envelope env;
LPF filter;
event on(Pitch pitch, Amplitude velocity) {
param f = pitch -> Frequency;
osc(f, 0);
adsr(0,0,1,0.25);
env = { { 0, f * 2 }, { 0.25, f * 10 }, { 2, f * 5 } };
filter.reset();
}
event off(Amplitude velocity) {
adsr.release();
}
void process() {
osc >> filter(env, 10) >> out;
out *= 0.25 * adsr;
if (adsr.finished())
stop();
}
};
Filter() {
notes.add<FilterNote>(32);
}
};
Envelope object.
Definition klang.h:5041
Abstract filter class.
Definition klang.h:7456
void reset()
Reset filter state.
Definition klang.h:7470

Modular routing

Subtractive/Modular.k

#include <klang.h>
using namespace klang::optimised;
struct Distortion : Modifier {
param gain = 1;
void set(param drive){
gain = drive;
}
void process() {
if(gain > 1){
in *= gain;
if(in > 1)
in = 1;
else if(in < -1)
in = -1;
}
in >> out;
}
};
signal invert(signal x){ return 1 - x; }
signal nonlinear(signal x){ return pow(10, 2 * (x - 1)); }
signal squared(signal x){ return x * x; }
signal bipolar(signal x){ return pow(10, x); }
struct OSC : Generator {
Saw osc1;
Saw osc2;
Square osc3;
param gain[3] = { 0 };
void set(param f, param detune, param gain1, param gain2, param gain3){
osc1(f * (1 - detune), 0);
osc2(f, 0);
osc3(f * (1 + detune), 0);
gain[0] = squared(gain1) / 3;
gain[1] = squared(gain2) / 3;
gain[2] = squared(gain3) / 3;
}
void process() {
osc1 * gain[0] + osc2 * gain[1] + osc3 * gain[2] >> out;
}
};
struct FLT : Modifier {
LPF lpf;
HPF hpf;
BPF bpf;
int filter = 0;
param f = 0, Q = 1;
Distortion drive;
void set(param type){
filter = (int)type;
lpf.reset();
hpf.reset();
bpf.reset();
}
void set(param frequency, param resonance){
f = frequency;
Q = resonance;
}
void process() {
switch(filter){
case 1:
in >> hpf(f,Q) >> drive >> out; break;
case 2:
in >> bpf(f,Q) >> drive >> out; break;
default:
in >> lpf(f,Q) >> drive >> out; break;
}
}
};
struct AMP : Modifier {
ADSR env;
param gain = 1;
void process() {
in * env * gain >> out;
}
};
struct MOD : Generator {
ADSR env;
param amount;
void release() {
if(env.R > 0.01)
env.release();
}
void process() {
// bipolar(env * amount) >> out;
if(amount <= 0)
nonlinear(invert(env * -amount)) >> out;
else
(1 + nonlinear(env * amount) * 9) >> out;
}
};
struct LFO : Generator {
Sine osc;
param amount;
param depth;
void set(param f, param depth){
osc.set(f);
LFO::depth = depth;
}
void set(param f, param depth, param phase){
set(f, depth);
LFO::depth = depth;
}
void process() {
// signal lfo = osc * depth + depth;
// bipolar(lfo * amount) >> out;
signal lfo = pow(2, (signal)osc) * 0.5;
if(amount <= 0)
nonlinear(invert(lfo * -amount * depth)) >> out;
else
(1 + nonlinear(lfo * amount) * depth * 2) >> out;
}
};
struct Modular : Synth {
struct ModularNote : public Note {
OSC osc;
FLT flt;
AMP amp;
MOD mod;
LFO lfo;
event on(Pitch pitch, Amplitude velocity) {
const param detune = controls[3] * 0.01f;
osc.set(pitch -> Frequency, detune, controls[0], controls[1], controls[2]);
amp.env(controls[4], controls[5], controls[6], controls[7]);
mod.env(controls[8], controls[9], controls[10], controls[11]);
flt.set(controls[12]);
flt.drive(controls[15]);
lfo.amount = controls[16];
mod.amount = controls[17];
lfo.set(controls[18], controls[19], 0);
}
event off(Amplitude velocity) {
amp.env.release();
mod.release();
}
void process() {
signal f = min(20000, mod * lfo * controls[13]);
signal Q = controls[14];
osc >> flt(f,Q) >> amp >> out;
if (amp.env.finished())
stop();
}
};
// Initialise plugin (called once at startup)
Modular() {
controls = {
{ "OSC", Slider("1", 0, 1, 1, { 28, 52, 40, 60 } ),
Slider("2", 0, 1, 1, { 73, 52, 40, 60 } ),
Slider("3", 0, 1, 1, { 118, 52, 40, 60 } ),
Dial("Detune", 0, 1, 0, { 163, 52, 40, 60 } ), },
{ "AMP", Slider("A", 0, 1, 0.5, { 230, 52, 10, 60 } ),
Slider("D", 0, 1, 0.5, { 245, 52, 10, 60 } ),
Slider("S", 0, 1, 1.0, { 260, 52, 10, 60 } ),
Slider("R", 0, 1, 0.5, { 275, 52, 10, 60 } ), },
{ "MOD", Slider("A", 0, 1, 0.5, { 312, 52, 10, 60 } ),
Slider("D", 0, 1, 0.5, { 327, 52, 10, 60 } ),
Slider("S", 0, 1, 1.0, { 342, 52, 10, 60 } ),
Slider("R", 0, 1, 0.5, { 357, 52, 10, 60 } ), },
{ "Filter", Menu("", { 178, 208, 71, 20 }, "Low-pass", "High-pass", "Band-pass" ),
Dial("f", 100, 10000, 10000, { 28, 168, 40, 60 } ),
Dial("Q", 0.1, 10, 1, { 78, 168, 40, 60 } ),
Dial("Drive", 1, 24, 1, { 128, 168, 40, 60 } ),
Dial("LFO", -1, 1, 0, { 185, 173, 25, 30 } ),
Dial("MOD", -1, 1, 0, { 220, 173, 25, 30 } )
},
{ "LFO", { 269, 131 },
Dial("Rate", .1, 10, 1, { 7, 0, 40, 60 } ),
Dial("Depth", 0, 1, 0, { 58, 0, 40, 60 } ),
},
};
presets = {
// { "Saint-Saens en Seine", { 1.000, 1.000, 1.000, 0.895, 0.491, 0.707, 0.984, 0.730, 0.349, 0.860, 0.790, 0.350, 0, 2617.376, 1.240, 1.000, 0.370, 0.280, 4.466, 0.650 } },
{ "Saint-Saens en Seine", { 1.000, 1.000, 1.000, 0.895, 0.491, 0.707, 0.984, 0.730, 0.349, 0.860, 0.790, 0.350, 0, 3313.470, 1.240, 1.000, 0.370, 0.350, 4.118, 0.125 } },
{ "Popcorn une fois", { 0.315, 1.000, 1.000, 0.720, 0.000, 0.465, 0.160, 0.510, 0.171, 0.000, 1.000, 0.000, 0, 5316.583, 3.088, 1.000, -0.400, -0.720, 4.966, 0.350 } },
};
notes.add<ModularNote>(32);
}
};
param R
Definition klang.h:5433
Signal generator object (mono)
Definition klang.h:3680
void set(param frequency) override
Definition klang.h:6490
Signal modifier object (mono input-output)
Definition klang.h:3682

Breakpoint envelope

Subtractive/Breakpoint.k

#include <klang.h>
using namespace klang::optimised;
struct Breakpoint : Synth {
struct BreakpointNote : public Note {
Sine osc;
Envelope env;
event on(Pitch pitch, Amplitude velocity) {
osc(pitch -> Frequency, 0);
param attack = controls[0];
param decay = controls[0];
env = { { 0, 0 }, { attack, 1 }, { attack + decay, 0 } };
}
void process() {
osc * env++ >> out;
}
};
Breakpoint() {
controls = {
Dial("Attack", 0.05, 1.0, 0.05),
Dial("Decay", 0.1, 1.0, 0.1)
};
notes.add<BreakpointNote>(32);
}
};

Ramp

Subtractive/Ramp.k

#include <klang.h>
using namespace klang::optimised;
struct Ramp : Synth {
struct RampNote : public Note {
Sine osc;
Envelope env;
event on(Pitch pitch, Amplitude velocity) {
osc(pitch -> Frequency, 0);
param time = controls[0];
env = { { 0, 1 }, { time, 0 } };
}
void process() {
osc * env++ >> out;
}
};
Ramp() {
controls = {
Dial("Time", 0.1, 1.0, 0.1)
};
notes.add<RampNote>(32);
}
};

Release

Subtractive/Release.k

#include <klang.h>
using namespace klang::optimised;
struct Release : Synth {
struct ReleaseNote : public Note {
Sine osc;
Envelope env;
event on(Pitch pitch, Amplitude velocity) {
osc(pitch -> Frequency, 0);
param A = controls[0];
param D = controls[1];
param S = controls[2];
env = { { 0, 0 }, { A, 1 }, { A + D, S } };
env.setLoop(2, 2);
}
event off(Amplitude velocity) {
param R = controls[3];
env.release(R);
}
void process() {
osc * env++ >> out;
if(env.finished())
stop();
}
};
Release() {
controls = {
Dial("Attack", 0, 1, 0.002),
Dial("Decay", 0, 1, 0.1),
Dial("Sustain",0, 1, 0.05),
Dial("Release",0, 1, 1.0),
};
notes.add<ReleaseNote>(32);
}
};
virtual void release(float time, float level=0.f)
Definition klang.h:5279
void setLoop(int startPoint, int endPoint)
Definition klang.h:5241

Expression

Subtractive/Expression.k

#include <klang.h>
using namespace klang::optimised;
struct Expression : Synth {
struct ExpressionNote : public Note {
Saw osc[3];
ADSR adsr;
Envelope env;
LPF filter;
param gain;
Sine lfo;
Envelope vibrato;
param f0;
Envelope rate;
event on(Pitch pitch, Amplitude velocity) {
f0 = pitch -> Frequency;
lfo.reset();
rate = { { 0, 0 }, { 0.5, 0 }, { 1.f, 5 }, { 2.f, 3 } };
adsr(random(0.2,0.3), random(0.2,0.3), random(0.3,0.4), random(1.5f, 2.f));
env = { { 0, max(1000,f0 * 1.5) }, { 0.5, f0 * 6 }, { 2.0, max(1000,f0 * 1.5) } };
vibrato = { { 0, 0.005f }, { 0.25f, 0.001f }, { 0.5f, -0.01f }, { 1.f, 0.005f } };
filter.reset();
}
event off(Amplitude velocity) {
adsr.release();
}
void process() {
lfo.set(rate++);
osc[0](f0 * (1.0000f + lfo * vibrato++));
osc[1](f0 * (0.9997f + lfo.out * -vibrato.out));
osc[2](f0 * (1.0004f + lfo.out * vibrato.out * 2));
signal oscs = (osc[0] - osc[1] + osc[2]) * 0.35f;
filter.set(env++, .25f);
(oscs * 0.075f) + (oscs >> filter) >> out;
out *= adsr++;
if (adsr.finished())
stop();
}
};
Expression() {
controls = {
// UI controls and parameters
};
notes.add<ExpressionNote>(32);
}
};
void reset() override
Definition klang.h:6484

Modulation

Compare amplitude modulation, frequency modulation, and operator routing.

Amplitude modulation

Modulation/AM.k

#include <klang.h>
using namespace klang::optimised;
struct AM : Synth {
struct AMNote : public Note {
Sine carrier;
Sine modulator;
ADSR adsr;
event on(Pitch pitch, Amplitude velocity) {
carrier(pitch -> Frequency, 0);
adsr(0, .1, 0.1, 0.25);
}
event off(Amplitude velocity) {
adsr.release();
}
void process() {
param mod_rate = controls[0] * carrier.frequency;
param mod_depth = controls[1];
signal mod = modulator(mod_rate) * mod_depth + (1 - mod_depth);
carrier * mod * adsr >> out;
if (adsr.finished())
stop();
}
};
// Initialise plugin (called once at startup)
AM() {
controls = {
Dial("Rate", 0.01, 3, 0.5),
Dial("Depth", 0, 1, 0.5),
};
notes.add<AMNote>(32);
}
};

Frequency modulation

Modulation/FM.k

#include <klang.h>
using namespace klang::optimised;
struct FM : Synth {
struct FMNote : public Note {
Sine carrier;
Sine modulator;
ADSR adsr;
param f0;
event on(Pitch pitch, Amplitude velocity) {
f0 = pitch -> Frequency;
carrier.reset();
modulator.reset();
adsr(0.001, 0, 1, 0.25);
}
event off(Amplitude velocity) {
adsr.release();
}
void process() {
param mod_rate = controls[0] * f0;
param mod_depth = controls[1] * f0;
signal mod = modulator(mod_rate) * mod_depth;
carrier(f0 + mod) * adsr >> out;
if (adsr.finished())
stop();
}
};
// Initialise plugin (called once at startup)
FM() {
controls = {
Dial("Rate", 0.01, 3, 0.5),
Dial("Depth", 0, 10, 0.5),
};
notes.add<FMNote>(32);
}
};

Second FM study

Modulation/FM2.k

#include <klang.h>
using namespace klang::optimised;
struct FM2 : Synth {
struct FM2Note : public Note {
Sine carrier;
Sine modulator1, modulator2;
ADSR adsr;
param f0;
event on(Pitch pitch, Amplitude velocity) {
f0 = pitch -> Frequency;
carrier.reset();
modulator1.reset();
modulator2.reset();
adsr(0.5, 0, 1, 0.25);
}
event off(Amplitude velocity) {
adsr.release();
}
void process() {
param mod_rate = controls[0] * f0;
param mod_depth1 = controls[1] * f0;
param mod_depth2 = controls[2] * f0;
signal mod1 = modulator1(mod_rate) * mod_depth1;
signal mod2 = modulator2(f0 + mod1) * mod_depth2;
carrier(f0 + mod2) * adsr >> out;
if (adsr.finished())
stop();
}
};
// Initialise plugin (called once at startup)
FM2() {
controls = {
Dial("Rate", 0.01, 3, 0.5),
Dial("Depth 1", 0, 10, 0.5),
Dial("Depth 2", 0, 10, 0.5),
};
presets = {
{ "Violin", { 3.000, 10.000, 6.791 } },
{ "Cello", { 1.490, 7.076, 1.523 } },
{ "Oboe", { 3.000, 0.755, 10.000 } },
{ "Harmonica", { 2.500, 4.900, 8.443 } },
};
notes.add<FM2Note>(32);
}
};

Operator routing

Modulation/Operators.k

#include <klang.h>
using namespace klang::optimised;
struct Operators : Synth {
struct OperatorsNote : public Note {
Operator<Sine> op1, op2, op3;
ADSR adsr;
param f0;
HPF dcfilter;
event on(Pitch pitch, Amplitude velocity) {
f0 = pitch -> Frequency;
op1.set(f0 * controls[0], 0);
op1 = { { 0,1 }, { 1, 0.5 }, { 5, 0.125 } };
op2.set(f0 * controls[2], 0);
op2 = { { 0,1 }, { 2, 0.5 }, { 5, 0.25 } };
op3.set(controls[4], 0);
op3 = { { 0,1 }, { 3, 1 }, { 5, 0.5 } };
dcfilter.set(50);
adsr(0.0, 0.05, 0.25, 0.5);
}
event off(Amplitude velocity) {
adsr.release();
}
void process() {
((op1 * controls[1]) >> (op2 * controls[3]) >> op3) * adsr >> dcfilter >> out;
//op2 >> debug;
if (adsr.finished())
stop();
}
};
// Initialise plugin (called once at startup)
Operators() {
controls = {
{ "Op1", Dial("f", 0, 2), Dial("Amt", 0, 10) },
{ "Op2", Dial("f", 0, 2), Dial("Amt", 0 ,10) },
{ "Op3", Dial("f", 0, 2), },
};
presets = {
{ "Bass", { 1.000, 0.500, 1.000, 4.296, 2 } },
};
notes.add<OperatorsNote>(32);
}
};
FM operator.
Definition klang.h:5465

Physical modelling

Excite and sustain models of resonating strings and other structures.

Plucked string

Physical/String.k

#include <klang.h>
using namespace klang::optimised;
struct String : Synth {
struct StringNote : public Note {
Envelope env;
Noise noise;
Delay<19200> ff_delay;
LPF ff_filter;
Delay<19200> delay;
LPF filter;
APF apf;
ADSR adsr;
DCF dc;
event on(Pitch pitch, Amplitude velocity) {
const param f = pitch -> Frequency;
const param fc = controls[1] * fs.nyquist * pitch / 96;
filter.set(fc, 0.5);
apf.set(fc, 0.99);
ff_filter.set(controls[3] * fs.nyquist);
const param t = fs.f / f - filter.delay(f) - apf.delay(f);
delay.set(t);
ff_delay.set(t * controls[2] * controls[2]);
env = { { 0.000, 0.0 }, { 0.001, 1.0 }, { 0.003, -1 }, { 0.004, 0.0 } };
adsr(0, 0, 1, 2.0);
}
event off(Amplitude velocity) {
adsr.release();
}
void process() {
signal excitation = env * (controls[4] * noise + 1 - controls[4])>> ff_filter;
excitation -= (excitation >> ff_delay);
signal feedback = (delay.lagrange(delay.time) >> filter >> apf) * controls[0];
(excitation + feedback) * adsr >> out;
delay << out;
if (adsr.finished())
stop();
out >> dc >> out;
}
};
// Initialise plugin (called once at startup)
String() {
controls = {
Dial("Resonance", 0.9, 0.999, 0.997),
Dial("Dampening", 0, 1, 0.9),
Dial("Finger Position", 0, 1, 0.5),
Dial("Pluck Material", 0, 1, 0.9),
Dial("Noise")
};
notes.add<StringNote>(32);
}
};
float time
Definition klang.h:4604
virtual void set(param samples) override
Definition klang.h:4697
signal lagrange(float delay) const
Definition klang.h:4646
All-pass filter (APF)
Definition klang.h:7650
void set(param f)
Set the filter cutoff (default r)
Definition klang.h:7654
float delay(float f) const
Return the group delay for the specified frequency (in samples)
Definition klang.h:7538
Simple DC filter / blocker.
Definition klang.h:7292

Bowed string

Physical/Bowed.k

#include <klang.h>
using namespace klang::optimised;
struct Bowed : Synth {
struct BowedNote : public Note {
struct Bow : public Generator {
Noise noise;
BPF filter;
Envelope shape;
param velocity;
void start(param f){
filter.set(f, 1);
shape = { { 0,0.25 }, { 0.125, 1 }, { 0.5, 0.5 }, { 1.5, 1 } };
shape.setLoop(1,3);
}
void stop() {
shape.release(0.01);
}
signal bow(signal x){
if(x > 0.95)
return x - 0.6;
else if(x < -0.95)
return x + 0.6;
else
return 0;
}
void process(){
bow(noise) * shape >> filter >> out;
}
} bow;
struct Resonator : Modifier {
Delay<19200> delay;
IIR<1> filter;
param gain;
void set(param frequency, param resonance, param dampening){
const param time = fs.f / frequency - 1;
delay.clear();
delay.set(time);
filter.reset();
filter.set(dampening);
gain = resonance;
}
void process() {
signal feedback = (delay >> filter) * gain;
(in + feedback) >> out;
delay << out;
}
} resonator;
ADSR adsr;
event on(Pitch pitch, Amplitude velocity) {
const param f = pitch -> Frequency;
bow.start(f);
resonator.set(f, controls[0], controls[1]);
adsr(0, 0, 1, 0.25);
}
event off(Amplitude velocity) {
bow.stop();
adsr.release();
}
void process() {
bow >> resonator >> out;
out *= adsr;
if (adsr.finished())
stop();
}
};
// Initialise plugin (called once at startup)
Bowed() {
controls = {
{ "Resonance",
Dial("Feedback", 0.9, 1, 1.000),
Dial("Material", 0, 1, 0.95),
},
};
notes.add<BowedNote>(32);
}
};
void clear()
Definition klang.h:4609

Waveguide

Physical/Waveguide.k

#include <klang.h>
using namespace klang::optimised;
signal hardclip(signal in){
if(in > 1)
return 1;
else if(in < -1)
return -1;
return in;
}
struct Waveguide : Synth {
struct WaveguideNote : public Note {
struct Exciter : Generator {
Noise noise;
Envelope impulse;
Delay<19200> delay;
LPF filter;
void set(param frequency, param position, param material) {
const param time = fs.f / frequency - 2;
delay.set(position * time);
delay.clear();
impulse = { { 0.000, 0.0 }, { 0.001, 1.0 }, { 0.003, -1.0 }, { 0.004, 0.0 } };
filter.reset();
filter.set(material * 20000);
}
void process() {
signal excitation = impulse;
excitation - (excitation >> delay) >> filter >> out;
}
} exciter;
struct Resonator : Modifier {
Delay<19200> delay;
IIR<1> filter;
param gain;
void set(param frequency, param resonance, param dampening){
const param time = fs.f / frequency - 2;
delay.set(time);
delay.clear();
filter.reset();
filter.set(dampening);
gain = resonance;
}
void process() {
signal feedback = (delay) * gain;
(in + feedback) >> out;
delay << hardclip(out);
}
} resonator;
ADSR adsr;
event on(Pitch pitch, Amplitude velocity) {
const param f = pitch -> Frequency;
exciter.set(f, controls[0], controls[1]);
resonator.set(f, controls[2], controls[3]);
adsr(0, 0, 1, .25);
}
event off(Amplitude velocity) {
adsr.release();
}
void process() {
exciter >> resonator >> out;
out *= adsr;
if (adsr.finished())
stop();
}
};
// Initialise plugin (called once at startup)
Waveguide() {
controls = {
{ "Pluck",
Dial("Position", 0, 1, 0.25),
Dial("Material", 0, 1, 0.5),
},
{ "String",
Dial("Resonance", 0.9, 1, 0.999),
Dial("Material", 0, 1, 0.9),
},
};
notes.add<WaveguideNote>(32);
}
};