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.

113 lines
4.0KB

  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. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. This class contains some helpful static methods for dealing with decibel values.
  22. @tags{Audio}
  23. */
  24. class Decibels
  25. {
  26. public:
  27. //==============================================================================
  28. /** Converts a dBFS value to its equivalent gain level.
  29. A gain of 1.0 = 0 dB, and lower gains map onto negative decibel values. Any
  30. decibel value lower than minusInfinityDb will return a gain of 0.
  31. */
  32. template <typename Type>
  33. static Type decibelsToGain (Type decibels,
  34. Type minusInfinityDb = Type (defaultMinusInfinitydB))
  35. {
  36. return decibels > minusInfinityDb ? std::pow (Type (10.0), decibels * Type (0.05))
  37. : Type();
  38. }
  39. /** Converts a gain level into a dBFS value.
  40. A gain of 1.0 = 0 dB, and lower gains map onto negative decibel values.
  41. If the gain is 0 (or negative), then the method will return the value
  42. provided as minusInfinityDb.
  43. */
  44. template <typename Type>
  45. static Type gainToDecibels (Type gain,
  46. Type minusInfinityDb = Type (defaultMinusInfinitydB))
  47. {
  48. return gain > Type() ? jmax (minusInfinityDb, static_cast<Type> (std::log10 (gain)) * Type (20.0))
  49. : minusInfinityDb;
  50. }
  51. //==============================================================================
  52. /** Converts a decibel reading to a string.
  53. By default the returned string will have the 'dB' suffix added, but this can be removed by
  54. setting the shouldIncludeSuffix argument to false. If a customMinusInfinityString argument
  55. is provided this will be returned if the value is lower than minusInfinityDb, otherwise
  56. the return value will be "-INF".
  57. */
  58. template <typename Type>
  59. static String toString (Type decibels,
  60. int decimalPlaces = 2,
  61. Type minusInfinityDb = Type (defaultMinusInfinitydB),
  62. bool shouldIncludeSuffix = true,
  63. StringRef customMinusInfinityString = {})
  64. {
  65. String s;
  66. s.preallocateBytes (20);
  67. if (decibels <= minusInfinityDb)
  68. {
  69. if (customMinusInfinityString.isEmpty())
  70. s << "-INF";
  71. else
  72. s << customMinusInfinityString;
  73. }
  74. else
  75. {
  76. if (decibels >= Type())
  77. s << '+';
  78. if (decimalPlaces <= 0)
  79. s << roundToInt (decibels);
  80. else
  81. s << String (decibels, decimalPlaces);
  82. }
  83. if (shouldIncludeSuffix)
  84. s << " dB";
  85. return s;
  86. }
  87. private:
  88. //==============================================================================
  89. enum { defaultMinusInfinitydB = -100 };
  90. Decibels() = delete; // This class can't be instantiated, it's just a holder for static methods..
  91. };
  92. } // namespace juce