wolffd@0: function v = mireval(d,file,single,export) wolffd@0: % mireval(d,filename) applies the mirdesign object d to the audio file wolffd@0: % named filename. wolffd@0: % mireval(d,'Folder') applied the mirdesign object to each audio files in wolffd@0: % the current directory. wolffd@0: % mireval(d,'Folders') applied the mirdesign object recursively to the wolffd@0: % subfolders. wolffd@0: % Optional argument: mireval(...,'Single') only keeps the first wolffd@0: % output when several output are returned for a given mirdesign wolffd@0: % object. wolffd@0: wolffd@0: % mireval performs the actual evaluation of the design flowchart. wolffd@0: % If 'Folder' is used, the evaluation is carried out for each audio file wolffd@0: % successively. wolffd@0: % If d is a structure or a cell array, evaluate each component wolffd@0: % separately. wolffd@0: % The evaluation starts with a top-down traversal of the design flowchart wolffd@0: % (evaleach). wolffd@0: wolffd@0: %if not(isa(d,'mirdesign')) wolffd@0: % error('ERROR IN MIREVAL: the first input should be a flowchart (using ''Design'')') wolffd@0: %end wolffd@0: if not(ischar(file)) wolffd@0: error('ERROR IN MIREVAL: the second input should be a file name or ''Folder''') wolffd@0: end wolffd@0: wolffd@0: if nargin<3 wolffd@0: single = []; wolffd@0: end wolffd@0: if nargin<4 wolffd@0: export = []; wolffd@0: end wolffd@0: wolffd@0: % First, let's look at the content of the file(s): size, sampling rate, wolffd@0: % etc. wolffd@0: w = []; % Array containing the index positions of the starting and ending dates. wolffd@0: s = getsize(d); wolffd@0: ch = 1; wolffd@0: if strcmpi(file,'Folder') || strcmpi(file,'Folders') wolffd@0: [l w sr a] = evalfolder('',s,0,[],[],{},strcmpi(file,'Folders')); wolffd@0: if l == 0 wolffd@0: disp('No sound file detected in this folder.') wolffd@0: end wolffd@0: elseif length(file)>3 && strcmpi(file(end-3:end),'.txt') wolffd@0: a = importdata(file); wolffd@0: l = length(a); wolffd@0: for i = 1:l wolffd@0: [di,tpi,fpi,fi] = mirread([],a{i},0,0,0); wolffd@0: if not(isempty(s)) wolffd@0: interval = s(1:2); wolffd@0: if s(3) wolffd@0: interval = round(interval*fi)+1; wolffd@0: end wolffd@0: if s(4) == 1 wolffd@0: interval = interval+round(di/2); wolffd@0: elseif s(4) == 2 wolffd@0: interval = interval+di; wolffd@0: end wolffd@0: w(:,i) = min(max(interval,1),di); wolffd@0: else wolffd@0: w(:,i) = [1;di]; wolffd@0: end wolffd@0: if getsampling(d) wolffd@0: sr(i) = getsampling(d); wolffd@0: else wolffd@0: sr(i) = fi; wolffd@0: end wolffd@0: end wolffd@0: else wolffd@0: l = 1; wolffd@0: [d1,tp1,fp1,f1,b,n,ch] = mirread([],file,0,0,0); wolffd@0: if length(s)>1 wolffd@0: interval = s(1:2)'; wolffd@0: if s(3) wolffd@0: interval = round(interval*f1)+1; wolffd@0: end wolffd@0: if s(4) == 1 wolffd@0: interval = interval+round(d1/2); wolffd@0: elseif s(4) == 2 wolffd@0: interval = interval+d1; wolffd@0: end wolffd@0: if d1 < interval(2) wolffd@0: warning('WARNING IN MIRAUDIO: The temporal region to be extracted exceeds the temporal extent of the whole audio file.'); wolffd@0: end wolffd@0: w = min(max(interval,1),d1); wolffd@0: else wolffd@0: w = [1;d1]; wolffd@0: end wolffd@0: if isa(d,'mirdesign') && getsampling(d) wolffd@0: sr = getsampling(d); wolffd@0: else wolffd@0: sr = f1; wolffd@0: end wolffd@0: a = {file}; wolffd@0: end wolffd@0: wolffd@0: if not(l) wolffd@0: v = []; wolffd@0: return wolffd@0: end wolffd@0: wolffd@0: if isempty(export) wolffd@0: y = cell(1,l); wolffd@0: end wolffd@0: order = 1:l; wolffd@0: if isa(d,'mirdesign') && isequal(get(d,'Method'),@mirplay) wolffd@0: op = get(d,'Option'); wolffd@0: if isfield(op,'inc') wolffd@0: if not(isnumeric(op.inc)) wolffd@0: op.inc = mirgetdata(op.inc); wolffd@0: end wolffd@0: [unused order] = sort(op.inc); wolffd@0: elseif isfield(op,'dec') wolffd@0: if not(isnumeric(op.inc)) wolffd@0: op.inc = mirgetdata(op.inc); wolffd@0: end wolffd@0: [unused order] = sort(op.dec,'descend'); wolffd@0: end wolffd@0: if isfield(op,'every') wolffd@0: order = order(1:op.every:end); wolffd@0: end wolffd@0: order = order(:)'; wolffd@0: end wolffd@0: wolffd@0: parallel = 0; wolffd@0: if mirparallel wolffd@0: try wolffd@0: matlabpool; wolffd@0: parallel = 1; wolffd@0: mirwaitbar(0) wolffd@0: mirverbose(0) wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: if parallel wolffd@0: % The evaluation is carried out for each audio file successively wolffd@0: % (or in parallel). wolffd@0: parfor i = 1:l wolffd@0: if l > 1 wolffd@0: fprintf('\n') wolffd@0: display(['*** File # ',num2str(i),'/',num2str(l),': ',a{i}]); wolffd@0: end wolffd@0: tic wolffd@0: yi = evalaudiofile(d,a{i},sr(i),w(:,i),{},0,i,single,'',ch); wolffd@0: toc wolffd@0: y{i} = yi; wolffd@0: if not(isempty(export)) wolffd@0: if strncmpi(export,'Separately',10) wolffd@0: filename = a{i}; wolffd@0: filename(filename == '/') = '.'; wolffd@0: filename = [filename export(11:end)]; wolffd@0: if i == 1 wolffd@0: mkdir('Backup'); wolffd@0: end wolffd@0: mirexport(filename,yi); wolffd@0: elseif i==1 wolffd@0: mirexport(export,yi); wolffd@0: else wolffd@0: mirexport(export,yi,'#add'); wolffd@0: end wolffd@0: end wolffd@0: clear yi wolffd@0: end wolffd@0: else wolffd@0: % The evaluation is carried out for each audio file successively. wolffd@0: isstat = isfield(d,'Stat'); wolffd@0: for i = 1:length(order) wolffd@0: f = order(i); wolffd@0: if l > 1 wolffd@0: fprintf('\n') wolffd@0: display(['*** File # ',num2str(i),'/',num2str(l),': ',a{f}]); wolffd@0: end wolffd@0: tic wolffd@0: yf = evalaudiofile(d,a{f},sr(f),w(:,f),{},0,f,single,'',ch); wolffd@0: toc wolffd@0: y{f} = yf; wolffd@0: if not(isempty(export)) wolffd@0: if strncmpi(export,'Separately',10) wolffd@0: filename = a{f}; wolffd@0: filename(filename == '/') = '.'; wolffd@0: filename = ['Backup/' filename export(11:end)]; wolffd@0: if i == 1 wolffd@0: mkdir('Backup'); wolffd@0: end wolffd@0: mirexport(filename,yf); wolffd@0: elseif i==1 wolffd@0: mirexport(export,yf); wolffd@0: else wolffd@0: mirexport(export,yf,'#add'); wolffd@0: end wolffd@0: end wolffd@0: clear yf wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: %if isempty(export) wolffd@0: v = combineaudiofile(a,isstat,y{:}); wolffd@0: %else wolffd@0: % v = []; wolffd@0: %end wolffd@0: wolffd@0: wolffd@0: function v = evalaudiofile(d,file,sampling,size,struc,istmp,index,single,name,ch) wolffd@0: % Now let's perform the analysis (or analyses) on the different files. wolffd@0: % If d is a structure or a cell array, evaluate each component wolffd@0: % separately. wolffd@0: if isstruct(d) wolffd@0: v = struct; wolffd@0: if istmp wolffd@0: struc.tmp = struct; wolffd@0: end wolffd@0: isstat = isfield(d,'Stat'); wolffd@0: if isstat wolffd@0: d = rmfield(d,'Stat'); wolffd@0: end wolffd@0: fields = fieldnames(d); wolffd@0: for fi = 1:length(fields) wolffd@0: fieldname = fields{fi}; wolffd@0: field = d.(fieldname); wolffd@0: display(['*******',fieldname,'******']); wolffd@0: if isstat wolffd@0: if isa(field,'mirstruct') wolffd@0: field = set(field,'Stat',1); wolffd@0: elseif isa(field,'mirdesign') wolffd@0: field = mirstat(field,'FileNames',0); wolffd@0: else wolffd@0: field.Stat = 1; wolffd@0: end wolffd@0: end wolffd@0: res = evalaudiofile(field,file,sampling,size,struc,istmp,index,... wolffd@0: single,fieldname,ch); wolffd@0: if not(isempty(single)) && not(isequal(single,0)) && ... wolffd@0: iscell(res) && isa(field,'mirdesign') wolffd@0: res = res{1}; wolffd@0: end wolffd@0: v.(fieldname) = res; wolffd@0: if istmp wolffd@0: struc.tmp.(fieldname) = res; wolffd@0: end wolffd@0: if fi == 1 wolffd@0: if isfield(res,'Class') wolffd@0: v.Class = res.Class; wolffd@0: v.(fieldname) = rmfield(res,'Class'); wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: if isfield(v,'tmp') wolffd@0: v = rmfield(v,'tmp'); wolffd@0: end wolffd@0: elseif iscell(d) wolffd@0: l = length(d); wolffd@0: v = cell(1,l); wolffd@0: for j = 1:l wolffd@0: v{j} = evalaudiofile(d{j},file,sampling,size,struc,istmp,index,... wolffd@0: single,[name,num2str(j)],ch); wolffd@0: end wolffd@0: elseif isa(d,'mirstruct') && isempty(get(d,'Argin')) wolffd@0: mirerror('MIRSTRUCT','You should always use tmp fields when using mirstruct. Else, just use struct.'); wolffd@0: else wolffd@0: if get(d,'SeparateChannels') wolffd@0: v = cell(1,ch); wolffd@0: for i = 1:ch wolffd@0: d = set(d,'File',file,'Sampling',sampling,'Size',size,... wolffd@0: 'Eval',1,'Index',index,'Struct',struc,'Channel',i); wolffd@0: % For that particular file or this particular feature, let's begin the wolffd@0: % actual evaluation process. wolffd@0: v{i} = evaleach(d,single,name); wolffd@0: % evaleach performs a top-down traversal of the design flowchart. wolffd@0: end wolffd@0: v = combinechannels(v); wolffd@0: else wolffd@0: d = set(d,'File',file,'Sampling',sampling,'Size',size,... wolffd@0: 'Eval',1,'Index',index,'Struct',struc); wolffd@0: % For that particular file or this particular feature, let's begin the wolffd@0: % actual evaluation process. wolffd@0: v = evaleach(d,single,name); wolffd@0: % evaleach performs a top-down traversal of the design flowchart. wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: wolffd@0: function y = combinechannels(c) wolffd@0: y = c{1}; wolffd@0: v = get(y,'Data'); wolffd@0: for h = 2:length(c) wolffd@0: d = get(c{h},'Data'); wolffd@0: for i = 1:length(d) wolffd@0: if isa(y,'mirmidi') wolffd@0: d{i}(:,3) = h; wolffd@0: v{i} = sortrows([v{i};d{i}]); wolffd@0: else wolffd@0: for j = 1:length(d{i}) wolffd@0: v{i}{j}(:,:,h) = d{i}{j}; wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: y = set(y,'Data',v); wolffd@0: wolffd@0: wolffd@0: function c = combineaudiofile(filename,isstat,varargin) % Combine output from several audio files into one single wolffd@0: c = varargin{1}; % The (series of) input(s) related to the first audio file wolffd@0: if isempty(c) wolffd@0: return wolffd@0: end wolffd@0: if isstruct(c) wolffd@0: %fields = fieldnames(c); wolffd@0: for j = 1:length(varargin) wolffd@0: if j == 1 wolffd@0: fields = fieldnames(varargin{1}); wolffd@0: else wolffd@0: fields = union(fields,fieldnames(varargin{j})); wolffd@0: end wolffd@0: end wolffd@0: for i = 1:length(fields) wolffd@0: field = fields{i}; wolffd@0: v = {}; wolffd@0: for j = 1:length(varargin) wolffd@0: if isfield(varargin{j},field) wolffd@0: v{j} = varargin{j}.(field); wolffd@0: else wolffd@0: v{j} = NaN; wolffd@0: end wolffd@0: end wolffd@0: c.(field) = combineaudiofile('',isstat,v{:}); wolffd@0: if strcmp(field,'Class') wolffd@0: c.Class = c.Class{1}; wolffd@0: end wolffd@0: end wolffd@0: if not(isempty(filename)) && isstat wolffd@0: c.FileNames = filename; wolffd@0: end wolffd@0: return wolffd@0: end wolffd@0: if ischar(c) wolffd@0: c = varargin; wolffd@0: return wolffd@0: end wolffd@0: if (not(iscell(c)) && not(isa(c,'mirdata'))) wolffd@0: for j = 1:length(varargin) wolffd@0: if j == 1 wolffd@0: lv = size(varargin{j},1); wolffd@0: else wolffd@0: lv = max(lv,size(varargin{j},1)); wolffd@0: end wolffd@0: end wolffd@0: c = NaN(lv,length(varargin)); wolffd@0: for i = 1:length(varargin) wolffd@0: if not(isempty(varargin{i})) wolffd@0: c(1:length(varargin{i}),i) = varargin{i}; wolffd@0: end wolffd@0: end wolffd@0: return wolffd@0: end wolffd@0: if (iscell(c) && not(isa(c{1},'mirdata'))) wolffd@0: for i = 1:length(c) wolffd@0: v = cell(1,nargin-2); wolffd@0: for j = 1:nargin-2 wolffd@0: v{j} = varargin{j}{i}; wolffd@0: end wolffd@0: c{i} = combineaudiofile(filename,isstat,v{:}); wolffd@0: end wolffd@0: return wolffd@0: end wolffd@0: if not(iscell(c)) wolffd@0: c = {c}; wolffd@0: end wolffd@0: nv = length(c); % The number of input variables for each different audio file wolffd@0: for j = 1:nv % Combine files for each different input variable wolffd@0: v = varargin; wolffd@0: for i = 1:length(varargin) wolffd@0: if iscell(v{i}) wolffd@0: v{i} = v{i}{j}; wolffd@0: end wolffd@0: end wolffd@0: if not(isempty(v)) && not(isempty(v{1})) wolffd@0: c{j} = combine(v{:}); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: wolffd@0: function s = getsize(d) wolffd@0: if isa(d,'mirstruct') wolffd@0: d = get(d,'Data'); wolffd@0: if isempty(d) wolffd@0: error('ERROR in MIREVAL: Your mirstruct object does not have any field (besides tmp).'); wolffd@0: s = 0; wolffd@0: else wolffd@0: s = getsize(d{1}); wolffd@0: end wolffd@0: elseif isstruct(d) wolffd@0: fields = fieldnames(d); wolffd@0: s = getsize(d.(fields{1})); wolffd@0: elseif iscell(d) wolffd@0: s = getsize(d{1}); wolffd@0: else wolffd@0: s = get(d,'Size'); % Starting and ending dates in seconds. wolffd@0: end wolffd@0: wolffd@0: wolffd@0: function d2 = sortnames(d,d2,n) wolffd@0: if length(n) == 1 wolffd@0: d2(end+1) = d(1); wolffd@0: return wolffd@0: end wolffd@0: first = zeros(1,length(n)); wolffd@0: for i = 1:length(n) wolffd@0: if isempty(n{i}) wolffd@0: first(i) = -Inf; wolffd@0: else wolffd@0: ni = n{i}{1}; wolffd@0: if ischar(ni) wolffd@0: first(i) = ni-10058; wolffd@0: else wolffd@0: first(i) = ni; wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: [o i] = sort(first); wolffd@0: n = {n{i}}; wolffd@0: d = d(i); wolffd@0: i = 0; wolffd@0: while i=0 && tmp<=9 wolffd@0: while j+1='0' && dn{i}(j+1)<='9' wolffd@0: j = j+1; wolffd@0: tmp = tmp*10 + (dn{i}(j)-'0'); wolffd@0: end wolffd@0: else wolffd@0: tmp = dn{i}(j); wolffd@0: end wolffd@0: nn{i}{end+1} = tmp; wolffd@0: end wolffd@0: end wolffd@0: dd = sortnames(dd,[],nn); wolffd@0: for i = 1:length(dd); wolffd@0: nf = dd(i).name; wolffd@0: if folders && dd(i).isdir wolffd@0: if not(strcmp(nf(1),'.')) wolffd@0: cd(dd(i).name) wolffd@0: [l w sr a] = evalfolder([path nf],s,l,w,sr,a,1); wolffd@0: %l = l + l2; wolffd@0: %w = [w w2]; wolffd@0: %sr = [sr sr2]; wolffd@0: %a = [a a2]; wolffd@0: cd .. wolffd@0: end wolffd@0: else wolffd@0: [di,tpi,fpi,fi,bi,ni] = mirread([],nf,0,1,0); wolffd@0: if not(isempty(ni)) wolffd@0: l = l+1; wolffd@0: if not(isempty(s)) wolffd@0: interval = s(1:2); wolffd@0: if s(3) wolffd@0: interval = round(interval*fi)+1; wolffd@0: end wolffd@0: if s(4) == 1 wolffd@0: interval = interval+round(di/2); wolffd@0: elseif s(4) == 2 wolffd@0: interval = interval+di; wolffd@0: end wolffd@0: w(:,l) = min(max(interval,1),di); wolffd@0: else wolffd@0: w(:,l) = [1;di]; wolffd@0: end wolffd@0: sr(l) = fi; wolffd@0: a{l} = [path ni]; wolffd@0: end wolffd@0: end wolffd@0: end