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.

99 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. #pragma once
  18. //==============================================================================
  19. /**
  20. This class contains some helpful static methods for dealing with decibel values.
  21. */
  22. class Decibels
  23. {
  24. public:
  25. //==============================================================================
  26. /** Converts a dBFS value to its equivalent gain level.
  27. A gain of 1.0 = 0 dB, and lower gains map onto negative decibel values. Any
  28. decibel value lower than minusInfinityDb will return a gain of 0.
  29. */
  30. template <typename Type>
  31. static Type decibelsToGain (const Type decibels,
  32. const Type minusInfinityDb = (Type) defaultMinusInfinitydB)
  33. {
  34. return decibels > minusInfinityDb ? std::pow ((Type) 10.0, decibels * (Type) 0.05)
  35. : Type();
  36. }
  37. /** Converts a gain level into a dBFS value.
  38. A gain of 1.0 = 0 dB, and lower gains map onto negative decibel values.
  39. If the gain is 0 (or negative), then the method will return the value
  40. provided as minusInfinityDb.
  41. */
  42. template <typename Type>
  43. static Type gainToDecibels (const Type gain,
  44. const Type minusInfinityDb = (Type) defaultMinusInfinitydB)
  45. {
  46. return gain > Type() ? jmax (minusInfinityDb, (Type) std::log10 (gain) * (Type) 20.0)
  47. : minusInfinityDb;
  48. }
  49. //==============================================================================
  50. /** Converts a decibel reading to a string, with the 'dB' suffix.
  51. If the decibel value is lower than minusInfinityDb, the return value will
  52. be "-INF dB".
  53. */
  54. template <typename Type>
  55. static String toString (const Type decibels,
  56. const int decimalPlaces = 2,
  57. const Type minusInfinityDb = (Type) defaultMinusInfinitydB)
  58. {
  59. String s;
  60. if (decibels <= minusInfinityDb)
  61. {
  62. s = "-INF dB";
  63. }
  64. else
  65. {
  66. if (decibels >= Type())
  67. s << '+';
  68. s << String (decibels, decimalPlaces) << " dB";
  69. }
  70. return s;
  71. }
  72. private:
  73. //==============================================================================
  74. enum
  75. {
  76. defaultMinusInfinitydB = -100
  77. };
  78. Decibels(); // This class can't be instantiated, it's just a holder for static methods..
  79. JUCE_DECLARE_NON_COPYABLE (Decibels)
  80. };