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.
audioOscillatorSystem objects –– Construct to create time-varying gain control signals.
dsp.AudioFileReaderSystem object –– Construct to read an audio signal from a file.
audioDeviceWriterSystem 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
processmethod to have a variable frame size. Frame size is determined for each input frame in the
processmethod of the plugin. Because frame size is used only in the
processmethod, you do not declare it in the properties section.
A valid audio plugin must allow input to the
processmethod to have a variable sample rate. The
resetmethod of a plugin is called when the environment changes the sample rate. Determine the sample rate in the
resetmethod using the
getSampleRatemethod inherited from the
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
Cosine objects in the constructor method of the
4. Add Reset Method
reset method of a plugin is called every time a new
session is started with the plugin, or when the environment changes sample rate.
reset method to update the
SampleRate property of your
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
processmethod. 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
6. Add Plugin Interface
The plugin interface lets users view the plugin and tune its properties.
PluginInterface as an
audioPluginInterface object that
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
Cosine objects are updated with the current frequency
Once your audio plugin class definition is complete:
Save your plugin class definition file.
Validate your plugin using
Prototype it using Audio Test Bench.
Generate is using