annotate toolboxes/MIRtoolbox1.3.2/MIRToolbox/mireval.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function v = mireval(d,file,single,export)
wolffd@0 2 % mireval(d,filename) applies the mirdesign object d to the audio file
wolffd@0 3 % named filename.
wolffd@0 4 % mireval(d,'Folder') applied the mirdesign object to each audio files in
wolffd@0 5 % the current directory.
wolffd@0 6 % mireval(d,'Folders') applied the mirdesign object recursively to the
wolffd@0 7 % subfolders.
wolffd@0 8 % Optional argument: mireval(...,'Single') only keeps the first
wolffd@0 9 % output when several output are returned for a given mirdesign
wolffd@0 10 % object.
wolffd@0 11
wolffd@0 12 % mireval performs the actual evaluation of the design flowchart.
wolffd@0 13 % If 'Folder' is used, the evaluation is carried out for each audio file
wolffd@0 14 % successively.
wolffd@0 15 % If d is a structure or a cell array, evaluate each component
wolffd@0 16 % separately.
wolffd@0 17 % The evaluation starts with a top-down traversal of the design flowchart
wolffd@0 18 % (evaleach).
wolffd@0 19
wolffd@0 20 %if not(isa(d,'mirdesign'))
wolffd@0 21 % error('ERROR IN MIREVAL: the first input should be a flowchart (using ''Design'')')
wolffd@0 22 %end
wolffd@0 23 if not(ischar(file))
wolffd@0 24 error('ERROR IN MIREVAL: the second input should be a file name or ''Folder''')
wolffd@0 25 end
wolffd@0 26
wolffd@0 27 if nargin<3
wolffd@0 28 single = [];
wolffd@0 29 end
wolffd@0 30 if nargin<4
wolffd@0 31 export = [];
wolffd@0 32 end
wolffd@0 33
wolffd@0 34 % First, let's look at the content of the file(s): size, sampling rate,
wolffd@0 35 % etc.
wolffd@0 36 w = []; % Array containing the index positions of the starting and ending dates.
wolffd@0 37 s = getsize(d);
wolffd@0 38 ch = 1;
wolffd@0 39 if strcmpi(file,'Folder') || strcmpi(file,'Folders')
wolffd@0 40 [l w sr a] = evalfolder('',s,0,[],[],{},strcmpi(file,'Folders'));
wolffd@0 41 if l == 0
wolffd@0 42 disp('No sound file detected in this folder.')
wolffd@0 43 end
wolffd@0 44 elseif length(file)>3 && strcmpi(file(end-3:end),'.txt')
wolffd@0 45 a = importdata(file);
wolffd@0 46 l = length(a);
wolffd@0 47 for i = 1:l
wolffd@0 48 [di,tpi,fpi,fi] = mirread([],a{i},0,0,0);
wolffd@0 49 if not(isempty(s))
wolffd@0 50 interval = s(1:2);
wolffd@0 51 if s(3)
wolffd@0 52 interval = round(interval*fi)+1;
wolffd@0 53 end
wolffd@0 54 if s(4) == 1
wolffd@0 55 interval = interval+round(di/2);
wolffd@0 56 elseif s(4) == 2
wolffd@0 57 interval = interval+di;
wolffd@0 58 end
wolffd@0 59 w(:,i) = min(max(interval,1),di);
wolffd@0 60 else
wolffd@0 61 w(:,i) = [1;di];
wolffd@0 62 end
wolffd@0 63 if getsampling(d)
wolffd@0 64 sr(i) = getsampling(d);
wolffd@0 65 else
wolffd@0 66 sr(i) = fi;
wolffd@0 67 end
wolffd@0 68 end
wolffd@0 69 else
wolffd@0 70 l = 1;
wolffd@0 71 [d1,tp1,fp1,f1,b,n,ch] = mirread([],file,0,0,0);
wolffd@0 72 if length(s)>1
wolffd@0 73 interval = s(1:2)';
wolffd@0 74 if s(3)
wolffd@0 75 interval = round(interval*f1)+1;
wolffd@0 76 end
wolffd@0 77 if s(4) == 1
wolffd@0 78 interval = interval+round(d1/2);
wolffd@0 79 elseif s(4) == 2
wolffd@0 80 interval = interval+d1;
wolffd@0 81 end
wolffd@0 82 if d1 < interval(2)
wolffd@0 83 warning('WARNING IN MIRAUDIO: The temporal region to be extracted exceeds the temporal extent of the whole audio file.');
wolffd@0 84 end
wolffd@0 85 w = min(max(interval,1),d1);
wolffd@0 86 else
wolffd@0 87 w = [1;d1];
wolffd@0 88 end
wolffd@0 89 if isa(d,'mirdesign') && getsampling(d)
wolffd@0 90 sr = getsampling(d);
wolffd@0 91 else
wolffd@0 92 sr = f1;
wolffd@0 93 end
wolffd@0 94 a = {file};
wolffd@0 95 end
wolffd@0 96
wolffd@0 97 if not(l)
wolffd@0 98 v = [];
wolffd@0 99 return
wolffd@0 100 end
wolffd@0 101
wolffd@0 102 if isempty(export)
wolffd@0 103 y = cell(1,l);
wolffd@0 104 end
wolffd@0 105 order = 1:l;
wolffd@0 106 if isa(d,'mirdesign') && isequal(get(d,'Method'),@mirplay)
wolffd@0 107 op = get(d,'Option');
wolffd@0 108 if isfield(op,'inc')
wolffd@0 109 if not(isnumeric(op.inc))
wolffd@0 110 op.inc = mirgetdata(op.inc);
wolffd@0 111 end
wolffd@0 112 [unused order] = sort(op.inc);
wolffd@0 113 elseif isfield(op,'dec')
wolffd@0 114 if not(isnumeric(op.inc))
wolffd@0 115 op.inc = mirgetdata(op.inc);
wolffd@0 116 end
wolffd@0 117 [unused order] = sort(op.dec,'descend');
wolffd@0 118 end
wolffd@0 119 if isfield(op,'every')
wolffd@0 120 order = order(1:op.every:end);
wolffd@0 121 end
wolffd@0 122 order = order(:)';
wolffd@0 123 end
wolffd@0 124
wolffd@0 125 parallel = 0;
wolffd@0 126 if mirparallel
wolffd@0 127 try
wolffd@0 128 matlabpool;
wolffd@0 129 parallel = 1;
wolffd@0 130 mirwaitbar(0)
wolffd@0 131 mirverbose(0)
wolffd@0 132 end
wolffd@0 133 end
wolffd@0 134
wolffd@0 135 if parallel
wolffd@0 136 % The evaluation is carried out for each audio file successively
wolffd@0 137 % (or in parallel).
wolffd@0 138 parfor i = 1:l
wolffd@0 139 if l > 1
wolffd@0 140 fprintf('\n')
wolffd@0 141 display(['*** File # ',num2str(i),'/',num2str(l),': ',a{i}]);
wolffd@0 142 end
wolffd@0 143 tic
wolffd@0 144 yi = evalaudiofile(d,a{i},sr(i),w(:,i),{},0,i,single,'',ch);
wolffd@0 145 toc
wolffd@0 146 y{i} = yi;
wolffd@0 147 if not(isempty(export))
wolffd@0 148 if strncmpi(export,'Separately',10)
wolffd@0 149 filename = a{i};
wolffd@0 150 filename(filename == '/') = '.';
wolffd@0 151 filename = [filename export(11:end)];
wolffd@0 152 if i == 1
wolffd@0 153 mkdir('Backup');
wolffd@0 154 end
wolffd@0 155 mirexport(filename,yi);
wolffd@0 156 elseif i==1
wolffd@0 157 mirexport(export,yi);
wolffd@0 158 else
wolffd@0 159 mirexport(export,yi,'#add');
wolffd@0 160 end
wolffd@0 161 end
wolffd@0 162 clear yi
wolffd@0 163 end
wolffd@0 164 else
wolffd@0 165 % The evaluation is carried out for each audio file successively.
wolffd@0 166 isstat = isfield(d,'Stat');
wolffd@0 167 for i = 1:length(order)
wolffd@0 168 f = order(i);
wolffd@0 169 if l > 1
wolffd@0 170 fprintf('\n')
wolffd@0 171 display(['*** File # ',num2str(i),'/',num2str(l),': ',a{f}]);
wolffd@0 172 end
wolffd@0 173 tic
wolffd@0 174 yf = evalaudiofile(d,a{f},sr(f),w(:,f),{},0,f,single,'',ch);
wolffd@0 175 toc
wolffd@0 176 y{f} = yf;
wolffd@0 177 if not(isempty(export))
wolffd@0 178 if strncmpi(export,'Separately',10)
wolffd@0 179 filename = a{f};
wolffd@0 180 filename(filename == '/') = '.';
wolffd@0 181 filename = ['Backup/' filename export(11:end)];
wolffd@0 182 if i == 1
wolffd@0 183 mkdir('Backup');
wolffd@0 184 end
wolffd@0 185 mirexport(filename,yf);
wolffd@0 186 elseif i==1
wolffd@0 187 mirexport(export,yf);
wolffd@0 188 else
wolffd@0 189 mirexport(export,yf,'#add');
wolffd@0 190 end
wolffd@0 191 end
wolffd@0 192 clear yf
wolffd@0 193 end
wolffd@0 194 end
wolffd@0 195
wolffd@0 196 %if isempty(export)
wolffd@0 197 v = combineaudiofile(a,isstat,y{:});
wolffd@0 198 %else
wolffd@0 199 % v = [];
wolffd@0 200 %end
wolffd@0 201
wolffd@0 202
wolffd@0 203 function v = evalaudiofile(d,file,sampling,size,struc,istmp,index,single,name,ch)
wolffd@0 204 % Now let's perform the analysis (or analyses) on the different files.
wolffd@0 205 % If d is a structure or a cell array, evaluate each component
wolffd@0 206 % separately.
wolffd@0 207 if isstruct(d)
wolffd@0 208 v = struct;
wolffd@0 209 if istmp
wolffd@0 210 struc.tmp = struct;
wolffd@0 211 end
wolffd@0 212 isstat = isfield(d,'Stat');
wolffd@0 213 if isstat
wolffd@0 214 d = rmfield(d,'Stat');
wolffd@0 215 end
wolffd@0 216 fields = fieldnames(d);
wolffd@0 217 for fi = 1:length(fields)
wolffd@0 218 fieldname = fields{fi};
wolffd@0 219 field = d.(fieldname);
wolffd@0 220 display(['*******',fieldname,'******']);
wolffd@0 221 if isstat
wolffd@0 222 if isa(field,'mirstruct')
wolffd@0 223 field = set(field,'Stat',1);
wolffd@0 224 elseif isa(field,'mirdesign')
wolffd@0 225 field = mirstat(field,'FileNames',0);
wolffd@0 226 else
wolffd@0 227 field.Stat = 1;
wolffd@0 228 end
wolffd@0 229 end
wolffd@0 230 res = evalaudiofile(field,file,sampling,size,struc,istmp,index,...
wolffd@0 231 single,fieldname,ch);
wolffd@0 232 if not(isempty(single)) && not(isequal(single,0)) && ...
wolffd@0 233 iscell(res) && isa(field,'mirdesign')
wolffd@0 234 res = res{1};
wolffd@0 235 end
wolffd@0 236 v.(fieldname) = res;
wolffd@0 237 if istmp
wolffd@0 238 struc.tmp.(fieldname) = res;
wolffd@0 239 end
wolffd@0 240 if fi == 1
wolffd@0 241 if isfield(res,'Class')
wolffd@0 242 v.Class = res.Class;
wolffd@0 243 v.(fieldname) = rmfield(res,'Class');
wolffd@0 244 end
wolffd@0 245 end
wolffd@0 246 end
wolffd@0 247 if isfield(v,'tmp')
wolffd@0 248 v = rmfield(v,'tmp');
wolffd@0 249 end
wolffd@0 250 elseif iscell(d)
wolffd@0 251 l = length(d);
wolffd@0 252 v = cell(1,l);
wolffd@0 253 for j = 1:l
wolffd@0 254 v{j} = evalaudiofile(d{j},file,sampling,size,struc,istmp,index,...
wolffd@0 255 single,[name,num2str(j)],ch);
wolffd@0 256 end
wolffd@0 257 elseif isa(d,'mirstruct') && isempty(get(d,'Argin'))
wolffd@0 258 mirerror('MIRSTRUCT','You should always use tmp fields when using mirstruct. Else, just use struct.');
wolffd@0 259 else
wolffd@0 260 if get(d,'SeparateChannels')
wolffd@0 261 v = cell(1,ch);
wolffd@0 262 for i = 1:ch
wolffd@0 263 d = set(d,'File',file,'Sampling',sampling,'Size',size,...
wolffd@0 264 'Eval',1,'Index',index,'Struct',struc,'Channel',i);
wolffd@0 265 % For that particular file or this particular feature, let's begin the
wolffd@0 266 % actual evaluation process.
wolffd@0 267 v{i} = evaleach(d,single,name);
wolffd@0 268 % evaleach performs a top-down traversal of the design flowchart.
wolffd@0 269 end
wolffd@0 270 v = combinechannels(v);
wolffd@0 271 else
wolffd@0 272 d = set(d,'File',file,'Sampling',sampling,'Size',size,...
wolffd@0 273 'Eval',1,'Index',index,'Struct',struc);
wolffd@0 274 % For that particular file or this particular feature, let's begin the
wolffd@0 275 % actual evaluation process.
wolffd@0 276 v = evaleach(d,single,name);
wolffd@0 277 % evaleach performs a top-down traversal of the design flowchart.
wolffd@0 278 end
wolffd@0 279 end
wolffd@0 280
wolffd@0 281
wolffd@0 282 function y = combinechannels(c)
wolffd@0 283 y = c{1};
wolffd@0 284 v = get(y,'Data');
wolffd@0 285 for h = 2:length(c)
wolffd@0 286 d = get(c{h},'Data');
wolffd@0 287 for i = 1:length(d)
wolffd@0 288 if isa(y,'mirmidi')
wolffd@0 289 d{i}(:,3) = h;
wolffd@0 290 v{i} = sortrows([v{i};d{i}]);
wolffd@0 291 else
wolffd@0 292 for j = 1:length(d{i})
wolffd@0 293 v{i}{j}(:,:,h) = d{i}{j};
wolffd@0 294 end
wolffd@0 295 end
wolffd@0 296 end
wolffd@0 297 end
wolffd@0 298 y = set(y,'Data',v);
wolffd@0 299
wolffd@0 300
wolffd@0 301 function c = combineaudiofile(filename,isstat,varargin) % Combine output from several audio files into one single
wolffd@0 302 c = varargin{1}; % The (series of) input(s) related to the first audio file
wolffd@0 303 if isempty(c)
wolffd@0 304 return
wolffd@0 305 end
wolffd@0 306 if isstruct(c)
wolffd@0 307 %fields = fieldnames(c);
wolffd@0 308 for j = 1:length(varargin)
wolffd@0 309 if j == 1
wolffd@0 310 fields = fieldnames(varargin{1});
wolffd@0 311 else
wolffd@0 312 fields = union(fields,fieldnames(varargin{j}));
wolffd@0 313 end
wolffd@0 314 end
wolffd@0 315 for i = 1:length(fields)
wolffd@0 316 field = fields{i};
wolffd@0 317 v = {};
wolffd@0 318 for j = 1:length(varargin)
wolffd@0 319 if isfield(varargin{j},field)
wolffd@0 320 v{j} = varargin{j}.(field);
wolffd@0 321 else
wolffd@0 322 v{j} = NaN;
wolffd@0 323 end
wolffd@0 324 end
wolffd@0 325 c.(field) = combineaudiofile('',isstat,v{:});
wolffd@0 326 if strcmp(field,'Class')
wolffd@0 327 c.Class = c.Class{1};
wolffd@0 328 end
wolffd@0 329 end
wolffd@0 330 if not(isempty(filename)) && isstat
wolffd@0 331 c.FileNames = filename;
wolffd@0 332 end
wolffd@0 333 return
wolffd@0 334 end
wolffd@0 335 if ischar(c)
wolffd@0 336 c = varargin;
wolffd@0 337 return
wolffd@0 338 end
wolffd@0 339 if (not(iscell(c)) && not(isa(c,'mirdata')))
wolffd@0 340 for j = 1:length(varargin)
wolffd@0 341 if j == 1
wolffd@0 342 lv = size(varargin{j},1);
wolffd@0 343 else
wolffd@0 344 lv = max(lv,size(varargin{j},1));
wolffd@0 345 end
wolffd@0 346 end
wolffd@0 347 c = NaN(lv,length(varargin));
wolffd@0 348 for i = 1:length(varargin)
wolffd@0 349 if not(isempty(varargin{i}))
wolffd@0 350 c(1:length(varargin{i}),i) = varargin{i};
wolffd@0 351 end
wolffd@0 352 end
wolffd@0 353 return
wolffd@0 354 end
wolffd@0 355 if (iscell(c) && not(isa(c{1},'mirdata')))
wolffd@0 356 for i = 1:length(c)
wolffd@0 357 v = cell(1,nargin-2);
wolffd@0 358 for j = 1:nargin-2
wolffd@0 359 v{j} = varargin{j}{i};
wolffd@0 360 end
wolffd@0 361 c{i} = combineaudiofile(filename,isstat,v{:});
wolffd@0 362 end
wolffd@0 363 return
wolffd@0 364 end
wolffd@0 365 if not(iscell(c))
wolffd@0 366 c = {c};
wolffd@0 367 end
wolffd@0 368 nv = length(c); % The number of input variables for each different audio file
wolffd@0 369 for j = 1:nv % Combine files for each different input variable
wolffd@0 370 v = varargin;
wolffd@0 371 for i = 1:length(varargin)
wolffd@0 372 if iscell(v{i})
wolffd@0 373 v{i} = v{i}{j};
wolffd@0 374 end
wolffd@0 375 end
wolffd@0 376 if not(isempty(v)) && not(isempty(v{1}))
wolffd@0 377 c{j} = combine(v{:});
wolffd@0 378 end
wolffd@0 379 end
wolffd@0 380
wolffd@0 381
wolffd@0 382 function s = getsize(d)
wolffd@0 383 if isa(d,'mirstruct')
wolffd@0 384 d = get(d,'Data');
wolffd@0 385 if isempty(d)
wolffd@0 386 error('ERROR in MIREVAL: Your mirstruct object does not have any field (besides tmp).');
wolffd@0 387 s = 0;
wolffd@0 388 else
wolffd@0 389 s = getsize(d{1});
wolffd@0 390 end
wolffd@0 391 elseif isstruct(d)
wolffd@0 392 fields = fieldnames(d);
wolffd@0 393 s = getsize(d.(fields{1}));
wolffd@0 394 elseif iscell(d)
wolffd@0 395 s = getsize(d{1});
wolffd@0 396 else
wolffd@0 397 s = get(d,'Size'); % Starting and ending dates in seconds.
wolffd@0 398 end
wolffd@0 399
wolffd@0 400
wolffd@0 401 function d2 = sortnames(d,d2,n)
wolffd@0 402 if length(n) == 1
wolffd@0 403 d2(end+1) = d(1);
wolffd@0 404 return
wolffd@0 405 end
wolffd@0 406 first = zeros(1,length(n));
wolffd@0 407 for i = 1:length(n)
wolffd@0 408 if isempty(n{i})
wolffd@0 409 first(i) = -Inf;
wolffd@0 410 else
wolffd@0 411 ni = n{i}{1};
wolffd@0 412 if ischar(ni)
wolffd@0 413 first(i) = ni-10058;
wolffd@0 414 else
wolffd@0 415 first(i) = ni;
wolffd@0 416 end
wolffd@0 417 end
wolffd@0 418 end
wolffd@0 419 [o i] = sort(first);
wolffd@0 420 n = {n{i}};
wolffd@0 421 d = d(i);
wolffd@0 422 i = 0;
wolffd@0 423 while i<length(n)
wolffd@0 424 i = i+1;
wolffd@0 425 if isempty(n{i})
wolffd@0 426 d2(end+1) = d(i);
wolffd@0 427 else
wolffd@0 428 dmp = (d(i));
wolffd@0 429 tmp = {n{i}(2:end)};
wolffd@0 430 while i+1<=length(n) && n{i+1}{1} == n{i}{1};
wolffd@0 431 i = i+1;
wolffd@0 432 dmp(end+1) = d(i);
wolffd@0 433 tmp{end+1} = n{i}(2:end);
wolffd@0 434 end
wolffd@0 435 d2 = sortnames(dmp,d2,tmp);
wolffd@0 436 end
wolffd@0 437 end
wolffd@0 438
wolffd@0 439
wolffd@0 440 function [l w sr a] = evalfolder(path,s,l,w,sr,a,folders)
wolffd@0 441 if not(isempty(path))
wolffd@0 442 path = [path '/'];
wolffd@0 443 end
wolffd@0 444 dd = dir;
wolffd@0 445 dn = {dd.name};
wolffd@0 446 nn = cell(1,length(dn)); % Modified file names
wolffd@0 447 for i = 1:length(dn) % Each file name is considered
wolffd@0 448 j = 0;
wolffd@0 449 while j<length(dn{i}) % Each successive character is modified if necessary
wolffd@0 450 j = j+1;
wolffd@0 451 tmp = dn{i}(j) - '0';
wolffd@0 452 if tmp>=0 && tmp<=9
wolffd@0 453 while j+1<length(dn{i}) && dn{i}(j+1)>='0' && dn{i}(j+1)<='9'
wolffd@0 454 j = j+1;
wolffd@0 455 tmp = tmp*10 + (dn{i}(j)-'0');
wolffd@0 456 end
wolffd@0 457 else
wolffd@0 458 tmp = dn{i}(j);
wolffd@0 459 end
wolffd@0 460 nn{i}{end+1} = tmp;
wolffd@0 461 end
wolffd@0 462 end
wolffd@0 463 dd = sortnames(dd,[],nn);
wolffd@0 464 for i = 1:length(dd);
wolffd@0 465 nf = dd(i).name;
wolffd@0 466 if folders && dd(i).isdir
wolffd@0 467 if not(strcmp(nf(1),'.'))
wolffd@0 468 cd(dd(i).name)
wolffd@0 469 [l w sr a] = evalfolder([path nf],s,l,w,sr,a,1);
wolffd@0 470 %l = l + l2;
wolffd@0 471 %w = [w w2];
wolffd@0 472 %sr = [sr sr2];
wolffd@0 473 %a = [a a2];
wolffd@0 474 cd ..
wolffd@0 475 end
wolffd@0 476 else
wolffd@0 477 [di,tpi,fpi,fi,bi,ni] = mirread([],nf,0,1,0);
wolffd@0 478 if not(isempty(ni))
wolffd@0 479 l = l+1;
wolffd@0 480 if not(isempty(s))
wolffd@0 481 interval = s(1:2);
wolffd@0 482 if s(3)
wolffd@0 483 interval = round(interval*fi)+1;
wolffd@0 484 end
wolffd@0 485 if s(4) == 1
wolffd@0 486 interval = interval+round(di/2);
wolffd@0 487 elseif s(4) == 2
wolffd@0 488 interval = interval+di;
wolffd@0 489 end
wolffd@0 490 w(:,l) = min(max(interval,1),di);
wolffd@0 491 else
wolffd@0 492 w(:,l) = [1;di];
wolffd@0 493 end
wolffd@0 494 sr(l) = fi;
wolffd@0 495 a{l} = [path ni];
wolffd@0 496 end
wolffd@0 497 end
wolffd@0 498 end