annotate osx/include/vamp-sdk/Plugin.h @ 116:e448888319fc

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