2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2014-01-13 22:30:36 +01:00
|
|
|
* Copyright (C) 2003-2014 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-05-08 14:04:23 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2004-05-08 00:42:54 +02:00
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-10-02 12:22:12 +02:00
|
|
|
#include "ReplayGainConfig.hxx"
|
2013-01-09 08:36:52 +01:00
|
|
|
#include "Idle.hxx"
|
2013-09-05 08:47:10 +02:00
|
|
|
#include "ConfigData.hxx"
|
|
|
|
#include "ConfigGlobal.hxx"
|
2013-09-05 18:20:52 +02:00
|
|
|
#include "system/FatalError.hxx"
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2009-10-17 22:40:50 +02:00
|
|
|
#include <assert.h>
|
2008-11-11 16:38:33 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-11-28 11:50:54 +01:00
|
|
|
#include <math.h>
|
2008-11-11 16:24:27 +01:00
|
|
|
|
2013-10-25 19:09:22 +02:00
|
|
|
ReplayGainMode replay_gain_mode = REPLAY_GAIN_OFF;
|
2004-05-08 00:42:54 +02:00
|
|
|
|
2013-01-09 08:56:14 +01:00
|
|
|
static constexpr bool DEFAULT_REPLAYGAIN_LIMIT = true;
|
2010-05-30 17:05:43 +02:00
|
|
|
|
2010-01-04 13:31:20 +01:00
|
|
|
float replay_gain_preamp = 1.0;
|
|
|
|
float replay_gain_missing_preamp = 1.0;
|
2013-01-09 08:58:04 +01:00
|
|
|
bool replay_gain_limit = DEFAULT_REPLAYGAIN_LIMIT;
|
2004-07-17 16:54:22 +02:00
|
|
|
|
2009-10-17 22:58:19 +02:00
|
|
|
const char *
|
|
|
|
replay_gain_get_mode_string(void)
|
|
|
|
{
|
|
|
|
switch (replay_gain_mode) {
|
2010-04-25 13:42:23 +02:00
|
|
|
case REPLAY_GAIN_AUTO:
|
|
|
|
return "auto";
|
|
|
|
|
2009-10-17 22:58:19 +02:00
|
|
|
case REPLAY_GAIN_OFF:
|
|
|
|
return "off";
|
|
|
|
|
|
|
|
case REPLAY_GAIN_TRACK:
|
|
|
|
return "track";
|
|
|
|
|
|
|
|
case REPLAY_GAIN_ALBUM:
|
|
|
|
return "album";
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(false);
|
2013-08-03 21:34:17 +02:00
|
|
|
gcc_unreachable();
|
2009-10-17 22:58:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2009-10-17 22:40:50 +02:00
|
|
|
replay_gain_set_mode_string(const char *p)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-10-28 23:58:17 +01:00
|
|
|
assert(p != nullptr);
|
2004-05-08 00:42:54 +02:00
|
|
|
|
2009-10-17 22:40:50 +02:00
|
|
|
if (strcmp(p, "off") == 0)
|
2009-10-17 21:38:32 +02:00
|
|
|
replay_gain_mode = REPLAY_GAIN_OFF;
|
2009-10-17 22:40:50 +02:00
|
|
|
else if (strcmp(p, "track") == 0)
|
2008-11-11 15:55:34 +01:00
|
|
|
replay_gain_mode = REPLAY_GAIN_TRACK;
|
2009-10-17 22:40:50 +02:00
|
|
|
else if (strcmp(p, "album") == 0)
|
2008-11-11 15:55:34 +01:00
|
|
|
replay_gain_mode = REPLAY_GAIN_ALBUM;
|
2010-04-25 13:42:23 +02:00
|
|
|
else if (strcmp(p, "auto") == 0)
|
|
|
|
replay_gain_mode = REPLAY_GAIN_AUTO;
|
2009-10-17 22:40:50 +02:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
2009-11-01 15:44:56 +01:00
|
|
|
idle_add(IDLE_OPTIONS);
|
|
|
|
|
2009-10-17 22:40:50 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void replay_gain_global_init(void)
|
|
|
|
{
|
|
|
|
const struct config_param *param = config_get_param(CONF_REPLAYGAIN);
|
|
|
|
|
2013-10-28 23:58:17 +01:00
|
|
|
if (param != nullptr &&
|
2013-10-15 22:32:39 +02:00
|
|
|
!replay_gain_set_mode_string(param->value.c_str())) {
|
2013-09-05 18:20:52 +02:00
|
|
|
FormatFatalError("replaygain value \"%s\" at line %i is invalid\n",
|
2013-10-15 22:32:39 +02:00
|
|
|
param->value.c_str(), param->line);
|
2004-05-08 00:42:54 +02:00
|
|
|
}
|
2004-07-17 16:54:22 +02:00
|
|
|
|
2009-01-17 20:23:27 +01:00
|
|
|
param = config_get_param(CONF_REPLAYGAIN_PREAMP);
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (param) {
|
|
|
|
char *test;
|
2013-10-15 22:32:39 +02:00
|
|
|
float f = strtod(param->value.c_str(), &test);
|
2004-07-17 16:54:22 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (*test != '\0') {
|
2013-09-05 18:20:52 +02:00
|
|
|
FormatFatalError("Replaygain preamp \"%s\" is not a number at "
|
2013-10-15 22:32:39 +02:00
|
|
|
"line %i\n",
|
|
|
|
param->value.c_str(), param->line);
|
2004-07-17 16:54:22 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (f < -15 || f > 15) {
|
2013-09-05 18:20:52 +02:00
|
|
|
FormatFatalError("Replaygain preamp \"%s\" is not between -15 and"
|
2013-10-15 22:32:39 +02:00
|
|
|
"15 at line %i\n",
|
|
|
|
param->value.c_str(), param->line);
|
2004-07-17 16:54:22 +02:00
|
|
|
}
|
|
|
|
|
2008-11-11 15:55:34 +01:00
|
|
|
replay_gain_preamp = pow(10, f / 20.0);
|
2004-07-17 16:54:22 +02:00
|
|
|
}
|
2009-06-19 09:02:12 +02:00
|
|
|
|
|
|
|
param = config_get_param(CONF_REPLAYGAIN_MISSING_PREAMP);
|
|
|
|
|
|
|
|
if (param) {
|
|
|
|
char *test;
|
2013-10-15 22:32:39 +02:00
|
|
|
float f = strtod(param->value.c_str(), &test);
|
2009-06-19 09:02:12 +02:00
|
|
|
|
|
|
|
if (*test != '\0') {
|
2013-09-05 18:20:52 +02:00
|
|
|
FormatFatalError("Replaygain missing preamp \"%s\" is not a number at "
|
2013-10-15 22:32:39 +02:00
|
|
|
"line %i\n",
|
|
|
|
param->value.c_str(), param->line);
|
2009-06-19 09:02:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (f < -15 || f > 15) {
|
2013-09-05 18:20:52 +02:00
|
|
|
FormatFatalError("Replaygain missing preamp \"%s\" is not between -15 and"
|
2013-10-15 22:32:39 +02:00
|
|
|
"15 at line %i\n",
|
|
|
|
param->value.c_str(), param->line);
|
2009-06-19 09:02:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
replay_gain_missing_preamp = pow(10, f / 20.0);
|
|
|
|
}
|
2010-05-30 17:05:43 +02:00
|
|
|
|
|
|
|
replay_gain_limit = config_get_bool(CONF_REPLAYGAIN_LIMIT, DEFAULT_REPLAYGAIN_LIMIT);
|
2004-05-08 00:42:54 +02:00
|
|
|
}
|
2010-05-18 20:30:03 +02:00
|
|
|
|
2013-10-25 19:09:22 +02:00
|
|
|
ReplayGainMode
|
2013-01-05 00:05:57 +01:00
|
|
|
replay_gain_get_real_mode(bool random_mode)
|
2010-05-18 20:30:03 +02:00
|
|
|
{
|
2013-10-25 19:09:22 +02:00
|
|
|
ReplayGainMode rgm;
|
2010-05-18 20:30:03 +02:00
|
|
|
|
|
|
|
rgm = replay_gain_mode;
|
|
|
|
|
|
|
|
if (rgm == REPLAY_GAIN_AUTO)
|
2013-01-05 00:05:57 +01:00
|
|
|
rgm = random_mode ? REPLAY_GAIN_TRACK : REPLAY_GAIN_ALBUM;
|
2010-05-18 20:30:03 +02:00
|
|
|
|
|
|
|
return rgm;
|
|
|
|
}
|