function HelperEqualizerEXEProcessing
%

% Copyright 2015-2016 The MathWorks, Inc.
%#codegen

%% Initialization
% Define System objects

reader = dsp.AudioFileReader('RockGuitar-16-44p1-stereo-72secs.wav','SamplesPerFrame',256);
Fs = reader.SampleRate;

player = audioDeviceWriter;

UDPSend = dsp.UDPSender('RemoteIPPort',20000); %For sending Simulation data
                                               %to the EXE
pauseSim = false;   
stopSim = 0;
x = zeros(reader.SamplesPerFrame,2);
c = zeros(15,1,'like',x);

%% Streaming
while ~isDone(reader)
   
    if ~pauseSim
        x = reader();
    end
        
    [y,c,pauseSim,stopSim] = HelperEqualizationProcessing_filter(x,Fs);

    if stopSim
        UDPSend(c);
        break;
    end
    
    if pauseSim   % If "Pause Simulation" button is pressed
        continue;
    end
    
    UDPSend(c);
    nUnderrun = player(y);
    if nUnderrun > 0
        fprintf('%u samples underrun',nUnderrun);
    end
end

release(UDPSend);
release(player);
release(reader);
end
%--------------------------------------------------------------------------
function [y,c,pauseSim,stopSim] =  HelperEqualizationProcessing_filter(x,Fs)
% Apply biquad filters to the input. Changes to biquad coefficients based
% on the input from UI are also taken into account here.
pauseSim = false;
stopSim = false;

persistent paramEQ UDPReceive
if isempty(paramEQ)
    filterDesignParams = [300 2 3 1000 1.5 -2 3000 2.5 2]; % Initial vals
    paramEQ = multibandParametricEQ;
    paramEQ.SampleRate = Fs;
    paramEQ.Frequencies = [filterDesignParams(1),filterDesignParams(4),filterDesignParams(7)];
    paramEQ.QualityFactors = [filterDesignParams(2),filterDesignParams(5),filterDesignParams(8)];
    paramEQ.PeakGains = [filterDesignParams(3),filterDesignParams(6),filterDesignParams(9)];
    UDPReceive = dsp.UDPReceiver('LocalIPPort',25000); %For receiving new Simulation Flags

end

udpRec = UDPReceive();

y=zeros(size(x),'like',x);
c=zeros(15,1,'like',x);
if ~isempty(udpRec)
    resetSim = logical(udpRec(end-2));
    pauseSim = logical(udpRec(end-1));
    stopSim = logical(udpRec(end));

    if stopSim
        return;  % Stop the simulation
    end
    
    if pauseSim
        return; % Pause the simulation (but keep checking for commands from UI)
    end
    
    if resetSim  % If "Reset" button is pressed
            reset(paramEQ);        
    else
        paramEQ.Frequencies    = [udpRec(1),udpRec(4),udpRec(7)];
        paramEQ.QualityFactors = [udpRec(2),udpRec(5),udpRec(8)];
        paramEQ.PeakGains      = [udpRec(3),udpRec(6),udpRec(9)];
    end
end
y = paramEQ(x);
[B,A] = coeffs(paramEQ);
c = [B(:); A(:)];
end
