ReplayGainConfig.cxx 3.58 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17 18
 *
 * 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.
 */
19

20
#include "config.h"
21
#include "ReplayGainConfig.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "Idle.hxx"
23 24
#include "config/ConfigData.hxx"
#include "config/ConfigGlobal.hxx"
25
#include "system/FatalError.hxx"
Avuton Olrich's avatar
Avuton Olrich committed
26

27
#include <assert.h>
28 29
#include <stdlib.h>
#include <string.h>
30
#include <math.h>
31

32
ReplayGainMode replay_gain_mode = REPLAY_GAIN_OFF;
33

34
static constexpr bool DEFAULT_REPLAYGAIN_LIMIT = true;
35

36 37
float replay_gain_preamp = 1.0;
float replay_gain_missing_preamp = 1.0;
38
bool replay_gain_limit = DEFAULT_REPLAYGAIN_LIMIT;
Warren Dukes's avatar
Warren Dukes committed
39

40 41 42 43
const char *
replay_gain_get_mode_string(void)
{
	switch (replay_gain_mode) {
44 45 46
	case REPLAY_GAIN_AUTO:
		return "auto";

47 48 49 50 51 52 53 54 55 56 57
	case REPLAY_GAIN_OFF:
		return "off";

	case REPLAY_GAIN_TRACK:
		return "track";

	case REPLAY_GAIN_ALBUM:
		return "album";
	}

	assert(false);
58
	gcc_unreachable();
59 60 61
}

bool
62
replay_gain_set_mode_string(const char *p)
Avuton Olrich's avatar
Avuton Olrich committed
63
{
64
	assert(p != nullptr);
65

66
	if (strcmp(p, "off") == 0)
67
		replay_gain_mode = REPLAY_GAIN_OFF;
68
	else if (strcmp(p, "track") == 0)
69
		replay_gain_mode = REPLAY_GAIN_TRACK;
70
	else if (strcmp(p, "album") == 0)
71
		replay_gain_mode = REPLAY_GAIN_ALBUM;
72 73
	else if (strcmp(p, "auto") == 0)
		replay_gain_mode = REPLAY_GAIN_AUTO;
74 75 76
	else
		return false;

77 78
	idle_add(IDLE_OPTIONS);

79 80 81 82 83 84 85
	return true;
}

void replay_gain_global_init(void)
{
	const struct config_param *param = config_get_param(CONF_REPLAYGAIN);

86
	if (param != nullptr &&
87
	    !replay_gain_set_mode_string(param->value.c_str())) {
88
		FormatFatalError("replaygain value \"%s\" at line %i is invalid\n",
89
				 param->value.c_str(), param->line);
90
	}
Warren Dukes's avatar
Warren Dukes committed
91

92
	param = config_get_param(CONF_REPLAYGAIN_PREAMP);
93

Avuton Olrich's avatar
Avuton Olrich committed
94 95
	if (param) {
		char *test;
96
		float f = strtod(param->value.c_str(), &test);
Warren Dukes's avatar
Warren Dukes committed
97

Avuton Olrich's avatar
Avuton Olrich committed
98
		if (*test != '\0') {
99
			FormatFatalError("Replaygain preamp \"%s\" is not a number at "
100 101
					 "line %i\n",
					 param->value.c_str(), param->line);
Warren Dukes's avatar
Warren Dukes committed
102 103
		}

Avuton Olrich's avatar
Avuton Olrich committed
104
		if (f < -15 || f > 15) {
105
			FormatFatalError("Replaygain preamp \"%s\" is not between -15 and"
106 107
					 "15 at line %i\n",
					 param->value.c_str(), param->line);
Warren Dukes's avatar
Warren Dukes committed
108 109
		}

110
		replay_gain_preamp = pow(10, f / 20.0);
Warren Dukes's avatar
Warren Dukes committed
111
	}
112 113 114 115 116

	param = config_get_param(CONF_REPLAYGAIN_MISSING_PREAMP);

	if (param) {
		char *test;
117
		float f = strtod(param->value.c_str(), &test);
118 119

		if (*test != '\0') {
120
			FormatFatalError("Replaygain missing preamp \"%s\" is not a number at "
121 122
					 "line %i\n",
					 param->value.c_str(), param->line);
123 124 125
		}

		if (f < -15 || f > 15) {
126
			FormatFatalError("Replaygain missing preamp \"%s\" is not between -15 and"
127 128
					 "15 at line %i\n",
					 param->value.c_str(), param->line);
129 130 131 132
		}

		replay_gain_missing_preamp = pow(10, f / 20.0);
	}
133 134

	replay_gain_limit = config_get_bool(CONF_REPLAYGAIN_LIMIT, DEFAULT_REPLAYGAIN_LIMIT);
135
}
136

137
ReplayGainMode
138
replay_gain_get_real_mode(bool random_mode)
139
{
140
	ReplayGainMode rgm;
141 142 143 144

	rgm = replay_gain_mode;

	if (rgm == REPLAY_GAIN_AUTO)
145
	    rgm = random_mode ? REPLAY_GAIN_TRACK : REPLAY_GAIN_ALBUM;
146 147 148

	return rgm;
}