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

parameterlink.cpp (1788B)


      1 
      2 #include "parameterlink.h"
      3 
      4 #include "parameter.h"
      5 
      6 namespace pluginLib
      7 {
      8 	ParameterLink::ParameterLink(Parameter* _source, Parameter* _dest, bool _applyCurrentSourceToTarget) : m_source(_source), m_sourceListener(_source)
      9 	{
     10 		m_sourceValue = _source->getUnnormalizedValue();
     11 
     12 		m_sourceListener = [this](Parameter*)
     13 		{
     14 			onSourceValueChanged();
     15 		};
     16 
     17 		_source->setLinkState(Source);
     18 		add(_dest, _applyCurrentSourceToTarget);
     19 	}
     20 
     21 	ParameterLink::~ParameterLink()
     22 	{
     23 		m_source->clearLinkState(Source);
     24 	}
     25 
     26 	bool ParameterLink::add(Parameter* _target, bool _applyCurrentSourceToTarget)
     27 	{
     28 		if(!_target)
     29 			return false;
     30 
     31 		if(!m_targets.insert(_target).second)
     32 			return false;
     33 
     34 		if(_applyCurrentSourceToTarget)
     35 			_target->setUnnormalizedValueNotifyingHost(m_sourceValue, Parameter::Origin::Ui);
     36 
     37 		return true;
     38 	}
     39 
     40 	// ReSharper disable once CppParameterMayBeConstPtrOrRef
     41 	bool ParameterLink::remove(Parameter* _target)
     42 	{
     43 		const auto it = m_targets.find(_target);
     44 		if(it == m_targets.end())
     45 			return false;
     46 		m_targets.erase(it);
     47 		return true;
     48 	}
     49 
     50 	void ParameterLink::onSourceValueChanged()
     51 	{
     52 		const auto newSourceValue = m_source->getUnnormalizedValue();
     53 
     54 		if(newSourceValue == m_sourceValue)
     55 			return;
     56 
     57 		const auto sourceDiff = newSourceValue - m_sourceValue;
     58 
     59 		m_sourceValue = newSourceValue;
     60 
     61 		// do not apply to linked parameters if the change is caused by a preset load
     62 		if(m_source->getChangeOrigin() == Parameter::Origin::PresetChange)
     63 			return;
     64 
     65 		const auto origin = m_source->getChangeOrigin();
     66 
     67 		for (auto* p : m_targets)
     68 		{
     69 			const auto newTargetValue = p->getUnnormalizedValue() + sourceDiff;
     70 			const auto clampedTargetValue = p->getDescription().range.clipValue(newTargetValue);
     71 			p->setUnnormalizedValue(clampedTargetValue, origin);
     72 		}
     73 	}
     74 }