Convert MATLAB Code to an Audio Plugin
Audio Toolbox™ supports several approaches for the development of audio processing algorithms. Two common approaches include procedural programming using MATLAB® scripts and object-oriented programming using MATLAB classes. The audio plugin class is the suggested paradigm for developing your audio processing algorithm in Audio Toolbox. See Audio Plugins in MATLAB for a tutorial on the structure, benefits, and uses of audio plugins.
This tutorial presents an existing algorithm developed as a MATLAB script, and then walks through the steps to convert the script to an audio plugin class. Use this tutorial to understand the relationship between procedural programming and object-oriented programming. You can also use this tutorial as a template to convert any audio processing you developed as MATLAB scripts to the audio plugin paradigm.
Inspect Existing MATLAB Script
The MATLAB script has these sections:
Variable Initialization. Variables are initialized with known values, including the number of samples per frame (
frameSize
) for frame-based stream processing.Object Construction.
Two
audioOscillator
System objects –– Construct to create time-varying gain control signals.dsp.AudioFileReader
System object –– Construct to read an audio signal from a file.audioDeviceWriter
System object –– Construct to write an audio signal to your default audio device.
Audio Stream Loop. Mixes stereo channels into a mono signal. The mono signal is used to create a new stereo signal. Each channel of the new stereo signal oscillates in applied gain between 0 and 2, with a respective 90-degree phase shift.
Convert MATLAB Script to Plugin Class
This tutorial converts a MATLAB script to an audio plugin class in six steps. You begin by creating a skeleton of a basic audio plugin class, and then map sections of the MATLAB script to the audio plugin class.
For an overview of how a MATLAB script is converted to a plugin class, inspect the script to plugin visual mapping. To perform this conversion, walk through the example for explanations and step-by-step instructions.
1. Create Skeleton of Audio Plugin Class
Begin with the basic skeleton of an audio plugin class. This skeleton is not the minimum required, but a common minimum to create an interesting audio plugin. See Audio Plugins in MATLAB for the minimum requirements to create a basic audio plugin.
2. Map Script Variable Initialization to Plugin Properties
Properties allow a plugin to store information across sections of the plugin class definition. If a property has access set to private, the property is not accessible to the end user of a plugin. Variable initialization in a script maps to plugin properties.
A valid plugin must allow input to the
process
method to have a variable frame size. Frame size is determined for each input frame in theprocess
method of the plugin. Because frame size is used only in theprocess
method, you do not declare it in the properties section.A valid audio plugin must allow input to the
process
method to have a variable sample rate. Thereset
method of a plugin is called when the environment changes the sample rate. Determine the sample rate in thereset
method using thegetSampleRate
method inherited from theaudioPlugin
base class.The objects used by a plugin must be declared as properties to be used in multiple sections of the plugin. However, the constructor method of a plugin performs object construction.
3. Map Script Object Construction to Plugin Constructor Method
Add a constructor method to the methods section of your audio plugin. The constructor method of a plugin has the form:
function plugin = myPluginClassName % Instructions to construct plugin object. end
In this example, you construct the Sine
and
Cosine
objects in the constructor method of the
plugin.
4. Add Reset Method
The reset
method of a plugin is called every time a new
session is started with the plugin, or when the environment changes sample rate.
Use the reset
method to update the
SampleRate
property of your Sine
and
Cosine
objects. To query the sample rate, use the
getSampleRate
base class method.
5. Map Script Audio Stream Loop to Plugin Process Method
The contents of the audio stream loop in a script maps to the
process
method of an audio plugin, with these differences:
A valid audio plugin must accept a variable frame size, so frame size is calculated for every call to the
process
method. Because frame size is variable, any processing that relies on frame size must update when input frame size changes.The environment handles the input and output to the
process
method.
6. Add Plugin Interface
The plugin interface lets users view the plugin and tune its properties.
Specify PluginInterface
as an audioPluginInterface
object that
contains an audioPluginParameter
object. The
first argument of audioPluginParameter
is the property you
want to synchronize with a tunable parameter. Choose the display name, label the
units, and set the parameter range. This example uses 0.1 to 10 as a reasonable
range for the Frequency
property. Write code so that during
each call to the process
method, your Sine
and Cosine
objects are updated with the current frequency
value.
Once your audio plugin class definition is complete:
Save your plugin class definition file.
Validate your plugin using
validateAudioPlugin
.Prototype it using Audio Test Bench.
Generate is using
generateAudioPlugin
.