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 3.8KB

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