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"
|
2006-08-26 08:25:57 +02:00
|
|
|
#include "utils.h"
|
2004-05-08 00:42:54 +02:00
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
#include "conf.h"
|
2008-09-07 19:19:55 +02:00
|
|
|
#include "audio_format.h"
|
2008-01-03 08:29:49 +01:00
|
|
|
#include "os_compat.h"
|
2006-07-20 18:02:40 +02:00
|
|
|
|
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
|
|
|
{
|
|
|
|
ConfigParam *param = getConfigParam(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 {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("replaygain value \"%s\" at line %i is invalid\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
param->value, param->line);
|
2004-05-08 00:42:54 +02:00
|
|
|
}
|
2004-07-17 16:54:22 +02:00
|
|
|
|
2004-10-28 07:14:55 +02:00
|
|
|
param = getConfigParam(CONF_REPLAYGAIN_PREAMP);
|
|
|
|
|
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') {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("Replaygain preamp \"%s\" is not a number at "
|
2006-07-20 18:02:40 +02:00
|
|
|
"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) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("Replaygain preamp \"%s\" is not between -15 and"
|
2006-07-20 18:02:40 +02:00
|
|
|
"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 15:55:34 +01:00
|
|
|
struct replay_gain_info *ret = xmalloc(sizeof(*ret));
|
2004-11-02 20:56:59 +01:00
|
|
|
|
2008-11-11 15:55:34 +01:00
|
|
|
ret->album_gain = 0.0;
|
|
|
|
ret->album_peak = 0.0;
|
2004-11-02 20:56:59 +01:00
|
|
|
|
2008-11-11 15:55:34 +01:00
|
|
|
ret->track_gain = 0.0;
|
|
|
|
ret->track_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
|
|
|
{
|
2004-11-02 20:56:59 +01:00
|
|
|
free(info);
|
|
|
|
}
|
|
|
|
|
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-09-29 13:29:33 +02:00
|
|
|
int16_t *buffer16;
|
|
|
|
int8_t *buffer8;
|
|
|
|
int32_t temp32;
|
2004-11-02 20:56:59 +01:00
|
|
|
float scale;
|
|
|
|
|
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 15:55:34 +01:00
|
|
|
switch (replay_gain_mode) {
|
|
|
|
case REPLAY_GAIN_TRACK:
|
2007-06-25 16:34:40 +02:00
|
|
|
DEBUG("computing ReplayGain track scale with gain %f, "
|
2008-11-11 15:55:34 +01:00
|
|
|
"peak %f\n", info->track_gain, info->track_peak);
|
|
|
|
info->scale = calc_replay_gain_scale(info->track_gain,
|
|
|
|
info->track_peak);
|
2004-11-02 20:56:59 +01:00
|
|
|
break;
|
|
|
|
default:
|
2007-06-25 16:34:40 +02:00
|
|
|
DEBUG("computing ReplayGain album scale with gain %f, "
|
2008-11-11 15:55:34 +01:00
|
|
|
"peak %f\n", info->album_gain, info->album_peak);
|
|
|
|
info->scale = calc_replay_gain_scale(info->album_gain,
|
|
|
|
info->album_peak);
|
2004-11-02 20:56:59 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (info->scale <= 1.01 && info->scale >= 0.99)
|
|
|
|
return;
|
2004-11-02 20:56:59 +01:00
|
|
|
|
2008-09-29 13:29:33 +02:00
|
|
|
buffer16 = (int16_t *) buffer;
|
|
|
|
buffer8 = (int8_t *) buffer;
|
2004-11-02 20:56:59 +01:00
|
|
|
|
|
|
|
scale = info->scale;
|
2004-05-08 00:42:54 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
switch (format->bits) {
|
|
|
|
case 16:
|
2008-11-11 15:55:34 +01:00
|
|
|
while (size > 0) {
|
2006-07-20 18:02:40 +02:00
|
|
|
temp32 = *buffer16;
|
|
|
|
temp32 *= scale;
|
|
|
|
*buffer16 = temp32 > 32767 ? 32767 :
|
|
|
|
(temp32 < -32768 ? -32768 : temp32);
|
|
|
|
buffer16++;
|
2008-11-11 15:55:34 +01:00
|
|
|
size -= 2;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 8:
|
2008-11-11 15:55:34 +01:00
|
|
|
while (size > 0) {
|
2006-07-20 18:02:40 +02:00
|
|
|
temp32 = *buffer8;
|
|
|
|
temp32 *= scale;
|
|
|
|
*buffer8 = temp32 > 127 ? 127 :
|
|
|
|
(temp32 < -128 ? -128 : temp32);
|
|
|
|
buffer8++;
|
2008-11-11 15:55:34 +01:00
|
|
|
size--;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERROR("%i bits not supported by doReplaygain!\n", format->bits);
|
2004-05-08 00:42:54 +02:00
|
|
|
}
|
|
|
|
}
|