ReplayGainFilterPlugin.cxx 5.76 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20
#include "ReplayGainFilterPlugin.hxx"
21 22
#include "filter/Filter.hxx"
#include "filter/Prepared.hxx"
23
#include "ReplayGainInfo.hxx"
24
#include "ReplayGainConfig.hxx"
Max Kellermann's avatar
Max Kellermann committed
25
#include "mixer/MixerControl.hxx"
26 27
#include "mixer/MixerInternal.hxx"
#include "mixer/Listener.hxx"
28
#include "pcm/AudioFormat.hxx"
29
#include "pcm/Volume.hxx"
30
#include "util/ConstBuffer.hxx"
31
#include "util/Domain.hxx"
32
#include "Idle.hxx"
33
#include "Log.hxx"
Max Kellermann's avatar
Max Kellermann committed
34

35
#include <cassert>
36
#include <exception>
37

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

40
class ReplayGainFilter final : public Filter {
41 42
	const ReplayGainConfig config;

43 44 45 46
	/**
	 * If set, then this hardware mixer is used for applying
	 * replay gain, instead of the software volume library.
	 */
47
	Mixer *const mixer;
48 49 50 51 52

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

55
	ReplayGainMode mode = ReplayGainMode::OFF;
56

57
	ReplayGainInfo info;
58 59

	/**
60 61
	 * About the current volume: it is between 0 and a value that
	 * may or may not exceed #PCM_VOLUME_1.
62 63 64 65 66 67 68 69
	 *
	 * 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.
70
	 */
71
	PcmVolume pv;
72 73

public:
74
	ReplayGainFilter(const ReplayGainConfig &_config, bool allow_convert,
75
			 const AudioFormat &audio_format,
76 77
			 Mixer *_mixer, unsigned _base)
		:Filter(audio_format),
78
		 config(_config),
79
		 mixer(_mixer), base(_base) {
80
		info.Clear();
81

82 83
		out_audio_format.format = pv.Open(out_audio_format.format,
						  allow_convert);
84 85
	}

86
	void SetInfo(const ReplayGainInfo *_info) {
87
		if (_info != nullptr)
88
			info = *_info;
89
		else
90
			info.Clear();
91 92 93 94

		Update();
	}

95
	void SetMode(ReplayGainMode _mode) {
96 97 98 99
		if (_mode == mode)
			/* no change */
			return;

100 101 102
		FmtDebug(replay_gain_domain,
			 "replay gain mode has changed {}->{}",
			 ToString(mode), ToString(_mode));
103 104 105 106 107 108 109 110 111 112

		mode = _mode;
		Update();
	}

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

113
	/* virtual methods from class Filter */
114
	ConstBuffer<void> FilterPCM(ConstBuffer<void> src) override;
115 116
};

117
class PreparedReplayGainFilter final : public PreparedFilter {
118 119
	const ReplayGainConfig config;

120 121 122 123 124 125
	/**
	 * If set, then this hardware mixer is used for applying
	 * replay gain, instead of the software volume library.
	 */
	Mixer *mixer = nullptr;

126 127 128 129 130 131
	/**
	 * Allow the class to convert to a different #SampleFormat to
	 * preserve quality?
	 */
	const bool allow_convert;

132 133 134 135 136 137 138
	/**
	 * The base volume level for scale=1.0, between 1 and 100
	 * (including).
	 */
	unsigned base;

public:
139 140 141
	explicit PreparedReplayGainFilter(const ReplayGainConfig _config,
					  bool _allow_convert)
		:config(_config), allow_convert(_allow_convert) {}
142

143 144 145 146 147 148 149 150
	void SetMixer(Mixer *_mixer, unsigned _base) {
		assert(_mixer == nullptr || (_base > 0 && _base <= 100));

		mixer = _mixer;
		base = _base;
	}

	/* virtual methods from class Filter */
151
	std::unique_ptr<Filter> Open(AudioFormat &af) override;
152 153
};

154 155
void
ReplayGainFilter::Update()
156
{
157
	unsigned volume = PCM_VOLUME_1;
158
	if (mode != ReplayGainMode::OFF) {
159
		const auto &tuple = info.Get(mode);
160
		float scale = tuple.CalculateScale(config);
161
		FmtDebug(replay_gain_domain, "scale={}\n", scale);
162

163
		volume = pcm_float_to_volume(scale);
164 165
	}

166
	if (mixer != nullptr) {
167 168
		/* update the hardware mixer volume */

169 170 171
		unsigned _volume = (volume * base) / PCM_VOLUME_1;
		if (_volume > 100)
			_volume = 100;
172

173 174
		try {
			mixer_set_volume(mixer, _volume);
175

176 177 178 179 180
			/* invoke the mixer's listener manually, just
			   in case the mixer implementation didn't do
			   that already (this depends on the
			   implementation) */
			mixer->listener.OnMixerVolumeChanged(*mixer, _volume);
181 182 183
		} catch (...) {
			LogError(std::current_exception(),
				 "Failed to update hardware mixer");
184
		}
185 186
	} else
		pv.SetVolume(volume);
187 188
}

189
std::unique_ptr<PreparedFilter>
190 191
NewReplayGainFilter(const ReplayGainConfig &config,
		    bool allow_convert) noexcept
192
{
193 194
	return std::make_unique<PreparedReplayGainFilter>(config,
							  allow_convert);
195 196
}

197
std::unique_ptr<Filter>
198
PreparedReplayGainFilter::Open(AudioFormat &af)
199
{
200 201
	return std::make_unique<ReplayGainFilter>(config, allow_convert,
						  af, mixer, base);
202 203
}

204
ConstBuffer<void>
205
ReplayGainFilter::FilterPCM(ConstBuffer<void> src)
206
{
207 208 209
	return mixer != nullptr
		? src
		: pv.Apply(src);
210 211
}

212
void
213
replay_gain_filter_set_mixer(PreparedFilter &_filter, Mixer *mixer,
214 215
			     unsigned base)
{
216
	auto &filter = (PreparedReplayGainFilter &)_filter;
217

218
	filter.SetMixer(mixer, base);
219 220
}

221
void
222
replay_gain_filter_set_info(Filter &_filter, const ReplayGainInfo *info)
223
{
224
	auto &filter = (ReplayGainFilter &)_filter;
225

226
	filter.SetInfo(info);
227
}
228 229

void
230
replay_gain_filter_set_mode(Filter &_filter, ReplayGainMode mode)
231
{
232
	auto &filter = (ReplayGainFilter &)_filter;
233

234
	filter.SetMode(mode);
235
}