annotate plugin/PluginInstance.cpp @ 60:3086ff194ea0

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