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.

101 lines
3.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. */
  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 ? std::pow ((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. } // namespace juce