%% Tune Limiter Parameters
% Create a dsp.AudioFileReader to read in audio frame-by-frame. Create a audioDeviceWriter to write audio to your sound card. Create a limiter to process the audio data.

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

dRL = limiter('SampleRate',fileReader.SampleRate);

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

parameterTuner(dRL)

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

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

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

release(deviceWriter)
release(fileReader)
release(dRL)

%
% Copyright 2018 The MathWorks, Inc.