annotate plugin/PluginXml.cpp @ 66:7afcfe666910

* Modify to use Vamp SDK for proper feature extraction plugins. Requires that the vamp-plugin-sdk directory tree be present below plugin/ (it's separate in Subversion).
author Chris Cannam
date Fri, 31 Mar 2006 15:56:35 +0000
parents
children 163f3428bbe0
rev   line source
Chris@66 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@66 2
Chris@66 3 /*
Chris@66 4 Sonic Visualiser
Chris@66 5 An audio file viewer and annotation editor.
Chris@66 6 Centre for Digital Music, Queen Mary, University of London.
Chris@66 7 This file copyright 2006 Chris Cannam.
Chris@66 8
Chris@66 9 This program is free software; you can redistribute it and/or
Chris@66 10 modify it under the terms of the GNU General Public License as
Chris@66 11 published by the Free Software Foundation; either version 2 of the
Chris@66 12 License, or (at your option) any later version. See the file
Chris@66 13 COPYING included with this distribution for more information.
Chris@66 14 */
Chris@66 15
Chris@66 16 #include "PluginXml.h"
Chris@66 17
Chris@66 18 #include <QRegExp>
Chris@66 19 #include <QXmlAttributes>
Chris@66 20
Chris@66 21 #include <QDomDocument>
Chris@66 22 #include <QDomElement>
Chris@66 23 #include <QDomNamedNodeMap>
Chris@66 24 #include <QDomAttr>
Chris@66 25
Chris@66 26 #include "vamp-sdk/PluginBase.h"
Chris@66 27
Chris@66 28 #include <iostream>
Chris@66 29
Chris@66 30 PluginXml::PluginXml(Vamp::PluginBase *plugin) :
Chris@66 31 m_plugin(plugin)
Chris@66 32 {
Chris@66 33 }
Chris@66 34
Chris@66 35 PluginXml::~PluginXml() { }
Chris@66 36
Chris@66 37 QString
Chris@66 38 PluginXml::toXmlString(QString indent, QString extraAttributes) const
Chris@66 39 {
Chris@66 40 QString s;
Chris@66 41 s += indent;
Chris@66 42
Chris@66 43 s += QString("<plugin name=\"%1\" description=\"%2\" maker=\"%3\" version=\"%4\" copyright=\"%5\" %6 ")
Chris@66 44 .arg(encodeEntities(QString(m_plugin->getName().c_str())))
Chris@66 45 .arg(encodeEntities(QString(m_plugin->getDescription().c_str())))
Chris@66 46 .arg(encodeEntities(QString(m_plugin->getMaker().c_str())))
Chris@66 47 .arg(m_plugin->getPluginVersion())
Chris@66 48 .arg(encodeEntities(QString(m_plugin->getCopyright().c_str())))
Chris@66 49 .arg(extraAttributes);
Chris@66 50
Chris@66 51 if (!m_plugin->getPrograms().empty()) {
Chris@66 52 s += QString("program=\"%1\" ")
Chris@66 53 .arg(encodeEntities(m_plugin->getCurrentProgram().c_str()));
Chris@66 54 }
Chris@66 55
Chris@66 56 Vamp::PluginBase::ParameterList parameters =
Chris@66 57 m_plugin->getParameterDescriptors();
Chris@66 58
Chris@66 59 for (Vamp::PluginBase::ParameterList::const_iterator i = parameters.begin();
Chris@66 60 i != parameters.end(); ++i) {
Chris@66 61 s += QString("param-%1=\"%2\" ")
Chris@66 62 .arg(stripInvalidParameterNameCharacters(QString(i->name.c_str())))
Chris@66 63 .arg(m_plugin->getParameter(i->name));
Chris@66 64 }
Chris@66 65
Chris@66 66 s += "/>\n";
Chris@66 67 return s;
Chris@66 68 }
Chris@66 69
Chris@66 70 #define CHECK_ATTRIBUTE(ATTRIBUTE, ACCESSOR) \
Chris@66 71 QString ATTRIBUTE = attrs.value(#ATTRIBUTE); \
Chris@66 72 if (ATTRIBUTE != "" && ATTRIBUTE != ACCESSOR().c_str()) { \
Chris@66 73 std::cerr << "WARNING: PluginXml::setParameters: Plugin " \
Chris@66 74 << #ATTRIBUTE << " does not match (attributes have \"" \
Chris@66 75 << ATTRIBUTE.toStdString() << "\", my " \
Chris@66 76 << #ATTRIBUTE << " is \"" << ACCESSOR() << "\")" << std::endl; \
Chris@66 77 }
Chris@66 78
Chris@66 79 void
Chris@66 80 PluginXml::setParameters(const QXmlAttributes &attrs)
Chris@66 81 {
Chris@66 82 CHECK_ATTRIBUTE(name, m_plugin->getName);
Chris@66 83 CHECK_ATTRIBUTE(description, m_plugin->getDescription);
Chris@66 84 CHECK_ATTRIBUTE(maker, m_plugin->getMaker);
Chris@66 85 CHECK_ATTRIBUTE(copyright, m_plugin->getCopyright);
Chris@66 86
Chris@66 87 bool ok;
Chris@66 88 int version = attrs.value("version").trimmed().toInt(&ok);
Chris@66 89 if (ok && version != m_plugin->getPluginVersion()) {
Chris@66 90 std::cerr << "WARNING: PluginXml::setParameters: Plugin version does not match (attributes have " << version << ", my version is " << m_plugin->getPluginVersion() << ")" << std::endl;
Chris@66 91 }
Chris@66 92
Chris@66 93 if (!m_plugin->getPrograms().empty()) {
Chris@66 94 m_plugin->selectProgram(attrs.value("program").toStdString());
Chris@66 95 }
Chris@66 96
Chris@66 97 Vamp::PluginBase::ParameterList parameters =
Chris@66 98 m_plugin->getParameterDescriptors();
Chris@66 99
Chris@66 100 for (Vamp::PluginBase::ParameterList::const_iterator i =
Chris@66 101 parameters.begin(); i != parameters.end(); ++i) {
Chris@66 102
Chris@66 103 QString name = QString("param-%1")
Chris@66 104 .arg(stripInvalidParameterNameCharacters
Chris@66 105 (QString(i->name.c_str())));
Chris@66 106
Chris@66 107 if (attrs.value(name) == "") {
Chris@66 108 std::cerr << "PluginXml::setParameters: no parameter \"" << i->name << "\" (attribute \"" << name.toStdString() << "\")" << std::endl;
Chris@66 109 continue;
Chris@66 110 }
Chris@66 111
Chris@66 112 bool ok;
Chris@66 113 float value = attrs.value(name).trimmed().toFloat(&ok);
Chris@66 114 if (ok) {
Chris@66 115 m_plugin->setParameter(i->name, value);
Chris@66 116 } else {
Chris@66 117 std::cerr << "WARNING: PluginXml::setParameters: Invalid value \"" << attrs.value(name).toStdString() << "\" for parameter \"" << i->name << "\" (attribute \"" << name.toStdString() << "\")" << std::endl;
Chris@66 118 }
Chris@66 119 }
Chris@66 120 }
Chris@66 121
Chris@66 122 void
Chris@66 123 PluginXml::setParametersFromXml(QString xml)
Chris@66 124 {
Chris@66 125 QDomDocument doc;
Chris@66 126
Chris@66 127 QString error;
Chris@66 128 int errorLine;
Chris@66 129 int errorColumn;
Chris@66 130
Chris@66 131 if (!doc.setContent(xml, false, &error, &errorLine, &errorColumn)) {
Chris@66 132 std::cerr << "PluginXml::setParametersFromXml: Error in parsing XML: " << error.toStdString() << " at line " << errorLine << ", column " << errorColumn << std::endl;
Chris@66 133 std::cerr << "Input follows:" << std::endl;
Chris@66 134 std::cerr << xml.toStdString() << std::endl;
Chris@66 135 std::cerr << "Input ends." << std::endl;
Chris@66 136 return;
Chris@66 137 }
Chris@66 138
Chris@66 139 QDomElement pluginElt = doc.firstChildElement("plugin");
Chris@66 140 QDomNamedNodeMap attrNodes = pluginElt.attributes();
Chris@66 141 QXmlAttributes attrs;
Chris@66 142
Chris@66 143 for (unsigned int i = 0; i < attrNodes.length(); ++i) {
Chris@66 144 QDomAttr attr = attrNodes.item(i).toAttr();
Chris@66 145 if (attr.isNull()) continue;
Chris@66 146 std::cerr << "Adding attribute \"" << attr.name().toStdString()
Chris@66 147 << "\" with value \"" << attr.value().toStdString() << "\"" << std::endl;
Chris@66 148 attrs.append(attr.name(), "", "", attr.value());
Chris@66 149 }
Chris@66 150
Chris@66 151 setParameters(attrs);
Chris@66 152 }
Chris@66 153
Chris@66 154 QString
Chris@66 155 PluginXml::stripInvalidParameterNameCharacters(QString s) const
Chris@66 156 {
Chris@66 157 s.replace(QRegExp("[^a-zA-Z0-9_]*"), "");
Chris@66 158 return s;
Chris@66 159 }
Chris@66 160