Mercurial > hg > svcore
comparison transform/Transform.cpp @ 388:370aa9714ef5
* Move plugin/transform to plain transform. This way transform can depend on
model and GUI classes, but plugin doesn't have to.
author | Chris Cannam |
---|---|
date | Wed, 12 Mar 2008 18:02:17 +0000 |
parents | plugin/transform/Transform.cpp@4bb19132da23 |
children | 9d70f577d6f5 |
comparison
equal
deleted
inserted
replaced
387:7aa1de571880 | 388:370aa9714ef5 |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 Sonic Visualiser | |
5 An audio file viewer and annotation editor. | |
6 Centre for Digital Music, Queen Mary, University of London. | |
7 This file copyright 2006-2007 Chris Cannam and QMUL. | |
8 | |
9 This program is free software; you can redistribute it and/or | |
10 modify it under the terms of the GNU General Public License as | |
11 published by the Free Software Foundation; either version 2 of the | |
12 License, or (at your option) any later version. See the file | |
13 COPYING included with this distribution for more information. | |
14 */ | |
15 | |
16 #include "Transform.h" | |
17 | |
18 #include "plugin/PluginIdentifier.h" | |
19 | |
20 #include "plugin/FeatureExtractionPluginFactory.h" | |
21 | |
22 #include <QXmlAttributes> | |
23 | |
24 #include <QDomDocument> | |
25 #include <QDomElement> | |
26 #include <QDomNamedNodeMap> | |
27 #include <QDomAttr> | |
28 | |
29 #include <QTextStream> | |
30 | |
31 #include <iostream> | |
32 | |
33 Transform::Transform() : | |
34 m_stepSize(0), | |
35 m_blockSize(0), | |
36 m_windowType(HanningWindow), | |
37 m_sampleRate(0) | |
38 { | |
39 } | |
40 | |
41 Transform::Transform(QString xml) : | |
42 m_stepSize(0), | |
43 m_blockSize(0), | |
44 m_windowType(HanningWindow), | |
45 m_sampleRate(0) | |
46 { | |
47 QDomDocument doc; | |
48 | |
49 QString error; | |
50 int errorLine; | |
51 int errorColumn; | |
52 | |
53 if (!doc.setContent(xml, false, &error, &errorLine, &errorColumn)) { | |
54 std::cerr << "Transform::Transform: Error in parsing XML: " | |
55 << error.toStdString() << " at line " << errorLine | |
56 << ", column " << errorColumn << std::endl; | |
57 std::cerr << "Input follows:" << std::endl; | |
58 std::cerr << xml.toStdString() << std::endl; | |
59 std::cerr << "Input ends." << std::endl; | |
60 return; | |
61 } | |
62 | |
63 QDomElement transformElt = doc.firstChildElement("transform"); | |
64 QDomNamedNodeMap attrNodes = transformElt.attributes(); | |
65 QXmlAttributes attrs; | |
66 | |
67 for (unsigned int i = 0; i < attrNodes.length(); ++i) { | |
68 QDomAttr attr = attrNodes.item(i).toAttr(); | |
69 if (!attr.isNull()) attrs.append(attr.name(), "", "", attr.value()); | |
70 } | |
71 | |
72 setFromXmlAttributes(attrs); | |
73 | |
74 for (QDomElement paramElt = transformElt.firstChildElement("parameter"); | |
75 !paramElt.isNull(); | |
76 paramElt = paramElt.nextSiblingElement("parameter")) { | |
77 | |
78 QDomNamedNodeMap paramAttrs = paramElt.attributes(); | |
79 | |
80 QDomAttr nameAttr = paramAttrs.namedItem("name").toAttr(); | |
81 if (nameAttr.isNull() || nameAttr.value() == "") continue; | |
82 | |
83 QDomAttr valueAttr = paramAttrs.namedItem("value").toAttr(); | |
84 if (valueAttr.isNull() || valueAttr.value() == "") continue; | |
85 | |
86 setParameter(nameAttr.value(), valueAttr.value().toFloat()); | |
87 } | |
88 | |
89 for (QDomElement configElt = transformElt.firstChildElement("configuration"); | |
90 !configElt.isNull(); | |
91 configElt = configElt.nextSiblingElement("configuration")) { | |
92 | |
93 QDomNamedNodeMap configAttrs = configElt.attributes(); | |
94 | |
95 QDomAttr nameAttr = configAttrs.namedItem("name").toAttr(); | |
96 if (nameAttr.isNull() || nameAttr.value() == "") continue; | |
97 | |
98 QDomAttr valueAttr = configAttrs.namedItem("value").toAttr(); | |
99 if (valueAttr.isNull() || valueAttr.value() == "") continue; | |
100 | |
101 setConfigurationValue(nameAttr.value(), valueAttr.value()); | |
102 } | |
103 } | |
104 | |
105 Transform::~Transform() | |
106 { | |
107 } | |
108 | |
109 bool | |
110 Transform::operator==(const Transform &t) | |
111 { | |
112 return | |
113 m_id == t.m_id && | |
114 m_parameters == t.m_parameters && | |
115 m_configuration == t.m_configuration && | |
116 m_program == t.m_program && | |
117 m_stepSize == t.m_stepSize && | |
118 m_blockSize == t.m_blockSize && | |
119 m_windowType == t.m_windowType && | |
120 m_startTime == t.m_startTime && | |
121 m_duration == t.m_duration && | |
122 m_sampleRate == t.m_sampleRate; | |
123 } | |
124 | |
125 void | |
126 Transform::setIdentifier(TransformId id) | |
127 { | |
128 m_id = id; | |
129 } | |
130 | |
131 TransformId | |
132 Transform::getIdentifier() const | |
133 { | |
134 return m_id; | |
135 } | |
136 | |
137 QString | |
138 Transform::createIdentifier(QString type, QString soName, QString label, | |
139 QString output) | |
140 { | |
141 QString pluginId = PluginIdentifier::createIdentifier(type, soName, label); | |
142 return pluginId + ":" + output; | |
143 } | |
144 | |
145 void | |
146 Transform::parseIdentifier(QString identifier, | |
147 QString &type, QString &soName, | |
148 QString &label, QString &output) | |
149 { | |
150 output = identifier.section(':', 3); | |
151 PluginIdentifier::parseIdentifier(identifier.section(':', 0, 2), | |
152 type, soName, label); | |
153 } | |
154 | |
155 Transform::Type | |
156 Transform::getType() const | |
157 { | |
158 if (FeatureExtractionPluginFactory::instanceFor(getPluginIdentifier())) { | |
159 return FeatureExtraction; | |
160 } else { | |
161 // We don't have an unknown/invalid return value, so always | |
162 // return this | |
163 return RealTimeEffect; | |
164 } | |
165 } | |
166 | |
167 QString | |
168 Transform::getPluginIdentifier() const | |
169 { | |
170 return m_id.section(':', 0, 2); | |
171 } | |
172 | |
173 QString | |
174 Transform::getOutput() const | |
175 { | |
176 return m_id.section(':', 3); | |
177 } | |
178 | |
179 void | |
180 Transform::setPluginIdentifier(QString pluginIdentifier) | |
181 { | |
182 m_id = pluginIdentifier + ':' + getOutput(); | |
183 } | |
184 | |
185 void | |
186 Transform::setOutput(QString output) | |
187 { | |
188 m_id = getPluginIdentifier() + ':' + output; | |
189 } | |
190 | |
191 TransformId | |
192 Transform::getIdentifierForPluginOutput(QString pluginIdentifier, | |
193 QString output) | |
194 { | |
195 return pluginIdentifier + ':' + output; | |
196 } | |
197 | |
198 const Transform::ParameterMap & | |
199 Transform::getParameters() const | |
200 { | |
201 return m_parameters; | |
202 } | |
203 | |
204 void | |
205 Transform::setParameters(const ParameterMap &pm) | |
206 { | |
207 m_parameters = pm; | |
208 } | |
209 | |
210 void | |
211 Transform::setParameter(QString name, float value) | |
212 { | |
213 std::cerr << "Transform::setParameter(" << name.toStdString() | |
214 << ") -> " << value << std::endl; | |
215 m_parameters[name] = value; | |
216 } | |
217 | |
218 const Transform::ConfigurationMap & | |
219 Transform::getConfiguration() const | |
220 { | |
221 return m_configuration; | |
222 } | |
223 | |
224 void | |
225 Transform::setConfiguration(const ConfigurationMap &cm) | |
226 { | |
227 m_configuration = cm; | |
228 } | |
229 | |
230 void | |
231 Transform::setConfigurationValue(QString name, QString value) | |
232 { | |
233 std::cerr << "Transform::setConfigurationValue(" << name.toStdString() | |
234 << ") -> " << value.toStdString() << std::endl; | |
235 m_configuration[name] = value; | |
236 } | |
237 | |
238 QString | |
239 Transform::getPluginVersion() const | |
240 { | |
241 return m_pluginVersion; | |
242 } | |
243 | |
244 void | |
245 Transform::setPluginVersion(QString version) | |
246 { | |
247 m_pluginVersion = version; | |
248 } | |
249 | |
250 QString | |
251 Transform::getProgram() const | |
252 { | |
253 return m_program; | |
254 } | |
255 | |
256 void | |
257 Transform::setProgram(QString program) | |
258 { | |
259 m_program = program; | |
260 } | |
261 | |
262 | |
263 size_t | |
264 Transform::getStepSize() const | |
265 { | |
266 return m_stepSize; | |
267 } | |
268 | |
269 void | |
270 Transform::setStepSize(size_t s) | |
271 { | |
272 m_stepSize = s; | |
273 } | |
274 | |
275 size_t | |
276 Transform::getBlockSize() const | |
277 { | |
278 return m_blockSize; | |
279 } | |
280 | |
281 void | |
282 Transform::setBlockSize(size_t s) | |
283 { | |
284 m_blockSize = s; | |
285 } | |
286 | |
287 WindowType | |
288 Transform::getWindowType() const | |
289 { | |
290 return m_windowType; | |
291 } | |
292 | |
293 void | |
294 Transform::setWindowType(WindowType type) | |
295 { | |
296 m_windowType = type; | |
297 } | |
298 | |
299 RealTime | |
300 Transform::getStartTime() const | |
301 { | |
302 return m_startTime; | |
303 } | |
304 | |
305 void | |
306 Transform::setStartTime(RealTime t) | |
307 { | |
308 m_startTime = t; | |
309 } | |
310 | |
311 RealTime | |
312 Transform::getDuration() const | |
313 { | |
314 return m_duration; | |
315 } | |
316 | |
317 void | |
318 Transform::setDuration(RealTime d) | |
319 { | |
320 m_duration = d; | |
321 } | |
322 | |
323 float | |
324 Transform::getSampleRate() const | |
325 { | |
326 return m_sampleRate; | |
327 } | |
328 | |
329 void | |
330 Transform::setSampleRate(float rate) | |
331 { | |
332 m_sampleRate = rate; | |
333 } | |
334 | |
335 void | |
336 Transform::toXml(QTextStream &out, QString indent, QString extraAttributes) const | |
337 { | |
338 out << indent; | |
339 | |
340 bool haveContent = true; | |
341 if (m_parameters.empty() && m_configuration.empty()) haveContent = false; | |
342 | |
343 out << QString("<transform id=\"%1\" pluginVersion=\"%2\" program=\"%3\" stepSize=\"%4\" blockSize=\"%5\" windowType=\"%6\" startTime=\"%7\" duration=\"%8\" sampleRate=\"%9\"") | |
344 .arg(encodeEntities(m_id)) | |
345 .arg(encodeEntities(m_pluginVersion)) | |
346 .arg(encodeEntities(m_program)) | |
347 .arg(m_stepSize) | |
348 .arg(m_blockSize) | |
349 .arg(encodeEntities(Window<float>::getNameForType(m_windowType).c_str())) | |
350 .arg(encodeEntities(m_startTime.toString().c_str())) | |
351 .arg(encodeEntities(m_duration.toString().c_str())) | |
352 .arg(m_sampleRate); | |
353 | |
354 if (extraAttributes != "") { | |
355 out << " " << extraAttributes; | |
356 } | |
357 | |
358 if (haveContent) { | |
359 | |
360 out << ">\n"; | |
361 | |
362 for (ParameterMap::const_iterator i = m_parameters.begin(); | |
363 i != m_parameters.end(); ++i) { | |
364 out << indent << " " | |
365 << QString("<parameter name=\"%1\" value=\"%2\"/>\n") | |
366 .arg(encodeEntities(i->first)) | |
367 .arg(i->second); | |
368 } | |
369 | |
370 for (ConfigurationMap::const_iterator i = m_configuration.begin(); | |
371 i != m_configuration.end(); ++i) { | |
372 out << indent << " " | |
373 << QString("<configuration name=\"%1\" value=\"%2\"/>\n") | |
374 .arg(encodeEntities(i->first)) | |
375 .arg(encodeEntities(i->second)); | |
376 } | |
377 | |
378 out << indent << "</transform>\n"; | |
379 | |
380 } else { | |
381 | |
382 out << "/>\n"; | |
383 } | |
384 } | |
385 | |
386 void | |
387 Transform::setFromXmlAttributes(const QXmlAttributes &attrs) | |
388 { | |
389 if (attrs.value("id") != "") { | |
390 setIdentifier(attrs.value("id")); | |
391 } | |
392 | |
393 if (attrs.value("pluginVersion") != "") { | |
394 setPluginVersion(attrs.value("pluginVersion")); | |
395 } | |
396 | |
397 if (attrs.value("program") != "") { | |
398 setProgram(attrs.value("program")); | |
399 } | |
400 | |
401 if (attrs.value("stepSize") != "") { | |
402 setStepSize(attrs.value("stepSize").toInt()); | |
403 } | |
404 | |
405 if (attrs.value("blockSize") != "") { | |
406 setBlockSize(attrs.value("blockSize").toInt()); | |
407 } | |
408 | |
409 if (attrs.value("windowType") != "") { | |
410 setWindowType(Window<float>::getTypeForName | |
411 (attrs.value("windowType").toStdString())); | |
412 } | |
413 | |
414 if (attrs.value("startTime") != "") { | |
415 setStartTime(RealTime::fromString(attrs.value("startTime").toStdString())); | |
416 } | |
417 | |
418 if (attrs.value("duration") != "") { | |
419 setStartTime(RealTime::fromString(attrs.value("duration").toStdString())); | |
420 } | |
421 | |
422 if (attrs.value("sampleRate") != "") { | |
423 setSampleRate(attrs.value("sampleRate").toFloat()); | |
424 } | |
425 } | |
426 |