Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
8.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. ParameterAttachment::ParameterAttachment (RangedAudioParameter& param,
  16. std::function<void (float)> parameterChangedCallback,
  17. UndoManager* um)
  18. : parameter (param),
  19. undoManager (um),
  20. setValue (std::move (parameterChangedCallback))
  21. {
  22. parameter.addListener (this);
  23. }
  24. ParameterAttachment::~ParameterAttachment()
  25. {
  26. parameter.removeListener (this);
  27. cancelPendingUpdate();
  28. }
  29. void ParameterAttachment::sendInitialUpdate()
  30. {
  31. parameterValueChanged ({}, parameter.getValue());
  32. }
  33. void ParameterAttachment::setValueAsCompleteGesture (float newDenormalisedValue)
  34. {
  35. callIfParameterValueChanged (newDenormalisedValue, [this] (float f)
  36. {
  37. beginGesture();
  38. parameter.setValueNotifyingHost (f);
  39. endGesture();
  40. });
  41. }
  42. void ParameterAttachment::beginGesture()
  43. {
  44. if (undoManager != nullptr)
  45. undoManager->beginNewTransaction();
  46. parameter.beginChangeGesture();
  47. }
  48. void ParameterAttachment::setValueAsPartOfGesture (float newDenormalisedValue)
  49. {
  50. callIfParameterValueChanged (newDenormalisedValue, [this] (float f)
  51. {
  52. parameter.setValueNotifyingHost (f);
  53. });
  54. }
  55. void ParameterAttachment::endGesture()
  56. {
  57. parameter.endChangeGesture();
  58. }
  59. template <typename Callback>
  60. void ParameterAttachment::callIfParameterValueChanged (float newDenormalisedValue,
  61. Callback&& callback)
  62. {
  63. const auto newValue = normalise (newDenormalisedValue);
  64. if (parameter.getValue() != newValue)
  65. callback (newValue);
  66. }
  67. void ParameterAttachment::parameterValueChanged (int, float newValue)
  68. {
  69. lastValue = newValue;
  70. if (MessageManager::getInstance()->isThisTheMessageThread())
  71. {
  72. cancelPendingUpdate();
  73. handleAsyncUpdate();
  74. }
  75. else
  76. {
  77. triggerAsyncUpdate();
  78. }
  79. }
  80. void ParameterAttachment::handleAsyncUpdate()
  81. {
  82. if (setValue != nullptr)
  83. setValue (parameter.convertFrom0to1 (lastValue));
  84. }
  85. //==============================================================================
  86. SliderParameterAttachment::SliderParameterAttachment (RangedAudioParameter& param,
  87. Slider& s,
  88. UndoManager* um)
  89. : slider (s),
  90. attachment (param, [this] (float f) { setValue (f); }, um)
  91. {
  92. slider.valueFromTextFunction = [&param] (const String& text) { return (double) param.convertFrom0to1 (param.getValueForText (text)); };
  93. slider.textFromValueFunction = [&param] (double value) { return param.getText (param.convertTo0to1 ((float) value), 0); };
  94. slider.setDoubleClickReturnValue (true, param.convertFrom0to1 (param.getDefaultValue()));
  95. auto range = param.getNormalisableRange();
  96. auto convertFrom0To1Function = [range] (double currentRangeStart,
  97. double currentRangeEnd,
  98. double normalisedValue) mutable
  99. {
  100. range.start = (float) currentRangeStart;
  101. range.end = (float) currentRangeEnd;
  102. return (double) range.convertFrom0to1 ((float) normalisedValue);
  103. };
  104. auto convertTo0To1Function = [range] (double currentRangeStart,
  105. double currentRangeEnd,
  106. double mappedValue) mutable
  107. {
  108. range.start = (float) currentRangeStart;
  109. range.end = (float) currentRangeEnd;
  110. return (double) range.convertTo0to1 ((float) mappedValue);
  111. };
  112. auto snapToLegalValueFunction = [range] (double currentRangeStart,
  113. double currentRangeEnd,
  114. double mappedValue) mutable
  115. {
  116. range.start = (float) currentRangeStart;
  117. range.end = (float) currentRangeEnd;
  118. return (double) range.snapToLegalValue ((float) mappedValue);
  119. };
  120. NormalisableRange<double> newRange { (double) range.start,
  121. (double) range.end,
  122. std::move (convertFrom0To1Function),
  123. std::move (convertTo0To1Function),
  124. std::move (snapToLegalValueFunction) };
  125. newRange.interval = range.interval;
  126. newRange.skew = range.skew;
  127. newRange.symmetricSkew = range.symmetricSkew;
  128. slider.setNormalisableRange (newRange);
  129. sendInitialUpdate();
  130. slider.valueChanged();
  131. slider.addListener (this);
  132. }
  133. SliderParameterAttachment::~SliderParameterAttachment()
  134. {
  135. slider.removeListener (this);
  136. }
  137. void SliderParameterAttachment::sendInitialUpdate() { attachment.sendInitialUpdate(); }
  138. void SliderParameterAttachment::setValue (float newValue)
  139. {
  140. const ScopedValueSetter<bool> svs (ignoreCallbacks, true);
  141. slider.setValue (newValue, sendNotificationSync);
  142. }
  143. void SliderParameterAttachment::sliderValueChanged (Slider*)
  144. {
  145. if (ignoreCallbacks || ModifierKeys::currentModifiers.isRightButtonDown())
  146. return;
  147. attachment.setValueAsPartOfGesture ((float) slider.getValue());
  148. }
  149. //==============================================================================
  150. ComboBoxParameterAttachment::ComboBoxParameterAttachment (RangedAudioParameter& param,
  151. ComboBox& c,
  152. UndoManager* um)
  153. : comboBox (c),
  154. attachment (param, [this] (float f) { setValue (f); }, um)
  155. {
  156. sendInitialUpdate();
  157. comboBox.addListener (this);
  158. }
  159. ComboBoxParameterAttachment::~ComboBoxParameterAttachment()
  160. {
  161. comboBox.removeListener (this);
  162. }
  163. void ComboBoxParameterAttachment::sendInitialUpdate()
  164. {
  165. attachment.sendInitialUpdate();
  166. }
  167. void ComboBoxParameterAttachment::setValue (float newValue)
  168. {
  169. const auto index = roundToInt (newValue);
  170. if (index == comboBox.getSelectedItemIndex())
  171. return;
  172. const ScopedValueSetter<bool> svs (ignoreCallbacks, true);
  173. comboBox.setSelectedItemIndex (index, sendNotificationSync);
  174. }
  175. void ComboBoxParameterAttachment::comboBoxChanged (ComboBox*)
  176. {
  177. if (ignoreCallbacks)
  178. return;
  179. attachment.setValueAsCompleteGesture ((float) comboBox.getSelectedItemIndex());
  180. }
  181. //==============================================================================
  182. ButtonParameterAttachment::ButtonParameterAttachment (RangedAudioParameter& param,
  183. Button& b,
  184. UndoManager* um)
  185. : button (b),
  186. attachment (param, [this] (float f) { setValue (f); }, um)
  187. {
  188. sendInitialUpdate();
  189. button.addListener (this);
  190. }
  191. ButtonParameterAttachment::~ButtonParameterAttachment()
  192. {
  193. button.removeListener (this);
  194. }
  195. void ButtonParameterAttachment::sendInitialUpdate()
  196. {
  197. attachment.sendInitialUpdate();
  198. }
  199. void ButtonParameterAttachment::setValue (float newValue)
  200. {
  201. const ScopedValueSetter<bool> svs (ignoreCallbacks, true);
  202. button.setToggleState (newValue >= 0.5f, sendNotificationSync);
  203. }
  204. void ButtonParameterAttachment::buttonClicked (Button*)
  205. {
  206. if (ignoreCallbacks)
  207. return;
  208. attachment.setValueAsCompleteGesture (button.getToggleState() ? 1.0f : 0.0f);
  209. }
  210. } // namespace juce