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_Decibels.h 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_DECIBELS_H_INCLUDED
  18. #define JUCE_DECIBELS_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. This class contains some helpful static methods for dealing with decibel values.
  22. */
  23. class Decibels
  24. {
  25. public:
  26. //==============================================================================
  27. /** Converts a dBFS value to its equivalent gain level.
  28. A gain of 1.0 = 0 dB, and lower gains map onto negative decibel values. Any
  29. decibel value lower than minusInfinityDb will return a gain of 0.
  30. */
  31. template <typename Type>
  32. static Type decibelsToGain (const Type decibels,
  33. const Type minusInfinityDb = (Type) defaultMinusInfinitydB)
  34. {
  35. return decibels > minusInfinityDb ? powf ((Type) 10.0, decibels * (Type) 0.05)
  36. : Type();
  37. }
  38. /** Converts a gain level into a dBFS value.
  39. A gain of 1.0 = 0 dB, and lower gains map onto negative decibel values.
  40. If the gain is 0 (or negative), then the method will return the value
  41. provided as minusInfinityDb.
  42. */
  43. template <typename Type>
  44. static Type gainToDecibels (const Type gain,
  45. const Type minusInfinityDb = (Type) defaultMinusInfinitydB)
  46. {
  47. return gain > Type() ? jmax (minusInfinityDb, (Type) std::log10 (gain) * (Type) 20.0)
  48. : minusInfinityDb;
  49. }
  50. //==============================================================================
  51. /** Converts a decibel reading to a string, with the 'dB' suffix.
  52. If the decibel value is lower than minusInfinityDb, the return value will
  53. be "-INF dB".
  54. */
  55. template <typename Type>
  56. static String toString (const Type decibels,
  57. const int decimalPlaces = 2,
  58. const Type minusInfinityDb = (Type) defaultMinusInfinitydB)
  59. {
  60. String s;
  61. if (decibels <= minusInfinityDb)
  62. {
  63. s = "-INF dB";
  64. }
  65. else
  66. {
  67. if (decibels >= Type())
  68. s << '+';
  69. s << String (decibels, decimalPlaces) << " dB";
  70. }
  71. return s;
  72. }
  73. private:
  74. //==============================================================================
  75. enum
  76. {
  77. defaultMinusInfinitydB = -100
  78. };
  79. Decibels(); // This class can't be instantiated, it's just a holder for static methods..
  80. JUCE_DECLARE_NON_COPYABLE (Decibels)
  81. };
  82. #endif // JUCE_DECIBELS_H_INCLUDED