view aux/clipfade.m @ 8:2afd6ff39f08

prepare2listen fixes
author Brecht De Man <b.deman@qmul.ac.uk>
date Fri, 28 Nov 2014 00:52:12 +0000
parents ed0a8621b96a
children 22964a1dc292
line wrap: on
line source
function clipfade(folder, starttime, endtime, fadetime, fs, bitdepth)
% CLIPFADE clips and fades a fragment [start end] of all audio files in
% a folder. 
%
% folder: path to all audio files (automatically selected)
% start:  start time in seconds
% end:    end time in seconds
%
% by Brecht De Man at Centre for Digital Music on 25 October 2013

if nargin < 6
    bitdepth = 24;
end
slash    = '/';

% list all audio files
list = dir([folder slash '*.wav']);

% remove hidden files from list
% see http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/258220
for k = length(list):-1:1
    fname = list(k).name;
    if fname(1) == '.'
        list(k) = [ ];
    end
end

for i = 1:length(list)
        [audio,fsfile] = audioread([folder slash list(i).name], [starttime*fs+1 endtime*fs]); % read part of file
        assert(fsfile == fs); % check file has expected sampling rate

        Nfade = fadetime*fs; % make fade vector (based on sampling rate)
        fadevector = [(1:Nfade)/Nfade ones(1,length(audio)-2*Nfade) (Nfade:-1:1)/Nfade];

        % apply fading and write to new folder
        if size(audio,2) == 2 % if stereo
            audiowrite([folder slash list(i).name], ... %[folder slash newFolder slash list(i).name]
                [fadevector'.*audio(:,1) fadevector'.*audio(:,2)], fs, 'BitsPerSample', bitdepth);
        else % if mono
            audiowrite([folder slash list(i).name], ... %[folder slash newFolder slash list(i).name]
                fadevector'.*audio, fs, 'BitsPerSample', bitdepth);
        end
end