annotate win32-mingw/include/vamp-sdk/Plugin.h @ 14:48209aa7aa51

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