The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

928 lines
36KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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. //==============================================================================
  16. AudioProcessorValueTreeState::Parameter::Parameter (const String& parameterID,
  17. const String& parameterName,
  18. const String& labelText,
  19. NormalisableRange<float> valueRange,
  20. float defaultParameterValue,
  21. std::function<String(float)> valueToTextFunction,
  22. std::function<float(const String&)> textToValueFunction,
  23. bool isMetaParameter,
  24. bool isAutomatableParameter,
  25. bool isDiscrete,
  26. AudioProcessorParameter::Category parameterCategory,
  27. bool isBoolean)
  28. : AudioParameterFloat (parameterID,
  29. parameterName,
  30. valueRange,
  31. defaultParameterValue,
  32. labelText,
  33. parameterCategory,
  34. valueToTextFunction == nullptr ? std::function<String(float v, int)>()
  35. : [valueToTextFunction](float v, int) { return valueToTextFunction (v); },
  36. std::move (textToValueFunction)),
  37. unsnappedDefault (valueRange.convertTo0to1 (defaultParameterValue)),
  38. metaParameter (isMetaParameter),
  39. automatable (isAutomatableParameter),
  40. discrete (isDiscrete),
  41. boolean (isBoolean)
  42. {
  43. }
  44. float AudioProcessorValueTreeState::Parameter::getDefaultValue() const { return unsnappedDefault; }
  45. int AudioProcessorValueTreeState::Parameter::getNumSteps() const { return RangedAudioParameter::getNumSteps(); }
  46. bool AudioProcessorValueTreeState::Parameter::isMetaParameter() const { return metaParameter; }
  47. bool AudioProcessorValueTreeState::Parameter::isAutomatable() const { return automatable; }
  48. bool AudioProcessorValueTreeState::Parameter::isDiscrete() const { return discrete; }
  49. bool AudioProcessorValueTreeState::Parameter::isBoolean() const { return boolean; }
  50. void AudioProcessorValueTreeState::Parameter::valueChanged (float newValue)
  51. {
  52. if (lastValue == newValue)
  53. return;
  54. lastValue = newValue;
  55. if (onValueChanged != nullptr)
  56. onValueChanged();
  57. }
  58. //==============================================================================
  59. class AudioProcessorValueTreeState::ParameterAdapter : private AudioProcessorParameter::Listener
  60. {
  61. private:
  62. using Listener = AudioProcessorValueTreeState::Listener;
  63. public:
  64. explicit ParameterAdapter (RangedAudioParameter& parameterIn)
  65. : parameter (parameterIn),
  66. // For legacy reasons, the unnormalised value should *not* be snapped on construction
  67. unnormalisedValue (getRange().convertFrom0to1 (parameter.getDefaultValue()))
  68. {
  69. parameter.addListener (this);
  70. if (auto* ptr = dynamic_cast<Parameter*> (&parameter))
  71. ptr->onValueChanged = [this] { parameterValueChanged ({}, {}); };
  72. }
  73. ~ParameterAdapter() override { parameter.removeListener (this); }
  74. void addListener (Listener* l) { listeners.add (l); }
  75. void removeListener (Listener* l) { listeners.remove (l); }
  76. RangedAudioParameter& getParameter() { return parameter; }
  77. const RangedAudioParameter& getParameter() const { return parameter; }
  78. const NormalisableRange<float>& getRange() const { return parameter.getNormalisableRange(); }
  79. float getDenormalisedDefaultValue() const { return denormalise (parameter.getDefaultValue()); }
  80. void setDenormalisedValue (float value)
  81. {
  82. if (value == unnormalisedValue)
  83. return;
  84. setNormalisedValue (normalise (value));
  85. }
  86. float getDenormalisedValueForText (const String& text) const
  87. {
  88. return denormalise (parameter.getValueForText (text));
  89. }
  90. String getTextForDenormalisedValue (float value) const
  91. {
  92. return parameter.getText (normalise (value), 0);
  93. }
  94. float getDenormalisedValue() const { return unnormalisedValue; }
  95. std::atomic<float>& getRawDenormalisedValue() { return unnormalisedValue; }
  96. bool flushToTree (const Identifier& key, UndoManager* um)
  97. {
  98. auto needsUpdateTestValue = true;
  99. if (! needsUpdate.compare_exchange_strong (needsUpdateTestValue, false))
  100. return false;
  101. if (auto valueProperty = tree.getPropertyPointer (key))
  102. {
  103. if ((float) *valueProperty != unnormalisedValue)
  104. {
  105. ScopedValueSetter<bool> svs (ignoreParameterChangedCallbacks, true);
  106. tree.setProperty (key, unnormalisedValue.load(), um);
  107. }
  108. }
  109. else
  110. {
  111. tree.setProperty (key, unnormalisedValue.load(), nullptr);
  112. }
  113. return true;
  114. }
  115. ValueTree tree;
  116. private:
  117. void parameterGestureChanged (int, bool) override {}
  118. void parameterValueChanged (int, float) override
  119. {
  120. const auto newValue = denormalise (parameter.getValue());
  121. if (unnormalisedValue == newValue && ! listenersNeedCalling)
  122. return;
  123. unnormalisedValue = newValue;
  124. listeners.call ([=](Listener& l) { l.parameterChanged (parameter.paramID, unnormalisedValue); });
  125. listenersNeedCalling = false;
  126. needsUpdate = true;
  127. }
  128. float denormalise (float normalised) const
  129. {
  130. return getParameter().convertFrom0to1 (normalised);
  131. }
  132. float normalise (float denormalised) const
  133. {
  134. return getParameter().convertTo0to1 (denormalised);
  135. }
  136. void setNormalisedValue (float value)
  137. {
  138. if (ignoreParameterChangedCallbacks)
  139. return;
  140. parameter.setValueNotifyingHost (value);
  141. }
  142. RangedAudioParameter& parameter;
  143. ListenerList<Listener> listeners;
  144. std::atomic<float> unnormalisedValue { 0.0f };
  145. std::atomic<bool> needsUpdate { true }, listenersNeedCalling { true };
  146. bool ignoreParameterChangedCallbacks { false };
  147. };
  148. //==============================================================================
  149. AudioProcessorValueTreeState::AudioProcessorValueTreeState (AudioProcessor& processorToConnectTo,
  150. UndoManager* undoManagerToUse,
  151. const Identifier& valueTreeType,
  152. ParameterLayout parameterLayout)
  153. : AudioProcessorValueTreeState (processorToConnectTo, undoManagerToUse)
  154. {
  155. struct PushBackVisitor : ParameterLayout::Visitor
  156. {
  157. explicit PushBackVisitor (AudioProcessorValueTreeState& stateIn)
  158. : state (&stateIn) {}
  159. void visit (std::unique_ptr<RangedAudioParameter> param) const override
  160. {
  161. if (param == nullptr)
  162. {
  163. jassertfalse;
  164. return;
  165. }
  166. state->addParameterAdapter (*param);
  167. state->processor.addParameter (param.release());
  168. }
  169. void visit (std::unique_ptr<AudioProcessorParameterGroup> group) const override
  170. {
  171. if (group == nullptr)
  172. {
  173. jassertfalse;
  174. return;
  175. }
  176. for (const auto param : group->getParameters (true))
  177. {
  178. if (const auto rangedParam = dynamic_cast<RangedAudioParameter*> (param))
  179. {
  180. state->addParameterAdapter (*rangedParam);
  181. }
  182. else
  183. {
  184. // If you hit this assertion then you are attempting to add a parameter that is
  185. // not derived from RangedAudioParameter to the AudioProcessorValueTreeState.
  186. jassertfalse;
  187. }
  188. }
  189. state->processor.addParameterGroup (move (group));
  190. }
  191. AudioProcessorValueTreeState* state;
  192. };
  193. for (auto& item : parameterLayout.parameters)
  194. item->accept (PushBackVisitor (*this));
  195. state = ValueTree (valueTreeType);
  196. }
  197. AudioProcessorValueTreeState::AudioProcessorValueTreeState (AudioProcessor& p, UndoManager* um)
  198. : processor (p), undoManager (um)
  199. {
  200. startTimerHz (10);
  201. state.addListener (this);
  202. }
  203. AudioProcessorValueTreeState::~AudioProcessorValueTreeState()
  204. {
  205. stopTimer();
  206. }
  207. //==============================================================================
  208. RangedAudioParameter* AudioProcessorValueTreeState::createAndAddParameter (const String& paramID,
  209. const String& paramName,
  210. const String& labelText,
  211. NormalisableRange<float> range,
  212. float defaultVal,
  213. std::function<String(float)> valueToTextFunction,
  214. std::function<float(const String&)> textToValueFunction,
  215. bool isMetaParameter,
  216. bool isAutomatableParameter,
  217. bool isDiscreteParameter,
  218. AudioProcessorParameter::Category category,
  219. bool isBooleanParameter)
  220. {
  221. return createAndAddParameter (std::make_unique<Parameter> (paramID,
  222. paramName,
  223. labelText,
  224. range,
  225. defaultVal,
  226. std::move (valueToTextFunction),
  227. std::move (textToValueFunction),
  228. isMetaParameter,
  229. isAutomatableParameter,
  230. isDiscreteParameter,
  231. category,
  232. isBooleanParameter));
  233. }
  234. RangedAudioParameter* AudioProcessorValueTreeState::createAndAddParameter (std::unique_ptr<RangedAudioParameter> param)
  235. {
  236. if (param == nullptr)
  237. return nullptr;
  238. // All parameters must be created before giving this manager a ValueTree state!
  239. jassert (! state.isValid());
  240. if (getParameter (param->paramID) != nullptr)
  241. return nullptr;
  242. addParameterAdapter (*param);
  243. processor.addParameter (param.get());
  244. return param.release();
  245. }
  246. //==============================================================================
  247. void AudioProcessorValueTreeState::addParameterAdapter (RangedAudioParameter& param)
  248. {
  249. adapterTable.emplace (param.paramID, std::make_unique<ParameterAdapter> (param));
  250. }
  251. AudioProcessorValueTreeState::ParameterAdapter* AudioProcessorValueTreeState::getParameterAdapter (StringRef paramID) const
  252. {
  253. auto it = adapterTable.find (paramID);
  254. return it == adapterTable.end() ? nullptr : it->second.get();
  255. }
  256. void AudioProcessorValueTreeState::addParameterListener (StringRef paramID, Listener* listener)
  257. {
  258. if (auto* p = getParameterAdapter (paramID))
  259. p->addListener (listener);
  260. }
  261. void AudioProcessorValueTreeState::removeParameterListener (StringRef paramID, Listener* listener)
  262. {
  263. if (auto* p = getParameterAdapter (paramID))
  264. p->removeListener (listener);
  265. }
  266. Value AudioProcessorValueTreeState::getParameterAsValue (StringRef paramID) const
  267. {
  268. if (auto* adapter = getParameterAdapter (paramID))
  269. if (adapter->tree.isValid())
  270. return adapter->tree.getPropertyAsValue (valuePropertyID, undoManager);
  271. return {};
  272. }
  273. NormalisableRange<float> AudioProcessorValueTreeState::getParameterRange (StringRef paramID) const noexcept
  274. {
  275. if (auto* p = getParameterAdapter (paramID))
  276. return p->getRange();
  277. return {};
  278. }
  279. RangedAudioParameter* AudioProcessorValueTreeState::getParameter (StringRef paramID) const noexcept
  280. {
  281. if (auto adapter = getParameterAdapter (paramID))
  282. return &adapter->getParameter();
  283. return nullptr;
  284. }
  285. std::atomic<float>* AudioProcessorValueTreeState::getRawParameterValue (StringRef paramID) const noexcept
  286. {
  287. if (auto* p = getParameterAdapter (paramID))
  288. return &p->getRawDenormalisedValue();
  289. return nullptr;
  290. }
  291. ValueTree AudioProcessorValueTreeState::copyState()
  292. {
  293. ScopedLock lock (valueTreeChanging);
  294. flushParameterValuesToValueTree();
  295. return state.createCopy();
  296. }
  297. void AudioProcessorValueTreeState::replaceState (const ValueTree& newState)
  298. {
  299. ScopedLock lock (valueTreeChanging);
  300. state = newState;
  301. if (undoManager != nullptr)
  302. undoManager->clearUndoHistory();
  303. }
  304. void AudioProcessorValueTreeState::setNewState (ValueTree vt)
  305. {
  306. jassert (vt.getParent() == state);
  307. if (auto* p = getParameterAdapter (vt.getProperty (idPropertyID).toString()))
  308. {
  309. p->tree = vt;
  310. p->setDenormalisedValue (p->tree.getProperty (valuePropertyID, p->getDenormalisedDefaultValue()));
  311. }
  312. }
  313. void AudioProcessorValueTreeState::updateParameterConnectionsToChildTrees()
  314. {
  315. ScopedLock lock (valueTreeChanging);
  316. for (auto& p : adapterTable)
  317. p.second->tree = ValueTree();
  318. for (const auto& child : state)
  319. setNewState (child);
  320. for (auto& p : adapterTable)
  321. {
  322. auto& adapter = *p.second;
  323. if (! adapter.tree.isValid())
  324. {
  325. adapter.tree = ValueTree (valueType);
  326. adapter.tree.setProperty (idPropertyID, adapter.getParameter().paramID, nullptr);
  327. state.appendChild (adapter.tree, nullptr);
  328. }
  329. }
  330. flushParameterValuesToValueTree();
  331. }
  332. void AudioProcessorValueTreeState::valueTreePropertyChanged (ValueTree& tree, const Identifier&)
  333. {
  334. if (tree.hasType (valueType) && tree.getParent() == state)
  335. setNewState (tree);
  336. }
  337. void AudioProcessorValueTreeState::valueTreeChildAdded (ValueTree& parent, ValueTree& tree)
  338. {
  339. if (parent == state && tree.hasType (valueType))
  340. setNewState (tree);
  341. }
  342. void AudioProcessorValueTreeState::valueTreeRedirected (ValueTree& v)
  343. {
  344. if (v == state)
  345. updateParameterConnectionsToChildTrees();
  346. }
  347. bool AudioProcessorValueTreeState::flushParameterValuesToValueTree()
  348. {
  349. ScopedLock lock (valueTreeChanging);
  350. bool anyUpdated = false;
  351. for (auto& p : adapterTable)
  352. anyUpdated |= p.second->flushToTree (valuePropertyID, undoManager);
  353. return anyUpdated;
  354. }
  355. void AudioProcessorValueTreeState::timerCallback()
  356. {
  357. auto anythingUpdated = flushParameterValuesToValueTree();
  358. startTimer (anythingUpdated ? 1000 / 50
  359. : jlimit (50, 500, getTimerInterval() + 20));
  360. }
  361. //==============================================================================
  362. template <typename Attachment, typename Control>
  363. std::unique_ptr<Attachment> makeAttachment (const AudioProcessorValueTreeState& stateToUse,
  364. const String& parameterID,
  365. Control& control)
  366. {
  367. if (auto* parameter = stateToUse.getParameter (parameterID))
  368. return std::make_unique<Attachment> (*parameter, control, stateToUse.undoManager);
  369. jassertfalse;
  370. return nullptr;
  371. }
  372. AudioProcessorValueTreeState::SliderAttachment::SliderAttachment (AudioProcessorValueTreeState& stateToUse,
  373. const String& parameterID,
  374. Slider& slider)
  375. : attachment (makeAttachment<SliderParameterAttachment> (stateToUse, parameterID, slider))
  376. {
  377. }
  378. AudioProcessorValueTreeState::ComboBoxAttachment::ComboBoxAttachment (AudioProcessorValueTreeState& stateToUse,
  379. const String& parameterID,
  380. ComboBox& combo)
  381. : attachment (makeAttachment<ComboBoxParameterAttachment> (stateToUse, parameterID, combo))
  382. {
  383. }
  384. AudioProcessorValueTreeState::ButtonAttachment::ButtonAttachment (AudioProcessorValueTreeState& stateToUse,
  385. const String& parameterID,
  386. Button& button)
  387. : attachment (makeAttachment<ButtonParameterAttachment> (stateToUse, parameterID, button))
  388. {
  389. }
  390. //==============================================================================
  391. //==============================================================================
  392. #if JUCE_UNIT_TESTS
  393. struct ParameterAdapterTests : public UnitTest
  394. {
  395. ParameterAdapterTests()
  396. : UnitTest ("Parameter Adapter", UnitTestCategories::audioProcessorParameters)
  397. {}
  398. void runTest() override
  399. {
  400. beginTest ("The default value is returned correctly");
  401. {
  402. const auto test = [&] (NormalisableRange<float> range, float value)
  403. {
  404. AudioParameterFloat param ({}, {}, range, value, {});
  405. AudioProcessorValueTreeState::ParameterAdapter adapter (param);
  406. expectEquals (adapter.getDenormalisedDefaultValue(), value);
  407. };
  408. test ({ -100, 100 }, 0);
  409. test ({ -2.5, 12.5 }, 10);
  410. }
  411. beginTest ("Denormalised parameter values can be retrieved");
  412. {
  413. const auto test = [&](NormalisableRange<float> range, float value)
  414. {
  415. AudioParameterFloat param ({}, {}, range, {}, {});
  416. AudioProcessorValueTreeState::ParameterAdapter adapter (param);
  417. adapter.setDenormalisedValue (value);
  418. expectEquals (adapter.getDenormalisedValue(), value);
  419. expectEquals (adapter.getRawDenormalisedValue().load(), value);
  420. };
  421. test ({ -20, -10 }, -15);
  422. test ({ 0, 7.5 }, 2.5);
  423. }
  424. beginTest ("Floats can be converted to text");
  425. {
  426. const auto test = [&](NormalisableRange<float> range, float value, String expected)
  427. {
  428. AudioParameterFloat param ({}, {}, range, {}, {});
  429. AudioProcessorValueTreeState::ParameterAdapter adapter (param);
  430. expectEquals (adapter.getTextForDenormalisedValue (value), expected);
  431. };
  432. test ({ -100, 100 }, 0, "0.0000000");
  433. test ({ -2.5, 12.5 }, 10, "10.0000000");
  434. test ({ -20, -10 }, -15, "-15.0000000");
  435. test ({ 0, 7.5 }, 2.5, "2.5000000");
  436. }
  437. beginTest ("Text can be converted to floats");
  438. {
  439. const auto test = [&](NormalisableRange<float> range, String text, float expected)
  440. {
  441. AudioParameterFloat param ({}, {}, range, {}, {});
  442. AudioProcessorValueTreeState::ParameterAdapter adapter (param);
  443. expectEquals (adapter.getDenormalisedValueForText (text), expected);
  444. };
  445. test ({ -100, 100 }, "0.0", 0);
  446. test ({ -2.5, 12.5 }, "10.0", 10);
  447. test ({ -20, -10 }, "-15.0", -15);
  448. test ({ 0, 7.5 }, "2.5", 2.5);
  449. }
  450. }
  451. };
  452. static ParameterAdapterTests parameterAdapterTests;
  453. namespace
  454. {
  455. template <typename ValueType>
  456. inline bool operator== (const NormalisableRange<ValueType>& a,
  457. const NormalisableRange<ValueType>& b)
  458. {
  459. return std::tie (a.start, a.end, a.interval, a.skew, a.symmetricSkew)
  460. == std::tie (b.start, b.end, b.interval, b.skew, b.symmetricSkew);
  461. }
  462. template <typename ValueType>
  463. inline bool operator!= (const NormalisableRange<ValueType>& a,
  464. const NormalisableRange<ValueType>& b)
  465. {
  466. return ! (a == b);
  467. }
  468. } // namespace
  469. class AudioProcessorValueTreeStateTests : public UnitTest
  470. {
  471. private:
  472. using Parameter = AudioProcessorValueTreeState::Parameter;
  473. using ParameterGroup = AudioProcessorParameterGroup;
  474. using ParameterLayout = AudioProcessorValueTreeState::ParameterLayout;
  475. class TestAudioProcessor : public AudioProcessor
  476. {
  477. public:
  478. TestAudioProcessor() = default;
  479. explicit TestAudioProcessor (ParameterLayout layout)
  480. : state (*this, nullptr, "state", std::move (layout)) {}
  481. const String getName() const override { return {}; }
  482. void prepareToPlay (double, int) override {}
  483. void releaseResources() override {}
  484. void processBlock (AudioBuffer<float>&, MidiBuffer&) override {}
  485. using AudioProcessor::processBlock;
  486. double getTailLengthSeconds() const override { return {}; }
  487. bool acceptsMidi() const override { return {}; }
  488. bool producesMidi() const override { return {}; }
  489. AudioProcessorEditor* createEditor() override { return {}; }
  490. bool hasEditor() const override { return {}; }
  491. int getNumPrograms() override { return 1; }
  492. int getCurrentProgram() override { return {}; }
  493. void setCurrentProgram (int) override {}
  494. const String getProgramName (int) override { return {}; }
  495. void changeProgramName (int, const String&) override {}
  496. void getStateInformation (MemoryBlock&) override {}
  497. void setStateInformation (const void*, int) override {}
  498. AudioProcessorValueTreeState state { *this, nullptr };
  499. };
  500. struct Listener final : public AudioProcessorValueTreeState::Listener
  501. {
  502. void parameterChanged (const String& idIn, float valueIn) override
  503. {
  504. id = idIn;
  505. value = valueIn;
  506. }
  507. String id;
  508. float value{};
  509. };
  510. public:
  511. AudioProcessorValueTreeStateTests()
  512. : UnitTest ("Audio Processor Value Tree State", UnitTestCategories::audioProcessorParameters)
  513. {}
  514. void runTest() override
  515. {
  516. ScopedJuceInitialiser_GUI scopedJuceInitialiser_gui;
  517. beginTest ("After calling createAndAddParameter, the number of parameters increases by one");
  518. {
  519. TestAudioProcessor proc;
  520. proc.state.createAndAddParameter (std::make_unique<Parameter> (String(), String(), String(), NormalisableRange<float>(),
  521. 0.0f, nullptr, nullptr));
  522. expectEquals (proc.getParameters().size(), 1);
  523. }
  524. beginTest ("After creating a normal named parameter, we can later retrieve that parameter");
  525. {
  526. TestAudioProcessor proc;
  527. const auto key = "id";
  528. const auto param = proc.state.createAndAddParameter (std::make_unique<Parameter> (key, String(), String(), NormalisableRange<float>(),
  529. 0.0f, nullptr, nullptr));
  530. expect (proc.state.getParameter (key) == param);
  531. }
  532. beginTest ("After construction, the value tree has the expected format");
  533. {
  534. TestAudioProcessor proc ({
  535. std::make_unique<AudioProcessorParameterGroup> ("", "", "",
  536. std::make_unique<AudioParameterBool> ("a", "", false),
  537. std::make_unique<AudioParameterFloat> ("b", "", NormalisableRange<float>{}, 0.0f)),
  538. std::make_unique<AudioProcessorParameterGroup> ("", "", "",
  539. std::make_unique<AudioParameterInt> ("c", "", 0, 1, 0),
  540. std::make_unique<AudioParameterChoice> ("d", "", StringArray { "foo", "bar" }, 0)) });
  541. const auto valueTree = proc.state.copyState();
  542. expectEquals (valueTree.getNumChildren(), 4);
  543. for (auto child : valueTree)
  544. {
  545. expect (child.hasType ("PARAM"));
  546. expect (child.hasProperty ("id"));
  547. expect (child.hasProperty ("value"));
  548. }
  549. }
  550. beginTest ("Meta parameters can be created");
  551. {
  552. TestAudioProcessor proc;
  553. const auto key = "id";
  554. const auto param = proc.state.createAndAddParameter (std::make_unique<Parameter> (key, String(), String(), NormalisableRange<float>(),
  555. 0.0f, nullptr, nullptr, true));
  556. expect (param->isMetaParameter());
  557. }
  558. beginTest ("Automatable parameters can be created");
  559. {
  560. TestAudioProcessor proc;
  561. const auto key = "id";
  562. const auto param = proc.state.createAndAddParameter (std::make_unique<Parameter> (key, String(), String(), NormalisableRange<float>(),
  563. 0.0f, nullptr, nullptr, false, true));
  564. expect (param->isAutomatable());
  565. }
  566. beginTest ("Discrete parameters can be created");
  567. {
  568. TestAudioProcessor proc;
  569. const auto key = "id";
  570. const auto param = proc.state.createAndAddParameter (std::make_unique<Parameter> (key, String(), String(), NormalisableRange<float>(),
  571. 0.0f, nullptr, nullptr, false, false, true));
  572. expect (param->isDiscrete());
  573. }
  574. beginTest ("Custom category parameters can be created");
  575. {
  576. TestAudioProcessor proc;
  577. const auto key = "id";
  578. const auto param = proc.state.createAndAddParameter (std::make_unique<Parameter> (key, String(), String(), NormalisableRange<float>(),
  579. 0.0f, nullptr, nullptr, false, false, false,
  580. AudioProcessorParameter::Category::inputMeter));
  581. expect (param->category == AudioProcessorParameter::Category::inputMeter);
  582. }
  583. beginTest ("Boolean parameters can be created");
  584. {
  585. TestAudioProcessor proc;
  586. const auto key = "id";
  587. const auto param = proc.state.createAndAddParameter (std::make_unique<Parameter> (key, String(), String(), NormalisableRange<float>(),
  588. 0.0f, nullptr, nullptr, false, false, false,
  589. AudioProcessorParameter::Category::genericParameter, true));
  590. expect (param->isBoolean());
  591. }
  592. beginTest ("After creating a custom named parameter, we can later retrieve that parameter");
  593. {
  594. const auto key = "id";
  595. auto param = std::make_unique<AudioParameterBool> (key, "", false);
  596. const auto paramPtr = param.get();
  597. TestAudioProcessor proc (std::move (param));
  598. expect (proc.state.getParameter (key) == paramPtr);
  599. }
  600. beginTest ("After adding a normal parameter that already exists, the AudioProcessor parameters are unchanged");
  601. {
  602. TestAudioProcessor proc;
  603. const auto key = "id";
  604. const auto param = proc.state.createAndAddParameter (std::make_unique<Parameter> (key, String(), String(), NormalisableRange<float>(),
  605. 0.0f, nullptr, nullptr));
  606. proc.state.createAndAddParameter (std::make_unique<Parameter> (key, String(), String(), NormalisableRange<float>(),
  607. 0.0f, nullptr, nullptr));
  608. expectEquals (proc.getParameters().size(), 1);
  609. expect (proc.getParameters().getFirst() == param);
  610. }
  611. beginTest ("After setting a parameter value, that value is reflected in the state");
  612. {
  613. TestAudioProcessor proc;
  614. const auto key = "id";
  615. const auto param = proc.state.createAndAddParameter (std::make_unique<Parameter> (key, String(), String(), NormalisableRange<float>(),
  616. 0.0f, nullptr, nullptr));
  617. const auto value = 0.5f;
  618. param->setValueNotifyingHost (value);
  619. expectEquals (proc.state.getRawParameterValue (key)->load(), value);
  620. }
  621. beginTest ("After adding an APVTS::Parameter, its value is the default value");
  622. {
  623. TestAudioProcessor proc;
  624. const auto key = "id";
  625. const auto value = 5.0f;
  626. proc.state.createAndAddParameter (std::make_unique<Parameter> (
  627. key,
  628. String(),
  629. String(),
  630. NormalisableRange<float> (0.0f, 100.0f, 10.0f),
  631. value,
  632. nullptr,
  633. nullptr));
  634. expectEquals (proc.state.getRawParameterValue (key)->load(), value);
  635. }
  636. beginTest ("Listeners receive notifications when parameters change");
  637. {
  638. Listener listener;
  639. TestAudioProcessor proc;
  640. const auto key = "id";
  641. const auto param = proc.state.createAndAddParameter (std::make_unique<Parameter> (key, String(), String(), NormalisableRange<float>(),
  642. 0.0f, nullptr, nullptr));
  643. proc.state.addParameterListener (key, &listener);
  644. const auto value = 0.5f;
  645. param->setValueNotifyingHost (value);
  646. expectEquals (listener.id, String { key });
  647. expectEquals (listener.value, value);
  648. }
  649. beginTest ("Bool parameters have a range of 0-1");
  650. {
  651. const auto key = "id";
  652. TestAudioProcessor proc (std::make_unique<AudioParameterBool> (key, "", false));
  653. expect (proc.state.getParameterRange (key) == NormalisableRange<float> (0.0f, 1.0f, 1.0f));
  654. }
  655. beginTest ("Float parameters retain their specified range");
  656. {
  657. const auto key = "id";
  658. const auto range = NormalisableRange<float> { -100, 100, 0.7f, 0.2f, true };
  659. TestAudioProcessor proc (std::make_unique<AudioParameterFloat> (key, "", range, 0.0f));
  660. expect (proc.state.getParameterRange (key) == range);
  661. }
  662. beginTest ("Int parameters retain their specified range");
  663. {
  664. const auto key = "id";
  665. const auto min = -27;
  666. const auto max = 53;
  667. TestAudioProcessor proc (std::make_unique<AudioParameterInt> (key, "", min, max, 0));
  668. expect (proc.state.getParameterRange (key) == NormalisableRange<float> (float (min), float (max), 1.0f));
  669. }
  670. beginTest ("Choice parameters retain their specified range");
  671. {
  672. const auto key = "id";
  673. const auto choices = StringArray { "", "", "" };
  674. TestAudioProcessor proc (std::make_unique<AudioParameterChoice> (key, "", choices, 0));
  675. expect (proc.state.getParameterRange (key) == NormalisableRange<float> (0.0f, (float) (choices.size() - 1), 1.0f));
  676. expect (proc.state.getParameter (key)->getNumSteps() == choices.size());
  677. }
  678. beginTest ("When the parameter value is changed, normal parameter values are updated");
  679. {
  680. TestAudioProcessor proc;
  681. const auto key = "id";
  682. const auto initialValue = 0.2f;
  683. auto param = proc.state.createAndAddParameter (std::make_unique<Parameter> (key, String(), String(), NormalisableRange<float>(),
  684. initialValue, nullptr, nullptr));
  685. proc.state.state = ValueTree { "state" };
  686. auto value = proc.state.getParameterAsValue (key);
  687. expectEquals (float (value.getValue()), initialValue);
  688. const auto newValue = 0.75f;
  689. value = newValue;
  690. expectEquals (param->getValue(), newValue);
  691. expectEquals (proc.state.getRawParameterValue (key)->load(), newValue);
  692. }
  693. beginTest ("When the parameter value is changed, custom parameter values are updated");
  694. {
  695. const auto key = "id";
  696. const auto choices = StringArray ("foo", "bar", "baz");
  697. auto param = std::make_unique<AudioParameterChoice> (key, "", choices, 0);
  698. const auto paramPtr = param.get();
  699. TestAudioProcessor proc (std::move (param));
  700. const auto newValue = 2.0f;
  701. auto value = proc.state.getParameterAsValue (key);
  702. value = newValue;
  703. expectEquals (paramPtr->getCurrentChoiceName(), choices[int (newValue)]);
  704. expectEquals (proc.state.getRawParameterValue (key)->load(), newValue);
  705. }
  706. beginTest ("When the parameter value is changed, listeners are notified");
  707. {
  708. Listener listener;
  709. TestAudioProcessor proc;
  710. const auto key = "id";
  711. proc.state.createAndAddParameter (std::make_unique<Parameter> (key, String(), String(), NormalisableRange<float>(),
  712. 0.0f, nullptr, nullptr));
  713. proc.state.addParameterListener (key, &listener);
  714. proc.state.state = ValueTree { "state" };
  715. const auto newValue = 0.75f;
  716. proc.state.getParameterAsValue (key) = newValue;
  717. expectEquals (listener.value, newValue);
  718. expectEquals (listener.id, String { key });
  719. }
  720. beginTest ("When the parameter value is changed, listeners are notified");
  721. {
  722. const auto key = "id";
  723. const auto choices = StringArray { "foo", "bar", "baz" };
  724. Listener listener;
  725. TestAudioProcessor proc (std::make_unique<AudioParameterChoice> (key, "", choices, 0));
  726. proc.state.addParameterListener (key, &listener);
  727. const auto newValue = 2.0f;
  728. proc.state.getParameterAsValue (key) = newValue;
  729. expectEquals (listener.value, newValue);
  730. expectEquals (listener.id, String (key));
  731. }
  732. }
  733. };
  734. static AudioProcessorValueTreeStateTests audioProcessorValueTreeStateTests;
  735. #endif
  736. } // namespace juce