%% Add Reverberation to Audio Signal
% Use the reverberator System object™ to add artificial reverberation to an audio signal read from a file.
% Create the dsp.AudioFileReader and audioDeviceWriter System objects. Use the sample rate of the reader as the sample rate of the writer.

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

%
% Play 10 seconds of the audio signal through your device.

tic
while toc < 10
    audio = fileReader();
    deviceWriter(audio);
end
release(fileReader)

%
% Construct a reverberator System object with default settings.

reverb = reverberator

%
% Construct a time scope to visualize the original audio signal and the audio signal with added artificial reverberation.

scope = dsp.TimeScope( ...
    'SampleRate',fileReader.SampleRate, ...
    'TimeSpanOverrunAction','Scroll', ...
    'TimeSpan',3, ...
    'BufferLength',3*fileReader.SampleRate*2, ...
    'YLimits',[-1,1], ...
    'ShowGrid',true, ...
    'ShowLegend',true, ...
    'Title','Audio with Reverberation vs. Original');

%
% Play the audio signal with artificial reverberation. Visualize the audio with reverberation and the original audio.

while ~isDone(fileReader)
    audio = fileReader();
    audioWithReverb = reverb(audio);
    deviceWriter(audioWithReverb);
    scope([audioWithReverb(:,1),audio(:,1)])
end
release(fileReader)
release(deviceWriter)
release(scope)

% Copyright 2012 The MathWorks, Inc.