Transform.cpp

Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2006-2007 Chris Cannam and QMUL.
00008    
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #include "Transform.h"
00017 
00018 #include "plugin/PluginIdentifier.h"
00019 
00020 #include "plugin/FeatureExtractionPluginFactory.h"
00021 
00022 #include <QXmlAttributes>
00023 
00024 #include <QDomDocument>
00025 #include <QDomElement>
00026 #include <QDomNamedNodeMap>
00027 #include <QDomAttr>
00028 
00029 #include <QTextStream>
00030 
00031 #include <iostream>
00032 
00033 Transform::Transform() :
00034     m_stepSize(0),
00035     m_blockSize(0),
00036     m_windowType(HanningWindow),
00037     m_sampleRate(0)
00038 {
00039 }
00040 
00041 Transform::Transform(QString xml) :
00042     m_stepSize(0),
00043     m_blockSize(0),
00044     m_windowType(HanningWindow),
00045     m_sampleRate(0)
00046 {
00047     QDomDocument doc;
00048     
00049     QString error;
00050     int errorLine;
00051     int errorColumn;
00052 
00053     if (!doc.setContent(xml, false, &error, &errorLine, &errorColumn)) {
00054         std::cerr << "Transform::Transform: Error in parsing XML: "
00055                   << error.toStdString() << " at line " << errorLine
00056                   << ", column " << errorColumn << std::endl;
00057         std::cerr << "Input follows:" << std::endl;
00058         std::cerr << xml.toStdString() << std::endl;
00059         std::cerr << "Input ends." << std::endl;
00060         return;
00061     }
00062     
00063     QDomElement transformElt = doc.firstChildElement("transform");
00064     QDomNamedNodeMap attrNodes = transformElt.attributes();
00065     QXmlAttributes attrs;
00066 
00067     for (unsigned int i = 0; i < attrNodes.length(); ++i) {
00068         QDomAttr attr = attrNodes.item(i).toAttr();
00069         if (!attr.isNull()) attrs.append(attr.name(), "", "", attr.value());
00070     }
00071 
00072     setFromXmlAttributes(attrs);
00073 
00074     for (QDomElement paramElt = transformElt.firstChildElement("parameter");
00075          !paramElt.isNull();
00076          paramElt = paramElt.nextSiblingElement("parameter")) {
00077 
00078         QDomNamedNodeMap paramAttrs = paramElt.attributes();
00079 
00080         QDomAttr nameAttr = paramAttrs.namedItem("name").toAttr();
00081         if (nameAttr.isNull() || nameAttr.value() == "") continue;
00082         
00083         QDomAttr valueAttr = paramAttrs.namedItem("value").toAttr();
00084         if (valueAttr.isNull() || valueAttr.value() == "") continue;
00085 
00086         setParameter(nameAttr.value(), valueAttr.value().toFloat());
00087     }
00088 
00089     for (QDomElement configElt = transformElt.firstChildElement("configuration");
00090          !configElt.isNull();
00091          configElt = configElt.nextSiblingElement("configuration")) {
00092 
00093         QDomNamedNodeMap configAttrs = configElt.attributes();
00094 
00095         QDomAttr nameAttr = configAttrs.namedItem("name").toAttr();
00096         if (nameAttr.isNull() || nameAttr.value() == "") continue;
00097         
00098         QDomAttr valueAttr = configAttrs.namedItem("value").toAttr();
00099         if (valueAttr.isNull() || valueAttr.value() == "") continue;
00100 
00101         setConfigurationValue(nameAttr.value(), valueAttr.value());
00102     }
00103 }
00104 
00105 Transform::~Transform()
00106 {
00107 }
00108 
00109 bool
00110 Transform::operator==(const Transform &t)
00111 {
00112     return 
00113         m_id == t.m_id &&
00114         m_parameters == t.m_parameters &&
00115         m_configuration == t.m_configuration &&
00116         m_program == t.m_program &&
00117         m_stepSize == t.m_stepSize &&
00118         m_blockSize == t.m_blockSize &&
00119         m_windowType == t.m_windowType &&
00120         m_startTime == t.m_startTime &&
00121         m_duration == t.m_duration &&
00122         m_sampleRate == t.m_sampleRate;
00123 }
00124 
00125 void
00126 Transform::setIdentifier(TransformId id)
00127 {
00128     m_id = id;
00129 }
00130 
00131 TransformId
00132 Transform::getIdentifier() const
00133 {
00134     return m_id;
00135 }
00136 
00137 QString
00138 Transform::createIdentifier(QString type, QString soName, QString label,
00139                             QString output)
00140 {
00141     QString pluginId = PluginIdentifier::createIdentifier(type, soName, label);
00142     return pluginId + ":" + output;
00143 }
00144 
00145 void
00146 Transform::parseIdentifier(QString identifier,
00147                            QString &type, QString &soName,
00148                            QString &label, QString &output)
00149 {
00150     output = identifier.section(':', 3);
00151     PluginIdentifier::parseIdentifier(identifier.section(':', 0, 2),
00152                                       type, soName, label);
00153 }
00154 
00155 Transform::Type
00156 Transform::getType() const
00157 {
00158     if (FeatureExtractionPluginFactory::instanceFor(getPluginIdentifier())) {
00159         return FeatureExtraction;
00160     } else {
00161         // We don't have an unknown/invalid return value, so always
00162         // return this
00163         return RealTimeEffect;
00164     }
00165 }
00166 
00167 QString
00168 Transform::getPluginIdentifier() const
00169 {
00170     return m_id.section(':', 0, 2);
00171 }
00172 
00173 QString
00174 Transform::getOutput() const
00175 {
00176     return m_id.section(':', 3);
00177 }
00178 
00179 void
00180 Transform::setPluginIdentifier(QString pluginIdentifier)
00181 {
00182     m_id = pluginIdentifier + ':' + getOutput();
00183 }
00184 
00185 void
00186 Transform::setOutput(QString output)
00187 {
00188     m_id = getPluginIdentifier() + ':' + output;
00189 }
00190 
00191 TransformId
00192 Transform::getIdentifierForPluginOutput(QString pluginIdentifier,
00193                                         QString output)
00194 {
00195     return pluginIdentifier + ':' + output;
00196 }
00197 
00198 const Transform::ParameterMap &
00199 Transform::getParameters() const
00200 {
00201     return m_parameters;
00202 }
00203 
00204 void
00205 Transform::setParameters(const ParameterMap &pm)
00206 {
00207     m_parameters = pm;
00208 }
00209 
00210 void
00211 Transform::setParameter(QString name, float value)
00212 {
00213     std::cerr << "Transform::setParameter(" << name.toStdString()
00214               << ") -> " << value << std::endl;
00215     m_parameters[name] = value;
00216 }
00217 
00218 const Transform::ConfigurationMap &
00219 Transform::getConfiguration() const
00220 {
00221     return m_configuration;
00222 }
00223 
00224 void
00225 Transform::setConfiguration(const ConfigurationMap &cm)
00226 {
00227     m_configuration = cm;
00228 }
00229 
00230 void
00231 Transform::setConfigurationValue(QString name, QString value)
00232 {
00233     std::cerr << "Transform::setConfigurationValue(" << name.toStdString()
00234               << ") -> " << value.toStdString() << std::endl;
00235     m_configuration[name] = value;
00236 }
00237 
00238 QString
00239 Transform::getPluginVersion() const
00240 {
00241     return m_pluginVersion;
00242 }
00243 
00244 void
00245 Transform::setPluginVersion(QString version)
00246 {
00247     m_pluginVersion = version;
00248 }
00249 
00250 QString
00251 Transform::getProgram() const
00252 {
00253     return m_program;
00254 }
00255 
00256 void
00257 Transform::setProgram(QString program)
00258 {
00259     m_program = program;
00260 }
00261 
00262     
00263 size_t
00264 Transform::getStepSize() const
00265 {
00266     return m_stepSize;
00267 }
00268 
00269 void
00270 Transform::setStepSize(size_t s)
00271 {
00272     m_stepSize = s;
00273 }
00274     
00275 size_t
00276 Transform::getBlockSize() const
00277 {
00278     return m_blockSize;
00279 }
00280 
00281 void
00282 Transform::setBlockSize(size_t s)
00283 {
00284     m_blockSize = s;
00285 }
00286 
00287 WindowType
00288 Transform::getWindowType() const
00289 {
00290     return m_windowType;
00291 }
00292 
00293 void
00294 Transform::setWindowType(WindowType type)
00295 {
00296     m_windowType = type;
00297 }
00298 
00299 RealTime
00300 Transform::getStartTime() const
00301 {
00302     return m_startTime;
00303 }
00304 
00305 void
00306 Transform::setStartTime(RealTime t)
00307 {
00308     m_startTime = t;
00309 }
00310 
00311 RealTime
00312 Transform::getDuration() const
00313 {
00314     return m_duration;
00315 }
00316 
00317 void
00318 Transform::setDuration(RealTime d)
00319 {
00320     m_duration = d;
00321 }
00322     
00323 float
00324 Transform::getSampleRate() const
00325 {
00326     return m_sampleRate;
00327 }
00328 
00329 void
00330 Transform::setSampleRate(float rate)
00331 {
00332     m_sampleRate = rate;
00333 }
00334 
00335 void
00336 Transform::toXml(QTextStream &out, QString indent, QString extraAttributes) const
00337 {
00338     out << indent;
00339 
00340     bool haveContent = true;
00341     if (m_parameters.empty() && m_configuration.empty()) haveContent = false;
00342 
00343     out << QString("<transform id=\"%1\" pluginVersion=\"%2\" program=\"%3\" stepSize=\"%4\" blockSize=\"%5\" windowType=\"%6\" startTime=\"%7\" duration=\"%8\" sampleRate=\"%9\"")
00344         .arg(encodeEntities(m_id))
00345         .arg(encodeEntities(m_pluginVersion))
00346         .arg(encodeEntities(m_program))
00347         .arg(m_stepSize)
00348         .arg(m_blockSize)
00349         .arg(encodeEntities(Window<float>::getNameForType(m_windowType).c_str()))
00350         .arg(encodeEntities(m_startTime.toString().c_str()))
00351         .arg(encodeEntities(m_duration.toString().c_str()))
00352         .arg(m_sampleRate);
00353 
00354     if (extraAttributes != "") {
00355         out << " " << extraAttributes;
00356     }
00357 
00358     if (haveContent) {
00359 
00360         out << ">\n";
00361 
00362         for (ParameterMap::const_iterator i = m_parameters.begin();
00363              i != m_parameters.end(); ++i) {
00364             out << indent << "  "
00365                 << QString("<parameter name=\"%1\" value=\"%2\"/>\n")
00366                 .arg(encodeEntities(i->first))
00367                 .arg(i->second);
00368         }
00369         
00370         for (ConfigurationMap::const_iterator i = m_configuration.begin();
00371              i != m_configuration.end(); ++i) {
00372             out << indent << "  "
00373                 << QString("<configuration name=\"%1\" value=\"%2\"/>\n")
00374                 .arg(encodeEntities(i->first))
00375                 .arg(encodeEntities(i->second));
00376         }
00377 
00378         out << indent << "</transform>\n";
00379 
00380     } else {
00381 
00382         out << "/>\n";
00383     }
00384 }
00385 
00386 void
00387 Transform::setFromXmlAttributes(const QXmlAttributes &attrs)
00388 {
00389     if (attrs.value("id") != "") {
00390         setIdentifier(attrs.value("id"));
00391     }
00392 
00393     if (attrs.value("pluginVersion") != "") {
00394         setPluginVersion(attrs.value("pluginVersion"));
00395     }
00396 
00397     if (attrs.value("program") != "") {
00398         setProgram(attrs.value("program"));
00399     }
00400 
00401     if (attrs.value("stepSize") != "") {
00402         setStepSize(attrs.value("stepSize").toInt());
00403     }
00404 
00405     if (attrs.value("blockSize") != "") {
00406         setBlockSize(attrs.value("blockSize").toInt());
00407     }
00408 
00409     if (attrs.value("windowType") != "") {
00410         setWindowType(Window<float>::getTypeForName
00411                       (attrs.value("windowType").toStdString()));
00412     }
00413 
00414     if (attrs.value("startTime") != "") {
00415         setStartTime(RealTime::fromString(attrs.value("startTime").toStdString()));
00416     }
00417 
00418     if (attrs.value("duration") != "") {
00419         setStartTime(RealTime::fromString(attrs.value("duration").toStdString()));
00420     }
00421     
00422     if (attrs.value("sampleRate") != "") {
00423         setSampleRate(attrs.value("sampleRate").toFloat());
00424     }
00425 }
00426 

Generated on Wed Feb 20 15:45:29 2008 for SonicVisualiser by  doxygen 1.5.1