annotate 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
rev   line source
b@4 1 function clipfade(folder, starttime, endtime, fadetime, fs, bitdepth)
b@0 2 % CLIPFADE clips and fades a fragment [start end] of all audio files in
b@0 3 % a folder.
b@0 4 %
b@0 5 % folder: path to all audio files (automatically selected)
b@0 6 % start: start time in seconds
b@0 7 % end: end time in seconds
b@0 8 %
b@0 9 % by Brecht De Man at Centre for Digital Music on 25 October 2013
b@0 10
b@4 11 if nargin < 6
b@6 12 bitdepth = 24;
b@0 13 end
b@4 14 slash = '/';
b@0 15
b@0 16 % list all audio files
b@0 17 list = dir([folder slash '*.wav']);
b@0 18
b@4 19 % remove hidden files from list
b@4 20 % see http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/258220
b@4 21 for k = length(list):-1:1
b@4 22 fname = list(k).name;
b@4 23 if fname(1) == '.'
b@4 24 list(k) = [ ];
b@4 25 end
b@4 26 end
b@0 27
b@0 28 for i = 1:length(list)
b@13 29 [audio,fsfile] = audioread([folder slash list(i).name], [floor(starttime*fs)+1 ceil(endtime*fs)]); % read part of file
b@0 30 assert(fsfile == fs); % check file has expected sampling rate
b@0 31
b@6 32 Nfade = fadetime*fs; % make fade vector (based on sampling rate)
b@6 33 fadevector = [(1:Nfade)/Nfade ones(1,length(audio)-2*Nfade) (Nfade:-1:1)/Nfade];
b@0 34
b@0 35 % apply fading and write to new folder
b@0 36 if size(audio,2) == 2 % if stereo
b@0 37 audiowrite([folder slash list(i).name], ... %[folder slash newFolder slash list(i).name]
b@6 38 [fadevector'.*audio(:,1) fadevector'.*audio(:,2)], fs, 'BitsPerSample', bitdepth);
b@0 39 else % if mono
b@0 40 audiowrite([folder slash list(i).name], ... %[folder slash newFolder slash list(i).name]
b@6 41 fadevector'.*audio, fs, 'BitsPerSample', bitdepth);
b@0 42 end
b@0 43 end