21#include "faust/gui/GUI.h"
22#include "faust/gui/JSONUI.h"
25#ifndef FAUST_GUI_STATICS_DEFINED
26std::list<GUI*> GUI::fGuiList;
27ztimedmap GUI::gTimedZoneMap;
35 std::string CleanLabel(
const char* label)
37 if (!label || strcmp(label,
"0x00") == 0)
47, mCtrlTagStart(ctrlTagStart)
51IPlugFaust::~IPlugFaust()
64 std::unique_ptr<MidiUI> pMidiUI;
65 std::unique_ptr<::dsp> pDSP;
66 std::unique_ptr<iplug2_midi_handler> pMidiHandler;
68 std::vector<Param> params;
71 ScopedDSPReplace replace(mDSPGuard);
72 std::swap(mMidiUI, pMidiUI);
73 std::swap(mDSP, pDSP);
74 std::swap(mMidiHandler, pMidiHandler);
75 std::swap(mParams, params);
82 pMidiHandler =
nullptr;
87 mMaxNInputs = maxNInputs;
88 mMaxNOutputs = maxNOutputs;
93 mSampleRate = sampleRate;
96 ScopedDSPReplace replace(mDSPGuard);
99 mDSP->init(
static_cast<int>(sampleRate));
109 ScopedDSPUser user(mDSPGuard);
111 if (user.IsEntered() && mMidiHandler)
112 mMidiHandler->decodeMessage(msg);
118 ScopedDSPUser user(mDSPGuard);
120 int nOutputsProcessed = 0;
122 if (user.IsEntered() && mDSP && !mChannelCountError && !mHasSoundfile)
124 mDSP->compute(nFrames, inputs, outputs);
125 nOutputsProcessed = mDSP->getNumOutputs();
128 if (mMaxNOutputs > -1)
130 for (
int c = nOutputsProcessed; c < mMaxNOutputs; c++)
132 memset(outputs[c], 0, nFrames *
sizeof(sample));
137#pragma mark - Parameters
141 assert(pPlug !=
nullptr);
146 mPlugParamStartIdx = startIdx;
147 mPlugParamEndIdx = (endIdx < 0 || endIdx >
NParams()) ?
NParams() : endIdx;
152 for (
int p = 0; p < mPlugParamEndIdx; p++)
154 const int plugParamIdx = mPlugParamStartIdx + p;
156 if (plugParamIdx >= pPlug->NParams())
158 DBGMSG(
"IPlugFaust-%s: the plug-in doesn't have enough parameters for the Faust DSP (needs %i)\n", mName.Get(), mPlugParamStartIdx + mPlugParamEndIdx);
159 mPlugParamEndIdx = p;
163 IParam* pPlugParam = pPlug->GetParam(plugParamIdx);
164 const double currentValueNormalized = pPlugParam->
GetNormalized();
165 pPlugParam->
Init(*mParams[p].param);
175 return mPlugParamStartIdx;
180 if (!mPlug || mPlugParamStartIdx < 0)
183 const int faustParamIdx = plugParamIdx - mPlugParamStartIdx;
185 if (faustParamIdx < 0 || faustParamIdx >= mPlugParamEndIdx)
188 WriteZone(faustParamIdx, mPlug->GetParam(plugParamIdx)->Value());
195 ScopedDSPUser user(mDSPGuard);
197 if (!user.IsEntered())
200 if (faustParamIdx < 0 || faustParamIdx >=
NParams())
202 DBGMSG(
"IPlugFaust-%s: no parameter %i\n", mName.Get(), faustParamIdx);
206 WriteZoneUnguarded(faustParamIdx, mParams[faustParamIdx].param->FromNormalized(normalizedValue));
211 WriteZone(faustParamIdx, value);
216 ScopedDSPUser user(mDSPGuard);
218 if (!user.IsEntered())
221 for (
int i = 0; i <
NParams(); i++)
223 const Param& p = mParams[i];
225 if (p.path == labelOrPath || strcmp(p.param->GetName(), labelOrPath) == 0)
227 WriteZoneUnguarded(i, value);
232 DBGMSG(
"IPlugFaust-%s: no parameter named %s\n", mName.Get(), labelOrPath);
237 ScopedDSPUser user(mDSPGuard);
240 if (!user.IsEntered())
243 for (
int p = 0; p <
NParams(); p++)
247 if (plugParamIdx > kNoParameter)
248 WriteZoneUnguarded(p, mPlug->GetParam(plugParamIdx)->Value());
250 WriteZoneUnguarded(p, mParams[p].param->Value());
256 for (
int p = 0; p <
NParams(); p++)
258 if (mParams[p].zone == zone)
267 if (!mPlug || mPlugParamStartIdx < 0 || faustParamIdx < 0 || faustParamIdx >= mPlugParamEndIdx)
270 return mPlugParamStartIdx + faustParamIdx;
273void IPlugFaust::WriteZone(
int faustParamIdx,
double value)
275 ScopedDSPUser user(mDSPGuard);
278 if (user.IsEntered())
279 WriteZoneUnguarded(faustParamIdx, value);
282void IPlugFaust::WriteZoneUnguarded(
int faustParamIdx,
double value)
284 if (faustParamIdx < 0 || faustParamIdx >=
NParams())
287 Param& p = mParams[faustParamIdx];
289 if (!p.enumValues.empty())
291 const int idx =
Clip(
static_cast<int>(value + 0.5), 0,
static_cast<int>(p.enumValues.size()) - 1);
292 value = p.enumValues[idx];
295 *p.zone =
static_cast<FAUSTFLOAT
>(value);
311 double valueA, valueB;
315 if (valueA != valueB || strcmp(textA, textB) != 0)
323void IPlugFaust::RelinkParams()
325 if (!mPlug || mPlugParamStartIdx < 0)
328 const int prevEndIdx = mPlugParamEndIdx;
329 mPlugParamEndIdx = std::min(
NParams(), mPlug->NParams() - mPlugParamStartIdx);
331 if (mPlugParamEndIdx <
NParams())
332 DBGMSG(
"IPlugFaust-%s: the plug-in doesn't have enough parameters for the Faust DSP (needs %i)\n", mName.Get(), mPlugParamStartIdx +
NParams());
334 bool changed = mPlugParamEndIdx != prevEndIdx;
339 for (
int p = 0; p < mPlugParamEndIdx; p++)
341 IParam* pPlugParam = mPlug->GetParam(mPlugParamStartIdx + p);
342 const IParam& faustParam = *mParams[p].param;
344 if (!SameDefinition(*pPlugParam, faustParam))
346 pPlugParam->
Init(faustParam);
347 pPlugParam->
Set(faustParam.
Value());
353 mParamsChanged =
true;
358 if (mParamsChanged && mPlug)
360 mParamsChanged =
false;
369 assert(pMonoDSP !=
nullptr);
372 bool midi =
false, midiSync =
false;
374 MidiMeta::analyse(pMonoDSP, midi, midiSync, nVoices);
376 if (nVoices == 0 && mNVoices > 1)
379 auto pMidiHandler = std::make_unique<iplug2_midi_handler>();
380 std::unique_ptr<::dsp> pDSP;
383 pDSP.reset(
new mydsp_poly(pMonoDSP, nVoices,
true ,
true ));
385 pDSP.reset(pMonoDSP);
388 auto pMidiUI = std::make_unique<MidiUI>(pMidiHandler.get());
389 pDSP->buildUserInterface(pMidiUI.get());
392 pDSP->init(mSampleRate > 0. ?
static_cast<int>(mSampleRate) :
static_cast<int>(DEFAULT_SAMPLE_RATE));
394 JSONUI jsonUI(pDSP->getNumInputs(), pDSP->getNumOutputs());
395 pDSP->buildUserInterface(&jsonUI);
396 pDSP->metadata(&jsonUI);
399 BuildDescription(pDSP.get());
402 for (
auto& newParam : mNewParams)
404 for (
int p = 0; p <
NParams(); p++)
406 if (mParams[p].path == newParam.path)
409 newParam.param->Set(plugParamIdx > kNoParameter ? mPlug->GetParam(plugParamIdx)->Value() : mParams[p].param->Value());
415 const bool channelCountError = (mMaxNInputs > -1 && pDSP->getNumInputs() > mMaxNInputs) ||
416 (mMaxNOutputs > -1 && pDSP->getNumOutputs() > mMaxNOutputs);
418 const bool hadDSP = mDSP !=
nullptr;
419 std::string json = jsonUI.JSON();
424 ScopedDSPReplace replace(mDSPGuard);
426 std::swap(mMidiUI, pMidiUI);
427 std::swap(mDSP, pDSP);
428 std::swap(mMidiHandler, pMidiHandler);
429 std::swap(mParams, mNewParams);
430 std::swap(mBargraphs, mNewBargraphs);
431 std::swap(mUIRoot, mNewUIRoot);
432 std::swap(mJSON, json);
433 mChannelCountError = channelCountError;
434 mHasSoundfile = mNewHasSoundfile;
438 mNewBargraphs.clear();
441 if (mChannelCountError)
443 DBGMSG(
"IPlugFaust-%s: the DSP has %i inputs and %i outputs, but the plug-in only provides %i inputs and %i outputs. Outputting silence.\n",
444 mName.Get(), mDSP->getNumInputs(), mDSP->getNumOutputs(), mMaxNInputs, mMaxNOutputs);
450 pMidiHandler =
nullptr;
457 if (!mBargraphs.empty() && !mUITimer)
461void IPlugFaust::BuildDescription(::dsp* pDSP)
465 mNewBargraphs.clear();
466 mNewHasSoundfile =
false;
470 mNewUIRoot.type = FaustUIItem::EType::VBox;
472 mGroupStack.push_back(&mNewUIRoot);
474 pDSP->buildUserInterface(
this);
479 if (mNewUIRoot.children.size() == 1 && mNewUIRoot.children[0].IsGroup())
481 FaustUIItem root = std::move(mNewUIRoot.children[0]);
482 mNewUIRoot = std::move(root);
490 mDSP->buildUserInterface(pUI);
492 if (
auto* pMeta =
dynamic_cast<Meta*
>(pUI))
493 mDSP->metadata(pMeta);
497void IPlugFaust::OnUITimer(
Timer& timer)
503 for (
auto& b : mBargraphs)
508 const double range = b.max - b.min;
509 const double normalized = range != 0. ?
Clip((
static_cast<double>(*b.zone) - b.min) / range, 0., 1.) : 0.;
511 if (normalized != b.lastSentValue)
513 b.lastSentValue = normalized;
514 mPlug->SendControlValueFromDelegate(b.ctrlTag, normalized);
519#pragma mark - Faust UI interface
521void IPlugFaust::OpenGroup(FaustUIItem::EType type,
const char* label)
523 std::string cleanLabel;
524 std::map<std::string, std::string> metadata;
525 extractMetadata(CleanLabel(label), cleanLabel, metadata);
528 parent.children.emplace_back();
531 group.label = cleanLabel;
532 group.path = parent.path +
"/" + cleanLabel;
533 group.hidden = metadata.count(
"hidden") && metadata[
"hidden"] ==
"1";
534 group.tooltip = metadata.count(
"tooltip") ? metadata[
"tooltip"] :
"";
536 mGroupStack.push_back(&group);
539void IPlugFaust::openTabBox(
const char* label)
541 OpenGroup(FaustUIItem::EType::TabBox, label);
544void IPlugFaust::openHorizontalBox(
const char* label)
546 OpenGroup(FaustUIItem::EType::HBox, label);
549void IPlugFaust::openVerticalBox(
const char* label)
551 OpenGroup(FaustUIItem::EType::VBox, label);
554void IPlugFaust::closeBox()
556 if (mGroupStack.size() > 1)
557 mGroupStack.pop_back();
560FaustUIItem& IPlugFaust::AddItem(FaustUIItem::EType type,
const char* label, FAUSTFLOAT* zone)
563 parent.children.emplace_back();
566 item.label = CleanLabel(label);
567 item.path = parent.path +
"/" + item.label;
569 item.hidden = isHidden(zone);
570 item.knob = isKnob(zone);
571 item.led = isLed(zone);
572 item.numerical = isNumerical(zone);
573 item.menu = isMenu(zone);
574 item.radio = isRadio(zone);
575 item.unit = getUnit(zone);
576 item.tooltip = getTooltip(zone);
579 item.options = getMenuDescription(zone);
581 item.options = getRadioDescription(zone);
591 for (
size_t i = 1; i < mGroupStack.size(); i++)
593 const std::string& label = mGroupStack[i]->label;
604 if (group.length() > MAX_PARAM_GROUP_LEN - 1)
605 group = group.substr(0, MAX_PARAM_GROUP_LEN - 4) +
"...";
611 const char* name = item.label.c_str();
612 const char* unit = item.unit.c_str();
617 case IParam::kTypeBool:
618 p.param->InitBool(name, item.init > 0.5, unit, flags, group.c_str());
620 case IParam::kTypeEnum:
622 const int nOptions =
static_cast<int>(item.options.size());
625 for (
int i = 0; i < nOptions; i++)
627 p.enumValues.push_back(item.options[i].second);
629 if (item.options[i].second == item.init)
633 p.param->InitEnum(name, defaultIdx, nOptions, unit, flags, group.c_str());
635 for (
int i = 0; i < nOptions; i++)
636 p.param->SetDisplayText(i, item.options[i].first.c_str());
640 case IParam::kTypeDouble:
643 const double step = item.step > 0. ? item.step : 0.001;
645 switch (getScale(item.zone))
647 case MetaDataUI::kLog:
649 p.param->InitDouble(name, item.init, item.min, item.max, step, unit, flags, group.c_str(),
IParam::ShapeExp());
651 p.param->InitDouble(name, item.init, item.min, item.max, step, unit, flags, group.c_str(),
IParam::ShapePowCurve(3.));
653 case MetaDataUI::kExp:
654 p.param->InitDouble(name, item.init, item.min, item.max, step, unit, flags, group.c_str(),
IParam::ShapePowCurve(1./3.));
656 case MetaDataUI::kLin:
658 p.param->InitDouble(name, item.init, item.min, item.max, step, unit, flags, group.c_str());
665 item.paramIdx =
static_cast<int>(mNewParams.size());
666 mNewParams.push_back(std::move(p));
669void IPlugFaust::addButton(
const char* label, FAUSTFLOAT* zone)
671 FaustUIItem& item = AddItem(FaustUIItem::EType::Button, label, zone);
672 AddParam(item, IParam::kTypeBool);
675void IPlugFaust::addCheckButton(
const char* label, FAUSTFLOAT* zone)
677 FaustUIItem& item = AddItem(FaustUIItem::EType::CheckButton, label, zone);
678 AddParam(item, IParam::kTypeBool);
681void IPlugFaust::addVerticalSlider(
const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT step)
683 FaustUIItem& item = AddItem(FaustUIItem::EType::VSlider, label, zone);
684 item.init = init; item.min = min; item.max = max; item.step = step;
685 AddParam(item, (item.menu || item.radio) ? IParam::kTypeEnum : IParam::kTypeDouble);
688void IPlugFaust::addHorizontalSlider(
const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT step)
690 FaustUIItem& item = AddItem(FaustUIItem::EType::HSlider, label, zone);
691 item.init = init; item.min = min; item.max = max; item.step = step;
692 AddParam(item, (item.menu || item.radio) ? IParam::kTypeEnum : IParam::kTypeDouble);
695void IPlugFaust::addNumEntry(
const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT step)
697 FaustUIItem& item = AddItem(FaustUIItem::EType::NumEntry, label, zone);
698 item.init = init; item.min = min; item.max = max; item.step = step;
699 AddParam(item, (item.menu || item.radio) ? IParam::kTypeEnum : IParam::kTypeDouble);
702void IPlugFaust::AddBargraph(FaustUIItem::EType type,
const char* label, FAUSTFLOAT* zone, FAUSTFLOAT min, FAUSTFLOAT max)
705 item.min = min; item.max = max;
706 item.ctrlTag = mCtrlTagStart +
static_cast<int>(mNewBargraphs.size());
709 bool hidden = item.hidden;
712 hidden = hidden || pGroup->hidden;
718 b.ctrlTag = item.ctrlTag;
720 mNewBargraphs.push_back(b);
723void IPlugFaust::addHorizontalBargraph(
const char* label, FAUSTFLOAT* zone, FAUSTFLOAT min, FAUSTFLOAT max)
725 AddBargraph(FaustUIItem::EType::HBargraph, label, zone, min, max);
728void IPlugFaust::addVerticalBargraph(
const char* label, FAUSTFLOAT* zone, FAUSTFLOAT min, FAUSTFLOAT max)
730 AddBargraph(FaustUIItem::EType::VBargraph, label, zone, min, max);
733void IPlugFaust::addSoundfile(
const char* label,
const char* filename, Soundfile** sfZone)
736 DBGMSG(
"IPlugFaust-%s: soundfiles are not supported (%s), the DSP will output silence\n", mName.Get(), label);
737 mNewHasSoundfile =
true;
740void IPlugFaust::declare(FAUSTFLOAT* zone,
const char* key,
const char* value)
742 MetaDataUI::declare(zone, key, value);
#define FAUST_UI_INTERVAL
Interval in milliseconds at which bargraph (meter) values are sent to the editor.
double GetStep() const
Returns the parameter's step size.
double GetDefault(bool normalized=false) const
Returns the parameter's default value.
void SetToDefault()
Replaces the parameter's current value with the default one
EParamType Type() const
Get the parameter's type.
int GetFlags() const
Returns the parameter's flags.
double GetMin() const
Returns the parameter's minimum value.
void Init(const IParam &p, const char *searchStr="", const char *replaceStr="", const char *newGroup="")
Initialize the parameter based on another parameter, replacing a CString in the name.
void Set(double value)
Sets the parameter value.
const char * GetLabel() const
Returns the parameter's label.
void SetNormalized(double normalizedValue)
Sets the parameter value from a normalized range (usually coming from the linked IControl)
const char * GetName() const
Returns the parameter's name.
double GetNormalized() const
Returns the parameter's normalized value.
const char * GetDisplayTextAtIdx(int idx, double *pValue=nullptr) const
Get the display text at a particular index.
const char * GetGroup() const
Returns the parameter's group.
double GetShapeValue() const
double Value() const
Gets a readable value of the parameter.
int NDisplayTexts() const
Get the number of display texts for the parameter.
EParamType
Defines types or parameter.
EShapeIDs GetShapeID() const
double GetMax() const
Returns the parameter's maximum value.
The base class of an IPlug plug-in, which interacts with the different plug-in APIs.
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,...
void InformHostIfParamsChanged()
If SetDSP() re-initialized the linked plug-in parameters (i.e.
int GetPlugParamIdx(int faustParamIdx) const
void ProcessMidiMsg(const IMidiMsg &msg)
Call this from the plug-in's ProcessMidiMsg()
void ReleaseDSP()
Destroys the DSP, waiting for a block that is being processed to finish first.
void SetSampleRate(double sampleRate)
Call this from the plug-in's OnReset().
void BuildUI(UI *pUI)
Calls buildUserInterface() and metadata() on the DSP, to build a Faust UI.
int GetFaustParamIdxForZone(FAUSTFLOAT *zone) const
void SetParameterValue(int faustParamIdx, double value)
Set a Faust widget's value via its parameter index (0...NParams()-1)
void ProcessBlock(sample **inputs, sample **outputs, int nFrames)
Call this from the plug-in's ProcessBlock().
IPlugFaust(const char *name, int nVoices=0, int ctrlTagStart=kDefaultCtrlTagStart)
void SetParameterValueNormalised(int faustParamIdx, double normalizedValue)
Set a Faust widget's value via its parameter index (0...NParams()-1)
bool OnParamChange(int plugParamIdx)
If plugParamIdx is linked to a Faust widget, update its zone from the plug-in's parameter value.
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...
void SetDSP(::dsp *pMonoDSP)
Takes ownership of a newly created DSP, wraps it for polyphony if required, (re)builds the parameters...
void SyncFaustParams()
Apply the values of all the parameters to the DSP (e.g.
virtual void InformHostOfParameterDetailsChange()
Implemented by the API class.
BEGIN_IPLUG_NAMESPACE T Clip(T x, T lo, T hi)
Clips the value x between lo and hi.
A node in the description of a Faust DSP's user interface, as declared by its buildUserInterface() me...
Encapsulates a MIDI message and provides helper functions.
Exponential parameter shaping.
PowCurve parameter shaping.
static Timer * Create(ITimerFunction func, uint32_t intervalMs)
On Linux, callbacks run on a worker; returns nullptr if that worker cannot be created.