view aux/clipfade.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 22964a1dc292
children
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], [floor(starttime*fs)+1 ceil(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