%% Identify and Locate Sounds in Time
% Read in an audio signal. Call classifySound to return the detected sounds and corresponding time stamps.

[audioIn,fs] = audioread('multipleSounds-16-16-mono-18secs.wav');
[sounds,timeStamps] = classifySound(audioIn,fs);

%
% Plot the audio signal and label the detected sound regions.

t = (0:numel(audioIn)-1)/fs;
plot(t,audioIn)
xlabel('Time (s)')
axis([t(1),t(end),-1,1])

textHeight = 1.1;
for idx = 1:numel(sounds)
    patch([timeStamps(idx,1),timeStamps(idx,1),timeStamps(idx,2),timeStamps(idx,2)], ...
        [-1,1,1,-1], ...
        [0.3010 0.7450 0.9330], ...
        'FaceAlpha',0.2);
    text(timeStamps(idx,1),textHeight+0.05*(-1)^idx,sounds(idx))
end

%
% Select a region and listen only to the selected region.

sampleStamps = floor(timeStamps*fs)+1;
soundEvent = 3;

isolatedSoundEvent = audioIn(sampleStamps(soundEvent,1):sampleStamps(soundEvent,2));
sound(isolatedSoundEvent,fs);
display('Detected Sound = ' + sounds(soundEvent))

% Copyright 2020 The MathWorks, Inc.