ReplayGainFilterPlugin.cxx 5.21 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"
21
#include "ReplayGainFilterPlugin.hxx"
22 23 24
#include "filter/FilterPlugin.hxx"
#include "filter/FilterInternal.hxx"
#include "filter/FilterRegistry.hxx"
25
#include "AudioFormat.hxx"
26 27
#include "ReplayGainInfo.hxx"
#include "ReplayGainConfig.hxx"
Max Kellermann's avatar
Max Kellermann committed
28
#include "mixer/MixerControl.hxx"
29
#include "pcm/Volume.hxx"
30
#include "util/ConstBuffer.hxx"
31 32
#include "util/Error.hxx"
#include "util/Domain.hxx"
33
#include "Log.hxx"
Max Kellermann's avatar
Max Kellermann committed
34

35 36
#include <stdexcept>

37
#include <assert.h>
38

39
static constexpr Domain replay_gain_domain("replay_gain");
40

41
class ReplayGainFilter final : public Filter {
42 43 44 45
	/**
	 * If set, then this hardware mixer is used for applying
	 * replay gain, instead of the software volume library.
	 */
46
	Mixer *const mixer;
47 48 49 50 51

	/**
	 * The base volume level for scale=1.0, between 1 and 100
	 * (including).
	 */
52
	const unsigned base;
53

54
	ReplayGainMode mode = REPLAY_GAIN_OFF;
55

56
	ReplayGainInfo info;
57 58

	/**
59 60
	 * About the current volume: it is between 0 and a value that
	 * may or may not exceed #PCM_VOLUME_1.
61 62 63 64 65 66 67 68
	 *
	 * If the default value of true is used for replaygain_limit, the
	 * application of the volume to the signal will never cause clipping.
	 *
	 * On the other hand, if the user has set replaygain_limit to false,
	 * the chance of clipping is explicitly preferred if that's required to
	 * maintain a consistent audio level. Whether clipping will actually
	 * occur depends on what value the user is using for replaygain_preamp.
69
	 */
70
	PcmVolume pv;
71 72

public:
73 74 75 76
	ReplayGainFilter(const AudioFormat &audio_format,
			 Mixer *_mixer, unsigned _base)
		:Filter(audio_format),
		 mixer(_mixer), base(_base), mode(REPLAY_GAIN_OFF) {
77
		info.Clear();
78

79
		pv.Open(out_audio_format.format);
80 81
	}

82
	void SetInfo(const ReplayGainInfo *_info) {
83
		if (_info != nullptr) {
84
			info = *_info;
85
			info.Complete();
86
		} else
87
			info.Clear();
88 89 90 91

		Update();
	}

92
	void SetMode(ReplayGainMode _mode) {
93 94 95 96
		if (_mode == mode)
			/* no change */
			return;

97 98 99
		FormatDebug(replay_gain_domain,
			    "replay gain mode has changed %d->%d\n",
			    mode, _mode);
100 101 102 103 104 105 106 107 108 109

		mode = _mode;
		Update();
	}

	/**
	 * Recalculates the new volume after a property was changed.
	 */
	void Update();

110
	/* virtual methods from class Filter */
111
	ConstBuffer<void> FilterPCM(ConstBuffer<void> src) override;
112 113
};

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
class PreparedReplayGainFilter final : public PreparedFilter {
	/**
	 * If set, then this hardware mixer is used for applying
	 * replay gain, instead of the software volume library.
	 */
	Mixer *mixer = nullptr;

	/**
	 * The base volume level for scale=1.0, between 1 and 100
	 * (including).
	 */
	unsigned base;

public:
	void SetMixer(Mixer *_mixer, unsigned _base) {
		assert(_mixer == nullptr || (_base > 0 && _base <= 100));

		mixer = _mixer;
		base = _base;
	}

	/* virtual methods from class Filter */
136
	Filter *Open(AudioFormat &af) override;
137 138
};

139 140
void
ReplayGainFilter::Update()
141
{
142
	unsigned volume = PCM_VOLUME_1;
143
	if (mode != REPLAY_GAIN_OFF) {
144 145 146 147
		const auto &tuple = info.tuples[mode];
		float scale = tuple.CalculateScale(replay_gain_preamp,
						   replay_gain_missing_preamp,
						   replay_gain_limit);
148 149
		FormatDebug(replay_gain_domain,
			    "scale=%f\n", (double)scale);
150

151
		volume = pcm_float_to_volume(scale);
152 153
	}

154
	if (mixer != nullptr) {
155 156
		/* update the hardware mixer volume */

157 158 159
		unsigned _volume = (volume * base) / PCM_VOLUME_1;
		if (_volume > 100)
			_volume = 100;
160

161 162 163 164 165
		try {
			mixer_set_volume(mixer, _volume);
		} catch (const std::runtime_error &e) {
			LogError(e, "Failed to update hardware mixer");
		}
166 167
	} else
		pv.SetVolume(volume);
168 169
}

170
static PreparedFilter *
171
replay_gain_filter_init(gcc_unused const ConfigBlock &block)
172
{
173
	return new PreparedReplayGainFilter();
174 175
}

176
Filter *
177
PreparedReplayGainFilter::Open(AudioFormat &af)
178
{
179
	return new ReplayGainFilter(af, mixer, base);
180 181
}

182
ConstBuffer<void>
183
ReplayGainFilter::FilterPCM(ConstBuffer<void> src)
184
{
185 186 187
	return mixer != nullptr
		? src
		: pv.Apply(src);
188 189 190
}

const struct filter_plugin replay_gain_filter_plugin = {
191 192
	"replay_gain",
	replay_gain_filter_init,
193 194
};

195
void
196
replay_gain_filter_set_mixer(PreparedFilter *_filter, Mixer *mixer,
197 198
			     unsigned base)
{
199
	PreparedReplayGainFilter *filter = (PreparedReplayGainFilter *)_filter;
200

201
	filter->SetMixer(mixer, base);
202 203
}

204
void
205
replay_gain_filter_set_info(Filter *_filter, const ReplayGainInfo *info)
206
{
207
	ReplayGainFilter *filter = (ReplayGainFilter *)_filter;
208

209
	filter->SetInfo(info);
210
}
211 212

void
213
replay_gain_filter_set_mode(Filter *_filter, ReplayGainMode mode)
214
{
215
	ReplayGainFilter *filter = (ReplayGainFilter *)_filter;
216

217
	filter->SetMode(mode);
218
}