%% Split by Number of Files and Label Table
% Specify the file path to the audio samples included with Audio Toolbox™. Create an audio datastore that points to the specified folder.

folder = fullfile(matlabroot,'toolbox','audio','samples');
ADS = audioDatastore(folder);

%
% Create a label table with two variables:
% containsMusic - Can be either true or false.
% instrument - Can be Guitar, Drums, or Unknown.

containsGuitar = contains(ADS.Files,'guitar','IgnoreCase',true);
containsDrums = contains(ADS.Files,'drum','IgnoreCase',true);
containsMusic = or(containsGuitar,containsDrums);

instrument = strings(size(ADS.Files));
instrument(:) = "Unknown";
instrument(containsGuitar) = "Guitar";
instrument(containsDrums) = "Drums";

%
% Assign the label table to the Labels property of audio datastore to associate the rows of the label table with the rows of the datastore. Call countEachLabel to determine the incidences of containsMusic and instrument.

labels = table(containsMusic,instrument);
ADS.Labels = labels;

containsMusicCount = countEachLabel(ADS,'TableVariable','containsMusic')
instrumentCount = countEachLabel(ADS,'TableVariable','instrument');

%
% Split the datastore ADS into two, based on whether the audio file contains music. ADS1 contains 5 of each label under the table variable containsMusic, and ADS2 contains the rest. Call countEachLabel to verify.

[ADS1,ADS2] = splitEachLabel(ADS,5,'TableVariable','containsMusic');
ADS1_containsMusicCount = countEachLabel(ADS1,'TableVariable','containsMusic')
ADS2_containsMusicCount = countEachLabel(ADS2,'TableVariable','containsMusic')

%
% Split the datastore ADS into two, based on the type of instrument present in the audio file. ADS3 contains 2 of each label under the table variable instrument, and ADS4 contains the rest. Call countEachLabel to verify.

[ADS3,ADS4] = splitEachLabel(ADS,2,'TableVariable','instrument');
ADS3_instrumentCount = countEachLabel(ADS3,'TableVariable','instrument')
ADS4_instrumentCount = countEachLabel(ADS4,'TableVariable','instrument')

%
% Copyright 2018 The MathWorks, Inc.