%% Create Keyword Spotting Mask Using Audio Labeler
% In this example, you create a logical mask for an audio signal where ones correspond to the utterance "yes" and zeros correspond to the absence of the utterance "yes". To create the mask, you use the IBM™ speech-to-text API through the Audio Labeler app.
% This example requires that you install the speech2text functionality.
%
% Listen to the audio file that you want to label and then visualize it in the time domain.

[audioIn,fs] = audioread("KeywordSpeech-16-16-mono-34secs.flac");

sound(audioIn,fs)

t = (0:numel(audioIn)-1)/fs;
plot(t,audioIn)
xlabel('Time (s)')
ylabel('Amplitude')

%
% Open the Audio Labeler app and load the KeywordSpeech-16-16-mono-34secs.flac file into the Data Browser.
%
%
% Under Automation, click Speech to Text. On the Speech to Text tab, select your preferred speech-to-text API. This example uses the IBM speech-to-text API. Select Segment Words so that the text labels are divided into individual words instead of sentences. Click Run to interface with the speech-to-text API and create a new region of interest (ROI) label. The ROI label contains words detected and labeled by IBM's speech-to-text API.
%
%
% Close the Speech to Text tab and then export the labeled signal set to the workspace.
%
%
% The labels are exported to the workspace as labeledSignalSet object with a time stamp. Set the variable labeledSet to the time-stamped labeledSignalSet object.

labeledSet = myLabeledSet;

%
%  Inspect the SpeechContent label.

speechContent = labeledSet.Labels.SpeechContent{1}

%
% The speech-to-text API returns the limits of the ROI labels in seconds. Use the SpeechContent table to create a logical vector.

keywordLabels = speechContent(speechContent.Value == "yes",:);
keywordROILimitsInSamples = round(keywordLabels.ROILimits*fs);

mask = zeros(size(audioIn),"logical");
for i = 1:size(keywordROILimitsInSamples)
    mask(keywordROILimitsInSamples(i,1):keywordROILimitsInSamples(i,2)) = true;
end

%
% Plot the speech signal and the keyword spotting mask.

plot(t,audioIn, ...
     t,mask)
xlabel('Time (s)')
ylabel('Amplitude')
legend('Audio','Keyword Spotting Mask','Location','southeast')

%
%
% Copyright 2019 The MathWorks, Inc.