%% Detect Voice Activity
% Use the default |voiceActivityDetector| System object? to detect the presence 
% of speech in a streaming audio signal.
% 
% Create an audio file reader to stream an audio file for processing. Define 
% parameters to chunk the audio signal into 10 ms non-overlapping frames.
%%
fileReader = dsp.AudioFileReader('Counting-16-44p1-mono-15secs.wav');
fs = fileReader.SampleRate;
fileReader.SamplesPerFrame = ceil(10e-3*fs);
%% 
% Create a default |voiceActivityDetector| System object to detect the presence 
% of speech in the audio file.
%%
VAD = voiceActivityDetector;
%% 
% Create a scope to plot the audio signal and corresponding probability 
% of speech presence as detected by the voice activity detector. Create an audio 
% device writer to play the audio through your sound card.
%%
scope = dsp.TimeScope( ...
    'NumInputPorts',2, ...
    'SampleRate',fs, ...
    'TimeSpan',3, ...
    'BufferLength',3*fs, ...
    'YLimits',[-1.5 1.5], ...
    'TimeSpanOverrunAction','Scroll', ...
    'ShowLegend',true, ...
    'ChannelNames',{'Audio','Probability of speech presence'});
deviceWriter = audioDeviceWriter('SampleRate',fs);

%% 
% In an audio stream loop:
% 
% # Read from the audio file.
% # Calculate the probability of speech presence.
% # Visualize the audio signal and speech presence probability.
% # Play the audio signal through your sound card.
%%
while ~isDone(fileReader)
    audioIn = fileReader();
    probability = VAD(audioIn);
    scope(audioIn,probability*ones(fileReader.SamplesPerFrame,1))
    deviceWriter(audioIn);
end

%% 
% Copyright 2012 The MathWorks, Inc.