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_AudioParameterInt.cpp 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. AudioParameterInt::AudioParameterInt (const ParameterID& idToUse, const String& nameToUse,
  21. int minValue, int maxValue, int def,
  22. const AudioParameterIntAttributes& attributes)
  23. : RangedAudioParameter (idToUse, nameToUse, attributes.getAudioProcessorParameterWithIDAttributes()),
  24. range ([minValue, maxValue]
  25. {
  26. NormalisableRange<float> rangeWithInterval { (float) minValue, (float) maxValue,
  27. [] (float start, float end, float v) { return jlimit (start, end, v * (end - start) + start); },
  28. [] (float start, float end, float v) { return jlimit (0.0f, 1.0f, (v - start) / (end - start)); },
  29. [] (float start, float end, float v) { return (float) roundToInt (juce::jlimit (start, end, v)); } };
  30. rangeWithInterval.interval = 1.0f;
  31. return rangeWithInterval;
  32. }()),
  33. value ((float) def),
  34. defaultValue (convertTo0to1 ((float) def)),
  35. stringFromIntFunction (attributes.getStringFromValueFunction() != nullptr
  36. ? attributes.getStringFromValueFunction()
  37. : [] (int v, int) { return String (v); }),
  38. intFromStringFunction (attributes.getValueFromStringFunction() != nullptr
  39. ? attributes.getValueFromStringFunction()
  40. : [] (const String& text) { return text.getIntValue(); })
  41. {
  42. jassert (minValue < maxValue); // must have a non-zero range of values!
  43. }
  44. AudioParameterInt::~AudioParameterInt()
  45. {
  46. #if __cpp_lib_atomic_is_always_lock_free
  47. static_assert (std::atomic<float>::is_always_lock_free,
  48. "AudioParameterInt requires a lock-free std::atomic<float>");
  49. #endif
  50. }
  51. float AudioParameterInt::getValue() const { return convertTo0to1 (value); }
  52. void AudioParameterInt::setValue (float newValue) { value = convertFrom0to1 (newValue); valueChanged (get()); }
  53. float AudioParameterInt::getDefaultValue() const { return defaultValue; }
  54. int AudioParameterInt::getNumSteps() const { return ((int) getNormalisableRange().getRange().getLength()) + 1; }
  55. float AudioParameterInt::getValueForText (const String& text) const { return convertTo0to1 ((float) intFromStringFunction (text)); }
  56. String AudioParameterInt::getText (float v, int length) const { return stringFromIntFunction ((int) convertFrom0to1 (v), length); }
  57. void AudioParameterInt::valueChanged (int) {}
  58. AudioParameterInt& AudioParameterInt::operator= (int newValue)
  59. {
  60. if (get() != newValue)
  61. setValueNotifyingHost (convertTo0to1 ((float) newValue));
  62. return *this;
  63. }
  64. //==============================================================================
  65. //==============================================================================
  66. #if JUCE_UNIT_TESTS
  67. struct AudioParameterIntTests : public UnitTest
  68. {
  69. AudioParameterIntTests()
  70. : UnitTest ("AudioParameterInt", UnitTestCategories::audioProcessorParameters)
  71. {}
  72. void runTest() override
  73. {
  74. beginTest ("Three options switches at the correct points");
  75. {
  76. AudioParameterInt intParam ({}, {}, 1, 3, 1);
  77. intParam.setValueNotifyingHost (0.0f);
  78. expectEquals (intParam.get(), 1);
  79. intParam.setValueNotifyingHost (0.2f);
  80. expectEquals (intParam.get(), 1);
  81. intParam.setValueNotifyingHost (0.3f);
  82. expectEquals (intParam.get(), 2);
  83. intParam.setValueNotifyingHost (0.7f);
  84. expectEquals (intParam.get(), 2);
  85. intParam.setValueNotifyingHost (0.8f);
  86. expectEquals (intParam.get(), 3);
  87. intParam.setValueNotifyingHost (1.0f);
  88. expectEquals (intParam.get(), 3);
  89. }
  90. beginTest ("Out-of-bounds input");
  91. {
  92. AudioParameterInt intParam ({}, {}, -1, 2, 0);
  93. intParam.setValueNotifyingHost (-0.5f);
  94. expectEquals (intParam.get(), -1);
  95. intParam.setValueNotifyingHost (1.5f);
  96. expectEquals (intParam.get(), 2);
  97. intParam = -5;
  98. expectEquals (intParam.get(), -1);
  99. intParam = 5;
  100. expectEquals (intParam.get(), 2);
  101. }
  102. }
  103. };
  104. static AudioParameterIntTests audioParameterIntTests;
  105. #endif
  106. } // namespace juce