Loading...
Searching...
No Matches
juce_Decibels.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
//==============================================================================
class Decibels
{
public:
//==============================================================================
template <typename Type>
static Type decibelsToGain (Type decibels,
Type minusInfinityDb = Type (defaultMinusInfinitydB))
{
return decibels > minusInfinityDb ? std::pow (Type (10.0), decibels * Type (0.05))
: Type();
}
template <typename Type>
static Type gainToDecibels (Type gain,
Type minusInfinityDb = Type (defaultMinusInfinitydB))
{
return gain > Type() ? jmax (minusInfinityDb, static_cast<Type> (std::log10 (gain)) * Type (20.0))
: minusInfinityDb;
}
template <typename Type>
static Type gainWithLowerBound (Type gain, Type lowerBoundDb)
{
// You probably want to use a negative decibel value or the gain will
// be restricted to boosting only!
jassert (lowerBoundDb < (Type) 0.0);
return jmax ((Type) gain, Decibels::decibelsToGain (lowerBoundDb, lowerBoundDb - (Type) 1.0));
}
//==============================================================================
template <typename Type>
static String toString (Type decibels,
int decimalPlaces = 2,
Type minusInfinityDb = Type (defaultMinusInfinitydB),
bool shouldIncludeSuffix = true,
StringRef customMinusInfinityString = {})
{
String s;
s.preallocateBytes (20);
if (decibels <= minusInfinityDb)
{
if (customMinusInfinityString.isEmpty())
s << "-INF";
else
s << customMinusInfinityString;
}
else
{
if (decibels >= Type())
s << '+';
if (decimalPlaces <= 0)
s << roundToInt (decibels);
else
s << String (decibels, decimalPlaces);
}
if (shouldIncludeSuffix)
s << " dB";
return s;
}
private:
//==============================================================================
enum { defaultMinusInfinitydB = -100 };
Decibels() = delete; // This class can't be instantiated, it's just a holder for static methods..
};
} // namespace juce
juce::Decibels::gainWithLowerBound
static Type gainWithLowerBound(Type gain, Type lowerBoundDb)
Definition juce_Decibels.h:68
juce::Decibels::decibelsToGain
static Type decibelsToGain(Type decibels, Type minusInfinityDb=Type(defaultMinusInfinitydB))
Definition juce_Decibels.h:42
juce::Decibels::gainToDecibels
static Type gainToDecibels(Type gain, Type minusInfinityDb=Type(defaultMinusInfinitydB))
Definition juce_Decibels.h:56
juce::Decibels::toString
static String toString(Type decibels, int decimalPlaces=2, Type minusInfinityDb=Type(defaultMinusInfinitydB), bool shouldIncludeSuffix=true, StringRef customMinusInfinityString={})
Definition juce_Decibels.h:86
juce::StringRef
Definition juce_StringRef.h:62
juce::String
Definition juce_String.h:53
juce::String::preallocateBytes
void preallocateBytes(size_t numBytesNeeded)
Definition juce_String.cpp:284