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.

143 lines
5.6KB

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