Audio Ordeal

Music Production, Podcast, and DJ Tutorials

How to build a VST – Lesson 3: Distortion

4 min read

The last tutorial covered coding an autopanner plugin using the generic UI. In this tutorial we’ll use a different UI that can be modified to use your own UI designs and code a basic distortion plugin.

Setting up the UI

By using the PluginEditor.h and .cpp files created by Projucer, we have more options when designing the plugin UI, than when using the Generic UI elements.

Create a new JUCE audio plugin project, then open up the PluginEditor.h file. There are a few more steps to setting up a UI using the PluginEditor files, but it will allow you to easily change the design of your UI elements. 

Load the PluginEditor.h file and add this line to the private section then, add the following lines to the PluginEditor.cpp file to make it visible in the plugin window.

Code to add a combobox UI element to the Plugin

addAndMakeVisible(&disChoice); // 1
disChoice.addItem(“Hard Clip”, 1);
disChoice.addItem(“Soft Clip”, 2);
disChoice.addItem(“Half-Wave Rect”, 3);
disChoice.setSelectedId(1);

disChoice.setBounds(50, 50, 200, 50); //2

This ComboBox will be used to change the type of distortion algorithm in the plugin. Build your code and you will see a ComboBox with three options. To make this box change anything, we need to add a listener. A listener will call a piece of code anytime this ComboBox is changed. 

Adding a listener to the combobox element

In the PluginEditor.h file add ComboBox::Listener in the private declarations section.

Adding a listener to the combobox element

Then in PluginEditor.cpp write this line to add a listener to the disChoice ComboBox.

Adding function that runs when the combobox is changed
Adding function that runs when the combobox is changed

Back in PluginEditor.h add this line,

void comboBoxChanged(ComboBox* comboBoxThatHasChanged) override;

then either let your IDE create a definition in PluginEditor.cpp or add it yourself. 

This function is called anytime a ComboBox is changed. 

Now in PluginProcessor.h declare a variable called menuChoice. 

int menuChoice;

By using the ComboBoxChanged void, we will change this variable from the UI objects we added to the Plugin.

Adding a distortion choice variable in the PluginProcessor

In PluginEditor.cpp add this line to the ComboBoxChanged void.

processor.menuChoice = comboBoxThatWasChanged->getSelectedId();

This code sets the variable we made in PluginProcessor.h to the value selected by the user in the UI.

Code to change the combobox using the comboboxchanged function

We are also going to add two sliders to the Plugin UI. All code in bold needs to be added. The only big difference when adding sliders is that we need to check which slider is being moved in the SliderValueChanged() function.

Be careful to copy these lines in the correct places, if you have trouble you can see the final plugin code at:

https://github.com/aRycroft/JuceTutorial3

PluginEditor.h:

class DistortionAudioProcessorEditor :
public
AudioProcessorEditor,
private
ComboBox::Listener,
Slider::Listener

private:
void comboBoxChanged(ComboBox* comboBoxThatHasChanged) override;
void sliderValueChanged(Slider* sliderThatHasChanged) override;
// This reference is provided as a quick way for your editor to
// access the processor object that created it.
DistortionAudioProcessor& processor;
ComboBox disChoice;
Slider Threshold;
Slider Mix;

PluginEditor.cpp:

{
// Make sure that before the constructor has finished, you’ve set the
// editor’s size to whatever you need it to be.
setSize (400, 300);

addAndMakeVisible(&disChoice);
disChoice.addItem(“Hard Clip”, 1);
disChoice.addItem(“Soft Clip”, 2);
disChoice.addItem(“Half-Wave Rect”, 3);
disChoice.setSelectedId(1);
disChoice.addListener(this);

addAndMakeVisible(&Threshold);
Threshold.setRange(0.0f, 1.0f, 0.001);
Threshold.addListener(this);

addAndMakeVisible(&Mix);
Mix.setRange(0.0f, 1.0f, 0.001);
Mix.addListener(this);
}

void DistortionAudioProcessorEditor::resized()
{
// This is generally where you’ll want to lay out the positions of any
// subcomponents in your editor..
disChoice.setBounds(50, 50, 200, 50);
Threshold.setBounds(50, 100, 200, 50);
Mix.setBounds(50, 150, 200, 50);
}

PluginProcesor.h:

int menuChoice;
float thresh = 0.0f;
float mix = 0.0f;

After adding the two slider elements to the UI, add this code to the sliderValueChanged function. This code checks which slider has been changed, then assigns a value to the variables in the processor files.

void DistortionAudioProcessorEditor::sliderValueChanged(Slider *slider)
{
if (&Mix == slider)
{
processor.mix = Mix.getValue();
}
if (&Threshold == slider)
{
processor.thresh = Threshold.getValue();
}
}

Coding the Process Block

After building your plugin you should see two sliders below the ComboBox. Now we can start building the distortion algorithms for this plugin.

I won’t go into detail on distortion algorithms here, but if you’re interested you can easily look up how these algorithms work. I would also encourage you to try and modify the ProcessBlock code to add more options or change how the signal is modified. 

Copy the following code into your process block. After building your plugin you should be able to change the type of distortion, and the threshold and mix values.

PluginProcessor.cpp -> ProcessBlock()

{
for (int channel = 0; channel < buffer.getNumChannels(); ++channel)
{
auto* channelData = buffer.getWritePointer(channel);

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

auto input = channelData[i];
auto cleanOut = channelData[i];

if (menuChoice == 1)
//Hard Clipping
{
if (input > thresh)
{
input = thresh;
}
else if (input < -thresh)
{
input = -thresh;
}
else
{
input = input;
}
}
if (menuChoice == 2)
//Soft Clipping Exp
{
if (input > thresh)
{
input = 1.0f – expf(-input);
}
else
{
input = -1.0f + expf(input);
}
}
if (menuChoice == 3)
//Half-Wave Rectifier
{
if (input > thresh)
{
input = input;
}
else
{
input = 0;
}
}
channelData[i] = ((1 – mix) * cleanOut) + (mix * input);
}
}
}

These are very simple distortion algorithms but can create some interesting results. Checking out some other JUCE projects is a great way to improve the distortion algorithms and find out what else is possible using JUCE. Any non-linear effects are easy to implement, such as saturation or exciter algorithms. 

Code for this tutorial can be found here: 

https://github.com/aRycroft/JuceTutorial3

I’ll continue to improve this plugin in the next tutorial, changing the design of the UI elements by coding the plugin look and feel.

1 thought on “How to build a VST – Lesson 3: Distortion

Leave a Reply

Copyright © Tom Jarvis 2020 All rights reserved. | Newsphere by AF themes.