Chris@320: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@320: Chris@320: /* Chris@320: Sonic Visualiser Chris@320: An audio file viewer and annotation editor. Chris@320: Centre for Digital Music, Queen Mary, University of London. Chris@328: This file copyright 2006-2007 Chris Cannam and QMUL. Chris@320: Chris@320: This program is free software; you can redistribute it and/or Chris@320: modify it under the terms of the GNU General Public License as Chris@320: published by the Free Software Foundation; either version 2 of the Chris@320: License, or (at your option) any later version. See the file Chris@320: COPYING included with this distribution for more information. Chris@320: */ Chris@320: Chris@320: #include "Transform.h" Chris@320: Chris@328: #include "plugin/PluginIdentifier.h" Chris@328: Chris@332: #include "plugin/FeatureExtractionPluginFactory.h" Chris@332: Chris@383: #include Chris@383: Chris@383: #include Chris@383: #include Chris@383: #include Chris@383: #include Chris@383: Chris@383: #include Chris@383: Chris@383: #include Chris@383: Chris@328: Transform::Transform() : Chris@328: m_stepSize(0), Chris@328: m_blockSize(0), Chris@328: m_windowType(HanningWindow), Chris@328: m_sampleRate(0) Chris@320: { Chris@320: } Chris@320: Chris@383: Transform::Transform(QString xml) : Chris@383: m_stepSize(0), Chris@383: m_blockSize(0), Chris@383: m_windowType(HanningWindow), Chris@383: m_sampleRate(0) Chris@383: { Chris@383: QDomDocument doc; Chris@383: Chris@383: QString error; Chris@383: int errorLine; Chris@383: int errorColumn; Chris@383: Chris@383: if (!doc.setContent(xml, false, &error, &errorLine, &errorColumn)) { Chris@383: std::cerr << "Transform::Transform: Error in parsing XML: " Chris@383: << error.toStdString() << " at line " << errorLine Chris@383: << ", column " << errorColumn << std::endl; Chris@383: std::cerr << "Input follows:" << std::endl; Chris@383: std::cerr << xml.toStdString() << std::endl; Chris@383: std::cerr << "Input ends." << std::endl; Chris@383: return; Chris@383: } Chris@383: Chris@383: QDomElement transformElt = doc.firstChildElement("transform"); Chris@383: QDomNamedNodeMap attrNodes = transformElt.attributes(); Chris@383: QXmlAttributes attrs; Chris@383: Chris@383: for (unsigned int i = 0; i < attrNodes.length(); ++i) { Chris@383: QDomAttr attr = attrNodes.item(i).toAttr(); Chris@383: if (!attr.isNull()) attrs.append(attr.name(), "", "", attr.value()); Chris@383: } Chris@383: Chris@383: setFromXmlAttributes(attrs); Chris@383: Chris@383: for (QDomElement paramElt = transformElt.firstChildElement("parameter"); Chris@383: !paramElt.isNull(); Chris@383: paramElt = paramElt.nextSiblingElement("parameter")) { Chris@383: Chris@383: QDomNamedNodeMap paramAttrs = paramElt.attributes(); Chris@383: Chris@383: QDomAttr nameAttr = paramAttrs.namedItem("name").toAttr(); Chris@383: if (nameAttr.isNull() || nameAttr.value() == "") continue; Chris@383: Chris@383: QDomAttr valueAttr = paramAttrs.namedItem("value").toAttr(); Chris@383: if (valueAttr.isNull() || valueAttr.value() == "") continue; Chris@383: Chris@383: setParameter(nameAttr.value(), valueAttr.value().toFloat()); Chris@383: } Chris@383: Chris@383: for (QDomElement configElt = transformElt.firstChildElement("configuration"); Chris@383: !configElt.isNull(); Chris@383: configElt = configElt.nextSiblingElement("configuration")) { Chris@383: Chris@383: QDomNamedNodeMap configAttrs = configElt.attributes(); Chris@383: Chris@383: QDomAttr nameAttr = configAttrs.namedItem("name").toAttr(); Chris@383: if (nameAttr.isNull() || nameAttr.value() == "") continue; Chris@383: Chris@383: QDomAttr valueAttr = configAttrs.namedItem("value").toAttr(); Chris@383: if (valueAttr.isNull() || valueAttr.value() == "") continue; Chris@383: Chris@383: setConfigurationValue(nameAttr.value(), valueAttr.value()); Chris@383: } Chris@383: } Chris@383: Chris@320: Transform::~Transform() Chris@320: { Chris@320: } Chris@320: Chris@383: bool Chris@383: Transform::operator==(const Transform &t) Chris@383: { Chris@383: return Chris@383: m_id == t.m_id && Chris@383: m_parameters == t.m_parameters && Chris@383: m_configuration == t.m_configuration && Chris@383: m_program == t.m_program && Chris@383: m_stepSize == t.m_stepSize && Chris@383: m_blockSize == t.m_blockSize && Chris@383: m_windowType == t.m_windowType && Chris@383: m_startTime == t.m_startTime && Chris@383: m_duration == t.m_duration && Chris@383: m_sampleRate == t.m_sampleRate; Chris@383: } Chris@383: Chris@383: void Chris@383: Transform::setIdentifier(TransformId id) Chris@383: { Chris@383: m_id = id; Chris@383: } Chris@383: Chris@383: TransformId Chris@383: Transform::getIdentifier() const Chris@383: { Chris@383: return m_id; Chris@383: } Chris@383: Chris@328: QString Chris@328: Transform::createIdentifier(QString type, QString soName, QString label, Chris@328: QString output) Chris@328: { Chris@328: QString pluginId = PluginIdentifier::createIdentifier(type, soName, label); Chris@328: return pluginId + ":" + output; Chris@328: } Chris@328: Chris@328: void Chris@328: Transform::parseIdentifier(QString identifier, Chris@328: QString &type, QString &soName, Chris@328: QString &label, QString &output) Chris@328: { Chris@328: output = identifier.section(':', 3); Chris@328: PluginIdentifier::parseIdentifier(identifier.section(':', 0, 2), Chris@328: type, soName, label); Chris@328: } Chris@328: Chris@328: Transform::Type Chris@328: Transform::getType() const Chris@328: { Chris@332: if (FeatureExtractionPluginFactory::instanceFor(getPluginIdentifier())) { Chris@332: return FeatureExtraction; Chris@332: } else { Chris@332: // We don't have an unknown/invalid return value, so always Chris@332: // return this Chris@332: return RealTimeEffect; Chris@332: } Chris@328: } Chris@328: Chris@328: QString Chris@328: Transform::getPluginIdentifier() const Chris@328: { Chris@328: return m_id.section(':', 0, 2); Chris@328: } Chris@328: Chris@328: QString Chris@328: Transform::getOutput() const Chris@328: { Chris@328: return m_id.section(':', 3); Chris@328: } Chris@328: Chris@328: void Chris@383: Transform::setPluginIdentifier(QString pluginIdentifier) Chris@328: { Chris@383: m_id = pluginIdentifier + ':' + getOutput(); Chris@383: } Chris@383: Chris@383: void Chris@383: Transform::setOutput(QString output) Chris@383: { Chris@383: m_id = getPluginIdentifier() + ':' + output; Chris@383: } Chris@383: Chris@383: TransformId Chris@383: Transform::getIdentifierForPluginOutput(QString pluginIdentifier, Chris@383: QString output) Chris@383: { Chris@383: return pluginIdentifier + ':' + output; Chris@383: } Chris@383: Chris@383: const Transform::ParameterMap & Chris@383: Transform::getParameters() const Chris@383: { Chris@383: return m_parameters; Chris@383: } Chris@383: Chris@383: void Chris@383: Transform::setParameters(const ParameterMap &pm) Chris@383: { Chris@383: m_parameters = pm; Chris@383: } Chris@383: Chris@383: void Chris@383: Transform::setParameter(QString name, float value) Chris@383: { Chris@383: std::cerr << "Transform::setParameter(" << name.toStdString() Chris@383: << ") -> " << value << std::endl; Chris@383: m_parameters[name] = value; Chris@383: } Chris@383: Chris@383: const Transform::ConfigurationMap & Chris@383: Transform::getConfiguration() const Chris@383: { Chris@383: return m_configuration; Chris@383: } Chris@383: Chris@383: void Chris@383: Transform::setConfiguration(const ConfigurationMap &cm) Chris@383: { Chris@383: m_configuration = cm; Chris@383: } Chris@383: Chris@383: void Chris@383: Transform::setConfigurationValue(QString name, QString value) Chris@383: { Chris@383: std::cerr << "Transform::setConfigurationValue(" << name.toStdString() Chris@383: << ") -> " << value.toStdString() << std::endl; Chris@383: m_configuration[name] = value; Chris@383: } Chris@383: Chris@383: QString Chris@383: Transform::getPluginVersion() const Chris@383: { Chris@383: return m_pluginVersion; Chris@383: } Chris@383: Chris@383: void Chris@383: Transform::setPluginVersion(QString version) Chris@383: { Chris@383: m_pluginVersion = version; Chris@383: } Chris@383: Chris@383: QString Chris@383: Transform::getProgram() const Chris@383: { Chris@383: return m_program; Chris@383: } Chris@383: Chris@383: void Chris@383: Transform::setProgram(QString program) Chris@383: { Chris@383: m_program = program; Chris@383: } Chris@383: Chris@328: Chris@383: size_t Chris@383: Transform::getStepSize() const Chris@383: { Chris@383: return m_stepSize; Chris@328: } Chris@383: Chris@383: void Chris@383: Transform::setStepSize(size_t s) Chris@383: { Chris@383: m_stepSize = s; Chris@383: } Chris@383: Chris@383: size_t Chris@383: Transform::getBlockSize() const Chris@383: { Chris@383: return m_blockSize; Chris@383: } Chris@383: Chris@383: void Chris@383: Transform::setBlockSize(size_t s) Chris@383: { Chris@383: m_blockSize = s; Chris@383: } Chris@383: Chris@383: WindowType Chris@383: Transform::getWindowType() const Chris@383: { Chris@383: return m_windowType; Chris@383: } Chris@383: Chris@383: void Chris@383: Transform::setWindowType(WindowType type) Chris@383: { Chris@383: m_windowType = type; Chris@383: } Chris@383: Chris@383: RealTime Chris@383: Transform::getStartTime() const Chris@383: { Chris@383: return m_startTime; Chris@383: } Chris@383: Chris@383: void Chris@383: Transform::setStartTime(RealTime t) Chris@383: { Chris@383: m_startTime = t; Chris@383: } Chris@383: Chris@383: RealTime Chris@383: Transform::getDuration() const Chris@383: { Chris@383: return m_duration; Chris@383: } Chris@383: Chris@383: void Chris@383: Transform::setDuration(RealTime d) Chris@383: { Chris@383: m_duration = d; Chris@383: } Chris@383: Chris@383: float Chris@383: Transform::getSampleRate() const Chris@383: { Chris@383: return m_sampleRate; Chris@383: } Chris@383: Chris@383: void Chris@383: Transform::setSampleRate(float rate) Chris@383: { Chris@383: m_sampleRate = rate; Chris@383: } Chris@383: Chris@383: void Chris@383: Transform::toXml(QTextStream &out, QString indent, QString extraAttributes) const Chris@383: { Chris@383: out << indent; Chris@383: Chris@383: bool haveContent = true; Chris@383: if (m_parameters.empty() && m_configuration.empty()) haveContent = false; Chris@383: Chris@383: out << QString("::getNameForType(m_windowType).c_str())) Chris@383: .arg(encodeEntities(m_startTime.toString().c_str())) Chris@383: .arg(encodeEntities(m_duration.toString().c_str())) Chris@383: .arg(m_sampleRate); Chris@383: Chris@383: if (extraAttributes != "") { Chris@383: out << " " << extraAttributes; Chris@383: } Chris@383: Chris@383: if (haveContent) { Chris@383: Chris@383: out << ">\n"; Chris@383: Chris@383: for (ParameterMap::const_iterator i = m_parameters.begin(); Chris@383: i != m_parameters.end(); ++i) { Chris@383: out << indent << " " Chris@383: << QString("\n") Chris@383: .arg(encodeEntities(i->first)) Chris@383: .arg(i->second); Chris@383: } Chris@383: Chris@383: for (ConfigurationMap::const_iterator i = m_configuration.begin(); Chris@383: i != m_configuration.end(); ++i) { Chris@383: out << indent << " " Chris@383: << QString("\n") Chris@383: .arg(encodeEntities(i->first)) Chris@383: .arg(encodeEntities(i->second)); Chris@383: } Chris@383: Chris@383: out << indent << "\n"; Chris@383: Chris@383: } else { Chris@383: Chris@383: out << "/>\n"; Chris@383: } Chris@383: } Chris@383: Chris@383: void Chris@383: Transform::setFromXmlAttributes(const QXmlAttributes &attrs) Chris@383: { Chris@383: if (attrs.value("id") != "") { Chris@383: setIdentifier(attrs.value("id")); Chris@383: } Chris@383: Chris@383: if (attrs.value("pluginVersion") != "") { Chris@383: setPluginVersion(attrs.value("pluginVersion")); Chris@383: } Chris@383: Chris@383: if (attrs.value("program") != "") { Chris@383: setProgram(attrs.value("program")); Chris@383: } Chris@383: Chris@383: if (attrs.value("stepSize") != "") { Chris@383: setStepSize(attrs.value("stepSize").toInt()); Chris@383: } Chris@383: Chris@383: if (attrs.value("blockSize") != "") { Chris@383: setBlockSize(attrs.value("blockSize").toInt()); Chris@383: } Chris@383: Chris@383: if (attrs.value("windowType") != "") { Chris@383: setWindowType(Window::getTypeForName Chris@383: (attrs.value("windowType").toStdString())); Chris@383: } Chris@383: Chris@383: if (attrs.value("startTime") != "") { Chris@383: setStartTime(RealTime::fromString(attrs.value("startTime").toStdString())); Chris@383: } Chris@383: Chris@383: if (attrs.value("duration") != "") { Chris@383: setStartTime(RealTime::fromString(attrs.value("duration").toStdString())); Chris@383: } Chris@383: Chris@383: if (attrs.value("sampleRate") != "") { Chris@383: setSampleRate(attrs.value("sampleRate").toFloat()); Chris@383: } Chris@383: } Chris@383: