annotate demo_batchProcessing.m @ 13:1a058eb51073

degradation folder re-sort; towards more comprehensive batch processing demo
author matthiasm
date Thu, 24 Oct 2013 20:26:35 +0100
parents 9d682f5e3927
children c3a002ed2815
rev   line source
matthiasm@0 1 function demo_batchProcessing()
matthiasm@0 2
matthiasm@0 3 % This is an example script to process a whole dataset using the Audio
matthiasm@0 4 % Degradation Toolbox including code to translate given ground truth
matthiasm@0 5 % annotations.
matthiasm@0 6
matthiasm@0 7 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
matthiasm@0 8 % Audio Degradation Toolbox
matthiasm@0 9 %
matthiasm@0 10 % Centre for Digital Music, Queen Mary University of London.
matthiasm@0 11 % This file copyright 2013 Sebastian Ewert, Matthias Mauch and QMUL.
matthiasm@0 12 %
matthiasm@0 13 % This program is free software; you can redistribute it and/or
matthiasm@0 14 % modify it under the terms of the GNU General Public License as
matthiasm@0 15 % published by the Free Software Foundation; either version 2 of the
matthiasm@0 16 % License, or (at your option) any later version. See the file
matthiasm@0 17 % COPYING included with this distribution for more information.
matthiasm@0 18 %
matthiasm@0 19 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
matthiasm@0 20
matthiasm@13 21 addpath(genpath(fullfile(pwd,'AudioDegradationToolbox')));
matthiasm@0 22
matthiasm@0 23 % the following file should list all the files to process. Every line
matthiasm@0 24 % should specify one audio file, one audio file and a ground truth file in
matthiasm@0 25 % CSV format, or just the ground truth file. This example scripts treats
matthiasm@0 26 % the first column of a CSV file as time information that needs to be
matthiasm@0 27 % adjusted. If this is not correct, the script needs to be adapted.
matthiasm@0 28 filename_listOfFiles = 'listOfFiles.txt';
matthiasm@0 29 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
matthiasm@0 30 % 1 testdata/472TNA3M_snippet.wav
matthiasm@0 31 % 2 testdata/clarinet.wav
matthiasm@0 32 % 3 testdata/p009m_drum.wav
matthiasm@0 33 % 4 testdata/RWC-C08.wav ; testdata/RWC-C08.csv
matthiasm@0 34 % 5 testdata/p009m_drum.csv
matthiasm@0 35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
matthiasm@0 36 % line 1-3: just process audio, line 4: process audio and ground truth data
matthiasm@0 37 % at the same time, line 5: process only ground truth data
matthiasm@0 38
matthiasm@0 39 % destination directory
matthiasm@0 40 outputDirectory = 'demoOutput/';
matthiasm@0 41
matthiasm@13 42 % desired degradations
matthiasm@13 43 degradationnames = {'liveRecording', ...
matthiasm@13 44 'pubEnvironment', ...
matthiasm@13 45 'radioBroadcast', ...
matthiasm@13 46 'smartPhonePlayback', ...
matthiasm@13 47 'smartPhoneRecording', ...
matthiasm@13 48 'strongMp3Compression', ...
matthiasm@13 49 'vinylRecording', ...
matthiasm@13 50 'unit_addSound', ...
matthiasm@13 51 'unit_addNoise'};
matthiasm@13 52 nDegradation = length(degradationnames);
matthiasm@0 53
matthiasm@0 54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
matthiasm@0 55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
matthiasm@0 56 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
matthiasm@0 57
matthiasm@0 58 % read list of files to process
matthiasm@0 59 fid = fopen(filename_listOfFiles);
matthiasm@0 60 files=textscan(fid,'%s %s','delimiter',';');
matthiasm@0 61 fclose(fid);
matthiasm@0 62 numLines = length(files{1});
matthiasm@0 63 if length(files)>1 && (length(files{2}) < numLines)
matthiasm@0 64 files{2}{length(files{2})+1:numLines} = '';
matthiasm@0 65 end
matthiasm@0 66 files{1} = strtrim(files{1});
matthiasm@0 67 files{2} = strtrim(files{2});
matthiasm@0 68
matthiasm@0 69 % create outputDirectory if necessary
matthiasm@0 70 if ~exist(outputDirectory,'dir')
matthiasm@0 71 mkdir(outputDirectory);
matthiasm@0 72 end
matthiasm@0 73
matthiasm@0 74 % loop over files to process
matthiasm@0 75 parfor k=1:numLines % use this if you have the parallel computing toolbox
matthiasm@0 76 % for k=1:numLines
matthiasm@0 77 [path1,name1,ext1] = fileparts(files{1}{k});
matthiasm@0 78 [path2,name2,ext2] = fileparts(files{2}{k});
matthiasm@0 79
matthiasm@0 80 % check whether a wav and/or csv file is given
matthiasm@0 81 audiofilename = []; audiopath = []; csvfilename=[]; csvpath = [];
matthiasm@0 82 if strcmpi(ext1,'.csv')
matthiasm@0 83 csvpath = path1;
matthiasm@0 84 csvfilename = [name1,ext1];
matthiasm@0 85 elseif strcmpi(ext1,'.wav')
matthiasm@0 86 audiopath = path1;
matthiasm@0 87 audiofilename = [name1,ext1];
matthiasm@0 88 end
matthiasm@0 89 if strcmpi(ext2,'.csv')
matthiasm@0 90 csvpath = path2;
matthiasm@0 91 csvfilename = [name2,ext2];
matthiasm@0 92 elseif strcmpi(ext2,'.wav')
matthiasm@0 93 audiopath = path2;
matthiasm@0 94 audiofilename = [name2,ext2];
matthiasm@0 95 end
matthiasm@0 96
matthiasm@0 97 % Read audio and CSV data
matthiasm@0 98 f_audio = []; samplingFreq = []; nbits = [];
matthiasm@0 99 timepositions_beforeDegr = []; remainingColumns = [];
matthiasm@0 100 if ~isempty(audiofilename)
matthiasm@0 101 fprintf('Reading %s\n',audiofilename);
matthiasm@0 102 [f_audio,samplingFreq,nbits] = wavread(fullfile(audiopath,audiofilename));
matthiasm@0 103 end
matthiasm@0 104 if ~isempty(csvfilename)
matthiasm@0 105 fprintf('Reading %s\n',csvfilename);
matthiasm@0 106
matthiasm@0 107 fid = fopen(fullfile(csvpath,csvfilename));
matthiasm@0 108 linestring = fgetl(fid);
matthiasm@0 109 numberOfColumns = length(strfind(linestring,','))+1;
matthiasm@0 110 fclose(fid);
matthiasm@0 111
matthiasm@0 112 fid = fopen(fullfile(csvpath,csvfilename));
matthiasm@0 113 data = textscan(fid,['%f' repmat('%s', 1, numberOfColumns-1) '%*[^\n]'], 'delimiter', ',', 'collectoutput', true);
matthiasm@0 114 fclose(fid);
matthiasm@0 115
matthiasm@0 116 timepositions_beforeDegr = data{1};
matthiasm@0 117 remainingColumns = data{2};
matthiasm@0 118 end
matthiasm@0 119
matthiasm@13 120 % apply degradations
matthiasm@0 121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
matthiasm@13 122 for iDegradation = 1:nDegradation
matthiasm@13 123 degradationname = degradationnames{iDegradation};
matthiasm@13 124 PathToOutput = fullfile(outputDirectory,degradationname);
matthiasm@13 125 if ~exist(PathToOutput,'dir') mkdir(PathToOutput); end
matthiasm@13 126 [f_audio_out,timepositions_afterDegr] = applyDegradation(degradationname, f_audio, samplingFreq, timepositions_beforeDegr);
matthiasm@13 127
matthiasm@13 128 if ~isempty(audiofilename)
matthiasm@13 129 wavwrite(f_audio_out,samplingFreq,nbits,fullfile(PathToOutput,audiofilename));
matthiasm@13 130 end
matthiasm@13 131 if ~isempty(csvfilename)
matthiasm@13 132 writeCsvFile(fullfile(PathToOutput,csvfilename),timepositions_afterDegr,remainingColumns);
matthiasm@13 133 end
matthiasm@0 134 end
matthiasm@0 135 end
matthiasm@0 136
matthiasm@0 137 end
matthiasm@0 138
matthiasm@0 139
matthiasm@0 140 function writeCsvFile(filename, cols_numeric, remainingColsCell)
matthiasm@0 141
matthiasm@0 142 numberOfAdditionalColumns = size(remainingColsCell,2);
matthiasm@0 143
matthiasm@0 144 fid = fopen(filename,'w');
matthiasm@0 145 for m=1:size(cols_numeric,1)
matthiasm@0 146 fprintf(fid,'%f',cols_numeric(m,1));
matthiasm@0 147 for n=2:size(cols_numeric,2)
matthiasm@0 148 fprintf(fid,' %f',cols_numeric(m,n));
matthiasm@0 149 end
matthiasm@0 150 for n=1:numberOfAdditionalColumns
matthiasm@0 151 fprintf(fid,' %s',remainingColsCell{m,n});
matthiasm@0 152 end
matthiasm@0 153 fprintf(fid,'\n');
matthiasm@0 154 end
matthiasm@0 155 fclose(fid);
matthiasm@0 156
matthiasm@0 157 end
matthiasm@0 158
matthiasm@0 159