Main Content

ConvertMATLABCode 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. SeeAudio Plugins in MATLABfor 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 ExistingMATLABScript

The MATLAB script has these sections:

  1. Variable Initialization.Variables are initialized with known values, including the number of samples per frame (frameSize) for frame-based stream processing.

  2. Object Construction.

    • TwoaudioOscillatorSystem objects –– Construct to create time-varying gain control signals.

    • dsp。AudioFileReader系统对象——构建阅读一个音频信号from a file.

    • audioDeviceWriterSystem object –– Construct to write an audio signal to your default audio device.

  3. 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.

View Code

ConvertMATLABScript 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. SeeAudio Plugins in MATLABfor the minimum requirements to create a basic audio plugin.

View Code

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 theprocessmethod to have a variable frame size. Frame size is determined for each input frame in theprocessmethod of the plugin. Because frame size is used only in theprocessmethod, you do not declare it in the properties section.

  • A valid audio plugin must allow input to theprocessmethod to have a variable sample rate. Theresetmethod of a plugin is called when the environment changes the sample rate. Determine the sample rate in theresetmethod using thegetSampleRatemethod inherited from theaudioPluginbase 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.

View Code

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:

functionplugin = myPluginClassName% Instructions to construct plugin object.end
If your plugin uses objects, construct them when the plugin is constructed. Set nontunable properties of objects used by your plugin during construction.

In this example, you construct theSineandCosineobjects in the constructor method of the plugin.

View Code

4. Add Reset Method

Theresetmethod of a plugin is called every time a new session is started with the plugin, or when the environment changes sample rate. Use theresetmethod to update theSampleRateproperty of yourSineandCosineobjects. To query the sample rate, use thegetSampleRatebase class method.

View Code

5. Map Script Audio Stream Loop to Plugin Process Method

The contents of the audio stream loop in a script maps to theprocessmethod 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 theprocessmethod. 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 theprocessmethod.

View Code

6. Add Plugin Interface

The plugin interface lets users view the plugin and tune its properties. SpecifyPluginInterfaceas anaudioPluginInterfaceobject that contains anaudioPluginParameterobject. The first argument ofaudioPluginParameteris 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 theFrequency财产。编写代码以便在每次调用theprocessmethod, yourSineandCosineobjects are updated with the current frequency value.

View Code

Once your audio plugin class definition is complete:

  1. Save your plugin class definition file.

  2. Validate your plugin usingvalidateAudioPlugin.

  3. Prototype it usingAudio Test Bench.

  4. Generate is usinggenerateAudioPlugin.

Related Topics