ReplayGainFilterPlugin.cxx 5.08 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
#include "filter/FilterInternal.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 33
#include <stdexcept>

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
	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 166 167 168
		try {
			mixer_set_volume(mixer, _volume);
		} catch (const std::runtime_error &e) {
			LogError(e, "Failed to update hardware mixer");
		}
169 170
	} else
		pv.SetVolume(volume);
171 172
}

173
PreparedFilter *
174
NewReplayGainFilter(const ReplayGainConfig &config)
175
{
176
	return new PreparedReplayGainFilter(config);
177 178
}

179
Filter *
180
PreparedReplayGainFilter::Open(AudioFormat &af)
181
{
182
	return new ReplayGainFilter(config, af, mixer, base);
183 184
}

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

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

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

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

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

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

215
	filter.SetMode(mode);
216
}