replay_gain.c 3.37 KB
Newer Older
1
/* the Music Player Daemon (MPD)
2
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * (c)2004 replayGain code by AliasMrJones
 * This project's homepage is: http://www.musicpd.org
 *
 * 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.
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
19

20
#include "replay_gain.h"
21
#include "conf.h"
22
#include "audio_format.h"
23
#include "pcm_volume.h"
Avuton Olrich's avatar
Avuton Olrich committed
24

25
#include <glib.h>
26 27 28
#include <stdlib.h>
#include <string.h>
#include <math.h>
29 30 31 32 33 34

static const char *const replay_gain_mode_names[] = {
	[REPLAY_GAIN_ALBUM] = "album",
	[REPLAY_GAIN_TRACK] = "track",
};

35
enum replay_gain_mode replay_gain_mode = REPLAY_GAIN_OFF;
36

37
static float replay_gain_preamp = 1.0;
Warren Dukes's avatar
Warren Dukes committed
38

39
void replay_gain_global_init(void)
Avuton Olrich's avatar
Avuton Olrich committed
40
{
41
	const struct config_param *param = config_get_param(CONF_REPLAYGAIN);
42

Avuton Olrich's avatar
Avuton Olrich committed
43 44
	if (!param)
		return;
45

Avuton Olrich's avatar
Avuton Olrich committed
46
	if (strcmp(param->value, "track") == 0) {
47
		replay_gain_mode = REPLAY_GAIN_TRACK;
Avuton Olrich's avatar
Avuton Olrich committed
48
	} else if (strcmp(param->value, "album") == 0) {
49
		replay_gain_mode = REPLAY_GAIN_ALBUM;
Avuton Olrich's avatar
Avuton Olrich committed
50
	} else {
51 52
		g_error("replaygain value \"%s\" at line %i is invalid\n",
			param->value, param->line);
53
	}
Warren Dukes's avatar
Warren Dukes committed
54

55
	param = config_get_param(CONF_REPLAYGAIN_PREAMP);
56

Avuton Olrich's avatar
Avuton Olrich committed
57 58
	if (param) {
		char *test;
59
		float f = strtod(param->value, &test);
Warren Dukes's avatar
Warren Dukes committed
60

Avuton Olrich's avatar
Avuton Olrich committed
61
		if (*test != '\0') {
62 63
			g_error("Replaygain preamp \"%s\" is not a number at "
				"line %i\n", param->value, param->line);
Warren Dukes's avatar
Warren Dukes committed
64 65
		}

Avuton Olrich's avatar
Avuton Olrich committed
66
		if (f < -15 || f > 15) {
67 68
			g_error("Replaygain preamp \"%s\" is not between -15 and"
				"15 at line %i\n", param->value, param->line);
Warren Dukes's avatar
Warren Dukes committed
69 70
		}

71
		replay_gain_preamp = pow(10, f / 20.0);
Warren Dukes's avatar
Warren Dukes committed
72
	}
73 74
}

75
static float calc_replay_gain_scale(float gain, float peak)
Avuton Olrich's avatar
Avuton Olrich committed
76
{
77 78
	float scale;

Avuton Olrich's avatar
Avuton Olrich committed
79 80 81
	if (gain == 0.0)
		return (1);
	scale = pow(10.0, gain / 20.0);
82
	scale *= replay_gain_preamp;
Avuton Olrich's avatar
Avuton Olrich committed
83 84
	if (scale > 15.0)
		scale = 15.0;
85 86 87 88

	if (scale * peak > 1.0) {
		scale = 1.0 / peak;
	}
Avuton Olrich's avatar
Avuton Olrich committed
89
	return (scale);
90 91
}

92
struct replay_gain_info *replay_gain_info_new(void)
Avuton Olrich's avatar
Avuton Olrich committed
93
{
94
	struct replay_gain_info *ret = g_new(struct replay_gain_info, 1);
95

96 97 98 99
	for (unsigned i = 0; i < G_N_ELEMENTS(ret->tuples); ++i) {
		ret->tuples[i].gain = 0.0;
		ret->tuples[i].peak = 0.0;
	}
100

101
	/* set to -1 so that we know in replay_gain_apply to compute the scale */
102 103 104 105 106
	ret->scale = -1.0;

	return ret;
}

107
void replay_gain_info_free(struct replay_gain_info *info)
Avuton Olrich's avatar
Avuton Olrich committed
108
{
109
	g_free(info);
110 111
}

112 113
void
replay_gain_apply(struct replay_gain_info *info, char *buffer, int size,
114
		  const struct audio_format *format)
115
{
116
	if (replay_gain_mode == REPLAY_GAIN_OFF || !info)
Avuton Olrich's avatar
Avuton Olrich committed
117
		return;
118

Avuton Olrich's avatar
Avuton Olrich committed
119
	if (info->scale < 0) {
120 121 122
		const struct replay_gain_tuple *tuple =
			&info->tuples[replay_gain_mode];

123 124 125
		g_debug("computing ReplayGain %s scale with gain %f, peak %f\n",
			replay_gain_mode_names[replay_gain_mode],
			tuple->gain, tuple->peak);
126 127

		info->scale = calc_replay_gain_scale(tuple->gain, tuple->peak);
128 129
	}

130
	pcm_volume(buffer, size, format, pcm_float_to_volume(info->scale));
131
}