ReplayGainFilterPlugin.cxx 5.41 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
#include <assert.h>
36

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

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

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

52
	ReplayGainMode mode = REPLAY_GAIN_OFF;
53

54
	ReplayGainInfo info;
55 56

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

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

78 79
	bool Open(Error &error) {
		return pv.Open(out_audio_format.format, error);
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 112
	ConstBuffer<void> FilterPCM(ConstBuffer<void> src,
				    Error &error) override;
113 114
};

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
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 */
	Filter *Open(AudioFormat &af, Error &error) override;
};

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

152
		volume = pcm_float_to_volume(scale);
153 154
	}

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

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

162 163
		Error error;
		if (!mixer_set_volume(mixer, _volume, error))
164
			LogError(error, "Failed to update hardware mixer");
165 166
	} else
		pv.SetVolume(volume);
167 168
}

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

176 177
Filter *
PreparedReplayGainFilter::Open(AudioFormat &af, gcc_unused Error &error)
178
{
179 180 181 182 183
	auto *filter = new ReplayGainFilter(af, mixer, base);
	if (!filter->Open(error)) {
		delete filter;
		return nullptr;
	}
184

185
	return filter;
186 187
}

188 189
ConstBuffer<void>
ReplayGainFilter::FilterPCM(ConstBuffer<void> src, gcc_unused Error &error)
190
{
191 192 193
	return mixer != nullptr
		? src
		: pv.Apply(src);
194 195 196
}

const struct filter_plugin replay_gain_filter_plugin = {
197 198
	"replay_gain",
	replay_gain_filter_init,
199 200
};

201
void
202
replay_gain_filter_set_mixer(PreparedFilter *_filter, Mixer *mixer,
203 204
			     unsigned base)
{
205
	PreparedReplayGainFilter *filter = (PreparedReplayGainFilter *)_filter;
206

207
	filter->SetMixer(mixer, base);
208 209
}

210
void
211
replay_gain_filter_set_info(Filter *_filter, const ReplayGainInfo *info)
212
{
213
	ReplayGainFilter *filter = (ReplayGainFilter *)_filter;
214

215
	filter->SetInfo(info);
216
}
217 218

void
219
replay_gain_filter_set_mode(Filter *_filter, ReplayGainMode mode)
220
{
221
	ReplayGainFilter *filter = (ReplayGainFilter *)_filter;
222

223
	filter->SetMode(mode);
224
}