view aux/batchResample.m @ 2:5e72201496c8

Bug fixes, added stripzeros function, added new loudness function, moved general documentation to top level, MATLAB_R2014b compatibility
author Brecht De Man <b.deman@qmul.ac.uk>
date Mon, 17 Nov 2014 19:43:43 +0000
parents 4fd284285159
children 2afd6ff39f08
line wrap: on
line source
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

    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)
            info     = audioinfo(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(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([files(k).name], audio, fsnew, ...
                    'BitsPerSample', bitdepthnew);
            end
    end
    
    cd(currentfolder); % go back to original folder

end