annotate plugin/transform/Transform.cpp @ 383:94fc0591ea43 1.2-stable

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