Saturday 11 May 2013

The getNumParameters Function

So, we have now decided what our plug-in needs to tell the host and have made a new project in The Introjucer. Let's edit some code shall we? It would be very helpful at this point if you did a little background reading on C++. I will try to give you a basic understanding of what the bits of code we edit mean. If you want to understand the structure of the plug-in properly however, you will need to learn quite a bit about C++.

If you have come here straight after the previous post you should still have your project open in The Introjucer. You should also still have the 'Files' tab selected. Open up 'PluginProcessor.cpp' by clicking on it in list on the left. You should now have a text editor on the right with the source code in it (you should be able to see that The Introjucer has already generated a lot of code for us).

Scroll down until you see a bit of code that looks like this (it's at line 30 currently but this may change as the JUCE library is constantly updated):
int GainPluginAudioProcessor::getNumParameters()
{
    return 0;
}
Your code may not be the same as this depending on what you called your Introjucer project. The name of your project will be prefixed to the 'AudioProcessor::' part. Hency why mine is 'GainPluginAudioProcessor::'.

What we have here is a function. This is a bit of code that an application can run to do a certain job. The syntax may be a little baffling at first but once you get used to C++ it won't phase you.

The function we see above is called 'getNumParameters'. As you may be able to guess, when this function is run it will tell the host how many parameters our plug-in has.

The 'GainPluginAudioProcessor::' part before this name tells us that this function belongs to 'GainPluginAudioProcessor'. In a manner of speaking this is our plug-in, so we have a function called 'getNumParameters' which belongs to our plug-in.

The 'int' part before that tells us that the function will output an integer number. Declaring what type of data a function returns is a fundamental part of C/C++ programming and something you will have to get used to.

So that first line tells the host that it can ask our Plug-In ('GainPluginAudioProcessor') to run its 'getNumParameters' function and it should expect an integer value in return.

After that line we have a block of code enclosed in curly brackets. This is the code that will be executed when we run (call) the function. For this function all we need to do is return the number of parameters for our plug-in. For this we only need one line of code. We use the 'return' keyword to define the output of our function. The code above has 'return 0;', we need to change this to 'return 1;'.  The whole bit of code should look like this:
int GainPluginAudioProcessor::getNumParameters()
{
    return 1;
}
The semicolon marks the end of the statement. Essentially semicolons break the code up into separate steps. Again this is something which is fundamental to C/C++ programming and you will need to get used to.

Now when the host asks our plug-in to call its 'getNumParameters' function, it will get the answer 1 in return. That's exactly what we wanted. Save these changes to your project and then we can move on.

Next we will look at how to tell the host the name of that parameter.

Have a nice day now!
Sean


No comments:

Post a Comment