1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Sonic Visualiser
An audio file viewer and annotation editor.
Centre for Digital Music, Queen Mary, University of London.
This file copyright 2006 Chris Cannam.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
#include "PluginXml.h"
#include <QRegExp>
#include <QXmlAttributes>
#include <QDomDocument>
#include <QDomElement>
#include <QDomNamedNodeMap>
#include <QDomAttr>
#include <QTextStream>
#include <vamp-hostsdk/PluginBase.h>
#include "RealTimePluginInstance.h"
#include <iostream>
PluginXml::PluginXml(Vamp::PluginBase *plugin) :
m_plugin(plugin)
{
}
PluginXml::~PluginXml() { }
QString
PluginXml::encodeConfigurationChars(QString text)
{
QString rv(text);
rv.replace(";", "[[SEMICOLON]]");
rv.replace("=", "[[EQUALS]]");
return rv;
}
QString
PluginXml::decodeConfigurationChars(QString text)
{
QString rv(text);
rv.replace("[[SEMICOLON]]", ";");
rv.replace("[[EQUALS]]", "=");
return rv;
}
void
PluginXml::toXml(QTextStream &stream,
QString indent, QString extraAttributes) const
{
stream << indent;
stream << QString("<plugin identifier=\"%1\" name=\"%2\" description=\"%3\" maker=\"%4\" version=\"%5\" copyright=\"%6\" %7 ")
.arg(encodeEntities(QString(m_plugin->getIdentifier().c_str())))
.arg(encodeEntities(QString(m_plugin->getName().c_str())))
.arg(encodeEntities(QString(m_plugin->getDescription().c_str())))
.arg(encodeEntities(QString(m_plugin->getMaker().c_str())))
.arg(m_plugin->getPluginVersion())
.arg(encodeEntities(QString(m_plugin->getCopyright().c_str())))
.arg(extraAttributes);
if (!m_plugin->getPrograms().empty()) {
stream << QString("program=\"%1\" ")
.arg(encodeEntities(m_plugin->getCurrentProgram().c_str()));
}
Vamp::PluginBase::ParameterList parameters =
m_plugin->getParameterDescriptors();
for (Vamp::PluginBase::ParameterList::const_iterator i = parameters.begin();
i != parameters.end(); ++i) {
// SVDEBUG << "PluginXml::toXml: parameter name \""
// << i->name.c_str() << "\" has value "
// << m_plugin->getParameter(i->name) << endl;
stream << QString("param-%1=\"%2\" ")
.arg(stripInvalidParameterNameCharacters(QString(i->identifier.c_str())))
.arg(m_plugin->getParameter(i->identifier));
}
RealTimePluginInstance *rtpi =
dynamic_cast<RealTimePluginInstance *>(m_plugin);
if (rtpi) {
std::map<std::string, std::string> configurePairs =
rtpi->getConfigurePairs();
QString config;
for (std::map<std::string, std::string>::iterator i = configurePairs.begin();
i != configurePairs.end(); ++i) {
QString key = i->first.c_str();
QString value = i->second.c_str();
key = encodeConfigurationChars(key);
value = encodeConfigurationChars(value);
if (config != "") config += ";";
config += QString("%1=%2").arg(key).arg(value);
}
if (config != "") {
stream << QString("configuration=\"%1\" ")
.arg(encodeEntities(config));
}
}
stream << "/>\n";
}
#define CHECK_ATTRIBUTE(ATTRIBUTE, ACCESSOR) \
QString ATTRIBUTE = attrs.value(#ATTRIBUTE); \
if (ATTRIBUTE != "" && ATTRIBUTE != ACCESSOR().c_str()) { \
cerr << "WARNING: PluginXml::setParameters: Plugin " \
<< #ATTRIBUTE << " does not match (attributes have \"" \
<< ATTRIBUTE << "\", my " \
<< #ATTRIBUTE << " is \"" << ACCESSOR() << "\")" << endl; \
}
void
PluginXml::setParameters(const QXmlAttributes &attrs)
{
CHECK_ATTRIBUTE(identifier, m_plugin->getIdentifier);
CHECK_ATTRIBUTE(name, m_plugin->getName);
CHECK_ATTRIBUTE(description, m_plugin->getDescription);
CHECK_ATTRIBUTE(maker, m_plugin->getMaker);
CHECK_ATTRIBUTE(copyright, m_plugin->getCopyright);
bool ok;
int version = attrs.value("version").trimmed().toInt(&ok);
if (ok && version != m_plugin->getPluginVersion()) {
cerr << "WARNING: PluginXml::setParameters: Plugin version does not match (attributes have " << version << ", my version is " << m_plugin->getPluginVersion() << ")" << endl;
}
RealTimePluginInstance *rtpi =
dynamic_cast<RealTimePluginInstance *>(m_plugin);
if (rtpi) {
QString config = attrs.value("configuration");
if (config != "") {
QStringList configList = config.split(";");
for (QStringList::iterator i = configList.begin();
i != configList.end(); ++i) {
QStringList kv = i->split("=");
if (kv.count() < 2) {
cerr << "WARNING: PluginXml::setParameters: Malformed configure pair string: \"" << *i << "\"" << endl;
continue;
}
QString key(kv[0]), value(kv[1]);
key = decodeConfigurationChars(key);
value = decodeConfigurationChars(value);
rtpi->configure(key.toStdString(), value.toStdString());
}
}
}
if (!m_plugin->getPrograms().empty()) {
m_plugin->selectProgram(attrs.value("program").toStdString());
}
Vamp::PluginBase::ParameterList parameters =
m_plugin->getParameterDescriptors();
for (Vamp::PluginBase::ParameterList::const_iterator i =
parameters.begin(); i != parameters.end(); ++i) {
QString pname = QString("param-%1")
.arg(stripInvalidParameterNameCharacters
(QString(i->identifier.c_str())));
if (attrs.value(pname) == "") {
// SVDEBUG << "PluginXml::setParameters: no parameter \"" << i->name << "\" (attribute \"" << name << "\")" << endl;
continue;
}
bool ok;
float value = attrs.value(pname).trimmed().toFloat(&ok);
if (ok) {
// SVDEBUG << "PluginXml::setParameters: setting parameter \""
// << i->identifier << "\" to value " << value << endl;
m_plugin->setParameter(i->identifier, value);
} else {
cerr << "WARNING: PluginXml::setParameters: Invalid value \"" << attrs.value(pname) << "\" for parameter \"" << i->identifier << "\" (attribute \"" << pname << "\")" << endl;
}
}
}
void
PluginXml::setParametersFromXml(QString xml)
{
QDomDocument doc;
QString error;
int errorLine;
int errorColumn;
// SVDEBUG << "PluginXml::setParametersFromXml: XML is \""
// << xml << "\"" << endl;
if (!doc.setContent(xml, false, &error, &errorLine, &errorColumn)) {
cerr << "PluginXml::setParametersFromXml: Error in parsing XML: " << error << " at line " << errorLine << ", column " << errorColumn << endl;
cerr << "Input follows:" << endl;
cerr << xml << endl;
cerr << "Input ends." << endl;
return;
}
QDomElement pluginElt = doc.firstChildElement("plugin");
QDomNamedNodeMap attrNodes = pluginElt.attributes();
QXmlAttributes attrs;
for (int i = 0; i < attrNodes.length(); ++i) {
QDomAttr attr = attrNodes.item(i).toAttr();
if (attr.isNull()) continue;
// SVDEBUG << "PluginXml::setParametersFromXml: Adding attribute \"" << attr.name()// << "\" with value \"" << attr.value() << "\"" << endl;
attrs.append(attr.name(), "", "", attr.value());
}
setParameters(attrs);
}
QString
PluginXml::stripInvalidParameterNameCharacters(QString s) const
{
s.replace(QRegExp("[^a-zA-Z0-9_]*"), "");
return s;
}
|