%% Tune Multiband Parametric EQ Parameters
% Create a dsp.AudioFileReader to read in audio frame-by-frame. Create a audioDeviceWriter to write audio to your sound card. Create a multibandParametricEQ to process the audio data. Call visualize to plot the frequency response of the equalizer.

frameLength = 1024;
fileReader = dsp.AudioFileReader('RockDrums-44p1-stereo-11secs.mp3','SamplesPerFrame',frameLength);
deviceWriter = audioDeviceWriter('SampleRate',fileReader.SampleRate);

equalizer = multibandParametricEQ('SampleRate',fileReader.SampleRate, 'PeakGains',[-2,2,4]);
visualize(equalizer)

%
%
% Call parameterTuner to open a UI to tune parameters of the equalizer while streaming.

parameterTuner(equalizer)

%
% In an audio stream loop:
% Read in a frame of audio from the file.
% Apply equalization.
% Write the frame of audio to your audio device for listening.
% While streaming, tune parameters of the equalizer and listen to the effect.

while ~isDone(fileReader)
    audioIn = fileReader();
    audioOut = equalizer(audioIn);
    deviceWriter(audioOut);
    drawnow limitrate % required to update parameter
end

%
% As a best practice, release your objects once done.

release(deviceWriter)
release(fileReader)
release(equalizer)

%
% Copyright 2018 The MathWorks, Inc.