2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
20 ==============================================================================
26 //==============================================================================
28 This class contains some helpful static methods for dealing with decibel values.
35 //==============================================================================
36 /** Converts a dBFS value to its equivalent gain level.
38 A gain of 1.0 = 0 dB, and lower gains map onto negative decibel values. Any
39 decibel value lower than minusInfinityDb will return a gain of 0.
41 template <typename Type
>
42 static Type
decibelsToGain (Type decibels
,
43 Type minusInfinityDb
= Type (defaultMinusInfinitydB
))
45 return decibels
> minusInfinityDb
? std::pow (Type (10.0), decibels
* Type (0.05))
49 /** Converts a gain level into a dBFS value.
51 A gain of 1.0 = 0 dB, and lower gains map onto negative decibel values.
52 If the gain is 0 (or negative), then the method will return the value
53 provided as minusInfinityDb.
55 template <typename Type
>
56 static Type
gainToDecibels (Type gain
,
57 Type minusInfinityDb
= Type (defaultMinusInfinitydB
))
59 return gain
> Type() ? jmax (minusInfinityDb
, static_cast<Type
> (std::log10 (gain
)) * Type (20.0))
63 //==============================================================================
64 /** Converts a decibel reading to a string.
66 By default the returned string will have the 'dB' suffix added, but this can be removed by
67 setting the shouldIncludeSuffix argument to false. If a customMinusInfinityString argument
68 is provided this will be returned if the value is lower than minusInfinityDb, otherwise
69 the return value will be "-INF".
71 template <typename Type
>
72 static String
toString (Type decibels
,
73 int decimalPlaces
= 2,
74 Type minusInfinityDb
= Type (defaultMinusInfinitydB
),
75 bool shouldIncludeSuffix
= true,
76 StringRef customMinusInfinityString
= {})
79 s
.preallocateBytes (20);
81 if (decibels
<= minusInfinityDb
)
83 if (customMinusInfinityString
.isEmpty())
86 s
<< customMinusInfinityString
;
90 if (decibels
>= Type())
93 if (decimalPlaces
<= 0)
94 s
<< roundToInt (decibels
);
96 s
<< String (decibels
, decimalPlaces
);
99 if (shouldIncludeSuffix
)
106 //==============================================================================
107 enum { defaultMinusInfinitydB
= -100 };
109 Decibels() = delete; // This class can't be instantiated, it's just a holder for static methods..