Audacity 3.2.0
Go to the documentation of this file.
3 Audacity: A Digital Audio Editor
5 @file LV2Ports.h
6 @brief Immutable descriptions of LV2 ports; associated state structures
8 Paul Licameli split from LV2Effect.h
10 Audacity(R) is copyright (c) 1999-2013 Audacity Team.
11 License: GPL v2 or later. See License.txt.
15#ifndef __AUDACITY_LV2_PORTS__
16#define __AUDACITY_LV2_PORTS__
18#if USE_LV2
20#include <functional>
21#include <optional>
22#include <unordered_map>
24#include "LV2Utils.h"
25#include "EffectInterface.h"
26#include "MemoryX.h"
27#include "TranslatableString.h"
28#include <wx/arrstr.h>
29#include "lv2/atom/forge.h"
30#include "zix/ring.h"
35class LV2Port {
37 LV2Port(const LilvPort *port, int index, bool isInput,
38 const wxString &symbol, const wxString &name,
39 const TranslatableString &group
40 ) : mPort(port), mIndex(index), mIsInput(isInput)
41 , mSymbol(symbol), mName(name), mGroup(group)
42 {}
43 const LilvPort *const mPort;
44 const uint32_t mIndex;
45 const bool mIsInput;
46 const wxString mSymbol;
47 const wxString mName;
52class LV2AudioPort final : public LV2Port {
54 using LV2Port::LV2Port;
56using LV2AudioPortPtr = std::shared_ptr<LV2AudioPort>;
57using LV2AudioPortArray = std::vector<LV2AudioPortPtr>;
60class LV2AtomPort final : public LV2Port {
62 LV2AtomPort(const LilvPort *port, int index, bool isInput,
63 const wxString & symbol, const wxString & name,
64 const TranslatableString & group,
65 uint32_t minimumSize, bool isMidi, bool wantsPosition
66 ) : LV2Port{ port, index, isInput, symbol, name, group }
67 , mMinimumSize{ minimumSize }
68 , mIsMidi{ isMidi }
69 , mWantsPosition{ wantsPosition }
70 {}
71 const uint32_t mMinimumSize;
72 const bool mIsMidi;
73 const bool mWantsPosition;
75using LV2AtomPortPtr = std::shared_ptr<LV2AtomPort>;
76using LV2AtomPortArray = std::vector<LV2AtomPortPtr>;
79struct LV2AtomPortState final {
82 : mpPort{ move(pPort) }
83 , mRing{ zix_ring_new(mpPort->mMinimumSize) }
84 , mBuffer{ safenew uint8_t[mpPort->mMinimumSize] }
85 {
86 assert(mpPort);
87 /*
88 There is no complementary munlock anywhere in the library!
89 Are we mis-using a class meant for plugin implementation?
90 Do we make a resource leak of physical memory pages?
91 Should we just skip this call?
92 Or is mlock a good idea we should implement in our own RingBuffer,
93 which we can also munlock in its destructor?
94 */
95 zix_ring_mlock(mRing.get());
98 }
105 void SendToInstance(LV2_Atom_Forge &forge, int64_t frameTime, float speed);
109 void ReceiveFromInstance();
115 void SendToDialog(
116 std::function<void(const LV2_Atom *atom, uint32_t size)> handler);
119 void ReceiveFromDialog(const void *buffer, uint32_t buffer_size);
123 const std::unique_ptr<uint8_t[]> mBuffer;
125using LV2AtomPortStatePtr = std::shared_ptr<LV2AtomPortState>;
126using LV2AtomPortStateArray = std::vector<LV2AtomPortStatePtr>;
129class LV2CVPort final : public LV2Port {
131 LV2CVPort(const LilvPort *port, int index, bool isInput,
132 const wxString & symbol, const wxString & name,
133 const TranslatableString & group,
134 float min, float max, float def, bool hasLo, bool hasHi
135 ) : LV2Port(port, index, isInput, symbol, name, group)
136 , mMin{ min }, mMax{ max }, mDef{ def }, mHasLo{ hasLo }, mHasHi{ hasHi }
137 {}
138 const float mMin;
139 const float mMax;
140 const float mDef;
141 const bool mHasLo;
142 const bool mHasHi;
144using LV2CVPortPtr = std::shared_ptr<LV2CVPort>;
145using LV2CVPortArray = std::vector<LV2CVPortPtr>;
148struct LV2CVPortState final {
150 explicit LV2CVPortState(LV2CVPortPtr pPort) : mpPort{ move(pPort) } {
151 assert(mpPort);
152 }
157using LV2CVPortStateArray = std::vector<LV2CVPortState>;
160class LV2ControlPort final : public LV2Port
163 LV2ControlPort(const LilvPort *port, int index, bool isInput,
164 const wxString & symbol, const wxString & name,
165 const TranslatableString & group,
166 std::vector<double> scaleValues, wxArrayString scaleLabels,
167 const wxString &units,
168 float min, float max, float def, bool hasLo, bool hasHi,
169 bool toggle, bool enumeration, bool integer, bool sampleRate,
170 bool trigger, bool logarithmic
171 ) : LV2Port{ port, index, isInput, symbol, name, group }
172 , mScaleValues{ move(scaleValues) }
173 , mScaleLabels{ std::move(scaleLabels) }
174 , mUnits{ units }
175 , mMin{ min }, mMax{ max }, mDef{ def }
176 , mHasLo{ hasLo }, mHasHi{ hasHi }
177 , mToggle{ toggle }, mEnumeration{ enumeration }, mInteger{ integer }
178 , mSampleRate{ sampleRate }
179 , mTrigger{ trigger }, mLogarithmic{ logarithmic }
180 {}
182 // ScalePoints
183 const std::vector<double> mScaleValues;
184 const wxArrayString mScaleLabels;
186 const wxString mUnits;
187 const float mMin;
188 const float mMax;
189 const float mDef;
190 const bool mHasLo;
191 const bool mHasHi;
192 const bool mToggle;
193 const bool mEnumeration;
194 const bool mInteger;
195 const bool mSampleRate;
196 const bool mTrigger;
197 const bool mLogarithmic;
200 size_t Discretize(float value) const;
202using LV2ControlPortPtr = std::shared_ptr<LV2ControlPort>;
203using LV2ControlPortArray = std::vector<LV2ControlPortPtr>;
206struct LV2EffectSettings final {
208 std::vector<float> values;
210 mutable std::shared_ptr<const LilvState> mpState;
217 auto pSettings = settings.cast<LV2EffectSettings>();
218 assert(pSettings);
219 return *pSettings;
224 return GetSettings(const_cast<EffectSettings &>(settings));
230 std::unique_ptr<EffectOutputs> Clone() const override;
231 void Assign(EffectOutputs &&src) override;
233 std::vector<float> values;
240 : mpPort{ move(pPort) }
241 {
242 assert(mpPort);
243 }
246 float mLst{ 0.0 };
248 float mTmp{ 0.0 };
250 float mLo{ 0.0 };
252 float mHi{ 0.0 };
255using LV2ControlPortStateArray = std::vector<LV2ControlPortState>;
257class LV2Ports {
260 explicit LV2Ports(const LilvPlugin &plug);
262 void EmitPortValues(
263 const LilvState &state, LV2EffectSettings &settings) const;
265 const void *GetPortValue(const LV2EffectSettings &settings,
266 const char *port_symbol, uint32_t *size, uint32_t *type) const;
268 void SetPortValue(LV2EffectSettings &settings, const char *port_symbol,
269 const void *value, uint32_t size, uint32_t type) const;
272 unsigned mAudioIn{ 0 };
273 unsigned mAudioOut{ 0 };
276 std::optional<size_t> mControlInIdx{};
277 std::optional<size_t> mControlOutIdx{};
278 unsigned mMidiIn{ 0 };
279 unsigned mMidiOut{ 0 };
285 std::unordered_map<TranslatableString, std::vector<int>> mGroupMap;
288 std::unordered_map<uint32_t, size_t> mControlPortMap;
289 int mLatencyPort{ -1 };
294 explicit LV2PortStates(const LV2Ports &ports);
301 LV2PortUIStates(const LV2PortStates &states, const LV2Ports &ports);
int min(int a, int b)
const TranslatableString name
Definition: Distortion.cpp:76
std::vector< LV2CVPortState > LV2CVPortStateArray
No need yet for extra indirection.
Definition: LV2Ports.h:157
std::shared_ptr< LV2AtomPortState > LV2AtomPortStatePtr
Definition: LV2Ports.h:125
std::shared_ptr< LV2AudioPort > LV2AudioPortPtr
Definition: LV2Ports.h:56
std::shared_ptr< LV2ControlPort > LV2ControlPortPtr
Definition: LV2Ports.h:202
std::vector< LV2AtomPortPtr > LV2AtomPortArray
Definition: LV2Ports.h:76
LV2EffectSettings & GetSettings(EffectSettings &settings)
Definition: LV2Ports.h:215
std::vector< LV2CVPortPtr > LV2CVPortArray
Definition: LV2Ports.h:145
std::vector< LV2AtomPortStatePtr > LV2AtomPortStateArray
Definition: LV2Ports.h:126
std::shared_ptr< LV2CVPort > LV2CVPortPtr
Definition: LV2Ports.h:144
std::vector< LV2AudioPortPtr > LV2AudioPortArray
Definition: LV2Ports.h:57
std::vector< LV2ControlPortState > LV2ControlPortStateArray
No need yet for extra indirection.
Definition: LV2Ports.h:255
std::shared_ptr< LV2AtomPort > LV2AtomPortPtr
Definition: LV2Ports.h:75
std::vector< LV2ControlPortPtr > LV2ControlPortArray
Definition: LV2Ports.h:203
RAII for lv2 resources.
std::unique_ptr< Type, Lilv_deleter< Type, f > > Lilv_ptr
Generate classes of smart pointers to lv2 resources.
Definition: LV2Utils.h:26
#define safenew
Definition: MemoryX.h:10
static Settings & settings()
Definition: TrackInfo.cpp:87
std::vector< TranslatableString > TranslatableStrings
Hold values to send to effect output meters.
Immutable description of an LV2 Atom port.
Definition: LV2Ports.h:60
LV2AtomPort(const LilvPort *port, int index, bool isInput, const wxString &symbol, const wxString &name, const TranslatableString &group, uint32_t minimumSize, bool isMidi, bool wantsPosition)
Definition: LV2Ports.h:62
const uint32_t mMinimumSize
Definition: LV2Ports.h:71
const bool mWantsPosition
Definition: LV2Ports.h:73
const bool mIsMidi
Definition: LV2Ports.h:72
Immutable description of an LV2 Audio port.
Definition: LV2Ports.h:52
Immutable description of an LV2 CV port (control data signal at sample rate)
Definition: LV2Ports.h:129
const float mMin
Definition: LV2Ports.h:138
const bool mHasLo
Definition: LV2Ports.h:141
const bool mHasHi
Definition: LV2Ports.h:142
const float mDef
Definition: LV2Ports.h:140
LV2CVPort(const LilvPort *port, int index, bool isInput, const wxString &symbol, const wxString &name, const TranslatableString &group, float min, float max, float def, bool hasLo, bool hasHi)
Definition: LV2Ports.h:131
const float mMax
Definition: LV2Ports.h:139
Immutable description of an LV2 control port.
Definition: LV2Ports.h:161
const bool mSampleRate
Definition: LV2Ports.h:195
const bool mInteger
Definition: LV2Ports.h:194
LV2ControlPort(const LilvPort *port, int index, bool isInput, const wxString &symbol, const wxString &name, const TranslatableString &group, std::vector< double > scaleValues, wxArrayString scaleLabels, const wxString &units, float min, float max, float def, bool hasLo, bool hasHi, bool toggle, bool enumeration, bool integer, bool sampleRate, bool trigger, bool logarithmic)
Definition: LV2Ports.h:163
size_t Discretize(float value) const
Map a real number to one of the scale points.
Definition: LV2Ports.cpp:128
const bool mToggle
Definition: LV2Ports.h:192
const bool mTrigger
Definition: LV2Ports.h:196
const wxArrayString mScaleLabels
Definition: LV2Ports.h:184
const bool mHasHi
Definition: LV2Ports.h:191
const bool mHasLo
Definition: LV2Ports.h:190
const float mMax
Definition: LV2Ports.h:188
const wxString mUnits
Definition: LV2Ports.h:186
const bool mEnumeration
Definition: LV2Ports.h:193
const float mDef
Definition: LV2Ports.h:189
const bool mLogarithmic
Definition: LV2Ports.h:197
const std::vector< double > mScaleValues
Definition: LV2Ports.h:183
const float mMin
Definition: LV2Ports.h:187
Immutable description of an LV2 port.
Definition: LV2Ports.h:35
const wxString mName
Definition: LV2Ports.h:47
const TranslatableString mGroup
Definition: LV2Ports.h:48
const uint32_t mIndex
Definition: LV2Ports.h:44
const wxString mSymbol
Definition: LV2Ports.h:46
const bool mIsInput
Definition: LV2Ports.h:45
const LilvPort *const mPort
Definition: LV2Ports.h:43
LV2Port(const LilvPort *port, int index, bool isInput, const wxString &symbol, const wxString &name, const TranslatableString &group)
Definition: LV2Ports.h:37
LV2PortStates(const LV2Ports &ports)
Definition: LV2Ports.cpp:419
LV2CVPortStateArray mCVPortStates
Definition: LV2Ports.h:296
LV2AtomPortStateArray mAtomPortStates
Definition: LV2Ports.h:295
LV2AtomPortStatePtr mControlIn
Definition: LV2Ports.h:302
LV2PortUIStates(const LV2PortStates &states, const LV2Ports &ports)
Definition: LV2Ports.cpp:429
LV2ControlPortStateArray mControlPortStates
Definition: LV2Ports.h:304
LV2AtomPortStatePtr mControlOut
Definition: LV2Ports.h:303
std::unordered_map< TranslatableString, std::vector< int > > mGroupMap
Definition: LV2Ports.h:285
void EmitPortValues(const LilvState &state, LV2EffectSettings &settings) const
Definition: LV2Ports.cpp:411
unsigned mMidiIn
Definition: LV2Ports.h:278
std::optional< size_t > mControlInIdx
Definition: LV2Ports.h:276
const void * GetPortValue(const LV2EffectSettings &settings, const char *port_symbol, uint32_t *size, uint32_t *type) const
Definition: LV2Ports.cpp:356
TranslatableStrings mGroups
Definition: LV2Ports.h:284
unsigned mAudioOut
Definition: LV2Ports.h:273
unsigned mAudioIn
Definition: LV2Ports.h:272
LV2Ports(const LilvPlugin &plug)
Definition: LV2Ports.cpp:153
LV2CVPortArray mCVPorts
Definition: LV2Ports.h:281
LV2ControlPortArray mControlPorts
Definition: LV2Ports.h:283
LV2AtomPortArray mAtomPorts
Definition: LV2Ports.h:275
std::optional< size_t > mControlOutIdx
Definition: LV2Ports.h:277
unsigned mMidiOut
Definition: LV2Ports.h:279
void SetPortValue(LV2EffectSettings &settings, const char *port_symbol, const void *value, uint32_t size, uint32_t type) const
Definition: LV2Ports.cpp:385
std::unordered_map< uint32_t, size_t > mControlPortMap
Definition: LV2Ports.h:288
int mLatencyPort
Definition: LV2Ports.h:289
LV2AudioPortArray mAudioPorts
Definition: LV2Ports.h:271
Holds a msgid for the translation catalog; may also bind format arguments.
ZixRing * zix_ring_new(uint32_t size)
Definition: ring.cpp:74
void zix_ring_mlock(ZixRing *ring)
Definition: ring.cpp:93
STL namespace.
Externalized state of a plug-in.
State of an instance of an LV2 Atom port.
Definition: LV2Ports.h:79
void SendToInstance(LV2_Atom_Forge &forge, int64_t frameTime, float speed)
Transfer incoming events from the ring buffer to the event buffer.
Definition: LV2Ports.cpp:21
const std::unique_ptr< uint8_t[]> mBuffer
Definition: LV2Ports.h:123
const Lilv_ptr< ZixRing, zix_ring_free > mRing
Definition: LV2Ports.h:122
LV2AtomPortState(LV2AtomPortPtr pPort)
Definition: LV2Ports.h:81
void SendToDialog(std::function< void(const LV2_Atom *atom, uint32_t size)> handler)
Dialog can poll one ring buffer for messages at idle time.
Definition: LV2Ports.cpp:87
void ReceiveFromDialog(const void *buffer, uint32_t buffer_size)
Dialog pushes to other ring buffer when it gets a user interface event.
Definition: LV2Ports.cpp:121
void ReceiveFromInstance()
Take responses from the instance and send cross-thread for the dialog.
Definition: LV2Ports.cpp:110
void ResetForInstanceOutput()
Definition: LV2Ports.cpp:78
const LV2AtomPortPtr mpPort
Definition: LV2Ports.h:121
State of an instance of an LV2 CV port.
Definition: LV2Ports.h:148
LV2CVPortState(LV2CVPortPtr pPort)
Definition: LV2Ports.h:150
Floats mBuffer
Definition: LV2Ports.h:154
const LV2CVPortPtr mpPort
Definition: LV2Ports.h:153
Other UI related state of an instance of an LV2 Control port.
Definition: LV2Ports.h:237
float mLst
Value of mTmp last seen by idle-time updater.
Definition: LV2Ports.h:246
float mLo
Lower bound, as scaled by sample rate if that is required.
Definition: LV2Ports.h:250
float mHi
Upper bound, as scaled by sample rate if that is required.
Definition: LV2Ports.h:252
float mTmp
Value of UI control, as scaled by sample rate if that is required.
Definition: LV2Ports.h:248
const LV2ControlPortPtr mpPort
Definition: LV2Ports.h:244
LV2ControlPortState(LV2ControlPortPtr pPort)
Definition: LV2Ports.h:239
Carry output control port information back to main thread.
Definition: LV2Ports.h:228
std::unique_ptr< EffectOutputs > Clone() const override
Definition: LV2Ports.cpp:139
void Assign(EffectOutputs &&src) override
Update one Outputs object from another.
Definition: LV2Ports.cpp:144
~LV2EffectOutputs() override
std::vector< float > values
vector of values in correspondence with the control ports
Definition: LV2Ports.h:233
Storage locations to be connected to LV2 control ports.
Definition: LV2Ports.h:206
std::vector< float > values
vector of values in correspondence with the control ports
Definition: LV2Ports.h:208
std::shared_ptr< const LilvState > mpState
Result of last load of a preset; may be null.
Definition: LV2Ports.h:210