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.

138 lines
5.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. AudioParameterChoice::AudioParameterChoice (const String& idToUse, const String& nameToUse,
  22. const StringArray& c, int def, const String& labelToUse,
  23. std::function<String(int, int)> stringFromIndex,
  24. std::function<int(const String&)> indexFromString)
  25. : RangedAudioParameter (idToUse, nameToUse, labelToUse), choices (c),
  26. range ([this]
  27. {
  28. NormalisableRange<float> rangeWithInterval { 0.0f, choices.size() - 1.0f,
  29. [](float, float end, float v) { return jlimit (0.0f, end, v * end); },
  30. [](float, float end, float v) { return jlimit (0.0f, 1.0f, v / end); },
  31. [](float start, float end, float v) { return (float) roundToInt (juce::jlimit (start, end, v)); } };
  32. rangeWithInterval.interval = 1.0f;
  33. return rangeWithInterval;
  34. }()),
  35. value ((float) def),
  36. defaultValue (convertTo0to1 ((float) def)),
  37. stringFromIndexFunction (stringFromIndex),
  38. indexFromStringFunction (indexFromString)
  39. {
  40. jassert (choices.size() > 1); // you must supply an actual set of items to choose from!
  41. if (stringFromIndexFunction == nullptr)
  42. stringFromIndexFunction = [this] (int index, int) { return choices [index]; };
  43. if (indexFromStringFunction == nullptr)
  44. indexFromStringFunction = [this] (const String& text) { return choices.indexOf (text); };
  45. }
  46. AudioParameterChoice::~AudioParameterChoice()
  47. {
  48. #if __cpp_lib_atomic_is_always_lock_free
  49. static_assert (std::atomic<float>::is_always_lock_free,
  50. "AudioParameterChoice requires a lock-free std::atomic<float>");
  51. #endif
  52. }
  53. float AudioParameterChoice::getValue() const { return convertTo0to1 (value); }
  54. void AudioParameterChoice::setValue (float newValue) { value = convertFrom0to1 (newValue); valueChanged (getIndex()); }
  55. float AudioParameterChoice::getDefaultValue() const { return defaultValue; }
  56. int AudioParameterChoice::getNumSteps() const { return choices.size(); }
  57. bool AudioParameterChoice::isDiscrete() const { return true; }
  58. float AudioParameterChoice::getValueForText (const String& text) const { return convertTo0to1 ((float) indexFromStringFunction (text)); }
  59. String AudioParameterChoice::getText (float v, int length) const { return stringFromIndexFunction ((int) convertFrom0to1 (v), length); }
  60. void AudioParameterChoice::valueChanged (int) {}
  61. AudioParameterChoice& AudioParameterChoice::operator= (int newValue)
  62. {
  63. if (getIndex() != newValue)
  64. setValueNotifyingHost (convertTo0to1 ((float) newValue));
  65. return *this;
  66. }
  67. //==============================================================================
  68. //==============================================================================
  69. #if JUCE_UNIT_TESTS
  70. struct AudioParameterChoiceTests : public UnitTest
  71. {
  72. AudioParameterChoiceTests()
  73. : UnitTest ("AudioParameterChoice", UnitTestCategories::audioProcessorParameters)
  74. {}
  75. void runTest() override
  76. {
  77. beginTest ("Three options switches at the correct points");
  78. {
  79. AudioParameterChoice choice ({}, {}, { "a", "b", "c" }, {});
  80. choice.setValueNotifyingHost (0.0f);
  81. expectEquals (choice.getIndex(), 0);
  82. choice.setValueNotifyingHost (0.2f);
  83. expectEquals (choice.getIndex(), 0);
  84. choice.setValueNotifyingHost (0.3f);
  85. expectEquals (choice.getIndex(), 1);
  86. choice.setValueNotifyingHost (0.7f);
  87. expectEquals (choice.getIndex(), 1);
  88. choice.setValueNotifyingHost (0.8f);
  89. expectEquals (choice.getIndex(), 2);
  90. choice.setValueNotifyingHost (1.0f);
  91. expectEquals (choice.getIndex(), 2);
  92. }
  93. beginTest ("Out-of-bounds input");
  94. {
  95. AudioParameterChoice choiceParam ({}, {}, { "a", "b", "c" }, {});
  96. choiceParam.setValueNotifyingHost (-0.5f);
  97. expectEquals (choiceParam.getIndex(), 0);
  98. choiceParam.setValueNotifyingHost (1.5f);
  99. expectEquals (choiceParam.getIndex(), 2);
  100. }
  101. }
  102. };
  103. static AudioParameterChoiceTests audioParameterChoiceTests;
  104. #endif
  105. } // namespace juce