%% Convert MATLAB Code to an Audio Plugin Existing Script
%% Section A: Variable Initialization

% Specify frequency of gain oscillation.
Frequency = 1;

% Determine sample rate of audio file (input audio signal).
fileInfo = audioinfo(...
    'RockGuitar-16-44p1-stereo-72secs.wav');
sampleRate = fileInfo.SampleRate;

% Specify size of frame to read in from audio file.
frameSize = 256;

%
%% Section B: Object Construction

Sine = audioOscillator(...
    'DCOffset',1,...
    'SamplesPerFrame',frameSize,...
    'Frequency',Frequency,...
    'SampleRate',sampleRate);

Cosine = audioOscillator(...
    'DCOffset',1,...
    'PhaseOffset',0.5,...
    'Frequency',Frequency,...
    'SamplesPerFrame',frameSize,...
    'SampleRate',sampleRate);

fileReader = dsp.AudioFileReader(...
    'Filename',fileInfo.Filename,...
    'SamplesPerFrame',frameSize);

deviceWriter = audioDeviceWriter(...
    'SampleRate',fileReader.SampleRate);

%
%% Section C: Audio Stream Loop

while ~isDone(fileReader)
    
    % Read in one frame of audio signal from file.
    in = fileReader();
    
    % Mix stereo input to mono.
    mono = 0.5*sum(in,2);
    
    % Get current frame of Sine and Cosine gain functions.
    gainLeft = Sine();
    gainRight = Cosine();
    
    % Process signal by multiplying by variable gain matrix.
    out = [mono,mono] .* [gainLeft,gainRight];
    
    % Write one frame of audio signal to device.
    deviceWriter(out);
    
end

% Copyright 2012 The MathWorks, Inc.