The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

105 lines
3.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_DECIBELS_JUCEHEADER__
  19. #define __JUCE_DECIBELS_JUCEHEADER__
  20. //==============================================================================
  21. /**
  22. This class contains some helpful static methods for dealing with decibel values.
  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 (const Type decibels,
  34. const Type minusInfinityDb = (Type) defaultMinusInfinitydB)
  35. {
  36. return decibels > minusInfinityDb ? powf ((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 (const Type gain,
  46. const Type minusInfinityDb = (Type) defaultMinusInfinitydB)
  47. {
  48. return gain > Type() ? jmax (minusInfinityDb, (Type) std::log10 (gain) * (Type) 20.0)
  49. : minusInfinityDb;
  50. }
  51. //==============================================================================
  52. /** Converts a decibel reading to a string, with the 'dB' suffix.
  53. If the decibel value is lower than minusInfinityDb, the return value will
  54. be "-INF dB".
  55. */
  56. template <typename Type>
  57. static String toString (const Type decibels,
  58. const int decimalPlaces = 2,
  59. const Type minusInfinityDb = (Type) defaultMinusInfinitydB)
  60. {
  61. String s;
  62. if (decibels <= minusInfinityDb)
  63. {
  64. s = "-INF dB";
  65. }
  66. else
  67. {
  68. if (decibels >= Type())
  69. s << '+';
  70. s << String (decibels, decimalPlaces) << " dB";
  71. }
  72. return s;
  73. }
  74. private:
  75. //==============================================================================
  76. enum
  77. {
  78. defaultMinusInfinitydB = -100
  79. };
  80. Decibels(); // This class can't be instantiated, it's just a holder for static methods..
  81. JUCE_DECLARE_NON_COPYABLE (Decibels);
  82. };
  83. #endif // __JUCE_DECIBELS_JUCEHEADER__