VST3: fetch midi mappings all at once, use it for note/sound-off
[carla.git] / source / modules / juce_audio_basics / utilities / juce_Decibels.h
blobe5eb8026220ec4a14ae6b4bb8b1c563d673c1fc3
1 /*
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
8 licensing.
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
18 DISCLAIMED.
20 ==============================================================================
23 namespace juce
26 //==============================================================================
27 /**
28 This class contains some helpful static methods for dealing with decibel values.
30 @tags{Audio}
32 class Decibels
34 public:
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))
46 : Type();
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))
60 : minusInfinityDb;
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 = {})
78 String s;
79 s.preallocateBytes (20);
81 if (decibels <= minusInfinityDb)
83 if (customMinusInfinityString.isEmpty())
84 s << "-INF";
85 else
86 s << customMinusInfinityString;
88 else
90 if (decibels >= Type())
91 s << '+';
93 if (decimalPlaces <= 0)
94 s << roundToInt (decibels);
95 else
96 s << String (decibels, decimalPlaces);
99 if (shouldIncludeSuffix)
100 s << " dB";
102 return s;
105 private:
106 //==============================================================================
107 enum { defaultMinusInfinitydB = -100 };
109 Decibels() = delete; // This class can't be instantiated, it's just a holder for static methods..
112 } // namespace juce