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.3KB

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