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_AudioParameterBool.cpp 4.0KB

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