gearmulator

Emulation of classic VA synths of the late 90s/2000s that are based on Motorola 56300 family DSPs
Log | Files | Refs | Submodules | README | LICENSE

softknob.cpp (2133B)


      1 #include "softknob.h"
      2 
      3 #include <cassert>
      4 
      5 #include "controller.h"
      6 
      7 namespace pluginLib
      8 {
      9 	SoftKnob::SoftKnob(const Controller& _controller, const uint8_t _part, const uint32_t _parameterIndex)
     10 		: m_controller(_controller)
     11 		, m_part(_part)
     12 	{
     13 		m_sourceParam = _controller.getParameter(_parameterIndex, _part);
     14 		assert(m_sourceParam);
     15 
     16 		const auto& desc = m_sourceParam->getDescription();
     17 
     18 		m_targetSelect = _controller.getParameter(desc.softKnobTargetSelect, _part);
     19 		assert(m_targetSelect);
     20 
     21 		m_targetSelectListener.set(m_targetSelect, [this](auto*)
     22 		{
     23 			onTargetChanged();
     24 		});
     25 
     26 		m_sourceParamListener.set(m_sourceParam, [this](auto*)
     27 		{
     28 			onSourceValueChanged();
     29 		});
     30 
     31 		bind();
     32 	}
     33 
     34 	SoftKnob::~SoftKnob()
     35 	{
     36 		unbind();
     37 		m_sourceParamListener.reset();
     38 		m_targetSelectListener.reset();
     39 	}
     40 
     41 	void SoftKnob::onTargetChanged()
     42 	{
     43 		bind();
     44 	}
     45 
     46 	void SoftKnob::onSourceValueChanged() const
     47 	{
     48 		if(!m_targetParam)
     49 			return;
     50 
     51 		const auto v = m_sourceParam->getUnnormalizedValue();
     52 		m_targetParam->setUnnormalizedValue(v, Parameter::Origin::Derived);
     53 	}
     54 
     55 	void SoftKnob::onTargetValueChanged() const
     56 	{
     57 		assert(m_targetParam);
     58 		const auto v = m_targetParam->getUnnormalizedValue();
     59 		m_sourceParam->setUnnormalizedValue(v, Parameter::Origin::Derived);
     60 	}
     61 
     62 	void SoftKnob::bind()
     63 	{
     64 		unbind();
     65 
     66 		const auto* valueList = m_controller.getParameterDescriptions().getValueList(m_sourceParam->getDescription().softKnobTargetList);
     67 		if(!valueList)
     68 			return;
     69 
     70 		const auto& targets = valueList->texts;
     71 
     72 		const auto targetIndex = m_targetSelect->getUnnormalizedValue();
     73 
     74 		if(targetIndex < 0 || targetIndex >= static_cast<int>(targets.size()))
     75 			return;
     76 
     77 		const auto targetName = targets[targetIndex];
     78 		if(targetName.empty())
     79 			return;
     80 
     81 		m_targetParam = m_controller.getParameter(targetName, m_part);
     82 		if(!m_targetParam)
     83 			return;
     84 
     85 		m_targetParamListener.set(m_targetParam, [this](pluginLib::Parameter*)
     86 		{
     87 			onTargetValueChanged();
     88 		});
     89 
     90 		onBind(m_targetParam);
     91 
     92 		onTargetValueChanged();
     93 	}
     94 
     95 	void SoftKnob::unbind()
     96 	{
     97 		m_targetParamListener.reset();
     98 		m_targetParam = nullptr;
     99 		onBind(nullptr);
    100 	}
    101 }