annotate transform/TransformFactory.h @ 460:93fb1ebff76b

* Add persistent cache file support to FileSource (e.g. for RDF descriptions) * Query RDF plugin data in a background thread on startup
author Chris Cannam
date Fri, 17 Oct 2008 13:32:55 +0000
parents ef14acd6d102
children 9525c9d7e54d
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@457 21 #include "base/TextMatcher.h"
Chris@457 22
Chris@350 23 #include <vamp-sdk/Plugin.h>
Chris@350 24
Chris@387 25 #include <QObject>
Chris@443 26 #include <QStringList>
Chris@460 27 #include <QThread>
Chris@460 28 #include <QMutex>
Chris@387 29
Chris@330 30 #include <map>
Chris@330 31 #include <set>
Chris@330 32
Chris@330 33 class TransformFactory : public QObject
Chris@330 34 {
Chris@330 35 Q_OBJECT
Chris@330 36
Chris@330 37 public:
Chris@457 38 TransformFactory();
Chris@330 39 virtual ~TransformFactory();
Chris@330 40
Chris@330 41 static TransformFactory *getInstance();
Chris@330 42
Chris@350 43 TransformList getAllTransformDescriptions();
Chris@350 44 TransformDescription getTransformDescription(TransformId id);
Chris@330 45
Chris@457 46 TransformList getUninstalledTransformDescriptions();
Chris@457 47 TransformDescription getUninstalledTransformDescription(TransformId id);
Chris@457 48
Chris@457 49 typedef enum {
Chris@457 50 TransformUnknown,
Chris@457 51 TransformInstalled,
Chris@457 52 TransformNotInstalled
Chris@457 53 } TransformInstallStatus;
Chris@457 54
Chris@457 55 TransformInstallStatus getTransformInstallStatus(TransformId id);
Chris@457 56
Chris@330 57 std::vector<QString> getAllTransformTypes();
Chris@330 58 std::vector<QString> getTransformCategories(QString transformType);
Chris@330 59 std::vector<QString> getTransformMakers(QString transformType);
Chris@330 60
Chris@457 61 typedef std::map<TransformId, TextMatcher::Match> SearchResults;
Chris@443 62 SearchResults search(QString keyword);
Chris@443 63 SearchResults search(QStringList keywords);
Chris@443 64
Chris@330 65 /**
Chris@330 66 * Return true if the given transform is known.
Chris@330 67 */
Chris@330 68 bool haveTransform(TransformId identifier);
Chris@330 69
Chris@330 70 /**
Chris@350 71 * A single transform ID can lead to many possible Transforms,
Chris@350 72 * with different parameters and execution context settings.
Chris@350 73 * Return the default one for the given transform.
Chris@350 74 */
Chris@350 75 Transform getDefaultTransformFor(TransformId identifier, size_t rate = 0);
Chris@350 76
Chris@350 77 /**
Chris@330 78 * Full name of a transform, suitable for putting on a menu.
Chris@330 79 */
Chris@330 80 QString getTransformName(TransformId identifier);
Chris@330 81
Chris@330 82 /**
Chris@330 83 * Brief but friendly name of a transform, suitable for use
Chris@330 84 * as the name of the output layer.
Chris@330 85 */
Chris@330 86 QString getTransformFriendlyName(TransformId identifier);
Chris@330 87
Chris@330 88 QString getTransformUnits(TransformId identifier);
Chris@330 89
Chris@350 90 Vamp::Plugin::InputDomain getTransformInputDomain(TransformId identifier);
Chris@350 91
Chris@330 92 /**
Chris@330 93 * Return true if the transform has any configurable parameters,
Chris@330 94 * i.e. if getConfigurationForTransform can ever return a non-trivial
Chris@330 95 * (not equivalent to empty) configuration string.
Chris@330 96 */
Chris@330 97 bool isTransformConfigurable(TransformId identifier);
Chris@330 98
Chris@330 99 /**
Chris@330 100 * If the transform has a prescribed number or range of channel
Chris@330 101 * inputs, return true and set minChannels and maxChannels to the
Chris@330 102 * minimum and maximum number of channel inputs the transform can
Chris@330 103 * accept. Return false if it doesn't care.
Chris@330 104 */
Chris@330 105 bool getTransformChannelRange(TransformId identifier,
Chris@330 106 int &minChannels, int &maxChannels);
Chris@332 107
Chris@332 108 /**
Chris@351 109 * Load an appropriate plugin for the given transform and set the
Chris@351 110 * parameters, program and configuration strings on that plugin
Chris@351 111 * from the Transform object.
Chris@351 112 *
Chris@351 113 * Note that this requires that the transform has a meaningful
Chris@351 114 * sample rate set, as that is used as the rate for the plugin. A
Chris@351 115 * Transform can legitimately have rate set at zero (= "use the
Chris@351 116 * rate of the input source"), so the caller will need to test for
Chris@351 117 * this case.
Chris@351 118 *
Chris@351 119 * Returns the plugin thus loaded. This will be a
Chris@351 120 * Vamp::PluginBase, but not necessarily a Vamp::Plugin (only if
Chris@351 121 * the transform was a feature-extraction type -- call
Chris@351 122 * downcastVampPlugin if you only want Vamp::Plugins). Returns
Chris@351 123 * NULL if no suitable plugin was available.
Chris@352 124 *
Chris@352 125 * The returned plugin is owned by the caller, and should be
Chris@352 126 * deleted (using "delete") when no longer needed.
Chris@351 127 */
Chris@351 128 Vamp::PluginBase *instantiatePluginFor(const Transform &transform);
Chris@351 129
Chris@351 130 /**
Chris@351 131 * Convert a Vamp::PluginBase to a Vamp::Plugin, if it is one.
Chris@351 132 * Return NULL otherwise. This ill-fitting convenience function
Chris@351 133 * is really just a dynamic_cast wrapper.
Chris@351 134 */
Chris@351 135 Vamp::Plugin *downcastVampPlugin(Vamp::PluginBase *);
Chris@351 136
Chris@351 137 /**
Chris@332 138 * Set the plugin parameters, program and configuration strings on
Chris@332 139 * the given Transform object from the given plugin instance.
Chris@332 140 * Note that no check is made whether the plugin is actually the
Chris@332 141 * "correct" one for the transform.
Chris@332 142 */
Chris@332 143 void setParametersFromPlugin(Transform &transform, Vamp::PluginBase *plugin);
Chris@332 144
Chris@332 145 /**
Chris@350 146 * Set the parameters, program and configuration strings on the
Chris@350 147 * given plugin from the given Transform object.
Chris@350 148 */
Chris@350 149 void setPluginParameters(const Transform &transform, Vamp::PluginBase *plugin);
Chris@350 150
Chris@350 151 /**
Chris@332 152 * If the given Transform object has no processing step and block
Chris@332 153 * sizes set, set them to appropriate defaults for the given
Chris@332 154 * plugin.
Chris@332 155 */
Chris@332 156 void makeContextConsistentWithPlugin(Transform &transform, Vamp::PluginBase *plugin);
Chris@332 157
Chris@332 158 /**
Chris@350 159 * Retrieve a <plugin ... /> XML fragment that describes the
Chris@350 160 * plugin parameters, program and configuration data for the given
Chris@350 161 * transform.
Chris@350 162 *
Chris@350 163 * This function is provided for backward compatibility only. Use
Chris@350 164 * Transform::toXml where compatibility with PluginXml
Chris@350 165 * descriptions of transforms is not required.
Chris@332 166 */
Chris@350 167 QString getPluginConfigurationXml(const Transform &transform);
Chris@350 168
Chris@350 169 /**
Chris@350 170 * Set the plugin parameters, program and configuration strings on
Chris@350 171 * the given Transform object from the given <plugin ... /> XML
Chris@350 172 * fragment.
Chris@350 173 *
Chris@350 174 * This function is provided for backward compatibility only. Use
Chris@350 175 * Transform(QString) where compatibility with PluginXml
Chris@350 176 * descriptions of transforms is not required.
Chris@350 177 */
Chris@350 178 void setParametersFromPluginConfigurationXml(Transform &transform,
Chris@350 179 QString xml);
Chris@332 180
Chris@330 181 protected:
Chris@330 182 typedef std::map<TransformId, TransformDescription> TransformDescriptionMap;
Chris@457 183
Chris@330 184 TransformDescriptionMap m_transforms;
Chris@457 185 bool m_transformsPopulated;
Chris@457 186
Chris@457 187 TransformDescriptionMap m_uninstalledTransforms;
Chris@457 188 bool m_uninstalledTransformsPopulated;
Chris@330 189
Chris@330 190 void populateTransforms();
Chris@457 191 void populateUninstalledTransforms();
Chris@330 192 void populateFeatureExtractionPlugins(TransformDescriptionMap &);
Chris@330 193 void populateRealTimePlugins(TransformDescriptionMap &);
Chris@330 194
Chris@351 195 Vamp::PluginBase *instantiateDefaultPluginFor(TransformId id, size_t rate);
Chris@350 196
Chris@460 197 QMutex m_transformsMutex;
Chris@460 198 QMutex m_uninstalledTransformsMutex;
Chris@460 199
Chris@460 200 class UninstalledTransformsPopulateThread : public QThread
Chris@460 201 {
Chris@460 202 public:
Chris@460 203 UninstalledTransformsPopulateThread(TransformFactory *factory) :
Chris@460 204 m_factory(factory) {
Chris@460 205 }
Chris@460 206 virtual void run() {
Chris@460 207 m_factory->populateUninstalledTransforms();
Chris@460 208 }
Chris@460 209 TransformFactory *m_factory;
Chris@460 210 };
Chris@460 211
Chris@330 212 static TransformFactory *m_instance;
Chris@330 213 };
Chris@330 214
Chris@330 215
Chris@330 216 #endif