%% Tune Noise Gate Parameters
% Create a dsp.AudioFileReader to read in audio frame-by-frame. Create an audioDeviceWriter to write audio to your sound card. Create a noiseGate to process the audio data.

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

dRG = noiseGate('SampleRate',fileReader.SampleRate);

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

parameterTuner(dRG)

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

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

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

release(deviceWriter)
release(fileReader)
release(dRG)

%
% Copyright 2018 The MathWorks, Inc.