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

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