annotate toolboxes/MIRtoolbox1.3.2/MIRToolbox/mirfunction.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function o = mirfunction(method,x,varg,nout,specif,init,main)
Daniel@0 2 % Meta function called by all MIRtoolbox functions.
Daniel@0 3 % Integrates the function into the general flowchart
Daniel@0 4 % and eventually launches the "mireval" evaluation process.
Daniel@0 5 % Here are the successive steps in the following code:
Daniel@0 6 % - If the input is an audio filename, instantiates a new design flowchart.
Daniel@0 7 % - Reads all the options specified by the user.
Daniel@0 8 % - Performs the 'init' part of the MIRtoolbox function:
Daniel@0 9 % - If the input is a design flowchart,
Daniel@0 10 % add the 'init' part in the flowchart.
Daniel@0 11 % - If the input is some MIRtoolbox data,
Daniel@0 12 % execute the 'init' part on that data.
Daniel@0 13 % - Performs the 'main' part of the MIRtoolbox function.
Daniel@0 14
Daniel@0 15 if isempty(x)
Daniel@0 16 o = {{},{},{}};
Daniel@0 17 return
Daniel@0 18 end
Daniel@0 19
Daniel@0 20 if ischar(x) % The input is a file name.
Daniel@0 21 % Starting point of the design process
Daniel@0 22 design_init = 1;
Daniel@0 23 filename = x;
Daniel@0 24 if strcmpi(func2str(method),'miraudio')
Daniel@0 25 postoption = {};
Daniel@0 26 else
Daniel@0 27 postoption.mono = 1;
Daniel@0 28 end
Daniel@0 29 orig = mirdesign(@miraudio,'Design',{varg},postoption,struct,'miraudio');
Daniel@0 30 % Implicitly, the audio file needs to be loaded first.
Daniel@0 31 elseif isnumeric(x)
Daniel@0 32 mirerror(func2str(method),'The input should be a file name or a MIRtoolbox object.');
Daniel@0 33 else
Daniel@0 34 design_init = 0;
Daniel@0 35 orig = x;
Daniel@0 36 end
Daniel@0 37
Daniel@0 38 % Reads all the options specified by the user.
Daniel@0 39 [orig during after] = miroptions(method,orig,specif,varg);
Daniel@0 40
Daniel@0 41 % Performs the 'init' part of the MIRtoolbox function.
Daniel@0 42 if isa(orig,'mirdesign')
Daniel@0 43 if not(get(orig,'Eval'))
Daniel@0 44 % Top-down construction of the general design flowchart
Daniel@0 45
Daniel@0 46 if isstruct(during) && isfield(during,'frame') && ...
Daniel@0 47 isstruct(during.frame) && during.frame.auto
Daniel@0 48 % 'Frame' option:
Daniel@0 49 % Automatic insertion of the mirframe step in the design
Daniel@0 50 orig = mirframe(orig,during.frame.length.val,...
Daniel@0 51 during.frame.length.unit,...
Daniel@0 52 during.frame.hop.val,...
Daniel@0 53 during.frame.hop.unit);
Daniel@0 54 end
Daniel@0 55
Daniel@0 56 % The 'init' part of the function can be integrated into the design
Daniel@0 57 % flowchart. This leads to a top-down construction of the
Daniel@0 58 % flowchart.
Daniel@0 59 % Automatic development of the implicit prerequisites,
Daniel@0 60 % with management of the data types throughout the design process.
Daniel@0 61 [orig type] = init(orig,during);
Daniel@0 62
Daniel@0 63 o = mirdesign(method,orig,during,after,specif,type);
Daniel@0 64
Daniel@0 65 if design_init && not(strcmpi(filename,'Design'))
Daniel@0 66 % Now the design flowchart has been completed created.
Daniel@0 67 % If the 'Design' keyword not used,
Daniel@0 68 % the function is immediately evaluated
Daniel@0 69 o = mireval(o,filename,nout);
Daniel@0 70 else
Daniel@0 71 o = returndesign(o,nout);
Daniel@0 72 end
Daniel@0 73 if not(iscell(o))
Daniel@0 74 o = {o};
Daniel@0 75 end
Daniel@0 76 return
Daniel@0 77 else
Daniel@0 78 % During the top-down traversal of the flowchart (evaleach), at the
Daniel@0 79 % beginning of the evaluation process.
Daniel@0 80
Daniel@0 81 if not(isempty(get(orig,'TmpFile'))) && get(orig,'ChunkDecomposed')
Daniel@0 82 orig = evaleach(orig);
Daniel@0 83 if iscell(orig)
Daniel@0 84 orig = orig{1};
Daniel@0 85 end
Daniel@0 86 x = orig;
Daniel@0 87 else
Daniel@0 88 [orig x] = evaleach(orig);
Daniel@0 89 end
Daniel@0 90
Daniel@0 91 if not(isequal(method,@nthoutput))
Daniel@0 92 if iscell(orig)
Daniel@0 93 orig = orig{1};
Daniel@0 94 end
Daniel@0 95 if isempty(get(orig,'InterChunk'))
Daniel@0 96 orig = set(orig,'InterChunk',get(x,'InterChunk'));
Daniel@0 97 end
Daniel@0 98 end
Daniel@0 99 end
Daniel@0 100 else
Daniel@0 101 design = 0;
Daniel@0 102 if iscell(orig)
Daniel@0 103 i = 0;
Daniel@0 104 while i<length(orig) && not(design)
Daniel@0 105 i = i+1;
Daniel@0 106 if isa(orig{i},'mirdesign')
Daniel@0 107 design = i;
Daniel@0 108 end
Daniel@0 109 end
Daniel@0 110 end
Daniel@0 111 if design
Daniel@0 112 % For function with multiple inputs
Daniel@0 113 if design == 1 && not(get(orig{1},'Eval'))
Daniel@0 114 % Progressive construction of the general design
Daniel@0 115 [orig type] = init(orig,during);
Daniel@0 116 o = mirdesign(method,orig,during,after,specif,type);
Daniel@0 117 o = set(o,'Size',get(orig{1},'Size'));
Daniel@0 118 o = returndesign(o,nout);
Daniel@0 119 return
Daniel@0 120 else
Daniel@0 121 % Evaluation of the design.
Daniel@0 122 % First top-down initiation (evaleach), then bottom-up process.
Daniel@0 123 for io = 1:length(orig)
Daniel@0 124 if isa(orig{io},'mirdesign')
Daniel@0 125 o = evaleach(orig{io});
Daniel@0 126 if iscell(o)
Daniel@0 127 o = o{:};
Daniel@0 128 end
Daniel@0 129 orig{io} = o;
Daniel@0 130 end
Daniel@0 131 end
Daniel@0 132 end
Daniel@0 133 elseif not(isempty(init)) && not(isempty(during))
Daniel@0 134 if isstruct(during) && isfield(during,'frame') && ...
Daniel@0 135 isstruct(during.frame) && during.frame.auto
Daniel@0 136 orig = mirframe(orig,during.frame.length,during.frame.hop);
Daniel@0 137 end
Daniel@0 138 % The input of the function is not a design flowchart, which
Daniel@0 139 % the 'init' part of the function could be integrated into.
Daniel@0 140 % (cf. previous call of 'init' in this script).
Daniel@0 141 % For that reason, the 'init' part of the function needs to be
Daniel@0 142 % evaluated now.
Daniel@0 143 orig = init(orig,during);
Daniel@0 144 end
Daniel@0 145 end
Daniel@0 146
Daniel@0 147 % Performs the 'main' part of the MIRtoolbox function.
Daniel@0 148 if not(iscell(orig) && not(ischar(orig{1}))) && ...
Daniel@0 149 not(isa(orig,'mirdesign') || isa(orig,'mirdata'))
Daniel@0 150 o = {orig};
Daniel@0 151 return
Daniel@0 152 end
Daniel@0 153 filenamearg = orig;
Daniel@0 154 if iscell(filenamearg) && not(ischar(filenamearg{1}))
Daniel@0 155 filenamearg = filenamearg{1};
Daniel@0 156 end
Daniel@0 157 if iscell(filenamearg) && not(ischar(filenamearg{1}))
Daniel@0 158 filenamearg = filenamearg{1};
Daniel@0 159 end
Daniel@0 160 filename = get(filenamearg,'Name');
Daniel@0 161 if not(isempty(during)) && mirverbose
Daniel@0 162 % if length(filename) == 1
Daniel@0 163 % disp(['Computing ',func2str(method),' related to ',filename{1},'...'])
Daniel@0 164 % else
Daniel@0 165 % disp(['Computing ',func2str(method),' for all audio files ...'])
Daniel@0 166 % end
Daniel@0 167 end
Daniel@0 168 if iscell(x)
Daniel@0 169 x1 = x{1};
Daniel@0 170 else
Daniel@0 171 x1 = x;
Daniel@0 172 end
Daniel@0 173 if not(iscell(orig) || isnumeric(x))
Daniel@0 174 orig = set(orig,'Index',get(x1,'Index'));
Daniel@0 175 end
Daniel@0 176 if iscell(orig)
Daniel@0 177 o = main(orig,during,after);
Daniel@0 178 else
Daniel@0 179 d = get(orig,'Data');
Daniel@0 180 if isamir(orig,'miraudio') && ...
Daniel@0 181 length(d) == 1 && length(d{1}) == 1 && isempty(d{1}{1})
Daniel@0 182 % To solve a problem when MP3read returns empty chunk.
Daniel@0 183 % Warning: it should not be a cell, because for instance nthoutput can have first input empty...
Daniel@0 184 o = orig;
Daniel@0 185 else
Daniel@0 186 o = main(orig,during,after);
Daniel@0 187 end
Daniel@0 188 end
Daniel@0 189 if not(iscell(o) && length(o)>1) || (isa(x,'mirdesign') && get(x,'Eval'))
Daniel@0 190 o = {o x};
Daniel@0 191 elseif iscell(x) && isa(x{1},'mirdesign') && get(x{1},'Eval')
Daniel@0 192 o = {o x{1}};
Daniel@0 193 elseif not(isempty(varg)) && isstruct(varg{1}) ...
Daniel@0 194 && not(iscell(o) && iscell(o{1}))
Daniel@0 195 % When the function was called by mireval, the output should be packed
Daniel@0 196 % into one single cell array (in order to be send back to calling
Daniel@0 197 % routines).
Daniel@0 198 o = {o};
Daniel@0 199 end
Daniel@0 200
Daniel@0 201
Daniel@0 202 function o = returndesign(i,nout)
Daniel@0 203 o = cell(1,nout);
Daniel@0 204 o{1} = i;
Daniel@0 205 for k = 2:nout
Daniel@0 206 o{k} = nthoutput(i,k);
Daniel@0 207 end