annotate transform/TransformFactory.h @ 683:f84f147572b9

Avoid crash when generating/processing a very short file
author Chris Cannam
date Wed, 11 May 2011 11:04:02 +0100
parents 2d551c765d51
children 06579b8ffb7b
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@475 23 #include <vamp-hostsdk/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@574 42 static void deleteInstance(); // only when exiting
Chris@330 43
Chris@477 44 /**
Chris@477 45 * TransformFactory has a background thread that can populate
Chris@477 46 * uninstalled transforms from network RDF resources. It is not
Chris@477 47 * started by default, but it's a good idea to start it when the
Chris@477 48 * program starts up, if the uninstalled transforms may be of use
Chris@477 49 * later; otherwise there will be a bottleneck the first time
Chris@477 50 * they're requested.
Chris@477 51 *
Chris@477 52 * If this thread is not already running, start it now.
Chris@477 53 */
Chris@477 54 void startPopulationThread();
Chris@477 55
Chris@350 56 TransformList getAllTransformDescriptions();
Chris@350 57 TransformDescription getTransformDescription(TransformId id);
Chris@485 58 bool haveInstalledTransforms();
Chris@330 59
Chris@457 60 TransformList getUninstalledTransformDescriptions();
Chris@457 61 TransformDescription getUninstalledTransformDescription(TransformId id);
Chris@485 62 bool haveUninstalledTransforms(bool waitForCheckToComplete = false);
Chris@457 63
Chris@457 64 typedef enum {
Chris@457 65 TransformUnknown,
Chris@457 66 TransformInstalled,
Chris@457 67 TransformNotInstalled
Chris@457 68 } TransformInstallStatus;
Chris@457 69
Chris@457 70 TransformInstallStatus getTransformInstallStatus(TransformId id);
Chris@457 71
Chris@487 72 std::vector<TransformDescription::Type> getAllTransformTypes();
Chris@487 73 std::vector<QString> getTransformCategories(TransformDescription::Type);
Chris@487 74 std::vector<QString> getTransformMakers(TransformDescription::Type);
Chris@487 75 QString getTransformTypeName(TransformDescription::Type) const;
Chris@330 76
Chris@457 77 typedef std::map<TransformId, TextMatcher::Match> SearchResults;
Chris@443 78 SearchResults search(QString keyword);
Chris@443 79 SearchResults search(QStringList keywords);
Chris@443 80
Chris@330 81 /**
Chris@330 82 * Return true if the given transform is known.
Chris@330 83 */
Chris@330 84 bool haveTransform(TransformId identifier);
Chris@330 85
Chris@330 86 /**
Chris@350 87 * A single transform ID can lead to many possible Transforms,
Chris@350 88 * with different parameters and execution context settings.
Chris@350 89 * Return the default one for the given transform.
Chris@350 90 */
Chris@350 91 Transform getDefaultTransformFor(TransformId identifier, size_t rate = 0);
Chris@350 92
Chris@350 93 /**
Chris@330 94 * Full name of a transform, suitable for putting on a menu.
Chris@330 95 */
Chris@330 96 QString getTransformName(TransformId identifier);
Chris@330 97
Chris@330 98 /**
Chris@330 99 * Brief but friendly name of a transform, suitable for use
Chris@330 100 * as the name of the output layer.
Chris@330 101 */
Chris@330 102 QString getTransformFriendlyName(TransformId identifier);
Chris@330 103
Chris@330 104 QString getTransformUnits(TransformId identifier);
Chris@330 105
Chris@472 106 QString getTransformInfoUrl(TransformId identifier);
Chris@472 107
Chris@350 108 Vamp::Plugin::InputDomain getTransformInputDomain(TransformId identifier);
Chris@350 109
Chris@330 110 /**
Chris@330 111 * Return true if the transform has any configurable parameters,
Chris@330 112 * i.e. if getConfigurationForTransform can ever return a non-trivial
Chris@330 113 * (not equivalent to empty) configuration string.
Chris@330 114 */
Chris@330 115 bool isTransformConfigurable(TransformId identifier);
Chris@330 116
Chris@330 117 /**
Chris@330 118 * If the transform has a prescribed number or range of channel
Chris@330 119 * inputs, return true and set minChannels and maxChannels to the
Chris@330 120 * minimum and maximum number of channel inputs the transform can
Chris@330 121 * accept. Return false if it doesn't care.
Chris@330 122 */
Chris@330 123 bool getTransformChannelRange(TransformId identifier,
Chris@330 124 int &minChannels, int &maxChannels);
Chris@332 125
Chris@332 126 /**
Chris@351 127 * Load an appropriate plugin for the given transform and set the
Chris@351 128 * parameters, program and configuration strings on that plugin
Chris@351 129 * from the Transform object.
Chris@351 130 *
Chris@351 131 * Note that this requires that the transform has a meaningful
Chris@351 132 * sample rate set, as that is used as the rate for the plugin. A
Chris@351 133 * Transform can legitimately have rate set at zero (= "use the
Chris@351 134 * rate of the input source"), so the caller will need to test for
Chris@351 135 * this case.
Chris@351 136 *
Chris@351 137 * Returns the plugin thus loaded. This will be a
Chris@351 138 * Vamp::PluginBase, but not necessarily a Vamp::Plugin (only if
Chris@351 139 * the transform was a feature-extraction type -- call
Chris@351 140 * downcastVampPlugin if you only want Vamp::Plugins). Returns
Chris@351 141 * NULL if no suitable plugin was available.
Chris@352 142 *
Chris@352 143 * The returned plugin is owned by the caller, and should be
Chris@352 144 * deleted (using "delete") when no longer needed.
Chris@351 145 */
Chris@351 146 Vamp::PluginBase *instantiatePluginFor(const Transform &transform);
Chris@351 147
Chris@351 148 /**
Chris@351 149 * Convert a Vamp::PluginBase to a Vamp::Plugin, if it is one.
Chris@351 150 * Return NULL otherwise. This ill-fitting convenience function
Chris@351 151 * is really just a dynamic_cast wrapper.
Chris@351 152 */
Chris@351 153 Vamp::Plugin *downcastVampPlugin(Vamp::PluginBase *);
Chris@351 154
Chris@351 155 /**
Chris@332 156 * Set the plugin parameters, program and configuration strings on
Chris@332 157 * the given Transform object from the given plugin instance.
Chris@332 158 * Note that no check is made whether the plugin is actually the
Chris@332 159 * "correct" one for the transform.
Chris@332 160 */
Chris@332 161 void setParametersFromPlugin(Transform &transform, Vamp::PluginBase *plugin);
Chris@332 162
Chris@332 163 /**
Chris@350 164 * Set the parameters, program and configuration strings on the
Chris@350 165 * given plugin from the given Transform object.
Chris@350 166 */
Chris@350 167 void setPluginParameters(const Transform &transform, Vamp::PluginBase *plugin);
Chris@350 168
Chris@350 169 /**
Chris@332 170 * If the given Transform object has no processing step and block
Chris@332 171 * sizes set, set them to appropriate defaults for the given
Chris@332 172 * plugin.
Chris@332 173 */
Chris@332 174 void makeContextConsistentWithPlugin(Transform &transform, Vamp::PluginBase *plugin);
Chris@332 175
Chris@332 176 /**
Chris@350 177 * Retrieve a <plugin ... /> XML fragment that describes the
Chris@350 178 * plugin parameters, program and configuration data for the given
Chris@350 179 * transform.
Chris@350 180 *
Chris@350 181 * This function is provided for backward compatibility only. Use
Chris@350 182 * Transform::toXml where compatibility with PluginXml
Chris@350 183 * descriptions of transforms is not required.
Chris@332 184 */
Chris@350 185 QString getPluginConfigurationXml(const Transform &transform);
Chris@350 186
Chris@350 187 /**
Chris@350 188 * Set the plugin parameters, program and configuration strings on
Chris@350 189 * the given Transform object from the given <plugin ... /> XML
Chris@350 190 * fragment.
Chris@350 191 *
Chris@350 192 * This function is provided for backward compatibility only. Use
Chris@350 193 * Transform(QString) where compatibility with PluginXml
Chris@350 194 * descriptions of transforms is not required.
Chris@350 195 */
Chris@350 196 void setParametersFromPluginConfigurationXml(Transform &transform,
Chris@350 197 QString xml);
Chris@332 198
Chris@330 199 protected:
Chris@330 200 typedef std::map<TransformId, TransformDescription> TransformDescriptionMap;
Chris@457 201
Chris@330 202 TransformDescriptionMap m_transforms;
Chris@457 203 bool m_transformsPopulated;
Chris@457 204
Chris@457 205 TransformDescriptionMap m_uninstalledTransforms;
Chris@457 206 bool m_uninstalledTransformsPopulated;
Chris@330 207
Chris@330 208 void populateTransforms();
Chris@457 209 void populateUninstalledTransforms();
Chris@330 210 void populateFeatureExtractionPlugins(TransformDescriptionMap &);
Chris@330 211 void populateRealTimePlugins(TransformDescriptionMap &);
Chris@330 212
Chris@351 213 Vamp::PluginBase *instantiateDefaultPluginFor(TransformId id, size_t rate);
Chris@460 214 QMutex m_transformsMutex;
Chris@460 215 QMutex m_uninstalledTransformsMutex;
Chris@460 216
Chris@460 217 class UninstalledTransformsPopulateThread : public QThread
Chris@460 218 {
Chris@460 219 public:
Chris@460 220 UninstalledTransformsPopulateThread(TransformFactory *factory) :
Chris@460 221 m_factory(factory) {
Chris@460 222 }
Chris@481 223 virtual void run();
Chris@460 224 TransformFactory *m_factory;
Chris@460 225 };
Chris@460 226
Chris@477 227 UninstalledTransformsPopulateThread *m_thread;
Chris@574 228 bool m_exiting;
Chris@479 229 bool m_populatingSlowly;
Chris@477 230
Chris@330 231 static TransformFactory *m_instance;
Chris@330 232 };
Chris@330 233
Chris@330 234
Chris@330 235 #endif