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.

juce_AudioParameterChoice.cpp 5.5KB

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