view aux/prepare2listen.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 85bff3d1b6fe
line wrap: on
line source
function prepare2listen(foldername)
% PREPARE2LISTEN batch processes all audio files in a folder to prepare for
% a listening test, by 
% - making sure everything is at the same sampling rate and bit depth;
% - removing subsequent zeros at the start and the end;
% - ensuring none of the files are equal to each other; 
% - clipping the desired region from the file; 
% - adding a fade in and fade out; and
% - setting the loudness at a predefined value. 
% It then saves the result in a folder within the audio folder. 
%
% by Brecht De Man at Centre for Digital Music, 5 June 2013

    % PARAMETERS
    newfolder   = 'fragments';
    fs          = 96000;    % Hz
    bitdepth    = 24; 
    starttime   = 0;        % seconds
    endtime     = 10;       % seconds
    fadetime    = 1;        % seconds
    loudness    = -18;      % dBLU
    format      = '.wav';
    slash       = '/';
    
    % get files
    list = dir([foldername '/*.' format]);
    
    % remove hidden files from list, and copy to new folder
    % see http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/258220
    newfoldername = [foldername slash newfolder];
    for k = length(list):-1:1
        fname = list(k).name;
        if fname(1) == '.'
            list(k) = [];
        else
            copyfile([foldername slash list(k).name], ...
                [newfoldername slash list(k).name]);
        end
    end
    
    % set at same sampling rate and bit depth
    batchresample(newfoldername, fs, bitdepth); 
    
    % strip zeros off beginning and end
    stripzeros(newfoldername); 
    
    % find doubles
    finddouble(newfoldername); 
    
    % clip and fade desired fragments
    clipfade(folder, starttime, endtime, fadetime, fs, bitdepth); 

    % open files and calculate minimum loudness
    MIN = 0;
    x = struct([]);
    for i = 1:length(list)
        disp([foldername newfolder slash list(i).name]); % DEBUG
        [x{i}.audio,fs] = audioread([foldername list(i).name]);
        x{i}.loudness   = loudness_itu(x{i}.audio, fs);
        MIN = min(MIN, floor(x{i}.loudness)); % compute minimum loudness
    end

    % make folder inside audio file folder
    if ~exist([foldername newfolder], 'dir') % make new folder if not there
        mkdir(foldername, newfolder);
    end

    % equalise loudness
    MAX = 0;
    for i = 1:length(list)
        x{i}.equalised = loudness_match(x{i}.audio, fs, MIN)*x{i}.audio;
        MAX = max(MAX, max(max(abs(x{i}.equalised))));
    end

    % normalise (keeping relative loudness) and save
    for i = 1:length(list)
        audiowrite([foldername newfolder slash list(i).name], x{i}.equalised/MAX, ...
            fs, 'BitsPerSample', 24);
    end

end