annotate vamp-sdk/Plugin.h @ 47:be8fdfe25693

* Change input buffers arg to process from float ** to const float *const * to avoid plugins modifying their input data * Some improvements to comments * Fix stupidity in frequency-domain input passing (there are n/2+1 values, not n/2)
author cannam
date Fri, 08 Dec 2006 17:39:32 +0000
parents 154f86cb8c99
children aa64a46320d4
rev   line source
cannam@3 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@3 2
cannam@3 3 /*
cannam@3 4 Vamp
cannam@3 5
cannam@3 6 An API for audio analysis and feature extraction plugins.
cannam@3 7
cannam@3 8 Centre for Digital Music, Queen Mary, University of London.
cannam@3 9 Copyright 2006 Chris Cannam.
cannam@3 10
cannam@3 11 Permission is hereby granted, free of charge, to any person
cannam@3 12 obtaining a copy of this software and associated documentation
cannam@3 13 files (the "Software"), to deal in the Software without
cannam@3 14 restriction, including without limitation the rights to use, copy,
cannam@3 15 modify, merge, publish, distribute, sublicense, and/or sell copies
cannam@3 16 of the Software, and to permit persons to whom the Software is
cannam@3 17 furnished to do so, subject to the following conditions:
cannam@3 18
cannam@3 19 The above copyright notice and this permission notice shall be
cannam@3 20 included in all copies or substantial portions of the Software.
cannam@3 21
cannam@3 22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@3 23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@3 24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
cannam@6 25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
cannam@3 26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@3 27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@3 28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@3 29
cannam@3 30 Except as contained in this notice, the names of the Centre for
cannam@3 31 Digital Music; Queen Mary, University of London; and Chris Cannam
cannam@3 32 shall not be used in advertising or otherwise to promote the sale,
cannam@3 33 use or other dealings in this Software without prior written
cannam@3 34 authorization.
cannam@3 35 */
cannam@3 36
cannam@3 37 #ifndef _VAMP_PLUGIN_H_
cannam@3 38 #define _VAMP_PLUGIN_H_
cannam@3 39
cannam@3 40 #include "PluginBase.h"
cannam@3 41 #include "RealTime.h"
cannam@3 42
cannam@3 43 #include <string>
cannam@3 44 #include <vector>
cannam@3 45 #include <map>
cannam@3 46
cannam@3 47 namespace Vamp {
cannam@3 48
cannam@3 49 /**
cannam@3 50 * Vamp::Plugin is a base class for plugin instance classes
cannam@3 51 * that provide feature extraction from audio or related data.
cannam@3 52 *
cannam@3 53 * In most cases, the input will be audio and the output will be a
cannam@3 54 * stream of derived data at a lower sampling resolution than the
cannam@3 55 * input.
cannam@3 56 *
cannam@3 57 * Note that this class inherits several abstract methods from
cannam@3 58 * PluginBase, that must be implemented by the subclass.
cannam@3 59 */
cannam@3 60
cannam@3 61 /**
cannam@3 62 * Plugin Lifecycle
cannam@3 63 * ================
cannam@3 64 *
cannam@3 65 * Feature extraction plugins are managed differently from real-time
cannam@3 66 * plugins (such as VST effects). The main difference is that the
cannam@3 67 * parameters for a feature extraction plugin are configured before
cannam@3 68 * the plugin is used, and do not change during use.
cannam@3 69 *
cannam@3 70 * 1. Host constructs the plugin, passing it the input sample rate.
cannam@3 71 * The plugin may do basic initialisation, but should not do anything
cannam@3 72 * computationally expensive at this point.
cannam@3 73 *
cannam@3 74 * 2. Host may query the plugin's available outputs.
cannam@3 75 *
cannam@3 76 * 3. Host queries programs and parameter descriptors, and may set
cannam@3 77 * some or all of them. Parameters that are not explicitly set should
cannam@3 78 * take their default values as specified in the parameter descriptor.
cannam@3 79 * When a program is set, the parameter values may change and the host
cannam@3 80 * will re-query them to check.
cannam@3 81 *
cannam@27 82 * 4. Host queries the preferred step size, block size and number of
cannam@27 83 * channels. These may all vary depending on the parameter values.
cannam@3 84 * (Note however that you cannot make the number of distinct outputs
cannam@27 85 * dependent on parameter values.)
cannam@3 86 *
cannam@3 87 * 5. Plugin is properly initialised with a call to initialise. This
cannam@3 88 * fixes the step size, block size, and number of channels, as well as
cannam@3 89 * all of the parameter and program settings. If the values passed in
cannam@3 90 * to initialise do not match the plugin's advertised preferred values
cannam@3 91 * from step 4, the plugin may refuse to initialise and return false
cannam@35 92 * (although if possible it should accept the new values). Any
cannam@35 93 * computationally expensive setup code should take place here.
cannam@3 94 *
cannam@27 95 * 6. Host finally checks the number of values per output (which may
cannam@27 96 * vary depending on the number of channels, step size and block size
cannam@27 97 * as well as the parameter values).
cannam@27 98 *
cannam@27 99 * 7. Host will repeatedly call the process method to pass in blocks
cannam@3 100 * of input data. This method may return features extracted from that
cannam@3 101 * data (if the plugin is causal).
cannam@3 102 *
cannam@27 103 * 8. Host will call getRemainingFeatures exactly once, after all the
cannam@3 104 * input data has been processed. This may return any non-causal or
cannam@3 105 * leftover features.
cannam@3 106 *
cannam@27 107 * 9. At any point after initialise was called, the host may
cannam@3 108 * optionally call the reset method and restart processing. (This
cannam@3 109 * does not mean it can change the parameters, which are fixed from
cannam@3 110 * initialise until destruction.)
cannam@3 111 *
cannam@3 112 * A plugin does not need to handle the case where setParameter or
cannam@3 113 * selectProgram is called after initialise has been called. It's the
cannam@35 114 * host's responsibility not to do that. Similarly, the plugin may
cannam@35 115 * safely assume that initialise is called no more than once.
cannam@3 116 */
cannam@3 117
cannam@3 118 class Plugin : public PluginBase
cannam@3 119 {
cannam@3 120 public:
cannam@20 121 virtual ~Plugin() { }
cannam@20 122
cannam@3 123 /**
cannam@3 124 * Initialise a plugin to prepare it for use with the given number
cannam@3 125 * of input channels, step size (window increment, in sample
cannam@3 126 * frames) and block size (window size, in sample frames).
cannam@3 127 *
cannam@3 128 * The input sample rate should have been already specified at
cannam@3 129 * construction time.
cannam@3 130 *
cannam@3 131 * Return true for successful initialisation, false if the number
cannam@3 132 * of input channels, step size and/or block size cannot be
cannam@3 133 * supported.
cannam@3 134 */
cannam@3 135 virtual bool initialise(size_t inputChannels,
cannam@3 136 size_t stepSize,
cannam@3 137 size_t blockSize) = 0;
cannam@3 138
cannam@3 139 /**
cannam@3 140 * Reset the plugin after use, to prepare it for another clean
cannam@3 141 * run. Not called for the first initialisation (i.e. initialise
cannam@3 142 * must also do a reset).
cannam@3 143 */
cannam@3 144 virtual void reset() = 0;
cannam@3 145
cannam@3 146 enum InputDomain { TimeDomain, FrequencyDomain };
cannam@3 147
cannam@3 148 /**
cannam@3 149 * Get the plugin's required input domain. If this is TimeDomain,
cannam@3 150 * the samples provided to the process() function (below) will be
cannam@3 151 * in the time domain, as for a traditional audio processing
cannam@3 152 * plugin. If this is FrequencyDomain, the host will carry out a
cannam@3 153 * windowed FFT of size equal to the negotiated block size on the
cannam@3 154 * data before passing the frequency bin data in to process().
cannam@3 155 * The plugin does not get to choose the window type -- the host
cannam@3 156 * will either let the user do so, or will use a Hanning window.
cannam@3 157 */
cannam@3 158 virtual InputDomain getInputDomain() const = 0;
cannam@3 159
cannam@3 160 /**
cannam@8 161 * Get the preferred block size (window size -- the number of
cannam@8 162 * sample frames passed in each block to the process() function).
cannam@8 163 * This should be called before initialise().
cannam@8 164 *
cannam@8 165 * A plugin that can handle any block size may return 0. The
cannam@8 166 * final block size will be set in the initialise() call.
cannam@8 167 */
cannam@8 168 virtual size_t getPreferredBlockSize() const { return 0; }
cannam@8 169
cannam@8 170 /**
cannam@3 171 * Get the preferred step size (window increment -- the distance
cannam@3 172 * in sample frames between the start frames of consecutive blocks
cannam@3 173 * passed to the process() function) for the plugin. This should
cannam@3 174 * be called before initialise().
cannam@8 175 *
cannam@8 176 * A plugin may return 0 if it has no particular interest in the
cannam@8 177 * step size. In this case, the host should make the step size
cannam@8 178 * equal to the block size if the plugin is accepting input in the
cannam@8 179 * time domain. If the plugin is accepting input in the frequency
cannam@8 180 * domain, the host may use any step size. The final step size
cannam@8 181 * will be set in the initialise() call.
cannam@3 182 */
cannam@8 183 virtual size_t getPreferredStepSize() const { return 0; }
cannam@3 184
cannam@3 185 /**
cannam@3 186 * Get the minimum supported number of input channels.
cannam@3 187 */
cannam@3 188 virtual size_t getMinChannelCount() const { return 1; }
cannam@3 189
cannam@3 190 /**
cannam@3 191 * Get the maximum supported number of input channels.
cannam@3 192 */
cannam@3 193 virtual size_t getMaxChannelCount() const { return 1; }
cannam@3 194
cannam@3 195 struct OutputDescriptor
cannam@3 196 {
cannam@3 197 /**
cannam@3 198 * The name of the output, in computer-usable form. Should be
cannam@3 199 * reasonably short and without whitespace or punctuation.
cannam@3 200 */
cannam@3 201 std::string name;
cannam@3 202
cannam@3 203 /**
cannam@3 204 * The human-readable name of the output.
cannam@3 205 */
cannam@3 206 std::string description;
cannam@3 207
cannam@3 208 /**
cannam@3 209 * The unit of the output, in human-readable form.
cannam@3 210 */
cannam@3 211 std::string unit;
cannam@3 212
cannam@3 213 /**
cannam@9 214 * True if the output has the same number of values per sample
cannam@9 215 * for every output sample. Outputs for which this is false
cannam@3 216 * are unlikely to be very useful in a general-purpose host.
cannam@3 217 */
cannam@9 218 bool hasFixedBinCount;
cannam@3 219
cannam@3 220 /**
cannam@3 221 * The number of values per result of the output. Undefined
cannam@9 222 * if hasFixedBinCount is false. If this is zero, the output
cannam@3 223 * is point data (i.e. only the time of each output is of
cannam@3 224 * interest, the value list will be empty).
cannam@3 225 */
cannam@9 226 size_t binCount;
cannam@3 227
cannam@3 228 /**
cannam@9 229 * The names of each of the bins, if appropriate. This is
cannam@3 230 * always optional.
cannam@3 231 */
cannam@9 232 std::vector<std::string> binNames;
cannam@3 233
cannam@3 234 /**
cannam@9 235 * True if the results in each output bin fall within a fixed
cannam@9 236 * numeric range (minimum and maximum values). Undefined if
cannam@9 237 * binCount is zero.
cannam@3 238 */
cannam@3 239 bool hasKnownExtents;
cannam@3 240
cannam@3 241 /**
cannam@3 242 * Minimum value of the results in the output. Undefined if
cannam@9 243 * hasKnownExtents is false or binCount is zero.
cannam@3 244 */
cannam@3 245 float minValue;
cannam@3 246
cannam@3 247 /**
cannam@3 248 * Maximum value of the results in the output. Undefined if
cannam@9 249 * hasKnownExtents is false or binCount is zero.
cannam@3 250 */
cannam@3 251 float maxValue;
cannam@3 252
cannam@3 253 /**
cannam@3 254 * True if the output values are quantized to a particular
cannam@9 255 * resolution. Undefined if binCount is zero.
cannam@3 256 */
cannam@3 257 bool isQuantized;
cannam@3 258
cannam@3 259 /**
cannam@3 260 * Quantization resolution of the output values (e.g. 1.0 if
cannam@3 261 * they are all integers). Undefined if isQuantized is false
cannam@9 262 * or binCount is zero.
cannam@3 263 */
cannam@3 264 float quantizeStep;
cannam@3 265
cannam@3 266 enum SampleType {
cannam@3 267
cannam@3 268 /// Results from each process() align with that call's block start
cannam@3 269 OneSamplePerStep,
cannam@3 270
cannam@3 271 /// Results are evenly spaced in time (sampleRate specified below)
cannam@3 272 FixedSampleRate,
cannam@3 273
cannam@3 274 /// Results are unevenly spaced and have individual timestamps
cannam@3 275 VariableSampleRate
cannam@3 276 };
cannam@3 277
cannam@3 278 /**
cannam@3 279 * Positioning in time of the output results.
cannam@3 280 */
cannam@3 281 SampleType sampleType;
cannam@3 282
cannam@3 283 /**
cannam@17 284 * Sample rate of the output results, as samples per second.
cannam@17 285 * Undefined if sampleType is OneSamplePerStep.
cannam@3 286 *
cannam@3 287 * If sampleType is VariableSampleRate and this value is
cannam@3 288 * non-zero, then it may be used to calculate a resolution for
cannam@17 289 * the output (i.e. the "duration" of each sample, in time,
cannam@17 290 * will be 1/sampleRate seconds). It's recommended to set
cannam@17 291 * this to zero if that behaviour is not desired.
cannam@3 292 */
cannam@3 293 float sampleRate;
cannam@3 294 };
cannam@3 295
cannam@3 296 typedef std::vector<OutputDescriptor> OutputList;
cannam@3 297
cannam@3 298 /**
cannam@3 299 * Get the outputs of this plugin. An output's index in this list
cannam@3 300 * is used as its numeric index when looking it up in the
cannam@3 301 * FeatureSet returned from the process() call.
cannam@3 302 */
cannam@3 303 virtual OutputList getOutputDescriptors() const = 0;
cannam@3 304
cannam@3 305 struct Feature
cannam@3 306 {
cannam@3 307 /**
cannam@3 308 * True if an output feature has its own timestamp. This is
cannam@3 309 * mandatory if the output has VariableSampleRate, and is
cannam@3 310 * likely to be disregarded otherwise.
cannam@3 311 */
cannam@3 312 bool hasTimestamp;
cannam@3 313
cannam@3 314 /**
cannam@3 315 * Timestamp of the output feature. This is mandatory if the
cannam@3 316 * output has VariableSampleRate, and is likely to be
cannam@3 317 * disregarded otherwise. Undefined if hasTimestamp is false.
cannam@3 318 */
cannam@3 319 RealTime timestamp;
cannam@3 320
cannam@3 321 /**
cannam@3 322 * Results for a single sample of this feature. If the output
cannam@9 323 * hasFixedBinCount, there must be the same number of values
cannam@9 324 * as the output's binCount count.
cannam@3 325 */
cannam@3 326 std::vector<float> values;
cannam@3 327
cannam@3 328 /**
cannam@3 329 * Label for the sample of this feature.
cannam@3 330 */
cannam@3 331 std::string label;
cannam@3 332 };
cannam@3 333
cannam@3 334 typedef std::vector<Feature> FeatureList;
cannam@3 335 typedef std::map<int, FeatureList> FeatureSet; // key is output no
cannam@3 336
cannam@3 337 /**
cannam@3 338 * Process a single block of input data.
cannam@3 339 *
cannam@3 340 * If the plugin's inputDomain is TimeDomain, inputBuffers will
cannam@3 341 * point to one array of floats per input channel, and each of
cannam@3 342 * these arrays will contain blockSize consecutive audio samples
cannam@9 343 * (the host will zero-pad as necessary). The timestamp will be
cannam@9 344 * the real time in seconds of the start of the supplied block of
cannam@9 345 * samples.
cannam@3 346 *
cannam@3 347 * If the plugin's inputDomain is FrequencyDomain, inputBuffers
cannam@3 348 * will point to one array of floats per input channel, and each
cannam@47 349 * of these arrays will contain blockSize/2+1 consecutive pairs of
cannam@3 350 * real and imaginary component floats corresponding to bins
cannam@47 351 * 0..(blockSize/2) of the FFT output, where bin 0 contains the DC
cannam@47 352 * output and bin blockSize/2 corresponds to the Nyquist output.
cannam@47 353 * The timestamp will be the real time in seconds of the centre of
cannam@47 354 * the FFT input window (i.e. the very first block passed to
cannam@47 355 * process might contain the FFT of half a block of zero samples
cannam@47 356 * and the first half-block of the actual data, with a timestamp
cannam@47 357 * of zero).
cannam@3 358 *
cannam@3 359 * Return any features that have become available after this
cannam@3 360 * process call. (These do not necessarily have to fall within
cannam@3 361 * the process block, except for OneSamplePerStep outputs.)
cannam@3 362 */
cannam@47 363 virtual FeatureSet process(const float *const *inputBuffers,
cannam@3 364 RealTime timestamp) = 0;
cannam@3 365
cannam@3 366 /**
cannam@3 367 * After all blocks have been processed, calculate and return any
cannam@3 368 * remaining features derived from the complete input.
cannam@3 369 */
cannam@3 370 virtual FeatureSet getRemainingFeatures() = 0;
cannam@3 371
cannam@3 372 virtual std::string getType() const { return "Feature Extraction Plugin"; }
cannam@3 373
cannam@3 374 protected:
cannam@3 375 Plugin(float inputSampleRate) :
cannam@3 376 m_inputSampleRate(inputSampleRate) { }
cannam@3 377
cannam@3 378 float m_inputSampleRate;
cannam@3 379 };
cannam@3 380
cannam@3 381 }
cannam@3 382
cannam@3 383 #endif
cannam@3 384
cannam@3 385
cannam@3 386