comparison plugin/transform/Transform.cpp @ 350:d7c41483af8f

* Merge from transforms branch -- switch over to using Transform object properly
author Chris Cannam
date Fri, 07 Dec 2007 16:47:31 +0000
parents 13e5870040e6
children 1c6816220185
comparison
equal deleted inserted replaced
348:edda24bb85fc 350:d7c41483af8f
17 17
18 #include "plugin/PluginIdentifier.h" 18 #include "plugin/PluginIdentifier.h"
19 19
20 #include "plugin/FeatureExtractionPluginFactory.h" 20 #include "plugin/FeatureExtractionPluginFactory.h"
21 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
22 Transform::Transform() : 33 Transform::Transform() :
23 m_stepSize(0), 34 m_stepSize(0),
24 m_blockSize(0), 35 m_blockSize(0),
25 m_windowType(HanningWindow), 36 m_windowType(HanningWindow),
26 m_sampleRate(0) 37 m_sampleRate(0)
27 { 38 {
28 } 39 }
29 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
30 Transform::~Transform() 105 Transform::~Transform()
31 { 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;
32 } 135 }
33 136
34 QString 137 QString
35 Transform::createIdentifier(QString type, QString soName, QString label, 138 Transform::createIdentifier(QString type, QString soName, QString label,
36 QString output) 139 QString output)
71 Transform::getOutput() const 174 Transform::getOutput() const
72 { 175 {
73 return m_id.section(':', 3); 176 return m_id.section(':', 3);
74 } 177 }
75 178
76 void 179 const Transform::ParameterMap &
77 Transform::toXml(QTextStream &stream, QString indent, QString extraAttributes) const 180 Transform::getParameters() const
78 { 181 {
79 182 return m_parameters;
80 } 183 }
184
185 void
186 Transform::setParameters(const ParameterMap &pm)
187 {
188 m_parameters = pm;
189 }
190
191 void
192 Transform::setParameter(QString name, float value)
193 {
194 std::cerr << "Transform::setParameter(" << name.toStdString()
195 << ") -> " << value << std::endl;
196 m_parameters[name] = value;
197 }
198
199 const Transform::ConfigurationMap &
200 Transform::getConfiguration() const
201 {
202 return m_configuration;
203 }
204
205 void
206 Transform::setConfiguration(const ConfigurationMap &cm)
207 {
208 m_configuration = cm;
209 }
210
211 void
212 Transform::setConfigurationValue(QString name, QString value)
213 {
214 std::cerr << "Transform::setConfigurationValue(" << name.toStdString()
215 << ") -> " << value.toStdString() << std::endl;
216 m_configuration[name] = value;
217 }
218
219 QString
220 Transform::getProgram() const
221 {
222 return m_program;
223 }
224
225 void
226 Transform::setProgram(QString program)
227 {
228 m_program = program;
229 }
230
231
232 size_t
233 Transform::getStepSize() const
234 {
235 return m_stepSize;
236 }
237
238 void
239 Transform::setStepSize(size_t s)
240 {
241 m_stepSize = s;
242 }
243
244 size_t
245 Transform::getBlockSize() const
246 {
247 return m_blockSize;
248 }
249
250 void
251 Transform::setBlockSize(size_t s)
252 {
253 m_blockSize = s;
254 }
255
256 WindowType
257 Transform::getWindowType() const
258 {
259 return m_windowType;
260 }
261
262 void
263 Transform::setWindowType(WindowType type)
264 {
265 m_windowType = type;
266 }
267
268 RealTime
269 Transform::getStartTime() const
270 {
271 return m_startTime;
272 }
273
274 void
275 Transform::setStartTime(RealTime t)
276 {
277 m_startTime = t;
278 }
279
280 RealTime
281 Transform::getDuration() const
282 {
283 return m_duration;
284 }
285
286 void
287 Transform::setDuration(RealTime d)
288 {
289 m_duration = d;
290 }
291
292 float
293 Transform::getSampleRate() const
294 {
295 return m_sampleRate;
296 }
297
298 void
299 Transform::setSampleRate(float rate)
300 {
301 m_sampleRate = rate;
302 }
303
304 void
305 Transform::toXml(QTextStream &out, QString indent, QString extraAttributes) const
306 {
307 out << indent;
308
309 bool haveContent = true;
310 if (m_parameters.empty() && m_configuration.empty()) haveContent = false;
311
312 out << QString("<transform id=\"%1\" program=\"%2\" stepSize=\"%3\" blockSize=\"%4\" windowType=\"%5\" startTime=\"%6\" duration=\"%7\" sampleRate=\"%8\" %9")
313 .arg(encodeEntities(m_id))
314 .arg(encodeEntities(m_program))
315 .arg(m_stepSize)
316 .arg(m_blockSize)
317 .arg(encodeEntities(Window<float>::getNameForType(m_windowType).c_str()))
318 .arg(encodeEntities(m_startTime.toString().c_str()))
319 .arg(encodeEntities(m_duration.toString().c_str()))
320 .arg(m_sampleRate)
321 .arg(extraAttributes);
322
323 if (haveContent) {
324
325 out << ">\n";
326
327 for (ParameterMap::const_iterator i = m_parameters.begin();
328 i != m_parameters.end(); ++i) {
329 out << indent << " "
330 << QString("<parameter name=\"%1\" value=\"%2\"/>\n")
331 .arg(encodeEntities(i->first))
332 .arg(i->second);
333 }
334
335 for (ConfigurationMap::const_iterator i = m_configuration.begin();
336 i != m_configuration.end(); ++i) {
337 out << indent << " "
338 << QString("<configuration name=\"%1\" value=\"%2\"/>\n")
339 .arg(encodeEntities(i->first))
340 .arg(encodeEntities(i->second));
341 }
342
343 out << indent << "</transform>\n";
344
345 } else {
346
347 out << "/>\n";
348 }
349 }
350
351 void
352 Transform::setFromXmlAttributes(const QXmlAttributes &attrs)
353 {
354 if (attrs.value("id") != "") {
355 setIdentifier(attrs.value("id"));
356 }
357
358 if (attrs.value("program") != "") {
359 setProgram(attrs.value("program"));
360 }
361
362 if (attrs.value("stepSize") != "") {
363 setStepSize(attrs.value("stepSize").toInt());
364 }
365
366 if (attrs.value("blockSize") != "") {
367 setBlockSize(attrs.value("blockSize").toInt());
368 }
369
370 if (attrs.value("windowType") != "") {
371 setWindowType(Window<float>::getTypeForName
372 (attrs.value("windowType").toStdString()));
373 }
374
375 if (attrs.value("startTime") != "") {
376 setStartTime(RealTime::fromString(attrs.value("startTime").toStdString()));
377 }
378
379 if (attrs.value("duration") != "") {
380 setStartTime(RealTime::fromString(attrs.value("duration").toStdString()));
381 }
382
383 if (attrs.value("sampleRate") != "") {
384 setSampleRate(attrs.value("sampleRate").toFloat());
385 }
386 }
387