Plugin.hpp
1 #pragma once
2 
3 #define RELEASE 1
4 #include <ossia/detail/flat_map.hpp>
5 
6 #include <QString>
7 
8 #include <pluginterfaces/base/funknown.h>
9 #include <pluginterfaces/gui/iplugview.h>
10 #include <pluginterfaces/vst/ivstaudioprocessor.h>
11 #include <pluginterfaces/vst/ivstcomponent.h>
12 #include <pluginterfaces/vst/ivstunits.h>
13 
14 #include <string_view>
15 
16 #include <public.sdk/source/vst/hosting/hostclasses.h>
17 #include <public.sdk/source/vst/hosting/module.h>
18 #include <public.sdk/source/vst/hosting/plugprovider.h>
19 
20 namespace vst3
21 {
22 class ApplicationPlugin;
23 class Model;
24 
25 inline QString fromString(const Steinberg::Vst::String128& str)
26 {
27 #if defined(_WIN32)
28  return [] <typename T>(const T& str) {
29  using char_type = std::decay_t<decltype(str[0])>;
30  if constexpr(std::is_same_v<char_type, char16_t>)
31  return QString::fromUtf16(str);
32  else
33  return QString::fromWCharArray(str);
34  }(str);
35 #else
36  return QString::fromUtf16(str);
37 #endif
38 }
39 
40 using MIDIControls = ossia::flat_map<std::pair<int, int>, Steinberg::Vst::ParamID>;
41 
42 struct Plugin
43 {
44  Plugin() = default;
45  Plugin(const Plugin&) = default;
46  Plugin(Plugin&&) = default;
47  Plugin& operator=(const Plugin&) = default;
48  Plugin& operator=(Plugin&&) = default;
49 
50  ~Plugin();
51 
52  void load(
53  Model& model, ApplicationPlugin& ctx, const std::string& path,
54  const VST3::UID& uid, double sr, int max_bs);
55  operator bool() const noexcept { return component && processor; }
56 
57  std::string path;
58  VST3::Hosting::Module::Ptr mdl;
59  Steinberg::Vst::IComponent* component{};
60  Steinberg::Vst::IAudioProcessor* processor{};
61  Steinberg::Vst::IEditController* controller{};
62  Steinberg::Vst::IUnitInfo* units{};
63  Steinberg::IPlugView* view{};
64 
65  void loadAudioProcessor(ApplicationPlugin& ctx);
66  void loadEditController(Model& model, ApplicationPlugin& ctx);
67  void loadBuses();
68  void loadPresets();
69 
70  void loadProcessorStateToController();
71 
72  void start(double sample_rate, int max_bs);
73  void stop();
74 
75  bool supportsDouble{};
76  bool hasUI{};
77  int audio_ins = 0;
78  int event_ins = 0;
79  int audio_outs = 0;
80  int event_outs = 0;
81 
82  MIDIControls midiControls{};
83 
84  Steinberg::Vst::ProgramListInfo programs;
85 };
86 
87 #if __cpp_concepts >= 201907
88 template <typename T>
89 concept BusVisitor = requires(T&& vis)
90 {
91  vis.audioIn(Steinberg::Vst::BusInfo{}, int{});
92  vis.audioOut(Steinberg::Vst::BusInfo{}, int{});
93  vis.eventIn(Steinberg::Vst::BusInfo{}, int{});
94  vis.eventOut(Steinberg::Vst::BusInfo{}, int{});
95 };
96 void forEachBus(BusVisitor auto&& visitor, Steinberg::Vst::IComponent& component)
97 #else
98 template <typename T>
99 void forEachBus(T&& visitor, Steinberg::Vst::IComponent& component)
100 #endif
101 {
102  const int audio_ins
103  = component.getBusCount(Steinberg::Vst::kAudio, Steinberg::Vst::kInput);
104  const int event_ins
105  = component.getBusCount(Steinberg::Vst::kEvent, Steinberg::Vst::kInput);
106  const int audio_outs
107  = component.getBusCount(Steinberg::Vst::kAudio, Steinberg::Vst::kOutput);
108  const int event_outs
109  = component.getBusCount(Steinberg::Vst::kEvent, Steinberg::Vst::kOutput);
110 
111  Steinberg::Vst::BusInfo bus;
112  for(int i = 0; i < audio_ins; i++)
113  {
114  component.getBusInfo(Steinberg::Vst::kAudio, Steinberg::Vst::kInput, i, bus);
115  visitor.audioIn(bus, i);
116  }
117 
118  for(int i = 0; i < event_ins; i++)
119  {
120  component.getBusInfo(Steinberg::Vst::kEvent, Steinberg::Vst::kInput, i, bus);
121  visitor.eventIn(bus, i);
122  }
123 
124  for(int i = 0; i < audio_outs; i++)
125  {
126  component.getBusInfo(Steinberg::Vst::kAudio, Steinberg::Vst::kOutput, i, bus);
127  visitor.audioOut(bus, i);
128  }
129 
130  for(int i = 0; i < event_outs; i++)
131  {
132  component.getBusInfo(Steinberg::Vst::kEvent, Steinberg::Vst::kOutput, i, bus);
133  visitor.eventOut(bus, i);
134  }
135 }
136 }
Definition: score-plugin-vst3/Vst3/ApplicationPlugin.hpp:86
Definition: score-plugin-vst3/Vst3/EffectModel.hpp:41
T & component(const score::Components &c)
component Fetch a Component from Components by type
Definition: ComponentUtils.hpp:20
Definition: Plugin.hpp:43