OpenShot Audio Library | OpenShotAudio  0.6.0
juce_Decibels.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2022 - Raw Material Software Limited
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
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.
15 
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.
19 
20  ==============================================================================
21 */
22 
23 namespace juce
24 {
25 
26 //==============================================================================
32 class Decibels
33 {
34 public:
35  //==============================================================================
41  template <typename Type>
42  static Type decibelsToGain (Type decibels,
43  Type minusInfinityDb = Type (defaultMinusInfinitydB))
44  {
45  return decibels > minusInfinityDb ? std::pow (Type (10.0), decibels * Type (0.05))
46  : Type();
47  }
48 
55  template <typename Type>
56  static Type gainToDecibels (Type gain,
57  Type minusInfinityDb = Type (defaultMinusInfinitydB))
58  {
59  return gain > Type() ? jmax (minusInfinityDb, static_cast<Type> (std::log10 (gain)) * Type (20.0))
60  : minusInfinityDb;
61  }
62 
67  template <typename Type>
68  static Type gainWithLowerBound (Type gain, Type lowerBoundDb)
69  {
70  // You probably want to use a negative decibel value or the gain will
71  // be restricted to boosting only!
72  jassert (lowerBoundDb < (Type) 0.0);
73 
74  return jmax ((Type) gain, Decibels::decibelsToGain (lowerBoundDb, lowerBoundDb - (Type) 1.0));
75  }
76 
77  //==============================================================================
85  template <typename Type>
86  static String toString (Type decibels,
87  int decimalPlaces = 2,
88  Type minusInfinityDb = Type (defaultMinusInfinitydB),
89  bool shouldIncludeSuffix = true,
90  StringRef customMinusInfinityString = {})
91  {
92  String s;
93  s.preallocateBytes (20);
94 
95  if (decibels <= minusInfinityDb)
96  {
97  if (customMinusInfinityString.isEmpty())
98  s << "-INF";
99  else
100  s << customMinusInfinityString;
101  }
102  else
103  {
104  if (decibels >= Type())
105  s << '+';
106 
107  if (decimalPlaces <= 0)
108  s << roundToInt (decibels);
109  else
110  s << String (decibels, decimalPlaces);
111  }
112 
113  if (shouldIncludeSuffix)
114  s << " dB";
115 
116  return s;
117  }
118 
119 private:
120  //==============================================================================
121  enum { defaultMinusInfinitydB = -100 };
122 
123  Decibels() = delete; // This class can't be instantiated, it's just a holder for static methods..
124 };
125 
126 } // namespace juce
static Type gainWithLowerBound(Type gain, Type lowerBoundDb)
Definition: juce_Decibels.h:68
static Type decibelsToGain(Type decibels, Type minusInfinityDb=Type(defaultMinusInfinitydB))
Definition: juce_Decibels.h:42
static Type gainToDecibels(Type gain, Type minusInfinityDb=Type(defaultMinusInfinitydB))
Definition: juce_Decibels.h:56
static String toString(Type decibels, int decimalPlaces=2, Type minusInfinityDb=Type(defaultMinusInfinitydB), bool shouldIncludeSuffix=true, StringRef customMinusInfinityString={})
Definition: juce_Decibels.h:86
void preallocateBytes(size_t numBytesNeeded)