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