function autoalign(folderName, masterIndex)
% AUTOALIGN aligns audio in folder with selected file (index 'masterIndex')
% by inserting zeros at the start of the other files. 
%
% by Brecht De Man at Centre for Digital Music on 17 November 2014

    error('Work in progress, function not finished');
    
    tic;                                % start measuring time
    list = dir([foldername '\*.wav']);  % get names of files
    
    % remove hidden files from list
    % see http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/258220
    for k = length(list):-1:1
        fname = list(k).name;
        if fname(1) == '.'
            list(k) = [ ];
        end
    end

    % load master file
    masterAudioChannels = audioread([foldername '/' list(masterIndex).name]); 
    masterAudio         = sum(masterAudioChannels, 2); % sum to mono
    disp(['Aligning audio files with ' list(masterIndex).name '...']);
    
    % for each other file:
    for i = 1:length(list)
        
        slaveAudioChannels = audioread([foldername '/' list(i).name]); % read file
        slaveAudio         = sum(slaveAudioChannels, 2); % sum to mono
        
        % check sampling rate is the same
        % ... 

        % crosscorrelation function
        xcorr(masterAudio, slaveAudio); 
        
        % add zeros to beginning 
        
        % monitoring/debugging
        
    end

    elapsedTime = toc;  % stop measuring time
    disp(['autoalign took ' elapsedTime ' seconds to align the files in folder ' ...
        folderName ' with master file ' list(masterIndex).name]);

end