Saturday 11 May 2013

Parameter Values

The value of a parameter can be changed at any time by the host. Because of this we need to assign a place to store the current value. We can then refer to or update this stored information as and when we need. We can do this by declaring a variable in our code in which we will store the current value of our gain parameter.

To do this we will edit the 'PluginProcessor.h' file, so select that from the menu on the left. We only need to add one line of code here. Scroll down to the bottom of the file. Just above the line which says 'private:' add the following line of code:
float gain;
This declares a variable in which we can store the value for our gain parameter. The first part, 'float', says what data type is stored in the variable (a float in this case). The second part, 'gain', defines the identifier for the variable. Whenever we want to access the value stored in the variable we can use this identifier.

Save these changes to 'PluginProcessor.h' and return to 'PluginProcessor.cpp'.

At the moment we have only told the computer that we want to store a float and refer to it as 'gain'. We haven't actually stored anything in the variable yet. Before we store anything in it we need to know how the host thinks about parameter values.

The host assumes that any parameters our plug-in has take a value between 0 and 1. It will give the plug-in values in the range when it wants to set a parameter and will expect a value in this range when it asks for the value of a parameter. If we require values outside of this range it is our job to scale the values given by the host within out plug-in code. To keep things simple we will not bother with scaling the values. Our gain will have a range from 1 (unity gain) to 0 (muted).

The first thing we need to do in our code, is to set a default value for the gain parameter. If we did not do this and the host asked for the value of a parameter it will just get given some random value (or even worse crash). We can set this default value in the part of our code that looks like this:
GainPluginAudioProcessor::GainPluginAudioProcessor()
{
}
This is our plug-in's constructor function. Essentially this function is called when the plug-in is first loaded. We can use it to do any set up we need before the plugin is used. (Remember that yours might not be the same as this depending on the name of your Introjucer project).

We want to add some code inside the curly brackets, like so:
GainPluginAudioProcessor::GainPluginAudioProcessor()
{
    gain = 1.0f;
}
This will set the value store in the variable 'gain' to 1. Writing it as '1.0f' makes it so the value is already a float and wont need to be converted in order to be stored (if we wrote '1.0' it would be interpreted as a double), it is not entirely necessary. Now when our plug-in loads up the gain will automatically be set to 1.

Save these changes to your project and you are ready to move on.

Have a nice day now!
Sean

No comments:

Post a Comment