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

32
#include <exception>
33

34
#include <assert.h>
35

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

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

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

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

53
	ReplayGainMode mode = ReplayGainMode::OFF;
54

55
	ReplayGainInfo info;
56 57

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

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

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

83
	void SetInfo(const ReplayGainInfo *_info) {
84
		if (_info != nullptr)
85
			info = *_info;
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
		FormatDebug(replay_gain_domain,
98 99
			    "replay gain mode has changed %s->%s\n",
			    ToString(mode), ToString(_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
class PreparedReplayGainFilter final : public PreparedFilter {
115 116
	const ReplayGainConfig config;

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

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

		mixer = _mixer;
		base = _base;
	}

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

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

154
		volume = pcm_float_to_volume(scale);
155 156
	}

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

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

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

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

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

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

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

200
	filter.SetMixer(mixer, base);
201 202
}

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

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

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

216
	filter.SetMode(mode);
217
}