In the previous tutorial we went over the basic methods that we will be using to create our plugin. Today I will show you how to do some audio processing, and explain to you how it works.

Before we do any processing, it’s time to go over a little bit of dsp theory.

DSP Basics

As you may already know, a sample is a single point in an audio wave. An audio wave is sampled at several thousand samples per second (for example 441.khz sample rate means 44,100samples/per second).  In audio, we use floating point numbers in the range of -1 to +1 to represent a sample.  The beauty of this is that when you multiply any numbers that are in this range, the result is always also in the range.  This is very useful, because if the range ever exceeds 1 or -1, then bad things will happen, and the plugin will most likely crash.

Overview

As I explained before, all of the audio processing will happen in the processBlock() method of the AudioProcessor.

Let’s break down the second for loop and see what is happening here.

for (int channel = 0; channel < getNumInputChannels(); ++channel)

The iterator channel is being used to traverse each input  channel connected to the plugin. The loop runs until channel is equal to the number of input channels. channel increments by 1 each time the loop runs.

float* channelData = buffer.getWritePointer (channel);

This statement creates a float pointer that points to the audio buffer for the current channel.  The buffer is an array of floats, so we need to make a way to traverse this array and process each sample stored in it. We will use a loop like this:

for(int sample = 0; sample < buffer.getNumSamples(); sample++){

channelData[sample] = //do the processing here

}

Saturating The Signal

With our new knowledge ready to go, lets start processing some audio! As I said earlier, our samples always need to stay in the range of -1 and 1, and saturation is just a distortion to the original signal. Basically what we need then is a function that for all x’s between -1 and 1, none of the y’s exceed -1 or 1. One family of functions that is perfect for this application are the root functions. For simplicity’s sake we will be using cubed root since we don’t have to worry about whether the input is negative or positive.

Lucky for us C++ has the cmath library that we can simply import, so at the top of PluginProcessor.h and the include for cmath like the picture below.

Now if we go back to the processBlock() method in PluginProcessor.cpp, we can assign the current sample to the cubed root of itself using cbrtf() function from the cmath library. Your code should look similar to the picture below.

Testing The Plugin

Now we need to build the plugin and test it to make sure everything is functioning right.

Under the Build tab, select Build Solution. Once the build process is finished, if you look at the output at the bottom of the screen, you should see the path where the .dll file was stored in.  Locate this file in you file browser and copy it to your vst plugins folder. Load it up with your host, and you should hear some distortion when you run audio through it.

That’s all for this tutorial! In the next tutoria, we will add a basic slider to control the amount of saturation!

[mc4wp_form id=”11930″]