function [results,figHandle] = helperAudioLoopTimerExample( ...
    setupObjectsOutsideLoop)
% This function helperAudioBenchmark is only in support of the featured
% example 'Measuring Performance of Streaming Real-Time Audio Algorithms'.
% It may change in a future release.

% Copyright 2015-2018 The MathWorks, Inc.

if nargin < 1
   setupObjectsOutsideLoop = false; 
end

%% Define simulation constants
numFrames = 100;
FrameSize = 512;
Fs = 44100;

%% Create AudioLoopTimer object
at = audioexample.AudioLoopTimer(numFrames,FrameSize,Fs);

%% BEGIN initialization time measurement
ticInit(at)

%% Define algorithm coefficients and objects
reader = dsp.AudioFileReader( ...
    'Filename','SoftGuitar-44p1_mono-10mins.ogg', ...
    'SamplesPerFrame',FrameSize);
N = 8;
G = -inf;
Q = 1.8;
Wo = 60/(Fs/2); % Notch at 60 Hz
BW = Wo/Q;      % Bandwidth will occur at -3 dB
[B,A] = designParamEQ(N,G,Wo,BW);
SOS = [B', ones(N/2,1), A'];
notch = dsp.BiquadFilter('SOSMatrix',SOS);

%% Optionally perform object setup outside of the simulation look
if setupObjectsOutsideLoop
    setup(reader);
    setup(notch,zeros(FrameSize,1));
end

%% END initialization time measurement
tocInit(at)

%% BEGIN simulation loop
for index = 1:numFrames
    ticLoop(at) % BEGIN loop timing measurement
    audioIn = reader();
    audioOut = notch(audioIn); %#ok
    tocLoop(at) % END loop timing measurement
end
%% END simulation loop


%% Generate report
[results,figHandle] = generateReport(at);