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.

131 lines
5.3KB

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