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

    if nargin < 2
        fsnew = 96000; 
    end

    if nargin < 3
        bitdepthnew = 24;
    end
    
    % go over all wav files in this folder
    files = dir([foldername '/*.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)
            info     = audioinfo([foldername '/' files(k).name]); 
            bitdepth = info.BitsPerSample; 
            fs       = info.SampleRate; 
            
            if fs==fsnew && bitdepth == bitdepthnew
                disp([files(k).name ' already at ' num2str(fs) ' Hz, ' num2str(bitdepth) ' bit.']);
            else
                [audio,fs] = audioread([foldername '/' files(k).name]); % read audio
                disp([files(k).name ' was ' num2str(fs) ' Hz, ' num2str(bitdepth) ' bit.']);
                if fs ~= fsnew
                    audio = resample(audio, fsnew, fs);
                end
                audiowrite([foldername '/' files(k).name], audio/max(max(abs(audio))), ...
                    fsnew, 'BitsPerSample', bitdepthnew);
            end
    end

end