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.

130 lines
5.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - 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 the technical preview this file cannot be licensed commercially.
  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. AudioParameterChoice::AudioParameterChoice (const ParameterID& idToUse,
  16. const String& nameToUse,
  17. const StringArray& c,
  18. int def,
  19. const AudioParameterChoiceAttributes& attributes)
  20. : RangedAudioParameter (idToUse, nameToUse, attributes.getAudioProcessorParameterWithIDAttributes()),
  21. choices (c),
  22. range ([this]
  23. {
  24. NormalisableRange<float> rangeWithInterval { 0.0f, (float) choices.size() - 1.0f,
  25. [] (float, float end, float v) { return jlimit (0.0f, end, v * end); },
  26. [] (float, float end, float v) { return jlimit (0.0f, 1.0f, v / end); },
  27. [] (float start, float end, float v) { return (float) roundToInt (juce::jlimit (start, end, v)); } };
  28. rangeWithInterval.interval = 1.0f;
  29. return rangeWithInterval;
  30. }()),
  31. value ((float) def),
  32. defaultValue (convertTo0to1 ((float) def)),
  33. stringFromIndexFunction (attributes.getStringFromValueFunction() != nullptr
  34. ? attributes.getStringFromValueFunction()
  35. : [this] (int index, int) { return choices [index]; }),
  36. indexFromStringFunction (attributes.getValueFromStringFunction() != nullptr
  37. ? attributes.getValueFromStringFunction()
  38. : [this] (const String& text) { return choices.indexOf (text); })
  39. {
  40. jassert (choices.size() > 1); // you must supply an actual set of items to choose from!
  41. }
  42. AudioParameterChoice::~AudioParameterChoice()
  43. {
  44. #if __cpp_lib_atomic_is_always_lock_free
  45. static_assert (std::atomic<float>::is_always_lock_free,
  46. "AudioParameterChoice requires a lock-free std::atomic<float>");
  47. #endif
  48. }
  49. float AudioParameterChoice::getValue() const { return convertTo0to1 (value); }
  50. void AudioParameterChoice::setValue (float newValue) { value = convertFrom0to1 (newValue); valueChanged (getIndex()); }
  51. float AudioParameterChoice::getDefaultValue() const { return defaultValue; }
  52. int AudioParameterChoice::getNumSteps() const { return choices.size(); }
  53. bool AudioParameterChoice::isDiscrete() const { return true; }
  54. float AudioParameterChoice::getValueForText (const String& text) const { return convertTo0to1 ((float) indexFromStringFunction (text)); }
  55. String AudioParameterChoice::getText (float v, int length) const { return stringFromIndexFunction ((int) convertFrom0to1 (v), length); }
  56. void AudioParameterChoice::valueChanged (int) {}
  57. AudioParameterChoice& AudioParameterChoice::operator= (int newValue)
  58. {
  59. if (getIndex() != newValue)
  60. setValueNotifyingHost (convertTo0to1 ((float) newValue));
  61. return *this;
  62. }
  63. //==============================================================================
  64. //==============================================================================
  65. #if JUCE_UNIT_TESTS
  66. struct AudioParameterChoiceTests : public UnitTest
  67. {
  68. AudioParameterChoiceTests()
  69. : UnitTest ("AudioParameterChoice", UnitTestCategories::audioProcessorParameters)
  70. {}
  71. void runTest() override
  72. {
  73. beginTest ("Three options switches at the correct points");
  74. {
  75. AudioParameterChoice choice ({}, {}, { "a", "b", "c" }, {});
  76. choice.setValueNotifyingHost (0.0f);
  77. expectEquals (choice.getIndex(), 0);
  78. choice.setValueNotifyingHost (0.2f);
  79. expectEquals (choice.getIndex(), 0);
  80. choice.setValueNotifyingHost (0.3f);
  81. expectEquals (choice.getIndex(), 1);
  82. choice.setValueNotifyingHost (0.7f);
  83. expectEquals (choice.getIndex(), 1);
  84. choice.setValueNotifyingHost (0.8f);
  85. expectEquals (choice.getIndex(), 2);
  86. choice.setValueNotifyingHost (1.0f);
  87. expectEquals (choice.getIndex(), 2);
  88. }
  89. beginTest ("Out-of-bounds input");
  90. {
  91. AudioParameterChoice choiceParam ({}, {}, { "a", "b", "c" }, {});
  92. choiceParam.setValueNotifyingHost (-0.5f);
  93. expectEquals (choiceParam.getIndex(), 0);
  94. choiceParam.setValueNotifyingHost (1.5f);
  95. expectEquals (choiceParam.getIndex(), 2);
  96. }
  97. }
  98. };
  99. static AudioParameterChoiceTests audioParameterChoiceTests;
  100. #endif
  101. } // namespace juce