view aux/batchResample.m @ 15:24be5e9ce25b tip

Update README
author Brecht De Man <brecht.deman@bcu.ac.uk>
date Thu, 20 Sep 2018 12:23:20 +0200
parents 866c5da4d357
children
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
    
    % 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);
                    
                    % exactly N seconds long
                    audio = [audio; zeros(fs - mod(size(audio,1), fs), size(audio, 2))];
                    
                    H = dsp.FIRRateConverter('InterpolationFactor', fsnew, 'DecimationFactor', fs);
                    audio = step(H, audio); % resample
                end
                audiowrite([foldername '/' files(k).name], audio/max(max(abs(audio))), ...
                    fsnew, 'BitsPerSample', bitdepthnew); % peak normalised! 
            end
    end

end