view aux/batchResample.m @ 12:866c5da4d357

Convert .mat output APE to .xml output format Web Audio Evaluation Tool (see code.soundsoftware.ac.uk/projects/webaudioevaluationtool)
author Brecht De Man <b.deman@qmul.ac.uk>
date Fri, 26 Jun 2015 20:56:42 +0100
parents 2afd6ff39f08
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