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.

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