view aux/clipfade.m @ 0:4fd284285159

Adding listening test plus some helpful functions and scripts.
author Brecht <b.deman@qmul.ac.uk>
date Thu, 24 Apr 2014 23:53:31 +0100
parents
children b28ffd29e6e1
line wrap: on
line source
function clipfade(folder, startTime, endTime, fs)
% 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 < 4
    fs = 96000;
end

fadeTime = 1; % fade time in seconds
bitDepth = 16;
slash    = '/'; % depending on OS

%newFolder = 'Clips'; % folder where output files will be stored
% MODIFICATION: store in place, do not keep unclipped files!

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

% make new folder if not present yet
% if ~exist([folder slash newFolder], 'dir') % make output folder if not there
%     mkdir(folder, newFolder);
% 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