%% Clean Audio Data Set
% Audio data sets, especially open-source audio data sets, might have inconsistent sampling rates, numbers of channels, or durations. They might also contain garbage data, such as clips that are labeled as containing speech but contain silence.
% It is often a first step in machine learning and deep learning workflows to clean the audio data set. This is particularly important for the validation and test data sets. Common types of cleaning include resampling, converting to mono or stereo, trimming or expanding the duration of clips to a consistent length, removing periods of silence, removing background noise, or gain normalization.
% In this example, you clean an audio data set so that all the files have a sample rate of 16 kHz, are mono, are in the FLAC format, and are normalized such that the max absolute magnitude of a signal is 1.
%
% Create an audioDatastore object that points to the audio samples included with Audio Toolbox™.

folder = fullfile(matlabroot,"toolbox","audio","samples");
ads = audioDatastore(folder);

%
% Define a function to apply a sequence of operations on the audio data. The function definition is located at the end of this example.
% function [audioOut,adsInfo] = myTransform(audioIn,adsInfo)
%     fs = adsInfo.SampleRate;
%     desiredFs = 16e3;
%
%     % Convert to mono
%     x = mean(audioIn,2);
%
%     % Resample to 16 kHz
%     y = resample(x,desiredFs,fs);
%     adsInfo.SampleRate = desiredFs;
%
%     % Normalize so that the max absolute value of a signal is 1
%     audioOut = y/max(abs(y));
% end
%
% Create a transform datastore object that applies the cleaning operations.

adsTransform = transform(ads,@myTransform,"IncludeInfo",true);

%
% Call writeall on the transform datastore object to create the clean data set. Specify the format as FLAC. Write the data set to your current folder.

outputLocation = pwd;
writeall(adsTransform,outputLocation,"OutputFormat","flac")

%
% Create a new datastore object that points to the clean data set.

adsClean = audioDatastore(outputLocation,"IncludeSubfolders",true)

%
%% Supporting Function

function [audioOut,adsInfo] = myTransform(audioIn,adsInfo)
    fs = adsInfo.SampleRate;
    desiredFs = 16e3;
    x = mean(audioIn,2);
    y = resample(x,desiredFs,fs);
    adsInfo.SampleRate = desiredFs;
    audioOut = y/max(abs(y));
end

% Copyright 2019 The MathWorks, Inc.