b@4: function prepare2listen(foldername) b@4: % PREPARE2LISTEN batch processes all audio files in a folder to prepare for b@4: % a listening test, by b@4: % - making sure everything is at the same sampling rate and bit depth; b@4: % - removing subsequent zeros at the start and the end; b@4: % - ensuring none of the files are equal to each other; b@4: % - clipping the desired region from the file; b@4: % - adding a fade in and fade out; and b@4: % - setting the loudness at a predefined value. b@4: % It then saves the result in a folder within the audio folder. b@0: % b@0: % by Brecht De Man at Centre for Digital Music, 5 June 2013 b@0: b@4: % PARAMETERS b@4: newfolder = 'fragments'; b@4: fs = 96000; % Hz b@4: bitdepth = 24; b@8: starttime = 76; % SR1 88; SR2 76 % seconds b@8: endtime = 152; % SR1 142; SR2 152 % seconds b@4: fadetime = 1; % seconds b@6: format = 'wav'; b@4: slash = '/'; b@4: b@6: target_loudness = -23; % dBLU (https://tech.ebu.ch/loudness) b@5: b@6: tic; % measure time b@6: list = dir([foldername '/*.' format]); % get files b@4: b@4: % remove hidden files from list, and copy to new folder b@4: % see http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/258220 b@6: newfoldername = [foldername slash newfolder]; % within original folder b@6: if ~exist(newfoldername, 'dir') % if it doesn't exist yet b@6: mkdir(foldername, newfolder); % make newfolder within folder b@6: end b@4: for k = length(list):-1:1 b@4: fname = list(k).name; b@4: if fname(1) == '.' b@4: list(k) = []; b@4: else b@4: copyfile([foldername slash list(k).name], ... b@4: [newfoldername slash list(k).name]); b@4: end b@4: end b@4: b@4: % set at same sampling rate and bit depth b@12: %disp(['-- Ensuring audio is at ' num2str(fs) 'Hz / ' ... b@6: num2str(bitdepth) ' bit...']); b@12: %batchresample(newfoldername, fs, bitdepth); b@6: % NOTE: clipfade(...) also tests sampling rate, but does not resample b@6: b@6: % find doubles b@6: disp('-- Checking for doubles...'); b@6: finddouble(newfoldername); b@4: b@4: % strip zeros off beginning and end b@6: disp('-- Stripping off leading and trailing zeros...'); b@4: stripzeros(newfoldername); b@4: b@6: % align all audio files b@6: disp('-- Aligning audio files...'); b@6: autoalign(newfoldername); b@4: b@4: % clip and fade desired fragments b@6: startmin = floor(starttime/60); b@6: startsec = mod(starttime, 60); b@6: endmin = floor(endtime/60); b@6: endsec = mod(endtime, 60); b@6: disp(['-- Fading in (' num2str(fadetime) 's) at ' num2str(startmin) ... b@6: ':' num2str(startsec) ', fading out (' num2str(fadetime) 's) at '... b@6: num2str(endmin) ':' num2str(endsec) '...']); b@5: clipfade(newfoldername, starttime, endtime, fadetime, fs, bitdepth); b@0: b@5: % open files and calculate their loudness, then adjust b@6: disp(['-- Setting each file at ' num2str(target_loudness) 'dBLU ...']); b@4: for i = 1:length(list) b@5: disp([newfoldername slash list(i).name]); % DEBUG b@6: audio = audioread([newfoldername slash list(i).name]); b@5: initial_loudness = getloudness( audio, fs, 'ITU', 0); b@5: b@5: % calculate difference in loudness b@5: difference_loudness = target_loudness - initial_loudness; b@5: b@5: % write back with adjusted loudness b@6: newaudio = 10^(difference_loudness/20) .* audio; b@5: audiowrite([newfoldername slash list(i).name],... b@6: newaudio, fs, 'BitsPerSample', bitdepth); b@4: end b@6: b@6: elapsed_time = toc; b@6: disp(['prepare2listen took ' num2str(elapsed_time) ... b@6: ' seconds on folder ' foldername]); b@0: end