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