In this lesson, you will learn how code works and take your first steps in programming, gaining an introduction to C++ through the Klang language, and write code to apply a gain effect to a simulated audio signal, ahead of creating your first plugin.

LESSON PLAN

INSTRUCTIONS

Use the < > arrow(s) above to move through the topic. Each page explains a key concept using text and graphics, leading up to a practical task. You can return to any page at any time, using the arrows or drop-down menu, and also restart animations by simply clicking them.

Coding Basics

Block diagrams are the blueprints for audio processes that we can implement in code.

Processes are expressed in source code; a set of written instructions designed to
generate or manipulate digital data (numbers, text, files, images, audio, video, ...).

For best performance, the code is compiled to machine code the processor can run natively — critical for realtime audio processing, which requires millions of calculations per second.

C++ and Klang

C++ is the industry standard programming language for audio software development.

On this course, we will use Klang, an extension of C++ specifically designed for audio,
which adds syntax, semantics, and objects to support digital signal processing (DSP).

Klang makes C++ more accessible and expressive while retaining its performance and
compatibility, supporting plugins, audio apps, web audio, mobile and embedded systems.

Delay<44100> delay; LPF filter; param gain; void process() { signal feedback = (delay * gain) >> filter; (in + feedback) >> out; delay << out; }
Getting Help

Programming and C++ can seem daunting at first, so don't worry about making mistakes —
learning to spot and fix 'bugs' is a great way to learn the language, even if it's slow at first.

Key coding concepts will be explored in sessions, but learning to source information is a
key skill in software development, for which there are many resources you can draw on.

Coding Basics

The block diagram below can be expressed with the following C++ code:

out = in;

The = operator sets the variable out to the value of in, as in mathematics.

Coding Basics

For audio, Klang's << / >> operators allows us to express signal flow more literally:

in >> out;

This yields exactly the same result as the = operator (which can also be used),
but enables us to write C++ code that more closely mirrors block diagrams.

Exercise: Compiling
Learn to compile, run, break, and debug code by understanding the compiler.
in >> out;
Compiler Feedback

Individual instructions are called statements, and must end with a ; semi-colon.

in >> out;

Missing or misplaced semi-colons are a common mistake that prevent the compiler
from understanding your code — though it will often provide useful hints:

main.cpp:16:33: error: expected ';' at end of declaration param gain = controls[0] ^ ; 1 error generated.
Applying Gain

For our very first plugin, we will code a simple level control based on the gain effect below,
in which the input is multiplied by the 'gain' control (from the UI) and fed to the output.

Initially, we'll focus on just the audio process (in the browser), and learn to translate block
diagrams into code, before taking a look at the source for the full plugin (in Klang Studio).

New concepts will be explained, but you should also learn to use the Klang/C++ Reference card, which provides a complete overview of C++ and Klang, along with examples.

Applying Gain

To get the current value of a dial in the user interface (UI) we use the controls[ ] array.
An array is a collection of multiple values, accessed by a numbered index (starting at 0):

To make our code more readable, we'll place the value of the corresponding control (0)
in a variable of type param named "gain", which we declare and initialise as follows:

param gain = controls[0];

Applying Gain

To multiply the two values, we simply use C++'s mathematical operators.

a + b
a - b
add
subtract
a * b
a / b
multiply
divide

in * gain >> out;

Exercise: Gain Effect
Edit the code below to complete the gain effect, controlled from the UI.
param gain = controls[0];
in * 0.5 >> out;
Lesson Summary

In this lesson, we created our first audio process, writing code to manipulate an input signal and
apply a gain effect using multiplication, which can be controlled from the user interface (UI).

KEY CONCEPTS

  • Code expresses audio processes as sets of instructions (source code) to generate or manipulate data, executed by the computer. Languages like C++ compile the code to run natively on the CPU, for best performance.
  • The compiler builds code into an executable form, but can also be a rich source of information (warning, errors).
  • Klang is an extension of C++ designed for audio, designed to make it more accessible and expressive for digital signal processing (DSP), making it easier to develop portable, high-performance audio processes in C++.
  • C++ (and Klang) are designed for object-oriented programming (OOP), where related variables and functions are grouped into logical structues, allowing complex systems to be modularised and broken down into simpler parts.
  • Klang provides syntax, semantics, and objects for audio, representing signal flow with the << and >> operators.

NEXT LESSON

  • Creating a gain plugin