Suggestion: Decibels::parse() (with implementation)


In consideration of the last parameter of AudioProcessorValueTreeState::createAndAddParameter() I think the following static function would be a welcome addition to the Decibels class:

/** Converts a decibel reading formatted via toString with the 'dB' suffix.
    back to a floating point value in dB.
    If the passed String is "-INF dB", minusInfinityDb is returned.
    minusInfinityDb is returned when the input string is empty or cannot be parsed.
    0.0 is returned if the number in front of " dB" could not be parsed.
template <typename Type>
static float parse (
    const String& str,
    const Type minusInfinityDb = Decibels::gainToDecibels(Type()) // Type(defaultMinusInfinitydB)
    StringArray tokens = StringArray::fromTokens(str, " ", "");
    jassert(tokens.size() == 2);
    jassert(tokens[1] == "dB");

    if(tokens.size() == 0)
        return minusInfinityDb;

    if (tokens[0] == "-INF")
        return minusInfinityDb;
        return Type(tokens[0].getDoubleValue());

I hereby place the above source-code in the public domain or any other license reasonably suitable for inclusion into JUCE. (In case you want it :slight_smile: )



would be nice to have