view aux/clipfade.m @ 4:b28ffd29e6e1

Audio file preparation for listening test
author Brecht De Man <b.deman@qmul.ac.uk>
date Wed, 19 Nov 2014 18:59:51 +0000
parents 4fd284285159
children ed0a8621b96a
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)
    if strcmp(list(i).name, 'bounce.wav')
        %disp(['     ' folder slash list(i).name]); % DEBUG 

        [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
end