%% Extract GTCC from Streaming Audio
%
% Copyright 2018 The MathWorks, Inc.

%%
% Create a |dsp.AudioFileReader| object to read in audio data
% frame-by-frame. Create an |audioDeviceWriter| to write the audio to your
% sound card. Create a |dsp.ArrayPlot| to visualize the GTCC over time.
%

fileReader = dsp.AudioFileReader('RandomOscThree-24-96-stereo-13secs.aif');
deviceWriter = audioDeviceWriter(fileReader.SampleRate);
scope = dsp.ArrayPlot;

%%
% Create a |cepstralFeatureExtractor| that extracts GTCC.
%

cepFeatures = cepstralFeatureExtractor('FilterBank','Gammatone', ...
                                       'SampleRate',fileReader.SampleRate);

%%
% In an audio stream loop:
%
% # Read in a frame of audio data.
% # Extract the GTCC from the frame of audio.
% # Visualize the GTCC.
% # Write the audio frame to your device.
%

while ~isDone(fileReader)
    audioIn = fileReader();
    coeffs = cepFeatures(audioIn);
    scope(coeffs)
    deviceWriter(audioIn);
end

release(cepFeatures)
release(scope)
release(fileReader)
%%
% 
% <<../arrayplot_gtcc.png>>
% 
