annotate transform/TransformFactory.h @ 443:381ec750eeee

* Add beginnings of transform-search-by-text function
author Chris Cannam
date Mon, 22 Sep 2008 15:44:03 +0000
parents 370aa9714ef5
children 14521503f196
rev   line source
Chris@330 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@330 2
Chris@330 3 /*
Chris@330 4 Sonic Visualiser
Chris@330 5 An audio file viewer and annotation editor.
Chris@330 6 Centre for Digital Music, Queen Mary, University of London.
Chris@330 7 This file copyright 2006-2007 Chris Cannam and QMUL.
Chris@330 8
Chris@330 9 This program is free software; you can redistribute it and/or
Chris@330 10 modify it under the terms of the GNU General Public License as
Chris@330 11 published by the Free Software Foundation; either version 2 of the
Chris@330 12 License, or (at your option) any later version. See the file
Chris@330 13 COPYING included with this distribution for more information.
Chris@330 14 */
Chris@330 15
Chris@330 16 #ifndef _TRANSFORM_FACTORY_H_
Chris@330 17 #define _TRANSFORM_FACTORY_H_
Chris@330 18
Chris@330 19 #include "TransformDescription.h"
Chris@330 20
Chris@350 21 #include <vamp-sdk/Plugin.h>
Chris@350 22
Chris@387 23 #include <QObject>
Chris@443 24 #include <QStringList>
Chris@387 25
Chris@330 26 #include <map>
Chris@330 27 #include <set>
Chris@330 28
Chris@330 29 class TransformFactory : public QObject
Chris@330 30 {
Chris@330 31 Q_OBJECT
Chris@330 32
Chris@330 33 public:
Chris@330 34 virtual ~TransformFactory();
Chris@330 35
Chris@330 36 static TransformFactory *getInstance();
Chris@330 37
Chris@350 38 TransformList getAllTransformDescriptions();
Chris@350 39 TransformDescription getTransformDescription(TransformId id);
Chris@330 40
Chris@330 41 std::vector<QString> getAllTransformTypes();
Chris@330 42 std::vector<QString> getTransformCategories(QString transformType);
Chris@330 43 std::vector<QString> getTransformMakers(QString transformType);
Chris@330 44
Chris@443 45 struct Match
Chris@443 46 {
Chris@443 47 TransformId transform;
Chris@443 48 int score;
Chris@443 49 QStringList fragments;
Chris@443 50
Chris@443 51 Match() : score(0) { }
Chris@443 52 Match(const Match &m) : transform(m.transform),
Chris@443 53 score(m.score), fragments(m.fragments) { }
Chris@443 54
Chris@443 55 bool operator<(const Match &m) {
Chris@443 56 if (score != m.score) {
Chris@443 57 return score < m.score;
Chris@443 58 } else if (transform != m.transform) {
Chris@443 59 return transform < m.transform;
Chris@443 60 } else if (fragments.size() != m.fragments.size()) {
Chris@443 61 return fragments.size() < m.fragments.size();
Chris@443 62 } else {
Chris@443 63 for (int i = 0; i < fragments.size(); ++i) {
Chris@443 64 if (fragments[i] != m.fragments[i]) {
Chris@443 65 return fragments[i] < m.fragments[i];
Chris@443 66 }
Chris@443 67 }
Chris@443 68 }
Chris@443 69 return false;
Chris@443 70 }
Chris@443 71 };
Chris@443 72
Chris@443 73 typedef std::map<TransformId, Match> SearchResults;
Chris@443 74 SearchResults search(QString keyword);
Chris@443 75 SearchResults search(QStringList keywords);
Chris@443 76
Chris@330 77 /**
Chris@330 78 * Return true if the given transform is known.
Chris@330 79 */
Chris@330 80 bool haveTransform(TransformId identifier);
Chris@330 81
Chris@330 82 /**
Chris@350 83 * A single transform ID can lead to many possible Transforms,
Chris@350 84 * with different parameters and execution context settings.
Chris@350 85 * Return the default one for the given transform.
Chris@350 86 */
Chris@350 87 Transform getDefaultTransformFor(TransformId identifier, size_t rate = 0);
Chris@350 88
Chris@350 89 /**
Chris@330 90 * Full name of a transform, suitable for putting on a menu.
Chris@330 91 */
Chris@330 92 QString getTransformName(TransformId identifier);
Chris@330 93
Chris@330 94 /**
Chris@330 95 * Brief but friendly name of a transform, suitable for use
Chris@330 96 * as the name of the output layer.
Chris@330 97 */
Chris@330 98 QString getTransformFriendlyName(TransformId identifier);
Chris@330 99
Chris@330 100 QString getTransformUnits(TransformId identifier);
Chris@330 101
Chris@350 102 Vamp::Plugin::InputDomain getTransformInputDomain(TransformId identifier);
Chris@350 103
Chris@330 104 /**
Chris@330 105 * Return true if the transform has any configurable parameters,
Chris@330 106 * i.e. if getConfigurationForTransform can ever return a non-trivial
Chris@330 107 * (not equivalent to empty) configuration string.
Chris@330 108 */
Chris@330 109 bool isTransformConfigurable(TransformId identifier);
Chris@330 110
Chris@330 111 /**
Chris@330 112 * If the transform has a prescribed number or range of channel
Chris@330 113 * inputs, return true and set minChannels and maxChannels to the
Chris@330 114 * minimum and maximum number of channel inputs the transform can
Chris@330 115 * accept. Return false if it doesn't care.
Chris@330 116 */
Chris@330 117 bool getTransformChannelRange(TransformId identifier,
Chris@330 118 int &minChannels, int &maxChannels);
Chris@332 119
Chris@332 120 /**
Chris@351 121 * Load an appropriate plugin for the given transform and set the
Chris@351 122 * parameters, program and configuration strings on that plugin
Chris@351 123 * from the Transform object.
Chris@351 124 *
Chris@351 125 * Note that this requires that the transform has a meaningful
Chris@351 126 * sample rate set, as that is used as the rate for the plugin. A
Chris@351 127 * Transform can legitimately have rate set at zero (= "use the
Chris@351 128 * rate of the input source"), so the caller will need to test for
Chris@351 129 * this case.
Chris@351 130 *
Chris@351 131 * Returns the plugin thus loaded. This will be a
Chris@351 132 * Vamp::PluginBase, but not necessarily a Vamp::Plugin (only if
Chris@351 133 * the transform was a feature-extraction type -- call
Chris@351 134 * downcastVampPlugin if you only want Vamp::Plugins). Returns
Chris@351 135 * NULL if no suitable plugin was available.
Chris@352 136 *
Chris@352 137 * The returned plugin is owned by the caller, and should be
Chris@352 138 * deleted (using "delete") when no longer needed.
Chris@351 139 */
Chris@351 140 Vamp::PluginBase *instantiatePluginFor(const Transform &transform);
Chris@351 141
Chris@351 142 /**
Chris@351 143 * Convert a Vamp::PluginBase to a Vamp::Plugin, if it is one.
Chris@351 144 * Return NULL otherwise. This ill-fitting convenience function
Chris@351 145 * is really just a dynamic_cast wrapper.
Chris@351 146 */
Chris@351 147 Vamp::Plugin *downcastVampPlugin(Vamp::PluginBase *);
Chris@351 148
Chris@351 149 /**
Chris@332 150 * Set the plugin parameters, program and configuration strings on
Chris@332 151 * the given Transform object from the given plugin instance.
Chris@332 152 * Note that no check is made whether the plugin is actually the
Chris@332 153 * "correct" one for the transform.
Chris@332 154 */
Chris@332 155 void setParametersFromPlugin(Transform &transform, Vamp::PluginBase *plugin);
Chris@332 156
Chris@332 157 /**
Chris@350 158 * Set the parameters, program and configuration strings on the
Chris@350 159 * given plugin from the given Transform object.
Chris@350 160 */
Chris@350 161 void setPluginParameters(const Transform &transform, Vamp::PluginBase *plugin);
Chris@350 162
Chris@350 163 /**
Chris@332 164 * If the given Transform object has no processing step and block
Chris@332 165 * sizes set, set them to appropriate defaults for the given
Chris@332 166 * plugin.
Chris@332 167 */
Chris@332 168 void makeContextConsistentWithPlugin(Transform &transform, Vamp::PluginBase *plugin);
Chris@332 169
Chris@332 170 /**
Chris@350 171 * Retrieve a <plugin ... /> XML fragment that describes the
Chris@350 172 * plugin parameters, program and configuration data for the given
Chris@350 173 * transform.
Chris@350 174 *
Chris@350 175 * This function is provided for backward compatibility only. Use
Chris@350 176 * Transform::toXml where compatibility with PluginXml
Chris@350 177 * descriptions of transforms is not required.
Chris@332 178 */
Chris@350 179 QString getPluginConfigurationXml(const Transform &transform);
Chris@350 180
Chris@350 181 /**
Chris@350 182 * Set the plugin parameters, program and configuration strings on
Chris@350 183 * the given Transform object from the given <plugin ... /> XML
Chris@350 184 * fragment.
Chris@350 185 *
Chris@350 186 * This function is provided for backward compatibility only. Use
Chris@350 187 * Transform(QString) where compatibility with PluginXml
Chris@350 188 * descriptions of transforms is not required.
Chris@350 189 */
Chris@350 190 void setParametersFromPluginConfigurationXml(Transform &transform,
Chris@350 191 QString xml);
Chris@332 192
Chris@330 193 protected:
Chris@330 194 typedef std::map<TransformId, TransformDescription> TransformDescriptionMap;
Chris@330 195 TransformDescriptionMap m_transforms;
Chris@330 196
Chris@330 197 void populateTransforms();
Chris@330 198 void populateFeatureExtractionPlugins(TransformDescriptionMap &);
Chris@330 199 void populateRealTimePlugins(TransformDescriptionMap &);
Chris@330 200
Chris@443 201 void searchTest(Match &match, QStringList keywords, QString text,
Chris@443 202 QString textType, int score);
Chris@443 203
Chris@351 204 Vamp::PluginBase *instantiateDefaultPluginFor(TransformId id, size_t rate);
Chris@350 205
Chris@330 206 static TransformFactory *m_instance;
Chris@330 207 };
Chris@330 208
Chris@330 209
Chris@330 210 #endif