ReplayGainFilterPlugin.cxx 5.19 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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
#include "filter/Filter.hxx"
#include "filter/Prepared.hxx"
24
#include "AudioFormat.hxx"
25
#include "ReplayGainInfo.hxx"
26
#include "ReplayGainConfig.hxx"
Max Kellermann's avatar
Max Kellermann committed
27
#include "mixer/MixerControl.hxx"
28
#include "pcm/Volume.hxx"
29
#include "util/ConstBuffer.hxx"
30
#include "util/Domain.hxx"
31
#include "Log.hxx"
Max Kellermann's avatar
Max Kellermann committed
32

33
#include <exception>
34

35
#include <assert.h>
36

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

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

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 = ReplayGainMode::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
	ReplayGainFilter(const ReplayGainConfig &_config,
			 const AudioFormat &audio_format,
75 76
			 Mixer *_mixer, unsigned _base)
		:Filter(audio_format),
77
		 config(_config),
78
		 mixer(_mixer), base(_base) {
79
		info.Clear();
80

81
		pv.Open(out_audio_format.format);
82 83
	}

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

		Update();
	}

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

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

		mode = _mode;
		Update();
	}

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

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

115
class PreparedReplayGainFilter final : public PreparedFilter {
116 117
	const ReplayGainConfig config;

118 119 120 121 122 123 124 125 126 127 128 129 130
	/**
	 * 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:
131 132 133
	explicit PreparedReplayGainFilter(const ReplayGainConfig _config)
		:config(_config) {}

134 135 136 137 138 139 140 141
	void SetMixer(Mixer *_mixer, unsigned _base) {
		assert(_mixer == nullptr || (_base > 0 && _base <= 100));

		mixer = _mixer;
		base = _base;
	}

	/* virtual methods from class Filter */
142
	std::unique_ptr<Filter> Open(AudioFormat &af) override;
143 144
};

145 146
void
ReplayGainFilter::Update()
147
{
148
	unsigned volume = PCM_VOLUME_1;
149
	if (mode != ReplayGainMode::OFF) {
150
		const auto &tuple = info.Get(mode);
151
		float scale = tuple.CalculateScale(config);
152 153
		FormatDebug(replay_gain_domain,
			    "scale=%f\n", (double)scale);
154

155
		volume = pcm_float_to_volume(scale);
156 157
	}

158
	if (mixer != nullptr) {
159 160
		/* update the hardware mixer volume */

161 162 163
		unsigned _volume = (volume * base) / PCM_VOLUME_1;
		if (_volume > 100)
			_volume = 100;
164

165 166
		try {
			mixer_set_volume(mixer, _volume);
167 168 169
		} catch (...) {
			LogError(std::current_exception(),
				 "Failed to update hardware mixer");
170
		}
171 172
	} else
		pv.SetVolume(volume);
173 174
}

175 176
std::unique_ptr<PreparedFilter>
NewReplayGainFilter(const ReplayGainConfig &config) noexcept
177
{
178
	return std::make_unique<PreparedReplayGainFilter>(config);
179 180
}

181
std::unique_ptr<Filter>
182
PreparedReplayGainFilter::Open(AudioFormat &af)
183
{
184
	return std::make_unique<ReplayGainFilter>(config, af, mixer, base);
185 186
}

187
ConstBuffer<void>
188
ReplayGainFilter::FilterPCM(ConstBuffer<void> src)
189
{
190 191 192
	return mixer != nullptr
		? src
		: pv.Apply(src);
193 194
}

195
void
196
replay_gain_filter_set_mixer(PreparedFilter &_filter, Mixer *mixer,
197 198
			     unsigned base)
{
199
	auto &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
	auto &filter = (ReplayGainFilter &)_filter;
208

209
	filter.SetInfo(info);
210
}
211 212

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

217
	filter.SetMode(mode);
218
}