annotate osx/include/vamp-sdk/Plugin.h @ 26:213792ad6ace

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