%% Specify Multiple Parameters of Custom Augmentation Method
% Read in an audio signal and listen to it.

[audioIn,fs] = audioread('RockDrums-44p1-stereo-11secs.mp3');
sound(audioIn,fs)

%
% Create and audioDataAugmenter object that outputs 5 augmentations. Set the AddNoiseProbability to 0.

aug = audioDataAugmenter('NumAugmentations',5,'AddNoiseProbability',0);

% Add reverberation as a custom augmentation algorithm. The applyReverb function creates a reverberator object, updates the sample rate, pre-delay, and wet/dry mix as indicated, and then applies reverberation. To minimize computational overhead, the reverberator object is persistent. The object is reset on every call to avoid mixing the reverberation tail between audio files.

type applyReverb.m

% Add applyReverb as a custom augmentation method. To specify multiple parameters for a custom method, specify the parameters, parameter ranges, and parameter values as cell arrays with the same number of cells. Set the probability of applying reverberation to 1.

algorithmName = 'Reverb';
algorithmHandle = @(x,preDelay,weDryMix)applyReverb(x,preDelay,weDryMix,fs);
parameters = {'PreDelay','WetDryMix'};
parameterRanges = {[0,1],[0,1]};
parameterValues = {0,0.3};

addAugmentationMethod(aug,algorithmName,algorithmHandle, ...
    'AugmentationParameter',parameters, ...
    'ParameterRange',parameterRanges, ...
    'ParameterValue',parameterValues)

aug.ReverbProbability = 1

% Call augment to create 5 augmentations.

data = augment(aug,audioIn,fs);

% Check the configuration of each augmentation using AugmentationInfo.
%

augmentationToInspect = 1;
data.AugmentationInfo(augmentationToInspect)

% Listen to the audio you are inspecting. Plot the time-domain representation of the original and augmented signals.

augmentation = data.Audio{augmentationToInspect};
sound(augmentation,fs)
t = (0:(size(audioIn,1)-1))/fs;
taug = (0:(size(augmentation,1)-1))/fs;
plot(t,audioIn(:,1),taug,augmentation(:,1))
legend("Original Audio","Augmented Audio")
ylabel("Amplitude")
xlabel("Time (s)")

% Copyright 2019 The MathWorks, Inc.