rt-vamp-plugin-sdk  0.3.1
Real-time Vamp plugin SDK for C++20
Plugin.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <array>
4 #include <complex>
5 #include <cstdint>
6 #include <optional>
7 #include <span>
8 #include <string>
9 #include <string_view>
10 #include <variant>
11 #include <vector>
12 
13 // Vamp C API uses unsigned int as size type (blockSize, stepSize, channelCount, outputCount, ...).
14 // Make sure it has at least 32 bit and use uint32_t as size type in C++ interfaces.
15 static_assert(sizeof(unsigned int) >= sizeof(uint32_t), "Size type must have at least 32 bit");
16 
17 namespace rtvamp::pluginsdk {
18 
22 class PluginBase {
23 public:
24  virtual ~PluginBase() = default;
25 
27  enum class InputDomain { Time, Frequency };
28 
30  // use const char* for compile-time evaluation and mapping to C API
31  const char* identifier = "";
32  const char* name = "";
33  const char* description = "";
34  const char* unit = "";
35  float defaultValue = 0.0f;
36  float minValue = 0.0f;
37  float maxValue = 0.0f;
38  std::optional<float> quantizeStep = std::nullopt;
39  // std::vector<const char*> valueNames{}; // currently not possible -> wait for constexpr vectors
40  };
41 
43  std::string identifier;
44  std::string name;
45  std::string description;
46  std::string unit;
47  uint32_t binCount = 1;
48  std::vector<std::string> binNames{};
49  bool hasKnownExtents = false;
50  float minValue = 0.0f;
51  float maxValue = 0.0f;
52  std::optional<float> quantizeStep = std::nullopt;
53  };
54 
55  using TimeDomainBuffer = std::span<const float>;
56  using FrequencyDomainBuffer = std::span<const std::complex<float>>;
57  using InputBuffer = std::variant<TimeDomainBuffer, FrequencyDomainBuffer>;
58  using Feature = std::vector<float>;
59 };
60 
72 template <uint32_t NOutputs>
73 class Plugin : public PluginBase {
74 public:
75  explicit constexpr Plugin(float inputSampleRate) : inputSampleRate_(inputSampleRate) {}
76 
77  using OutputList = std::array<OutputDescriptor, NOutputs>;
78  using FeatureSet = std::array<Feature, NOutputs>;
79 
80  static constexpr uint32_t outputCount = NOutputs;
81 
83  struct Meta {
84  const char* identifier = "";
85  const char* name = "";
86  const char* description = "";
87  const char* maker = "";
88  const char* copyright = "";
89  int pluginVersion = 1;
91  };
92 
93  static constexpr Meta meta{};
94  static constexpr std::array<ParameterDescriptor, 0> parameters{};
95  static constexpr std::array<const char*, 0> programs{};
96 
97  virtual std::optional<float> getParameter(std::string_view id) const { return {}; }
98  virtual bool setParameter(std::string_view id, float value) { return false; }
99 
100  virtual std::string_view getCurrentProgram() const { return {}; }
101  virtual bool selectProgram(std::string_view name) { return false; }
102 
103  virtual uint32_t getPreferredStepSize() const { return 0; }
104  virtual uint32_t getPreferredBlockSize() const { return 0; }
105 
106  virtual OutputList getOutputDescriptors() const = 0;
107 
108  virtual bool initialise(uint32_t stepSize, uint32_t blockSize) = 0;
109  virtual void reset() = 0;
110  virtual const FeatureSet& process(InputBuffer buffer, uint64_t nsec) = 0;
111 
112 protected:
113  float getInputSampleRate() const noexcept { return inputSampleRate_; };
114  FeatureSet& getFeatureSet() noexcept { return featureSet_; }
115  void initialiseFeatureSet();
116 
117 private:
118  const float inputSampleRate_;
119  FeatureSet featureSet_;
120 };
121 
122 /* --------------------------------------- Implementation --------------------------------------- */
123 
124 template <uint32_t NOutputs>
126  const auto outputs = getOutputDescriptors();
127  auto& featureSet = getFeatureSet();
128  for (size_t i = 0; i < outputCount; ++i) {
129  featureSet[i].resize(outputs[i].binCount);
130  }
131 }
132 
133 /* ------------------------------------------- Concept ------------------------------------------ */
134 
135 namespace detail {
136 
141 template <typename T, size_t MaxOutputCount = 32>
142 consteval bool isPlugin() {
143  return []<std::size_t... Ns>(std::index_sequence<Ns...>) {
144  return std::disjunction<
145  std::is_base_of<Plugin<Ns>, T>...
146  >::value;
147  }(std::make_index_sequence<MaxOutputCount>{});
148 }
149 
150 template <typename T>
151 concept IsPlugin = detail::isPlugin<T>();
152 
153 } // namespace detail
154 
155 } // namespace rtvamp::pluginsdk
rtvamp::pluginsdk::Plugin::meta
static constexpr Meta meta
Required static plugin descriptor.
Definition: Plugin.hpp:93
rtvamp::pluginsdk::PluginBase::OutputDescriptor::binNames
std::vector< std::string > binNames
Definition: Plugin.hpp:48
rtvamp::pluginsdk::Plugin::Meta::pluginVersion
int pluginVersion
Definition: Plugin.hpp:89
rtvamp::pluginsdk::Plugin::getPreferredBlockSize
virtual uint32_t getPreferredBlockSize() const
Definition: Plugin.hpp:104
rtvamp::pluginsdk::PluginBase
Non-templated plugin base class with type definitions.
Definition: Plugin.hpp:22
rtvamp::pluginsdk::PluginBase::ParameterDescriptor::unit
const char * unit
Definition: Plugin.hpp:34
rtvamp::pluginsdk::PluginBase::Feature
std::vector< float > Feature
Feature with one or more values (defined by OutputDescriptor::binCount)
Definition: Plugin.hpp:58
rtvamp::pluginsdk::Plugin::getPreferredStepSize
virtual uint32_t getPreferredStepSize() const
Definition: Plugin.hpp:103
rtvamp::pluginsdk::Plugin::getParameter
virtual std::optional< float > getParameter(std::string_view id) const
Definition: Plugin.hpp:97
rtvamp::pluginsdk::PluginBase::OutputDescriptor::hasKnownExtents
bool hasKnownExtents
Definition: Plugin.hpp:49
rtvamp::pluginsdk::PluginBase::TimeDomainBuffer
std::span< const float > TimeDomainBuffer
Time domain buffer.
Definition: Plugin.hpp:55
rtvamp::pluginsdk::PluginBase::ParameterDescriptor::minValue
float minValue
Definition: Plugin.hpp:36
rtvamp::pluginsdk::PluginBase::OutputDescriptor::quantizeStep
std::optional< float > quantizeStep
Definition: Plugin.hpp:52
rtvamp::pluginsdk::Plugin::initialise
virtual bool initialise(uint32_t stepSize, uint32_t blockSize)=0
rtvamp::pluginsdk::PluginBase::ParameterDescriptor::defaultValue
float defaultValue
Definition: Plugin.hpp:35
rtvamp::pluginsdk::Plugin::setParameter
virtual bool setParameter(std::string_view id, float value)
Definition: Plugin.hpp:98
rtvamp::pluginsdk::Plugin::parameters
static constexpr std::array< ParameterDescriptor, 0 > parameters
Optional parameter descriptors (default: none)
Definition: Plugin.hpp:94
rtvamp::pluginsdk::Plugin::OutputList
std::array< OutputDescriptor, NOutputs > OutputList
List of output descriptors.
Definition: Plugin.hpp:77
rtvamp::pluginsdk::Plugin::selectProgram
virtual bool selectProgram(std::string_view name)
Definition: Plugin.hpp:101
rtvamp::pluginsdk::PluginBase::OutputDescriptor::maxValue
float maxValue
Definition: Plugin.hpp:51
rtvamp::pluginsdk::Plugin::getInputSampleRate
float getInputSampleRate() const noexcept
Definition: Plugin.hpp:113
rtvamp::pluginsdk::PluginBase::ParameterDescriptor::quantizeStep
std::optional< float > quantizeStep
Definition: Plugin.hpp:38
rtvamp::pluginsdk::Plugin::Meta::identifier
const char * identifier
Definition: Plugin.hpp:84
rtvamp::pluginsdk::PluginBase::ParameterDescriptor::identifier
const char * identifier
Definition: Plugin.hpp:31
rtvamp::pluginsdk::PluginBase::InputDomain::Time
@ Time
rtvamp::pluginsdk::Plugin::reset
virtual void reset()=0
rtvamp::pluginsdk::PluginBase::FrequencyDomainBuffer
std::span< const std::complex< float > > FrequencyDomainBuffer
Frequency domain buffer (FFT)
Definition: Plugin.hpp:56
rtvamp::pluginsdk::PluginBase::OutputDescriptor::name
std::string name
Definition: Plugin.hpp:44
rtvamp::pluginsdk::PluginBase::~PluginBase
virtual ~PluginBase()=default
rtvamp::pluginsdk::PluginBase::ParameterDescriptor::maxValue
float maxValue
Definition: Plugin.hpp:37
rtvamp::pluginsdk::Plugin::Meta
Static plugin descriptor.
Definition: Plugin.hpp:83
rtvamp::pluginsdk::Plugin
Base class to implement feature extraction plugins.
Definition: Plugin.hpp:73
rtvamp::pluginsdk::Plugin::Meta::maker
const char * maker
Definition: Plugin.hpp:87
rtvamp::pluginsdk::PluginBase::OutputDescriptor::minValue
float minValue
Definition: Plugin.hpp:50
rtvamp::pluginsdk::PluginBase::OutputDescriptor::identifier
std::string identifier
Definition: Plugin.hpp:43
rtvamp::pluginsdk::Plugin::programs
static constexpr std::array< const char *, 0 > programs
Optional program list (default: none)
Definition: Plugin.hpp:95
rtvamp::pluginsdk::Plugin::initialiseFeatureSet
void initialiseFeatureSet()
Definition: Plugin.hpp:125
rtvamp::pluginsdk::Plugin::getOutputDescriptors
virtual OutputList getOutputDescriptors() const =0
rtvamp::pluginsdk::Plugin::Meta::inputDomain
InputDomain inputDomain
Definition: Plugin.hpp:90
rtvamp::pluginsdk::PluginBase::InputDomain
InputDomain
Input domain of the plugin.
Definition: Plugin.hpp:27
rtvamp::pluginsdk::Plugin::getFeatureSet
FeatureSet & getFeatureSet() noexcept
Definition: Plugin.hpp:114
rtvamp::pluginsdk::PluginBase::ParameterDescriptor::name
const char * name
Definition: Plugin.hpp:32
rtvamp::pluginsdk::PluginBase::OutputDescriptor::binCount
uint32_t binCount
Definition: Plugin.hpp:47
rtvamp::pluginsdk::Plugin::getCurrentProgram
virtual std::string_view getCurrentProgram() const
Definition: Plugin.hpp:100
rtvamp::pluginsdk::PluginBase::OutputDescriptor::unit
std::string unit
Definition: Plugin.hpp:46
rtvamp::pluginsdk::PluginBase::OutputDescriptor::description
std::string description
Definition: Plugin.hpp:45
rtvamp::pluginsdk::Plugin::process
virtual const FeatureSet & process(InputBuffer buffer, uint64_t nsec)=0
rtvamp::pluginsdk
Definition: EntryPoint.hpp:8
rtvamp::pluginsdk::PluginBase::OutputDescriptor
Definition: Plugin.hpp:42
rtvamp::pluginsdk::Plugin::outputCount
static constexpr uint32_t outputCount
Number of outputs (defined by template parameter)
Definition: Plugin.hpp:80
rtvamp::pluginsdk::Plugin::Meta::copyright
const char * copyright
Definition: Plugin.hpp:88
rtvamp::pluginsdk::Plugin::Plugin
constexpr Plugin(float inputSampleRate)
Definition: Plugin.hpp:75
rtvamp::pluginsdk::PluginBase::InputBuffer
std::variant< TimeDomainBuffer, FrequencyDomainBuffer > InputBuffer
Input domain variant.
Definition: Plugin.hpp:57
rtvamp::pluginsdk::Plugin::Meta::description
const char * description
Definition: Plugin.hpp:86
rtvamp::pluginsdk::PluginBase::ParameterDescriptor::description
const char * description
Definition: Plugin.hpp:33
rtvamp::pluginsdk::PluginBase::InputDomain::Frequency
@ Frequency
rtvamp::pluginsdk::PluginBase::ParameterDescriptor
Definition: Plugin.hpp:29
rtvamp::pluginsdk::Plugin::Meta::name
const char * name
Definition: Plugin.hpp:85
rtvamp::pluginsdk::Plugin::FeatureSet
std::array< Feature, NOutputs > FeatureSet
Computed features for each output.
Definition: Plugin.hpp:78