view aux/prepare2listen.m @ 5:85bff3d1b6fe

Equalise loudness in prepare2listen.m
author Brecht De Man <b.deman@qmul.ac.uk>
date Sun, 23 Nov 2014 23:24:35 +0000
parents b28ffd29e6e1
children ed0a8621b96a
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
    format      = '.wav';
    slash       = '/';
    
    target_loudness    = -18;      % dBLU
    
    % 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(newfoldername, starttime, endtime, fadetime, fs, bitdepth); 

    % open files and calculate their loudness, then adjust
    for i = 1:length(list)
        disp([newfoldername slash list(i).name]); % DEBUG
        audio = audioread([newfoldername list(i).name]);
        initial_loudness   = getloudness( audio, fs, 'ITU', 0); 
        
        % calculate difference in loudness
        difference_loudness = target_loudness - initial_loudness; 
        
        % write back with adjusted loudness
        audiowrite([newfoldername slash list(i).name],...
            10^(difference_loudness/10) .* audio, ...
            fs, 'BitsPerSample', bitdepth);
    end

end