function prepare2listen
% PREPARE2LISTEN equalises loudness of files to prepare for
% listening test.
%
% by Brecht De Man at Centre for Digital Music, 5 June 2013

folder  = '/Users/Brecht/Documents/MATLAB/McGillTest2014/listeningTest/AUDIO/SR2_UnderACoveredSky/Clips/';
newfolder = 'equalloudness';
list = dir([folder '/*.wav']);
slash = '/';

% open files and calculate minimum loudness
MIN = 0;
x = struct([]);
for i = 1:length(list)
    disp([folder newfolder slash list(i).name]); % DEBUG
    [x{i}.audio,fs] = audioread([folder list(i).name]);
    x{i}.loudness   = loudness_itu(x{i}.audio, fs);
    MIN = min(MIN, floor(x{i}.loudness)); % compute minimum loudness
end

% make folder
if ~exist([folder 'equalloudness'], 'dir') % make folder 'equalloudness' if not there
    mkdir(folder, 'equalloudness');
end

% equalise loudness
MAX = 0;
for i = 1:length(list)
    x{i}.equalised = loudness_match(x{i}.audio, fs, MIN)*x{i}.audio;
    MAX = max(MAX, max(max(abs(x{i}.equalised))));
end

% normalise (keeping relative loudness) and save
for i = 1:length(list)
    audiowrite([folder newfolder '\' list(i).name], x{i}.equalised/MAX, ...
        fs, 'BitsPerSample', 24);
end

end