rt-vamp-plugin-sdk  0.3.1
Real-time Vamp plugin SDK for C++20
PluginExt.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm> // clamp
4 #include <array>
5 #include <cassert>
6 #include <cmath> // round
7 #include <cstdint>
8 #include <optional>
9 #include <string_view>
10 #include <vector>
11 
13 
14 namespace rtvamp::pluginsdk {
15 
27 template <typename Self, uint32_t NOutputs>
28 class PluginExt : public Plugin<NOutputs> {
29 public:
30  explicit PluginExt(float inputSampleRate) : Plugin<NOutputs>(inputSampleRate) {}
31 
32  std::optional<float> getParameter(std::string_view id) const override final;
33  bool setParameter(std::string_view id, float value) override final;
34 
35  std::string_view getCurrentProgram() const override final;
36  bool selectProgram(std::string_view name) override final;
37 
38  // custom logic can be implemented with following callbacks
39  virtual void onParameterChange(std::string_view id, float newValue) {}
40  virtual void onProgramChange(std::string_view newProgram) {}
41 
42 private:
43  static std::vector<float> defaultParameterValues();
44  static constexpr std::optional<size_t> findParameterIndex(std::string_view id);
45  static constexpr std::optional<size_t> findProgramIndex(std::string_view name);
46 
47  std::vector<float> parameterValues_{defaultParameterValues()};
48  size_t programIndex_{0};
49 };
50 
51 /* --------------------------------------- Implementation --------------------------------------- */
52 
53 template <typename Self, uint32_t NOutputs>
54 std::optional<float> PluginExt<Self, NOutputs>::getParameter(std::string_view id) const {
55  if (const auto index = findParameterIndex(id)) {
56  return parameterValues_[index.value()];
57  }
58  return {};
59 }
60 
61 template <typename Self, uint32_t NOutputs>
62 bool PluginExt<Self, NOutputs>::setParameter(std::string_view id, float value) {
63  if (const auto index = findParameterIndex(id)) {
64  const auto& descriptor = Self::parameters[index.value()];
65 
66  if (descriptor.quantizeStep) {
67  const auto quantizeStep = descriptor.quantizeStep.value();
68  value = std::round(value / quantizeStep) * quantizeStep;
69  }
70  value = std::clamp(value, descriptor.minValue, descriptor.maxValue);
71 
72  parameterValues_[index.value()] = value;
73  onParameterChange(id, value);
74  return true;
75  }
76  return false;
77 }
78 
79 template <typename Self, uint32_t NOutputs>
81  assert(programIndex_ < Self::programs.size());
82  return Self::programs[programIndex_];
83 }
84 
85 template <typename Self, uint32_t NOutputs>
86 bool PluginExt<Self, NOutputs>::selectProgram(std::string_view name) {
87  if (const auto index = findProgramIndex(name)) {
88  programIndex_ = index.value();
89  onProgramChange(name);
90  return true;
91  }
92  return false;
93 }
94 
95 template <typename Self, uint32_t NOutputs>
97  std::vector<float> values(Self::parameters.size());
98  for (size_t i = 0; i < Self::parameters.size(); ++i) {
99  values[i] = Self::parameters[i].defaultValue;
100  }
101  return values;
102 }
103 
104 template <typename Self, uint32_t NOutputs>
105 constexpr std::optional<size_t> PluginExt<Self, NOutputs>::findParameterIndex(std::string_view id) {
106  for (size_t i = 0; i < Self::parameters.size(); ++i) {
107  if (Self::parameters[i].identifier == id) return i;
108  }
109  return {};
110 }
111 
112 template <typename Self, uint32_t NOutputs>
113 constexpr std::optional<size_t> PluginExt<Self, NOutputs>::findProgramIndex(std::string_view name) {
114  for (size_t i = 0; i < Self::programs.size(); ++i) {
115  if (Self::programs[i] == name) return i;
116  }
117  return {};
118 }
119 
120 } // namespace rtvamp::pluginsdk
rtvamp::pluginsdk::PluginExt::onProgramChange
virtual void onProgramChange(std::string_view newProgram)
Definition: PluginExt.hpp:40
Plugin.hpp
rtvamp::pluginsdk::PluginExt::setParameter
bool setParameter(std::string_view id, float value) override final
Definition: PluginExt.hpp:62
rtvamp::pluginsdk::Plugin
Base class to implement feature extraction plugins.
Definition: Plugin.hpp:73
rtvamp::pluginsdk::PluginExt::selectProgram
bool selectProgram(std::string_view name) override final
Definition: PluginExt.hpp:86
rtvamp::pluginsdk::PluginExt::PluginExt
PluginExt(float inputSampleRate)
Definition: PluginExt.hpp:30
rtvamp::pluginsdk
Definition: EntryPoint.hpp:8
rtvamp::pluginsdk::PluginExt::getParameter
std::optional< float > getParameter(std::string_view id) const override final
Definition: PluginExt.hpp:54
rtvamp::pluginsdk::PluginExt::onParameterChange
virtual void onParameterChange(std::string_view id, float newValue)
Definition: PluginExt.hpp:39
rtvamp::pluginsdk::PluginExt
Extended plugin base class with automatic parameter / program handling.
Definition: PluginExt.hpp:28
rtvamp::pluginsdk::PluginExt::getCurrentProgram
std::string_view getCurrentProgram() const override final
Definition: PluginExt.hpp:80