Audio Ordeal

Music Production, Podcast, and DJ Tutorials

How to build a VST – Lesson 2: Autopanner

4 min read

In the last tutorial we covered the basics of setting up a VST plugin using the JUCE library. In this tutorial we will be making an autopanner plugin, this doesn’t take too long to set up and there is a lot of room to modify the final code and get some interesting results!

To start we are going to create a stereo panner to control manually. To start, load up or download the final code from tutorial 1, which can be downloaded here: (https://github.com/aRycroft/JuceTutorial1)

Stereo Panner

To pan audio we need to attenuate the audio from one channel while boosting the other. To control the plugin we are going to modify the gain slider we created in tutorial 1.

At the top of your PluginProcessor.cpp file you will see the addParameter line of code we added last time. Change the default value of the slider to 0.5 by changing the final float value to 0.5f. 

addParameter(gain = new AudioParameterFloat(“gain”, “Gain”, 0.0f, 1.0f, 0.5f));

Now scroll down to the process block, in PluginProcessor.cpp, it should look like this.

Process Block before Changes

Currently the gSlider variable is used to multiply both audio channels by the same value. To pan audio one of the channels needs to be multiplied by the inverse of the other, e.g

inputL = inputL * 0.3;

inputR = inputR * 0.7;

 

inputL = inputL * 1;

inputR = inputR * 0;

With the gSlider variable we can multiply the left channel by 1 – gSlider, and the other just with gSlider.

Process Block after Panning Changes

Replace this

//inputL = inputL * gSlider;
//inputR = inputR * gSlider;

With this

inputL = inputL * (1 – gSlider);
inputR = inputR * gSlider;

Now build the plugin and move the VST file to your DAWs VST folder. When changing the gain slider, you should see the audio pan left or right. This isn’t the best possible panning algorithm. When panned to the centre you will notice a dip in volume of -3dB. There are different panning algorithms you can use to fix this problem we will use one of these later in the tutorial.

AutoPan

Now we can pan audio left and right, we need to find a way to modulate the gSlider value over time. To achieve this we are going to find the value of a Sin wave at different points, then multiply these values with the channel data.  To start, we will modulate the value between 0-1 every second.

To modulate the levels the correct amount we need to do a bit of maths.

Every cycle of Sin = 2 * pi radians.

The sample rate of the DAW means that a certain number of samples happen every second, therefore

2pi radians / sample rate = Radians per Sample

For every sample, we can find Sin (Radians per Sample) then add another ‘Radians per Sample’ length to find a value for the next sample. These values can be multiplied to each channel of audio instead of the gSlider variable.

Sample Rate and Radians per Sample

const int numberSamples = getSampleRate();

const float radsPerSample = (2 * double_Pi) / numberSamples;

These lines of code find the sample rate and then how many radians are required for each sample length. We can use const for these values as they won’t change after each iteration.  

Next, we need to add a new float variable to hold the value of the radians for a given sample. We don’t want to initialise this variable every buffer length so we will add it to the header file. Go to PluginProcessor.h and add this line in the private section.

float nextRad = 0;

Now add the following code to your ProcessBlock function in PluginProcessor.cpp.

float sinValue = 0.5 * std::sin(nextRad) + 0.5; //1

inputL = inputL * (1 – sinValue); //2
inputR = inputR * sinValue;

nextRad += radsPerSample; //3

1.Sets sinValue as the value of Sin at the value of nextRad, Sin is multiplied by 0.5 and 0.5 is added to find a value between 0-1.

2. Replaces gSlider with sinValue.

3. Adds radsPerSample to nextRad.

Build the plugin, and you will hear the audio pan between left and right channel every second.

SIDENOTE—It’s not possible to build a plugin while the last version is still active in your DAW. Remove the VST from any channels before building your code.

Currently, the nextRad variable will get bigger forever while the plugin is active, to solve this we can add an if statement to the bottom of the process block.

Adding if statement to process block

if (nextRad > numberSamples)
{
nextRad = 0;
}

This resets the nexRad variable after it gets larger than the numerSamples variable.

We are going to add a control to change how often the audio pans between each ear.

Add a new audio parameter float to your code. I called mine mS and set the range from between 10 – 5000. If you have trouble with this check back at Tutorial 1.

Then add this code to the process block.

Adding milliseconds control

float mSeconds = mS->get() / 1000;

//Replace this
//const int numberSamples = getSampleRate(); 

//With this
int numberSamples = getSampleRate() * mSeconds; 

This finds the value of the mS slider and divides it by 1000, to find the value in seconds, then multiplies this to the sample rate. Make sure to remove the const from the numberSamples variable.

Build the plugin, and you can use the mS slider to change the rate of the autopanner.

As mentioned before there is a better algorithm for panning audio. We can use a constant power panning algorithm by changing some code in the process block.

//Replace This

//float sinValue = 0.5 * std::sin(nextRad) + 0.5; //1

//inputL = inputL * (1 – sinValue); //2

//inputR = inputR * sinValue;

//With This

float sinValue = std::sin(nextRad) + 1;

sinValue = (sinValue * double_Pi) / 4;

inputL = inputL * cos(sinValue);

inputR = inputR * sin(sinValue);

After adding this code and building the plugin you should see a smoother transition between the left and right channels.

In this tutorial we developed a simple autopanner plugin. You can create some interesting sounds by messing around with the code at this point. If you make mS very small you should hear some amplitude modulation effects. You could also make the gain slider control another part of the code, as currently it doesn’t change anything.

The final code for this tutorial is available here: https://github.com/aRycroft/JuceTutorial2

4 thoughts on “How to build a VST – Lesson 2: Autopanner

  1. Thanks for such a great tutorial! I rally appreciate it!
    By the way, where are those code? “inputL = inputL * 0.3; inputR = inputR * 0.7; inputL = inputL * 1;
    inputR = inputR * 0;”
    I cannot find these in your screenshot.

  2. I’m so stupid, I copied the wrong code. . .
    sinValue = (sinValue * juce::MathConstants::pi) / 4;
    sinValue = (sinValue 2 juce::MathConstants::pi) / 4; -__-||

Leave a Reply

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