iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
IPlugFaust.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5
6 See LICENSE.txt for more info.
7
8 ==============================================================================
9*/
10
11#pragma once
12
18#include "IPlugConstants.h"
19
20// FAUSTFLOAT is the sample type of the buffers passed to dsp::compute(). It must match iplug::sample.
21#ifndef FAUSTFLOAT
22 #ifdef SAMPLE_TYPE_FLOAT
23 #define FAUSTFLOAT float
24 #else
25 #define FAUSTFLOAT double
26 #endif
27#endif
28
29#include <atomic>
30#include <functional>
31#include <memory>
32#include <mutex>
33#include <string>
34#include <thread>
35#include <vector>
36
37#include "faust/dsp/dsp.h"
38#include "faust/dsp/poly-dsp.h"
39#include "faust/gui/UI.h"
40#include "faust/gui/meta.h"
41#include "faust/gui/MetaDataUI.h"
42#include "faust/gui/MidiUI.h"
43#include "faust/midi/iplug2-midi.h"
44
45#include "IPlugAPIBase.h"
46#include "IPlugParameter.h"
47#include "IPlugTimer.h"
48
49static_assert(sizeof(FAUSTFLOAT) == sizeof(iplug::sample), "FAUSTFLOAT must be the same type as iplug::sample");
50
53#ifndef FAUST_SHARE_PATH
54 #define FAUST_SHARE_PATH ""
55#endif
56
58#ifndef FAUST_UI_INTERVAL
59 #define FAUST_UI_INTERVAL 30
60#endif
61
62BEGIN_IPLUG_NAMESPACE
63
68{
69 enum class EType { TabBox, HBox, VBox, Button, CheckButton, HSlider, VSlider, NumEntry, HBargraph, VBargraph, Soundfile };
70
71 EType type = EType::VBox;
72 std::string label; // the widget/group label, with Faust's anonymous "0x00" groups mapped to ""
73 std::string path; // the full path of the item, e.g. "/synth/osc/freq"
74 FAUSTFLOAT* zone = nullptr;
75 double init = 0., min = 0., max = 1., step = 0.;
76
77 int paramIdx = kNoParameter; // index of the IPlug parameter linked to this widget (see IPlugFaust::CreateIPlugParameters)
78 int ctrlTag = kNoTag; // for bargraphs: the control tag that values are sent to
79
80 // metadata
81 bool hidden = false;
82 bool knob = false;
83 bool led = false;
84 bool numerical = false;
85 bool menu = false;
86 bool radio = false;
87 std::string unit;
88 std::string tooltip;
89 std::vector<std::pair<std::string, double>> options; // menu/radio items
90
91 std::vector<FaustUIItem> children;
92
93 bool IsGroup() const { return type == EType::TabBox || type == EType::HBox || type == EType::VBox; }
94 bool IsBargraph() const { return type == EType::HBargraph || type == EType::VBargraph; }
95};
96
105class IPlugFaust : public UI, public MetaDataUI
106{
107public:
108 static constexpr int kDefaultCtrlTagStart = 1000;
109
113 IPlugFaust(const char* name, int nVoices = 0, int ctrlTagStart = kDefaultCtrlTagStart);
114 virtual ~IPlugFaust();
115
116 IPlugFaust(const IPlugFaust&) = delete;
117 IPlugFaust& operator=(const IPlugFaust&) = delete;
118
120 virtual void Init() = 0;
121
125 void SetMaxChannelCount(int maxNInputs, int maxNOutputs);
126
129 void SetSampleRate(double sampleRate);
130
132 void ProcessMidiMsg(const IMidiMsg& msg);
133
135 void ProcessBlock(sample** inputs, sample** outputs, int nFrames);
136
138 bool IsReady() const { return mDSP != nullptr && !mChannelCountError && !mHasSoundfile; }
139
140 int NInputs() const { return mDSP ? mDSP->getNumInputs() : 0; }
141 int NOutputs() const { return mDSP ? mDSP->getNumOutputs() : 0; }
142 int GetMaxNInputs() const { return mMaxNInputs; }
143 int GetMaxNOutputs() const { return mMaxNOutputs; }
144
145#pragma mark - Parameters
146
148 int NParams() const { return static_cast<int>(mParams.size()); }
149
158 int CreateIPlugParameters(IPlugAPIBase* pPlug, int startIdx = 0, int endIdx = -1, bool setToDefault = true);
159
162 bool OnParamChange(int plugParamIdx);
163
167 void SetParameterValueNormalised(int faustParamIdx, double normalizedValue);
168
172 void SetParameterValue(int faustParamIdx, double value);
173
178 void SetParameterValue(const char* labelOrPath, double value);
179
181 void SyncFaustParams();
182
184 int GetFaustParamIdxForZone(FAUSTFLOAT* zone) const;
185
187 int GetPlugParamIdx(int faustParamIdx) const;
188
189#pragma mark - UI
190
192 const FaustUIItem& GetUIRoot() const { return mUIRoot; }
193
195 const char* GetJSON() const { return mJSON.c_str(); }
196
198 void BuildUI(UI* pUI);
199
200#pragma mark - Faust UI interface (used internally)
201
202 void openTabBox(const char* label) override;
203 void openHorizontalBox(const char* label) override;
204 void openVerticalBox(const char* label) override;
205 void closeBox() override;
206 void addButton(const char* label, FAUSTFLOAT* zone) override;
207 void addCheckButton(const char* label, FAUSTFLOAT* zone) override;
208 void addVerticalSlider(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT step) override;
209 void addHorizontalSlider(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT step) override;
210 void addNumEntry(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT step) override;
211 void addHorizontalBargraph(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT min, FAUSTFLOAT max) override;
212 void addVerticalBargraph(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT min, FAUSTFLOAT max) override;
213 void addSoundfile(const char* label, const char* filename, Soundfile** sfZone) override;
214 void declare(FAUSTFLOAT* zone, const char* key, const char* value) override;
215
216protected:
221 void SetDSP(::dsp* pMonoDSP);
222
225 void ReleaseDSP();
226
230
231 WDL_String mName;
232
233private:
234 struct Param
235 {
236 std::unique_ptr<IParam> param = std::make_unique<IParam>(); // describes the parameter, copied to the plug-in's IParam in CreateIPlugParameters()
237 FAUSTFLOAT* zone = nullptr;
238 std::vector<double> enumValues; // for menus and radio buttons: the Faust value for each enum index
239 std::string path;
240 };
241
242 struct Bargraph
243 {
244 FAUSTFLOAT* zone = nullptr;
245 double min = 0.;
246 double max = 1.;
247 int ctrlTag = kNoTag;
248 bool hidden = false; // [hidden:1] or in a hidden group: IGraphicsFaustUI creates no control, so no values are sent
249 double lastSentValue = -1.;
250 };
251
257 class DSPGuard
258 {
259 public:
260 bool TryEnter()
261 {
262 mNUsers.fetch_add(1, std::memory_order_seq_cst);
263
264 if (mReplacing.load(std::memory_order_seq_cst))
265 {
266 mNUsers.fetch_sub(1, std::memory_order_release);
267 return false;
268 }
269
270 return true;
271 }
272
273 void Exit() { mNUsers.fetch_sub(1, std::memory_order_release); }
274
275 void BeginReplace()
276 {
277 mReplaceMutex.lock(); // only serializes replacing threads, the audio thread never takes it
278 mReplacing.store(true, std::memory_order_seq_cst);
279
280 while (mNUsers.load(std::memory_order_seq_cst) > 0)
281 std::this_thread::yield();
282 }
283
284 void EndReplace()
285 {
286 mReplacing.store(false, std::memory_order_release);
287 mReplaceMutex.unlock();
288 }
289
290 private:
291 std::atomic<int> mNUsers {0};
292 std::atomic<bool> mReplacing {false};
293 std::mutex mReplaceMutex;
294 };
295
297 class ScopedDSPUser
298 {
299 public:
300 ScopedDSPUser(DSPGuard& guard) : mGuard(guard), mEntered(guard.TryEnter()) {}
301 ~ScopedDSPUser() { if (mEntered) mGuard.Exit(); }
302 bool IsEntered() const { return mEntered; }
303 private:
304 DSPGuard& mGuard;
305 bool mEntered;
306 };
307
309 class ScopedDSPReplace
310 {
311 public:
312 ScopedDSPReplace(DSPGuard& guard) : mGuard(guard) { mGuard.BeginReplace(); }
313 ~ScopedDSPReplace() { mGuard.EndReplace(); }
314 private:
315 DSPGuard& mGuard;
316 };
317
318 void BuildDescription(::dsp* pDSP);
319 void OpenGroup(FaustUIItem::EType type, const char* label);
320 FaustUIItem& AddItem(FaustUIItem::EType type, const char* label, FAUSTFLOAT* zone);
321 void AddBargraph(FaustUIItem::EType type, const char* label, FAUSTFLOAT* zone, FAUSTFLOAT min, FAUSTFLOAT max);
322 void AddParam(FaustUIItem& item, IParam::EParamType type);
323 void RelinkParams();
324 void WriteZone(int faustParamIdx, double value);
325 void WriteZoneUnguarded(int faustParamIdx, double value);
326 void OnUITimer(Timer& timer);
327
328 int mNVoices;
329 int mCtrlTagStart;
330 int mMaxNInputs = -1;
331 int mMaxNOutputs = -1;
332 bool mChannelCountError = false;
333 bool mHasSoundfile = false; // soundfiles are not supported
334 double mSampleRate = 0.;
335
336 // Members used on the audio thread are only replaced while mDSPGuard keeps it out
337 DSPGuard mDSPGuard;
338 std::unique_ptr<iplug2_midi_handler> mMidiHandler;
339 std::unique_ptr<MidiUI> mMidiUI;
340 std::unique_ptr<::dsp> mDSP;
341 std::vector<Param> mParams;
342 std::vector<Bargraph> mBargraphs;
343 FaustUIItem mUIRoot;
344 std::string mJSON;
345
346 // The description of a new DSP is built into these (by the Faust UI interface methods), then swapped in
347 std::vector<Param> mNewParams;
348 std::vector<Bargraph> mNewBargraphs;
349 FaustUIItem mNewUIRoot;
350 bool mNewHasSoundfile = false;
351 std::vector<FaustUIItem*> mGroupStack;
352
353 IPlugAPIBase* mPlug = nullptr;
354 // Set on the main thread, read by OnParamChange() on any thread
355 std::atomic<int> mPlugParamStartIdx {-1}; // < 0 means not linked
356 std::atomic<int> mPlugParamEndIdx {-1};
357 bool mParamsChanged = false; // set when linked parameters were re-initialized. Main thread only (SetDSP() and InformHostIfParamsChanged())
358
359 std::unique_ptr<Timer> mUITimer;
360};
361
362END_IPLUG_NAMESPACE
IPlug Constant definitions, Types, magic numbers.
Platform timer primitives.
EParamType
Defines types or parameter.
The base class of an IPlug plug-in, which interacts with the different plug-in APIs.
Definition: IPlugAPIBase.h:43
IPlugFaust is the base class for hosting a Faust DSP inside an iPlug2 plug-in.
Definition: IPlugFaust.h:106
int NParams() const
Definition: IPlugFaust.h:148
int CreateIPlugParameters(IPlugAPIBase *pPlug, int startIdx=0, int endIdx=-1, bool setToDefault=true)
Initialize the plug-in's parameters startIdx...startIdx+NParams() from the Faust widgets,...
Definition: IPlugFaust.cpp:139
void InformHostIfParamsChanged()
If SetDSP() re-initialized the linked plug-in parameters (i.e.
Definition: IPlugFaust.cpp:356
int GetPlugParamIdx(int faustParamIdx) const
Definition: IPlugFaust.cpp:265
void ProcessMidiMsg(const IMidiMsg &msg)
Call this from the plug-in's ProcessMidiMsg()
Definition: IPlugFaust.cpp:105
void ReleaseDSP()
Destroys the DSP, waiting for a block that is being processed to finish first.
Definition: IPlugFaust.cpp:56
void SetSampleRate(double sampleRate)
Call this from the plug-in's OnReset().
Definition: IPlugFaust.cpp:91
void BuildUI(UI *pUI)
Calls buildUserInterface() and metadata() on the DSP, to build a Faust UI.
Definition: IPlugFaust.cpp:486
bool IsReady() const
Definition: IPlugFaust.h:138
const char * GetJSON() const
Definition: IPlugFaust.h:195
int GetFaustParamIdxForZone(FAUSTFLOAT *zone) const
Definition: IPlugFaust.cpp:254
void SetParameterValue(int faustParamIdx, double value)
Set a Faust widget's value via its parameter index (0...NParams()-1)
Definition: IPlugFaust.cpp:209
const FaustUIItem & GetUIRoot() const
Definition: IPlugFaust.h:192
void ProcessBlock(sample **inputs, sample **outputs, int nFrames)
Call this from the plug-in's ProcessBlock().
Definition: IPlugFaust.cpp:115
virtual void Init()=0
Create the DSP.
void SetParameterValueNormalised(int faustParamIdx, double normalizedValue)
Set a Faust widget's value via its parameter index (0...NParams()-1)
Definition: IPlugFaust.cpp:192
bool OnParamChange(int plugParamIdx)
If plugParamIdx is linked to a Faust widget, update its zone from the plug-in's parameter value.
Definition: IPlugFaust.cpp:178
void SetMaxChannelCount(int maxNInputs, int maxNOutputs)
Tell IPlugFaust the maximum number of channels the plug-in provides, if the DSP needs more it will ou...
Definition: IPlugFaust.cpp:85
void SetDSP(::dsp *pMonoDSP)
Takes ownership of a newly created DSP, wraps it for polyphony if required, (re)builds the parameters...
Definition: IPlugFaust.cpp:367
void SyncFaustParams()
Apply the values of all the parameters to the DSP (e.g.
Definition: IPlugFaust.cpp:235
A node in the description of a Faust DSP's user interface, as declared by its buildUserInterface() me...
Definition: IPlugFaust.h:68
Encapsulates a MIDI message and provides helper functions.
Definition: IPlugMidi.h:31
Base class for timer.
Definition: IPlugTimer.h:43