2009-03-13 18:43:16 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2009 The Music Player Daemon Project
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* (c)2004 replayGain code by AliasMrJones
|
2004-05-08 14:04:23 +02:00
|
|
|
*/
|
2004-05-08 00:42:54 +02:00
|
|
|
|
2008-11-11 15:55:34 +01:00
|
|
|
#include "replay_gain.h"
|
2004-05-08 00:42:54 +02:00
|
|
|
#include "conf.h"
|
2008-09-07 19:19:55 +02:00
|
|
|
#include "audio_format.h"
|
2009-01-07 18:05:38 +01:00
|
|
|
#include "pcm_volume.h"
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-11-11 16:24:27 +01:00
|
|
|
#include <glib.h>
|
2008-11-11 16:38:33 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
2008-11-11 16:24:27 +01:00
|
|
|
|
|
|
|
static const char *const replay_gain_mode_names[] = {
|
|
|
|
[REPLAY_GAIN_ALBUM] = "album",
|
|
|
|
[REPLAY_GAIN_TRACK] = "track",
|
|
|
|
};
|
|
|
|
|
2008-11-11 16:18:31 +01:00
|
|
|
enum replay_gain_mode replay_gain_mode = REPLAY_GAIN_OFF;
|
2004-05-08 00:42:54 +02:00
|
|
|
|
2008-11-11 15:55:34 +01:00
|
|
|
static float replay_gain_preamp = 1.0;
|
2009-06-19 09:02:12 +02:00
|
|
|
static float replay_gain_missing_preamp = 1.0;
|
2004-07-17 16:54:22 +02:00
|
|
|
|
2008-11-11 15:55:34 +01:00
|
|
|
void replay_gain_global_init(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-01-25 16:03:49 +01:00
|
|
|
const struct config_param *param = config_get_param(CONF_REPLAYGAIN);
|
2004-05-08 00:42:54 +02:00
|
|
|
|
2009-10-17 21:38:45 +02:00
|
|
|
if (param == NULL || strcmp(param->value, "off") == 0) {
|
2009-10-17 21:38:32 +02:00
|
|
|
replay_gain_mode = REPLAY_GAIN_OFF;
|
|
|
|
} else if (strcmp(param->value, "track") == 0) {
|
2008-11-11 15:55:34 +01:00
|
|
|
replay_gain_mode = REPLAY_GAIN_TRACK;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (strcmp(param->value, "album") == 0) {
|
2008-11-11 15:55:34 +01:00
|
|
|
replay_gain_mode = REPLAY_GAIN_ALBUM;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2008-11-11 16:38:26 +01:00
|
|
|
g_error("replaygain value \"%s\" at line %i is invalid\n",
|
|
|
|
param->value, 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;
|
2004-10-28 07:14:55 +02:00
|
|
|
float f = strtod(param->value, &test);
|
2004-07-17 16:54:22 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (*test != '\0') {
|
2008-11-11 16:38:26 +01:00
|
|
|
g_error("Replaygain preamp \"%s\" is not a number at "
|
|
|
|
"line %i\n", param->value, param->line);
|
2004-07-17 16:54:22 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (f < -15 || f > 15) {
|
2008-11-11 16:38:26 +01:00
|
|
|
g_error("Replaygain preamp \"%s\" is not between -15 and"
|
|
|
|
"15 at line %i\n", param->value, 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;
|
|
|
|
float f = strtod(param->value, &test);
|
|
|
|
|
|
|
|
if (*test != '\0') {
|
|
|
|
g_error("Replaygain missing preamp \"%s\" is not a number at "
|
|
|
|
"line %i\n", param->value, param->line);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f < -15 || f > 15) {
|
|
|
|
g_error("Replaygain missing preamp \"%s\" is not between -15 and"
|
|
|
|
"15 at line %i\n", param->value, param->line);
|
|
|
|
}
|
|
|
|
|
|
|
|
replay_gain_missing_preamp = pow(10, f / 20.0);
|
|
|
|
}
|
2004-05-08 00:42:54 +02:00
|
|
|
}
|
|
|
|
|
2008-11-11 15:55:34 +01:00
|
|
|
static float calc_replay_gain_scale(float gain, float peak)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-05-08 00:42:54 +02:00
|
|
|
float scale;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (gain == 0.0)
|
|
|
|
return (1);
|
|
|
|
scale = pow(10.0, gain / 20.0);
|
2008-11-11 15:55:34 +01:00
|
|
|
scale *= replay_gain_preamp;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (scale > 15.0)
|
|
|
|
scale = 15.0;
|
2004-05-08 00:42:54 +02:00
|
|
|
|
|
|
|
if (scale * peak > 1.0) {
|
|
|
|
scale = 1.0 / peak;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
return (scale);
|
2004-05-08 00:42:54 +02:00
|
|
|
}
|
|
|
|
|
2008-11-11 15:55:34 +01:00
|
|
|
struct replay_gain_info *replay_gain_info_new(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-11-11 16:38:26 +01:00
|
|
|
struct replay_gain_info *ret = g_new(struct replay_gain_info, 1);
|
2004-11-02 20:56:59 +01:00
|
|
|
|
2008-11-11 16:24:27 +01:00
|
|
|
for (unsigned i = 0; i < G_N_ELEMENTS(ret->tuples); ++i) {
|
|
|
|
ret->tuples[i].gain = 0.0;
|
|
|
|
ret->tuples[i].peak = 0.0;
|
|
|
|
}
|
2004-11-02 20:56:59 +01:00
|
|
|
|
2008-11-11 15:55:34 +01:00
|
|
|
/* set to -1 so that we know in replay_gain_apply to compute the scale */
|
2004-11-02 20:56:59 +01:00
|
|
|
ret->scale = -1.0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-11 15:55:34 +01:00
|
|
|
void replay_gain_info_free(struct replay_gain_info *info)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-11-11 16:38:26 +01:00
|
|
|
g_free(info);
|
2004-11-02 20:56:59 +01:00
|
|
|
}
|
|
|
|
|
2008-11-11 15:55:34 +01:00
|
|
|
void
|
|
|
|
replay_gain_apply(struct replay_gain_info *info, char *buffer, int size,
|
2008-09-07 19:19:55 +02:00
|
|
|
const struct audio_format *format)
|
2004-05-08 00:42:54 +02:00
|
|
|
{
|
2009-06-19 09:02:12 +02:00
|
|
|
float scale;
|
|
|
|
|
|
|
|
if (replay_gain_mode == REPLAY_GAIN_OFF)
|
2006-07-20 18:02:40 +02:00
|
|
|
return;
|
2004-11-02 20:56:59 +01:00
|
|
|
|
2009-06-19 09:02:12 +02:00
|
|
|
if (info) {
|
|
|
|
if (info->scale < 0) {
|
|
|
|
const struct replay_gain_tuple *tuple =
|
|
|
|
&info->tuples[replay_gain_mode];
|
2008-11-11 16:24:27 +01:00
|
|
|
|
2009-06-19 09:02:12 +02:00
|
|
|
g_debug("computing ReplayGain %s scale with gain %f, peak %f\n",
|
|
|
|
replay_gain_mode_names[replay_gain_mode],
|
|
|
|
tuple->gain, tuple->peak);
|
2008-11-11 16:24:27 +01:00
|
|
|
|
2009-06-19 09:02:12 +02:00
|
|
|
info->scale = calc_replay_gain_scale(tuple->gain, tuple->peak);
|
|
|
|
}
|
|
|
|
scale = info->scale;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
scale = replay_gain_missing_preamp;
|
|
|
|
g_debug("ReplayGain is missing, computing scale %f\n", scale);
|
2004-11-02 20:56:59 +01:00
|
|
|
}
|
|
|
|
|
2009-06-19 09:02:12 +02:00
|
|
|
pcm_volume(buffer, size, format, pcm_float_to_volume(scale));
|
2004-05-08 00:42:54 +02:00
|
|
|
}
|