%% Design an Audio Plugin
% An audio plugin encapsulates an audio processing algorithm and enables you to tune the parameters of the algorithm while streaming audio.
%% Define an Audio Plugin
% To define a plugin that enables users to adjust stereo width:
% Create a class definition that inherits from audioPlugin.
% Parameterize the stereo width of the processing algorithm by defining the public property Width.
% Enable users to tune the stereo width by defining an audioPluginInterface that contains Width as an audioPluginParameter.
% Define the audio processing by creating a process method. The process method takes the audio input, in, and adjusts the stereo width by: (a) applying mid-side encoding, (b) adjusting the stereo width based on the user-controlled Width parameter, and then (c) applying mid-side decoding.
% classdef StereoWidth < audioPlugin                            % <== (1) Inherit from audioPlugin.
%     properties
%         Width = 1;                                            % <== (2) Define tunable property.
%     end
%     properties (Constant)
%         PluginInterface = audioPluginInterface( ...           % <== (3) Map tunable property to plugin parameter.
%             audioPluginParameter('Width', ...
%                 'Mapping',{'pow',2,0,4}));
%     end
%     methods
%         function out = process(plugin,in)                     %< == (4) Define audio processing.
%
%             x = [in(:,1) + in(:,2), in(:,1) - in(:,2)];             %  (a) Mid-side encoding.
%             y = [x(:,1), x(:,2)*plugin.Width];                      %  (b) Adjust stereo width.
%             out = [(y(:,1) + y(:,2))/2, (y(:,1) - y(:,2))/2];       %  (c) Mid-side decoding.
%
%         end
%     end
% end
%
%% Prototype the Audio Plugin
% Once you have defined an audio plugin, you can prototype it using the Audio Test Bench app. The Audio Test Bench app enables you to stream audio through the plugin while you tune parameters, perform listening tests, and visualize the original and processed audio. To open your StereoWidth plugin in the Audio Test Bench app, at the MATLAB® command prompt, enter:

audioTestBench(StereoWidth)

%
%
%% Validate and Generate a VST Plugin
% You can validate a MATLAB® audio plugin and generate a VST plugin from the Audio Test Bench. You can also validate and generate the plugin from the command line by using the validateAudioPlugin and generateAudioPlugin functions. Once generated, you can deploy your plugin to a digital audio workstation (DAW).

validateAudioPlugin StereoWidth
generateAudioPlugin StereoWidth

% The VST plugin is saved to your working directory.
%
% Copyright 2019 The MathWorks, Inc.