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