OpenShot Library | libopenshot-audio 0.2.0
juce_Decibels.h
1
2/** @weakgroup juce_audio_basics-utilities
3 * @{
4 */
5/*
6 ==============================================================================
7
8 This file is part of the JUCE library.
9 Copyright (c) 2017 - ROLI Ltd.
10
11 JUCE is an open source library subject to commercial or open-source
12 licensing.
13
14 The code included in this file is provided under the terms of the ISC license
15 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16 To use, copy, modify, and/or distribute this software for any purpose with or
17 without fee is hereby granted provided that the above copyright notice and
18 this permission notice appear in all copies.
19
20 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22 DISCLAIMED.
23
24 ==============================================================================
25*/
26
27namespace juce
28{
29
30//==============================================================================
31/**
32 This class contains some helpful static methods for dealing with decibel values.
33
34 @tags{Audio}
35*/
37{
38public:
39 //==============================================================================
40 /** Converts a dBFS value to its equivalent gain level.
41
42 A gain of 1.0 = 0 dB, and lower gains map onto negative decibel values. Any
43 decibel value lower than minusInfinityDb will return a gain of 0.
44 */
45 template <typename Type>
46 static Type decibelsToGain (Type decibels,
47 Type minusInfinityDb = Type (defaultMinusInfinitydB))
48 {
49 return decibels > minusInfinityDb ? std::pow (Type (10.0), decibels * Type (0.05))
50 : Type();
51 }
52
53 /** Converts a gain level into a dBFS value.
54
55 A gain of 1.0 = 0 dB, and lower gains map onto negative decibel values.
56 If the gain is 0 (or negative), then the method will return the value
57 provided as minusInfinityDb.
58 */
59 template <typename Type>
60 static Type gainToDecibels (Type gain,
61 Type minusInfinityDb = Type (defaultMinusInfinitydB))
62 {
63 return gain > Type() ? jmax (minusInfinityDb, static_cast<Type> (std::log10 (gain)) * Type (20.0))
65 }
66
67 //==============================================================================
68 /** Converts a decibel reading to a string.
69
70 By default the returned string will have the 'dB' suffix added, but this can be removed by
71 setting the shouldIncludeSuffix argument to false. If a customMinusInfinityString argument
72 is provided this will be returned if the value is lower than minusInfinityDb, otherwise
73 the return value will be "-INF".
74 */
75 template <typename Type>
76 static String toString (Type decibels,
77 int decimalPlaces = 2,
78 Type minusInfinityDb = Type (defaultMinusInfinitydB),
79 bool shouldIncludeSuffix = true,
81 {
82 String s;
83 s.preallocateBytes (20);
84
86 {
88 s << "-INF";
89 else
91 }
92 else
93 {
94 if (decibels >= Type())
95 s << '+';
96
97 if (decimalPlaces <= 0)
98 s << roundToInt (decibels);
99 else
100 s << String (decibels, decimalPlaces);
101 }
102
103 if (shouldIncludeSuffix)
104 s << " dB";
105
106 return s;
107 }
108
109private:
110 //==============================================================================
111 enum { defaultMinusInfinitydB = -100 };
112
113 Decibels() = delete; // This class can't be instantiated, it's just a holder for static methods..
114};
115
116} // namespace juce
117
118/** @}*/
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:60
bool isEmpty() const noexcept
Returns true if the array is empty, false otherwise.
Definition juce_Array.h:226
This class contains some helpful static methods for dealing with decibel values.
static Type decibelsToGain(Type decibels, Type minusInfinityDb=Type(defaultMinusInfinitydB))
Converts a dBFS value to its equivalent gain level.
static Type gainToDecibels(Type gain, Type minusInfinityDb=Type(defaultMinusInfinitydB))
Converts a gain level into a dBFS value.
static String toString(Type decibels, int decimalPlaces=2, Type minusInfinityDb=Type(defaultMinusInfinitydB), bool shouldIncludeSuffix=true, StringRef customMinusInfinityString={})
Converts a decibel reading to a string.
A simple class for holding temporary references to a string literal or String.
The JUCE String class!
Definition juce_String.h:43