Main Content

audioPlugin class

Base class for audio plugins

Description

audioPlugin is the base class for audio plugins. In your class definition file, you must subclass your object from this base class or from the audioPluginSource class, which inherits from audioPlugin. Subclassing enables you to inherit the attributes necessary to generate plugins and access Audio Toolbox™ functionality.

To inherit from the audioPlugin base class directly, type this syntax as the first line of your class definition file:

classdef myAudioPlugin < audioPlugin
myAudioPlugin is the name of your object.

For a tutorial on designing audio plugins, see Audio Plugins in MATLAB.

Methods

getSampleRateGet sample rate at which the plugin is run
setLatencyInSamplesSet latency in samples reported to DAW
setSampleRateSet sample rate at which the plugin is run

Copy Semantics

Handle. To learn how handle classes affect copy operations, see Object Behavior.

Examples

collapse all

Design a valid basic audio plugin class.

Terminology:

  • A valid audio plugin is one that can be deployed in a digital audio workstation (DAW) environment. To validate it, use the validateAudioPlugin function. To generate it, use the generateAudioPlugin function.

  • A basic audio plugin inherits from the audioPlugin class but not the matlab.System class.

Define a basic audio plugin class that inherits from audioPlugin.

classdef myAudioPlugin < audioPlugin
end

Add a processing function to your plugin class. All valid audio plugins include a processing function. For basic audio plugins, the processing function is named process. The processing function is where audio processing occurs. It always has an output.

classdef myAudioPlugin < audioPlugin
    methods
        function out = process(~,in)
            out = in;
        end
    end
end

Design an audioPlugin class that uses the getSampleRate method to get the sample rate at which the plugin is run. The plugin in this example, simpleStrobe, uses the sample rate to determine a constant 50 ms strobe period.

classdef simpleStrobe < audioPlugin
    % simpleStrobe Add audio strobe effect
    %   Add a strobe effect by gain switching between 0 and 1 in
    %   50 ms increments.  Although the input sample rate can change,
    %   the strobe period remains constant.
    %
    %   simpleStrobe properties:
    %   period	- Number of samples between gain switches
    %   gain	- Gain multiplier, one or zero
    %   count	- Number of samples since last gain switch
    %
    %
    %   simpleStrobe methods:
    %   process	- Multiply input frame by gain, element by element
    %   reset	- Reset count and gain to initial conditions 
    %             and get sample rate

    properties
        Period = 44100*0.05;
        Gain = 1;
    end
    properties (Access = private)
        Count = 1;
    end
    methods
        function out = process(plugin,in)
            for i = 1:size(in,1)
                if plugin.Count == plugin.Period
                    plugin.Gain = 1 - plugin.Gain;
                    plugin.Count = 1;
                end
                in(i,:) = in(i,:)*plugin.Gain;
                plugin.Count = plugin.Count + 1;
            end
            out = in;
        end
        function reset(plugin)
            plugin.Period = floor( getSampleRate(plugin)*0.05 );
            plugin.Count = 1;
            plugin.Gain = 1;
        end
    end
end

Design an audioPlugin class that uses the setLatencyInSamples method to report the latency of the plugin. The plugin in this example, simpleDelay, delays the audio signal by a fixed integer and reports the delay to the host application.

classdef simpleDelay < audioPlugin
    % simpleDelay Add delay to audio signal
    %   This plugin adds a 100 sample delay to the audio input and reports
    %   the latency to the host application.
    properties (Access = private)
        Delay
    end
    methods
        function plugin = simpleDelay
            plugin.Delay = dsp.Delay(100);
        end
        function out = process(plugin,in)
            out = plugin.Delay(in);
        end
        function reset(plugin)
            setLatencyInSamples(plugin,100)
        end
    end
end

This example is intended to show the pattern for using setLatencyInSamples. For a detailed use-case, see audiopluginexample.FastConvolver in the Audio Plugin Example Gallery.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Introduced in R2016a