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.

111 lines
4.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_DECIBELS_H_INCLUDED
  24. #define JUCE_DECIBELS_H_INCLUDED
  25. //==============================================================================
  26. /**
  27. This class contains some helpful static methods for dealing with decibel values.
  28. */
  29. class Decibels
  30. {
  31. public:
  32. //==============================================================================
  33. /** Converts a dBFS value to its equivalent gain level.
  34. A gain of 1.0 = 0 dB, and lower gains map onto negative decibel values. Any
  35. decibel value lower than minusInfinityDb will return a gain of 0.
  36. */
  37. template <typename Type>
  38. static Type decibelsToGain (const Type decibels,
  39. const Type minusInfinityDb = (Type) defaultMinusInfinitydB)
  40. {
  41. return decibels > minusInfinityDb ? std::pow ((Type) 10.0, decibels * (Type) 0.05)
  42. : Type();
  43. }
  44. /** Converts a gain level into a dBFS value.
  45. A gain of 1.0 = 0 dB, and lower gains map onto negative decibel values.
  46. If the gain is 0 (or negative), then the method will return the value
  47. provided as minusInfinityDb.
  48. */
  49. template <typename Type>
  50. static Type gainToDecibels (const Type gain,
  51. const Type minusInfinityDb = (Type) defaultMinusInfinitydB)
  52. {
  53. return gain > Type() ? jmax (minusInfinityDb, (Type) std::log10 (gain) * (Type) 20.0)
  54. : minusInfinityDb;
  55. }
  56. //==============================================================================
  57. /** Converts a decibel reading to a string, with the 'dB' suffix.
  58. If the decibel value is lower than minusInfinityDb, the return value will
  59. be "-INF dB".
  60. */
  61. template <typename Type>
  62. static String toString (const Type decibels,
  63. const int decimalPlaces = 2,
  64. const Type minusInfinityDb = (Type) defaultMinusInfinitydB)
  65. {
  66. String s;
  67. if (decibels <= minusInfinityDb)
  68. {
  69. s = "-INF dB";
  70. }
  71. else
  72. {
  73. if (decibels >= Type())
  74. s << '+';
  75. s << String (decibels, decimalPlaces) << " dB";
  76. }
  77. return s;
  78. }
  79. private:
  80. //==============================================================================
  81. enum
  82. {
  83. defaultMinusInfinitydB = -100
  84. };
  85. Decibels(); // This class can't be instantiated, it's just a holder for static methods..
  86. JUCE_DECLARE_NON_COPYABLE (Decibels)
  87. };
  88. #endif // JUCE_DECIBELS_H_INCLUDED