function prepare2listen(foldername)
% PREPARE2LISTEN batch processes all audio files in a folder to prepare for
% a listening test, by 
% - making sure everything is at the same sampling rate and bit depth;
% - removing subsequent zeros at the start and the end;
% - ensuring none of the files are equal to each other; 
% - clipping the desired region from the file; 
% - adding a fade in and fade out; and
% - setting the loudness at a predefined value. 
% It then saves the result in a folder within the audio folder. 
%
% by Brecht De Man at Centre for Digital Music, 5 June 2013

    % PARAMETERS
    newfolder   = 'fragments';
    fs          = 96000;    % Hz
    bitdepth    = 24; 
    starttime   = 76;  % SR1 88; SR2 76      % seconds
    endtime     = 152; % SR1 142;  SR2 152    % seconds
    fadetime    = 1;        % seconds
    format      = 'wav';
    slash       = '/';
    
    target_loudness = -23;  % dBLU (https://tech.ebu.ch/loudness)
    
    tic;                                    % measure time
    list = dir([foldername '/*.' format]);  % get files
    
    % remove hidden files from list, and copy to new folder
    % see http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/258220
    newfoldername = [foldername slash newfolder]; % within original folder
    if ~exist(newfoldername, 'dir')   % if it doesn't exist yet
        mkdir(foldername, newfolder); % make newfolder within folder
    end
    for k = length(list):-1:1
        fname = list(k).name;
        if fname(1) == '.'
            list(k) = [];
        else
            copyfile([foldername slash list(k).name], ...
                [newfoldername slash list(k).name]);
        end
    end
    
    % set at same sampling rate and bit depth
    disp(['-- Ensuring audio is at ' num2str(fs) 'Hz / ' ...
        num2str(bitdepth) ' bit...']);
    batchresample(newfoldername, fs, bitdepth); 
    % NOTE: clipfade(...) also tests sampling rate, but does not resample
    
    % find doubles
    disp('-- Checking for doubles...');
    finddouble(newfoldername);
    
    % strip zeros off beginning and end
    disp('-- Stripping off leading and trailing zeros...');
    stripzeros(newfoldername); 
    
    % align all audio files
    disp('-- Aligning audio files...');
    autoalign(newfoldername); 
    
    % clip and fade desired fragments
    startmin = floor(starttime/60);
    startsec = mod(starttime, 60);
    endmin   = floor(endtime/60);
    endsec   = mod(endtime, 60);
    disp(['-- Fading in (' num2str(fadetime) 's) at ' num2str(startmin) ...
        ':' num2str(startsec) ', fading out (' num2str(fadetime) 's) at '...
        num2str(endmin) ':' num2str(endsec) '...']);
    clipfade(newfoldername, starttime, endtime, fadetime, fs, bitdepth); 

    % open files and calculate their loudness, then adjust
    disp(['-- Setting each file at ' num2str(target_loudness) 'dBLU ...']);
    for i = 1:length(list)
        disp([newfoldername slash list(i).name]); % DEBUG
        audio = audioread([newfoldername slash list(i).name]);
        initial_loudness   = getloudness( audio, fs, 'ITU', 0); 
        
        % calculate difference in loudness
        difference_loudness = target_loudness - initial_loudness; 
        
        % write back with adjusted loudness
        newaudio = 10^(difference_loudness/20) .* audio;
        audiowrite([newfoldername slash list(i).name],...
            newaudio, fs, 'BitsPerSample', bitdepth);
    end
    
    elapsed_time = toc;
    disp(['prepare2listen took ' num2str(elapsed_time) ...
        ' seconds on folder ' foldername]); 
end