parameter.h (4018B)
1 #pragma once 2 3 #include <set> 4 5 #include "parameterdescription.h" 6 7 #include "juce_audio_processors/juce_audio_processors.h" 8 9 #include "types.h" 10 11 #include "baseLib/event.h" 12 13 namespace pluginLib 14 { 15 class Controller; 16 17 class Parameter : juce::Value::Listener, public juce::RangedAudioParameter 18 { 19 public: 20 using PartFormatter = std::function<juce::String(uint8_t, bool)>; // part, non-part-exclusive 21 22 enum class Origin 23 { 24 Unknown, 25 PresetChange, 26 Midi, 27 HostAutomation, 28 Ui, 29 Derived 30 }; 31 32 baseLib::Event<Parameter*, bool> onLockedChanged; 33 baseLib::Event<Parameter*, ParameterLinkType> onLinkStateChanged; 34 baseLib::Event<Parameter*> onValueChanged; 35 36 Parameter(Controller& _controller, const Description& _desc, uint8_t _partNum, int _uniqueId, const PartFormatter& _partFormatter); 37 38 juce::Value& getValueObject() { return m_value; } 39 40 const Description& getDescription() const { return m_desc; } 41 42 uint8_t getPart() const { return m_part; } 43 44 const juce::NormalisableRange<float> &getNormalisableRange() const override { return m_range; } 45 46 bool isMetaParameter() const override; 47 48 ParamValue getUnnormalizedValue() const { return juce::roundToInt(m_value.getValue()); } 49 float getValue() const override { return convertTo0to1(m_value.getValue()); } 50 51 void setValue(float _newValue) override; 52 53 void setUnnormalizedValue(int _newValue, Origin _origin); 54 void setValueNotifyingHost(float _value, Origin _origin); 55 void setUnnormalizedValueNotifyingHost(float _value, Origin _origin); 56 void setUnnormalizedValueNotifyingHost(int _value, Origin _origin); 57 58 void setValueFromSynth(int _newValue, Origin _origin); 59 60 bool isDiscrete() const override { return m_desc.isDiscrete; } 61 bool isBoolean() const override { return m_desc.isBool; } 62 bool isBipolar() const { return m_desc.isBipolar; } 63 64 float getValueForText(const juce::String& _text) const override; 65 66 float getDefaultValue() const override 67 { 68 return convertTo0to1(static_cast<float>(getDefault())); 69 } 70 71 virtual ParamValue getDefault() const; 72 73 juce::String getText(float _normalisedValue, int /*maximumStringLength*/) const override; 74 75 void setLocked(bool _locked); 76 bool isLocked() const { return m_isLocked; } 77 78 void addDerivedParameter(Parameter* _param); 79 80 int getUniqueId() const { return m_uniqueId; } 81 82 const std::set<Parameter*>& getDerivedParameters() { return m_derivedParameters; } 83 84 Origin getChangeOrigin() const { return m_lastValueOrigin; } 85 86 void setRateLimitMilliseconds(uint32_t _ms); 87 88 void setLinkState(ParameterLinkType _type); 89 void clearLinkState(ParameterLinkType _type); 90 91 ParameterLinkType getLinkState() const { return m_linkType; } 92 93 void pushChangeGesture(); 94 void popChangeGesture(); 95 96 private: 97 98 struct ScopedChangeGesture 99 { 100 explicit ScopedChangeGesture(Parameter& _p); 101 ~ScopedChangeGesture(); 102 103 private: 104 Parameter& m_parameter; 105 }; 106 107 static juce::String genId(const Description &d, int part, int uniqueId); 108 void valueChanged(juce::Value &) override; 109 void setDerivedValue(const int _value); 110 void sendToSynth(); 111 static uint64_t milliseconds(); 112 void sendParameterChangeDelayed(ParamValue _value, uint32_t _uniqueId); 113 void forwardToDerived(const int _newValue); 114 void notifyHost(float _value); 115 116 int clampValue(int _value) const; 117 118 Controller& m_controller; 119 const Description& m_desc; 120 juce::NormalisableRange<float> m_range; 121 const uint8_t m_part; 122 const int m_uniqueId; // 0 for all unique parameters, > 0 if multiple Parameter instances reference a single synth parameter 123 124 int m_lastValue{-1}; 125 Origin m_lastValueOrigin = Origin::Unknown; 126 juce::Value m_value; 127 std::set<Parameter*> m_derivedParameters; 128 bool m_changingDerivedValues = false; 129 130 uint32_t m_rateLimit = 0; // milliseconds 131 uint64_t m_lastSendTime = 0; 132 uint32_t m_uniqueDelayCallbackId = 0; 133 134 bool m_isLocked = false; 135 ParameterLinkType m_linkType = None; 136 uint32_t m_changeGestureCount = 0; 137 bool m_notifyingHost = false; 138 }; 139 }