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

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