function [] = batchResample(foldername, fsnew, bitDepth)
% BATCH RESAMPLE converts sample rate of all files in folder. 
%
% by Brecht De Man at Centre for Digital Music on 13 April 2014

% TODO read sampling rate without reading whole file

    if nargin <3
        bitDepth = 24;
    end

    currentfolder = pwd;
    cd(foldername); % go to specified folder
    % go over all wav files in this folder
    files = dir('*.wav');

    % remove hidden files from list
    % see http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/258220
    for k = length(files):-1:1
        fname = files(k).name;
        if fname(1) == '.'
            files(k) = [ ];
        end
    end

    for k=1:length(files)
            disp(['Reading ' files(k).name '...']);

            [audio,fs] = audioread(files(k).name); % read audio
            
            if fs==fsnew
                warning('Sampling rate of original audio file is equal to current sampling rate');
            else
                resampledAudio = resample(audio, fsnew, fs);
                audiowrite([files(k).name],resampledAudio, fsnew, 'BitsPerSample', bitDepth);
            end
    end
    cd(currentfolder); % go back to original folder

end