2
* Copyright (C) 2003-2009 The Music Player Daemon Project
3
* http://www.musicpd.org
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License along
16
* with this program; if not, write to the Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
* (c)2004 replayGain code by AliasMrJones
23
#include "replay_gain.h"
25
#include "audio_format.h"
26
#include "pcm_volume.h"
33
static const char *const replay_gain_mode_names[] = {
34
[REPLAY_GAIN_ALBUM] = "album",
35
[REPLAY_GAIN_TRACK] = "track",
38
enum replay_gain_mode replay_gain_mode = REPLAY_GAIN_OFF;
40
static float replay_gain_preamp = 1.0;
42
void replay_gain_global_init(void)
44
const struct config_param *param = config_get_param(CONF_REPLAYGAIN);
49
if (strcmp(param->value, "track") == 0) {
50
replay_gain_mode = REPLAY_GAIN_TRACK;
51
} else if (strcmp(param->value, "album") == 0) {
52
replay_gain_mode = REPLAY_GAIN_ALBUM;
54
g_error("replaygain value \"%s\" at line %i is invalid\n",
55
param->value, param->line);
58
param = config_get_param(CONF_REPLAYGAIN_PREAMP);
62
float f = strtod(param->value, &test);
65
g_error("Replaygain preamp \"%s\" is not a number at "
66
"line %i\n", param->value, param->line);
69
if (f < -15 || f > 15) {
70
g_error("Replaygain preamp \"%s\" is not between -15 and"
71
"15 at line %i\n", param->value, param->line);
74
replay_gain_preamp = pow(10, f / 20.0);
78
static float calc_replay_gain_scale(float gain, float peak)
84
scale = pow(10.0, gain / 20.0);
85
scale *= replay_gain_preamp;
89
if (scale * peak > 1.0) {
95
struct replay_gain_info *replay_gain_info_new(void)
97
struct replay_gain_info *ret = g_new(struct replay_gain_info, 1);
99
for (unsigned i = 0; i < G_N_ELEMENTS(ret->tuples); ++i) {
100
ret->tuples[i].gain = 0.0;
101
ret->tuples[i].peak = 0.0;
104
/* set to -1 so that we know in replay_gain_apply to compute the scale */
110
void replay_gain_info_free(struct replay_gain_info *info)
116
replay_gain_apply(struct replay_gain_info *info, char *buffer, int size,
117
const struct audio_format *format)
119
if (replay_gain_mode == REPLAY_GAIN_OFF || !info)
122
if (info->scale < 0) {
123
const struct replay_gain_tuple *tuple =
124
&info->tuples[replay_gain_mode];
126
g_debug("computing ReplayGain %s scale with gain %f, peak %f\n",
127
replay_gain_mode_names[replay_gain_mode],
128
tuple->gain, tuple->peak);
130
info->scale = calc_replay_gain_scale(tuple->gain, tuple->peak);
133
pcm_volume(buffer, size, format, pcm_float_to_volume(info->scale));