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.

89 lines
3.8KB

  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. AudioParameterBool::AudioParameterBool (const ParameterID& idToUse,
  16. const String& nameToUse,
  17. bool def,
  18. const AudioParameterBoolAttributes& attributes)
  19. : RangedAudioParameter (idToUse, nameToUse, attributes.getAudioProcessorParameterWithIDAttributes()),
  20. value (def ? 1.0f : 0.0f),
  21. valueDefault (def),
  22. stringFromBoolFunction (attributes.getStringFromValueFunction() != nullptr
  23. ? attributes.getStringFromValueFunction()
  24. : [] (bool v, int) { return v ? TRANS("On") : TRANS("Off"); }),
  25. boolFromStringFunction (attributes.getValueFromStringFunction() != nullptr
  26. ? attributes.getValueFromStringFunction()
  27. : [] (const String& text)
  28. {
  29. static const StringArray onStrings { TRANS ("on"), TRANS ("yes"), TRANS ("true") };
  30. static const StringArray offStrings { TRANS ("off"), TRANS ("no"), TRANS ("false") };
  31. String lowercaseText (text.toLowerCase());
  32. for (auto& testText : onStrings)
  33. if (lowercaseText == testText)
  34. return true;
  35. for (auto& testText : offStrings)
  36. if (lowercaseText == testText)
  37. return false;
  38. return text.getIntValue() != 0;
  39. })
  40. {
  41. }
  42. AudioParameterBool::~AudioParameterBool()
  43. {
  44. #if __cpp_lib_atomic_is_always_lock_free
  45. static_assert (std::atomic<float>::is_always_lock_free,
  46. "AudioParameterBool requires a lock-free std::atomic<float>");
  47. #endif
  48. }
  49. float AudioParameterBool::getValue() const { return value; }
  50. void AudioParameterBool::setValue (float newValue) { value = newValue; valueChanged (get()); }
  51. float AudioParameterBool::getDefaultValue() const { return valueDefault; }
  52. int AudioParameterBool::getNumSteps() const { return 2; }
  53. bool AudioParameterBool::isDiscrete() const { return true; }
  54. bool AudioParameterBool::isBoolean() const { return true; }
  55. void AudioParameterBool::valueChanged (bool) {}
  56. float AudioParameterBool::getValueForText (const String& text) const
  57. {
  58. return boolFromStringFunction (text) ? 1.0f : 0.0f;
  59. }
  60. String AudioParameterBool::getText (float v, int maximumLength) const
  61. {
  62. return stringFromBoolFunction (v >= 0.5f, maximumLength);
  63. }
  64. AudioParameterBool& AudioParameterBool::operator= (bool newValue)
  65. {
  66. if (get() != newValue)
  67. setValueNotifyingHost (newValue ? 1.0f : 0.0f);
  68. return *this;
  69. }
  70. } // namespace juce