Audacity  2.2.2
LV2Effect.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  LV2Effect.h
6 
7  Audacity(R) is copyright (c) 1999-2013 Audacity Team.
8  License: GPL v2. See License.txt.
9 
10 *********************************************************************/
11 
12 #include "../../Audacity.h"
13 
14 #if USE_LV2
15 
16 #include "../../MemoryX.h"
17 #include <vector>
18 #include <wx/checkbox.h>
19 #include <wx/dialog.h>
20 #include <wx/event.h>
21 #include <wx/slider.h>
22 #include <wx/stattext.h>
23 #include <wx/string.h>
24 #include <wx/textctrl.h>
25 
26 #include "lv2/lv2plug.in/ns/ext/atom/forge.h"
27 #include "lv2/lv2plug.in/ns/ext/data-access/data-access.h"
28 #include "lv2/lv2plug.in/ns/ext/options/options.h"
29 #include "lv2/lv2plug.in/ns/ext/uri-map/uri-map.h"
30 #include "lv2/lv2plug.in/ns/ext/urid/urid.h"
31 #include "lv2/lv2plug.in/ns/extensions/ui/ui.h"
32 
33 #include <lilv/lilv.h>
34 #include <suil/suil.h>
35 
36 #include "../../SampleFormat.h"
37 #include "../../widgets/NumericTextCtrl.h"
38 
39 #include "LoadLV2.h"
40 
41 #include <unordered_map>
42 
43 #define LV2EFFECTS_VERSION wxT("1.0.0.0")
44 /* i18n-hint: abbreviates
45  "Linux Audio Developer's Simple Plugin API (LADSPA) version 2" */
46 #define LV2EFFECTS_FAMILY XO("LV2")
47 
49 class LV2Port
50 {
51 public:
52  LV2Port()
53  {
54  mInput = false;
55  mToggle = false;
56  mTrigger = false;
57  mInteger = false;
58  mSampleRate = false;
59  mEnumeration = false;
60  mLogarithmic = false;
61  mHasLo = false;
62  mHasHi = false;
63  }
64  LV2Port( const LV2Port & ) = default;
65  LV2Port& operator = ( const LV2Port & ) = default;
66  //LV2Port( LV2Port && ) = default;
67  //LV2Port& operator = ( LV2Port && ) = default;
68 
69  uint32_t mIndex;
70  wxString mSymbol;
71  wxString mName;
72  wxString mGroup;
73  wxString mUnits;
74  float mMin;
75  float mMax;
76  float mDef;
77  float mVal;
78  float mTmp;
79  float mLo;
80  float mHi;
81  bool mHasLo;
82  bool mHasHi;
83  bool mInput;
84  bool mToggle;
85  bool mTrigger;
86  bool mInteger;
87  bool mSampleRate;
88  bool mEnumeration;
89  bool mLogarithmic;
90 
91  LilvPort *mPort;
92 
93  // ScalePoints
94  std::vector<double> mScaleValues;
95  wxArrayString mScaleLabels;
96 };
97 
98 using LV2GroupMap = std::unordered_map<wxString, std::vector<int>>;
99 
100 class LV2EffectSettingsDialog;
101 
102 class LV2Effect final : public wxEvtHandler,
103  public EffectClientInterface,
105 {
106 public:
107  LV2Effect(const LilvPlugin *plug);
108  virtual ~LV2Effect();
109 
110  // IdentInterface implementation
111 
112  wxString GetPath() override;
113  IdentInterfaceSymbol GetSymbol() override;
114  IdentInterfaceSymbol GetVendor() override;
115  wxString GetVersion() override;
116  wxString GetDescription() override;
117 
118  // EffectDefinitionInterface implementation
119 
120  EffectType GetType() override;
122  bool IsInteractive() override;
123  bool IsDefault() override;
124  bool IsLegacy() override;
125  bool SupportsRealtime() override;
126  bool SupportsAutomation() override;
127 
128  // EffectClientInterface implementation
129 
130  bool SetHost(EffectHostInterface *host) override;
131 
132  unsigned GetAudioInCount() override;
133  unsigned GetAudioOutCount() override;
134 
135  int GetMidiInCount() override;
136  int GetMidiOutCount() override;
137 
138  void SetSampleRate(double rate) override;
139  size_t SetBlockSize(size_t maxBlockSize) override;
140 
141  sampleCount GetLatency() override;
142  size_t GetTailSize() override;
143 
144  bool IsReady() override;
145  bool ProcessInitialize(sampleCount totalLen, ChannelNames chanMap = NULL) override;
146  bool ProcessFinalize() override;
147  size_t ProcessBlock(float **inbuf, float **outbuf, size_t size) override;
148 
149  bool RealtimeInitialize() override;
150  bool RealtimeAddProcessor(unsigned numChannels, float sampleRate) override;
151  bool RealtimeFinalize() override;
152  bool RealtimeSuspend() override;
153  bool RealtimeResume() override;
154  bool RealtimeProcessStart() override;
155  size_t RealtimeProcess(int group,
156  float **inbuf,
157  float **outbuf,
158  size_t numSamples) override;
159  bool RealtimeProcessEnd() override;
160 
161  bool ShowInterface(wxWindow *parent, bool forceModal = false) override;
162 
163  bool GetAutomationParameters(CommandParameters & parms) override;
164  bool SetAutomationParameters(CommandParameters & parms) override;
165 
166  // EffectUIClientInterface implementation
167 
168  void SetHostUI(EffectUIHostInterface *host) override;
169  bool PopulateUI(wxWindow *parent) override;
170  bool IsGraphicalUI() override;
171  bool ValidateUI() override;
172  bool HideUI() override;
173  bool CloseUI() override;
174 
175  bool LoadUserPreset(const wxString & name) override;
176  bool SaveUserPreset(const wxString & name) override;
177 
178  wxArrayString GetFactoryPresets() override;
179  bool LoadFactoryPreset(int id) override;
180  bool LoadFactoryDefaults() override;
181 
182  bool CanExportPresets() override;
183  void ExportPresets() override;
184  void ImportPresets() override;
185 
186  bool HasOptions() override;
187  void ShowOptions() override;
188 
189  // LV2Effect implementation
190 
191 private:
192  bool Load();
193  void Unload();
194 
195  bool LoadParameters(const wxString & group);
196  bool SaveParameters(const wxString & group);
197 
198  LilvInstance *InitInstance(float sampleRate);
199  void FreeInstance(LilvInstance *handle);
200 
201  static uint32_t uri_to_id(LV2_URI_Map_Callback_Data callback_data,
202  const char *map,
203  const char *uri);
204 
205  static LV2_URID urid_map(LV2_URID_Map_Handle handle, const char *uri);
206  LV2_URID URID_Map(const char *uri);
207 
208  static const char *urid_unmap(LV2_URID_Unmap_Handle handle, LV2_URID urid);
209  const char *URID_Unmap(LV2_URID urid);
210 
211  static int ui_resize(LV2UI_Feature_Handle handle, int width, int height);
212  int UIResize(int width, int height);
213 
214  size_t AddOption(const char *key, uint32_t size, const char *type, void *value);
215  LV2_Feature *AddFeature(const char *uri, void *data);
216 
217  bool BuildFancy();
218  bool BuildPlain();
219 
220  bool TransferDataToWindow() /* not override */;
221  bool TransferDataFromWindow() /* not override */;
222  void SetSlider(wxSlider *slider, const LV2Port & ctrl);
223 
224  void OnTrigger(wxCommandEvent & evt);
225  void OnToggle(wxCommandEvent & evt);
226  void OnChoice(wxCommandEvent & evt);
227  void OnText(wxCommandEvent & evt);
228  void OnSlider(wxCommandEvent & evt);
229 
230  void OnIdle(wxIdleEvent & evt);
231 
232  static void suil_write_func(SuilController controller,
233  uint32_t port_index,
234  uint32_t buffer_size,
235  uint32_t protocol,
236  const void *buffer);
237 
238  void UIWrite(uint32_t port_index,
239  uint32_t buffer_size,
240  uint32_t protocol,
241  const void *buffer);
242 
243  static void set_value_func(const char *port_symbol,
244  void *user_data,
245  const void *value,
246  uint32_t size,
247  uint32_t type);
248 
249  void SetPortValue(const char *port_symbol,
250  const void *value,
251  uint32_t size,
252  uint32_t type);
253 
254 private:
255  // Declare the static URI nodes
256  #undef URI
257  #define URI(n, u) static LilvNode *n;
258  URILIST
259 
260  const LilvPlugin *mPlug;
261 
262  EffectHostInterface *mHost;
263 
264  size_t mBlockSize;
265  double mSampleRate;
266 
267  wxLongToLongHashMap mControlsMap;
268  std::vector<LV2Port> mControls;
269  std::vector<int> mAudioInputs;
270  std::vector<int> mAudioOutputs;
271 
272  LV2GroupMap mGroupMap;
273  wxArrayString mGroups;
274 
275  bool mUseLatency;
276  int mLatencyPort;
277  bool mLatencyDone;
278  float mLatency;
279 
280  LilvInstance *mMaster;
281  LilvInstance *mProcess;
282  std::vector<LilvInstance*> mSlaves;
283 
284  FloatBuffers mMasterIn, mMasterOut;
285  size_t mNumSamples;
286 
287  double mLength;
288 
289  wxDialog *mDialog;
290  wxWindow *mParent;
291  EffectUIHostInterface *mUIHost;
292 
293  bool mUseGUI;
294 
295  std::vector< std::unique_ptr<char, freer> > mURIMap;
296 
297  LV2_URI_Map_Feature mUriMapFeature;
298  LV2_URID_Map mURIDMapFeature;
299  LV2_URID_Unmap mURIDUnmapFeature;
300  LV2UI_Resize mUIResizeFeature;
301  LV2_Extension_Data_Feature mExtDataFeature;
302 
303  size_t mBlockSizeOption;
304  size_t mSampleRateOption;
305 
306  LV2_Options_Interface *mOptionsInterface;
307  std::vector<LV2_Options_Option> mOptions;
308 
309  std::vector<std::unique_ptr<LV2_Feature>> mFeatures;
310 
311  LV2_Feature *mInstanceAccessFeature;
312  LV2_Feature *mParentFeature;
313 
314  const LV2UI_Idle_Interface *mIdleFeature;
315 
316  SuilHost *mSuilHost;
317  SuilInstance *mSuilInstance;
318 
319  NumericTextCtrl *mDuration;
320  ArrayOf<wxSlider*> mSliders;
321  ArrayOf<wxTextCtrl*> mFields;
322 
323  bool mFactoryPresetsLoaded;
324  wxArrayString mFactoryPresetNames;
325  wxArrayString mFactoryPresetUris;
326 
327  DECLARE_EVENT_TABLE()
328 
329  friend class LV2EffectSettingsDialog;
330  friend class LV2EffectsModule;
331 };
332 
333 inline wxString LilvString(const LilvNode *node)
334 {
335  return wxString::FromUTF8(lilv_node_as_string(node));
336 };
337 
338 inline wxString LilvString(LilvNode *node, bool free)
339 {
340  wxString str = LilvString(node);
341  if (free)
342  {
343  lilv_node_free(node);
344  }
345 
346  return str;
347 };
348 
349 
350 #endif
virtual bool IsGraphicalUI()=0
virtual bool PopulateUI(wxWindow *parent)=0
EffectUIClientInterface is an abstract base class to populate a UI and validate UI values...
virtual size_t SetBlockSize(size_t maxBlockSize)=0
virtual int GetMidiOutCount()=0
virtual IdentInterfaceSymbol GetFamilyId()=0
void OnToggle(const CommandContext &context)
virtual bool HasOptions()=0
virtual bool RealtimeFinalize()=0
virtual bool CanExportPresets()=0
virtual void ShowOptions()=0
virtual unsigned GetAudioInCount()=0
#define URILIST
Definition: LoadLV2.h:38
virtual bool ShowInterface(wxWindow *parent, bool forceModal=false)=0
virtual bool SupportsRealtime()=0
virtual bool RealtimeProcessEnd()=0
virtual void ExportPresets()=0
virtual bool RealtimeProcessStart()=0
virtual size_t GetTailSize()=0
virtual IdentInterfaceSymbol GetSymbol()=0
virtual bool HideUI()=0
virtual wxString GetDescription()=0
virtual void ImportPresets()=0
virtual wxString GetVersion()=0
virtual bool RealtimeAddProcessor(unsigned numChannels, float sampleRate)=0
virtual bool ProcessInitialize(sampleCount totalLen, ChannelNames chanMap=NULL)=0
virtual bool SaveUserPreset(const wxString &name)=0
virtual sampleCount GetLatency()=0
virtual bool GetAutomationParameters(CommandParameters &parms)=0
virtual bool IsReady()=0
CommandParameters, derived from wxFileConfig, is essentially doing the same things as the Shuttle cla...
virtual bool RealtimeSuspend()=0
virtual wxArrayString GetFactoryPresets()=0
virtual bool RealtimeInitialize()=0
virtual bool LoadFactoryDefaults()=0
virtual bool IsDefault()=0
IdentInterfaceSymbol pairs a persistent string identifier used internally with an optional...
enum ChannelName * ChannelNames
virtual bool SupportsAutomation()=0
virtual bool ValidateUI()=0
virtual int GetMidiInCount()=0
virtual EffectType GetType()=0
EffectHostInterface is a decorator of a EffectUIClientInterface. It adds virtual (abstract) functions...
virtual bool SetHost(EffectHostInterface *host)=0
EffectType
const wxChar * name
Definition: Distortion.cpp:94
virtual IdentInterfaceSymbol GetVendor()=0
virtual bool RealtimeResume()=0
EffectClientInterface provides the ident interface to Effect, and is what makes Effect into a plug-in...
virtual void SetSampleRate(double rate)=0
virtual bool LoadFactoryPreset(int id)=0
virtual unsigned GetAudioOutCount()=0
virtual bool IsInteractive()=0
virtual wxString GetPath()=0
virtual size_t ProcessBlock(float **inBlock, float **outBlock, size_t blockLen)=0
virtual bool SetAutomationParameters(CommandParameters &parms)=0
virtual bool LoadUserPreset(const wxString &name)=0
virtual void SetHostUI(EffectUIHostInterface *host)=0
virtual bool IsLegacy()=0
EffectUIHostInterface has nothing in it. It is provided so that an Effect can call SetHostUI passing ...
virtual bool CloseUI()=0
virtual size_t RealtimeProcess(int group, float **inBuf, float **outBuf, size_t numSamples)=0
virtual bool ProcessFinalize()=0