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.

99 lines
3.5KB

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