function audioEqualizerEXEExampleApp
%audioEqualizerEXEExampleApp Graphical interface for audio equalization 
% with standalone executable.

% Copyright 2015-2018 The MathWorks, Inc.

%% Setup
Fs = 44100;
FNyquist = Fs/2 - (eps(Fs/2));

UDPReceive = dsp.UDPReceiver('LocalIPPort',20000, ...  %Receives UI flags from
    'MessageDataType','double', ...                    %the simulation.
    'MaximumMessageLength',15+1, ...
    'ReceiveBufferSize',15*80);
c = onCleanup(@()release(UDPReceive));
udpRec = UDPReceive(); %#ok

UDPSend = dsp.UDPSender('RemoteIPPort',25000);  %Sends UI flags to the EXE

v = dsp.DynamicFilterVisualizer(8192, Fs, [20 20e3], 'XScale','Log');
show(v);
                              
% Define parameters to be tuned
param(1).Name = 'Center Frequency1';
param(1).InitialValue = 300;
param(1).Limits = [0, FNyquist];

param(2).Name = 'Quality Factor 1';
param(2).InitialValue = 2;
param(2).Limits = [0.2, 700];

param(3).Name = 'Peak Gain1 (dB)';
param(3).InitialValue = 3;
param(3).Limits = [-20 20];

param(4).Name = 'Center Frequency2';
param(4).InitialValue = 1000;
param(4).Limits = [0, FNyquist];

param(5).Name = 'Quality Factor 2';
param(5).InitialValue = 1.5;
param(5).Limits = [0.2, 700];

param(6).Name = 'Peak Gain2 (dB)';
param(6).InitialValue = -2;
param(6).Limits = [-20 20];

param(7).Name = 'Center Frequency3';
param(7).InitialValue = 3000;
param(7).Limits = [0, FNyquist];

param(8).Name = 'Quality Factor 3';
param(8).InitialValue = 2.5;
param(8).Limits = [0.2, 700];

param(9).Name = 'Peak Gain3 (dB)';
param(9).InitialValue = 2;
param(9).Limits = [-20 20];    

% Create the UI and pass it the parameters
tuningUI = HelperCreateParamTuningUI(param, 'Parametric Equalizer Tuning');

clear HelperUnpackUIData

% Start processing
if ispc
    !start audioEqualizerExecutable.exe & exit
else
    !./audioEqualizerExecutable &
end

stopSim = false;
tic
while ~stopSim && toc < 72
    drawnow limitrate;  % needed to flush out UI event queue

    udpRec = UDPReceive();
    S = HelperUnpackUIData(tuningUI);
    if isempty(S)
        continue;
    end
    if S.Stop
        release(UDPSend)
        release(UDPReceive)
        break;
    end
    if ~isempty(udpRec)
        B = reshape(udpRec(1:9),3,3).';
        A = [ones(1,3);reshape(udpRec(10:15),2,3)].';
        v(B,A);        
    end
    if S.ValuesChanged
         UDPSend([S.TuningValues,S.Reset,S.Pause,S.Stop]);
    end
end

if ishghandle(tuningUI)
    close(tuningUI);
end
