2004-05-08 14:04:23 +02:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2004-05-08 14:04:23 +02:00
|
|
|
* (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
|
|
|
|
*/
|
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;
|
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
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!param)
|
|
|
|
return;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
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
|
|
|
}
|
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
|
|
|
{
|
2008-11-11 15:55:34 +01:00
|
|
|
if (replay_gain_mode == REPLAY_GAIN_OFF || !info)
|
2006-07-20 18:02:40 +02:00
|
|
|
return;
|
2004-11-02 20:56:59 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (info->scale < 0) {
|
2008-11-11 16:24:27 +01:00
|
|
|
const struct replay_gain_tuple *tuple =
|
|
|
|
&info->tuples[replay_gain_mode];
|
|
|
|
|
2008-11-11 16:38:26 +01: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
|
|
|
|
|
|
|
info->scale = calc_replay_gain_scale(tuple->gain, tuple->peak);
|
2004-11-02 20:56:59 +01:00
|
|
|
}
|
|
|
|
|
2008-11-11 16:38:12 +01:00
|
|
|
pcm_volume(buffer, size, format, pcm_float_to_volume(info->scale));
|
2004-05-08 00:42:54 +02:00
|
|
|
}
|