% POSSIBLE VALUES FOR filter_type:
% 'low'
% 'high'
% 'bandpass'
% 'stop'

function [] = timeDomainPlot(filename, filter_type, w_cutoff)
 
global player;
global stopped;

stopped = 0;

[Y,fs,NBITS] = wavread(filename);

if nargin == 3
    [B, A] = butter(5, w_cutoff/(fs/2), filter_type);
    Y = filter(B, A, Y);
elseif nargin == 2
    'Warning: wrong number of arguments. No filter applied.'
end

player = audioplayer(Y, fs, NBITS);

figure(1);
title('Waveform');
xlabel('Time'); ylabel('Amplitude')
timevector = [0:1/fs:size(Y, 1)/fs];
plot(timevector(1:end-1), Y(:,1));

uicontrol('Style', 'pushbutton', 'String', 'Stop',...
         'Position', [20 20 60 25],...
        'Callback', 'pause(player); stopped = 1;');   
   
tic
lastTime = toc;

play(player);
timeRange = 0.4;

for ii = 2:length(Y)
    
    timeDiff = toc - lastTime;
    
    if timeDiff > 0.1
        xlim([toc toc+timeRange ]);
        drawnow
        lastTime = toc;
    end
    
    if(stopped == 1)
        pause(player);
        close all;
        break;
    end
    
end
