OpenShot Library | OpenShotAudio  0.2.2
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 
27 namespace juce
28 {
29 
30 //==============================================================================
31 /**
32  This class contains some helpful static methods for dealing with decibel values.
33 
34  @tags{Audio}
35 */
36 class Decibels
37 {
38 public:
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))
64  : minusInfinityDb;
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,
80  StringRef customMinusInfinityString = {})
81  {
82  String s;
83  s.preallocateBytes (20);
84 
85  if (decibels <= minusInfinityDb)
86  {
87  if (customMinusInfinityString.isEmpty())
88  s << "-INF";
89  else
90  s << customMinusInfinityString;
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 
109 private:
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 /** @}*/
static Type decibelsToGain(Type decibels, Type minusInfinityDb=Type(defaultMinusInfinitydB))
Converts a dBFS value to its equivalent gain level.
Definition: juce_Decibels.h:46
A simple class for holding temporary references to a string literal or String.
static Type gainToDecibels(Type gain, Type minusInfinityDb=Type(defaultMinusInfinitydB))
Converts a gain level into a dBFS value.
Definition: juce_Decibels.h:60
static String toString(Type decibels, int decimalPlaces=2, Type minusInfinityDb=Type(defaultMinusInfinitydB), bool shouldIncludeSuffix=true, StringRef customMinusInfinityString={})
Converts a decibel reading to a string.
Definition: juce_Decibels.h:76
The JUCE String class!
Definition: juce_String.h:42
This class contains some helpful static methods for dealing with decibel values.
Definition: juce_Decibels.h:36
void preallocateBytes(size_t numBytesNeeded)
Increases the string&#39;s internally allocated storage.