2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2004-05-08 00:42:54 +02:00
|
|
|
|
2013-10-02 12:22:12 +02:00
|
|
|
#include "ReplayGainConfig.hxx"
|
2021-12-03 23:04:39 +01:00
|
|
|
#include "Data.hxx"
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2020-03-24 04:36:38 +01:00
|
|
|
#include <cmath>
|
2020-03-16 07:33:21 +01:00
|
|
|
#include <cstdlib>
|
2019-12-16 23:47:49 +01:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2016-11-24 17:45:27 +01:00
|
|
|
static float
|
|
|
|
ParsePreamp(const char *s)
|
|
|
|
{
|
|
|
|
assert(s != nullptr);
|
|
|
|
|
|
|
|
char *endptr;
|
2020-03-16 07:33:21 +01:00
|
|
|
float f = std::strtof(s, &endptr);
|
2016-11-24 17:45:27 +01:00
|
|
|
if (endptr == s || *endptr != '\0')
|
|
|
|
throw std::invalid_argument("Not a numeric value");
|
|
|
|
|
2020-03-16 07:33:21 +01:00
|
|
|
if (f < -15.0f || f > 15.0f)
|
2016-11-24 17:45:27 +01:00
|
|
|
throw std::invalid_argument("Number must be between -15 and 15");
|
|
|
|
|
2020-03-16 07:33:21 +01:00
|
|
|
return std::pow(10.0f, f / 20.0f);
|
2016-11-24 17:45:27 +01:00
|
|
|
}
|
|
|
|
|
2021-12-03 23:04:39 +01:00
|
|
|
ReplayGainConfig::ReplayGainConfig(const ConfigData &config)
|
|
|
|
:preamp(config.With(ConfigOption::REPLAYGAIN_PREAMP, [](const char *s){
|
2019-05-29 21:45:58 +02:00
|
|
|
return s != nullptr
|
|
|
|
? ParsePreamp(s)
|
2020-03-16 07:33:21 +01:00
|
|
|
: 1.0f;
|
2021-12-03 23:04:39 +01:00
|
|
|
})),
|
|
|
|
missing_preamp(config.With(ConfigOption::REPLAYGAIN_MISSING_PREAMP, [](const char *s){
|
|
|
|
return s != nullptr
|
|
|
|
? ParsePreamp(s)
|
|
|
|
: 1.0f;
|
|
|
|
})),
|
|
|
|
limit(config.GetBool(ConfigOption::REPLAYGAIN_LIMIT,
|
|
|
|
ReplayGainConfig::DEFAULT_LIMIT))
|
|
|
|
{
|
2004-05-08 00:42:54 +02:00
|
|
|
}
|