Add remaining files
[juce-lv2.git] / juce / source / src / audio / dsp / juce_Decibels.h
blobf139be006f6a6ae1fdedf2ebb83950e7c519d537
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCE_DECIBELS_JUCEHEADER__
27 #define __JUCE_DECIBELS_JUCEHEADER__
29 //==============================================================================
30 /**
31 This class contains some helpful static methods for dealing with decibel values.
33 class Decibels
35 public:
36 //==============================================================================
37 /** Converts a dBFS value to its equivalent gain level.
39 A gain of 1.0 = 0 dB, and lower gains map onto negative decibel values. Any
40 decibel value lower than minusInfinityDb will return a gain of 0.
42 template <typename Type>
43 static Type decibelsToGain (const Type decibels,
44 const Type minusInfinityDb = (Type) defaultMinusInfinitydB)
46 return decibels > minusInfinityDb ? powf ((Type) 10.0, decibels * (Type) 0.05)
47 : Type();
50 /** Converts a gain level into a dBFS value.
52 A gain of 1.0 = 0 dB, and lower gains map onto negative decibel values.
53 If the gain is 0 (or negative), then the method will return the value
54 provided as minusInfinityDb.
56 template <typename Type>
57 static Type gainToDecibels (const Type gain,
58 const Type minusInfinityDb = (Type) defaultMinusInfinitydB)
60 return gain > Type() ? jmax (minusInfinityDb, (Type) std::log10 (gain) * (Type) 20.0)
61 : minusInfinityDb;
64 //==============================================================================
65 /** Converts a decibel reading to a string, with the 'dB' suffix.
66 If the decibel value is lower than minusInfinityDb, the return value will
67 be "-INF dB".
69 template <typename Type>
70 static String toString (const Type decibels,
71 const int decimalPlaces = 2,
72 const Type minusInfinityDb = (Type) defaultMinusInfinitydB)
74 String s;
76 if (decibels <= minusInfinityDb)
78 s = "-INF dB";
80 else
82 if (decibels >= Type())
83 s << '+';
85 s << String (decibels, decimalPlaces) << " dB";
88 return s;
92 private:
93 //==============================================================================
94 enum
96 defaultMinusInfinitydB = -100
99 Decibels(); // This class can't be instantiated, it's just a holder for static methods..
100 JUCE_DECLARE_NON_COPYABLE (Decibels);
104 #endif // __JUCE_DECIBELS_JUCEHEADER__