b@2: function [] = batchresample(foldername, fsnew, bitdepthnew) b@2: % BATCHRESAMPLE converts sample rate of all files in folder. b@2: % b@0: % by Brecht De Man at Centre for Digital Music on 13 April 2014 b@0: b@2: if nargin < 2 b@2: fsnew = 96000; b@2: end b@0: b@2: if nargin < 3 b@2: bitdepthnew = 24; b@0: end b@8: b@0: % go over all wav files in this folder b@8: files = dir([foldername '/*.wav']); b@0: b@0: % remove hidden files from list b@0: % see http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/258220 b@0: for k = length(files):-1:1 b@0: fname = files(k).name; b@0: if fname(1) == '.' b@2: files(k) = []; b@0: end b@0: end b@0: b@0: for k=1:length(files) b@8: info = audioinfo([foldername '/' files(k).name]); b@2: bitdepth = info.BitsPerSample; b@2: fs = info.SampleRate; b@0: b@2: if fs==fsnew && bitdepth == bitdepthnew b@2: disp([files(k).name ' already at ' num2str(fs) ' Hz, ' num2str(bitdepth) ' bit.']); b@0: else b@8: [audio,fs] = audioread([foldername '/' files(k).name]); % read audio b@2: disp([files(k).name ' was ' num2str(fs) ' Hz, ' num2str(bitdepth) ' bit.']); b@2: if fs ~= fsnew b@12: %audio = resample(audio, fsnew, fs); b@12: b@12: % exactly N seconds long b@12: audio = [audio; zeros(fs - mod(size(audio,1), fs), size(audio, 2))]; b@12: b@12: H = dsp.FIRRateConverter('InterpolationFactor', fsnew, 'DecimationFactor', fs); b@12: audio = step(H, audio); % resample b@2: end b@8: audiowrite([foldername '/' files(k).name], audio/max(max(abs(audio))), ... b@12: fsnew, 'BitsPerSample', bitdepthnew); % peak normalised! b@0: end b@0: end b@0: b@0: end