ReplayGainFilterPlugin.cxx 4.82 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 "pcm/PcmBuffer.hxx"
31
#include "util/ConstBuffer.hxx"
32 33
#include "util/Error.hxx"
#include "util/Domain.hxx"
34
#include "Log.hxx"
Max Kellermann's avatar
Max Kellermann committed
35

36
#include <assert.h>
37 38
#include <string.h>

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

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

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

54
	ReplayGainMode mode;
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 73

public:
	ReplayGainFilter()
74
		:mixer(nullptr), mode(REPLAY_GAIN_OFF) {
75
		info.Clear();
76 77
	}

78
	void SetMixer(Mixer *_mixer, unsigned _base) {
79
		assert(_mixer == nullptr || (_base > 0 && _base <= 100));
80 81 82 83 84 85 86

		mixer = _mixer;
		base = _base;

		Update();
	}

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

		Update();
	}

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

102 103 104
		FormatDebug(replay_gain_domain,
			    "replay gain mode has changed %d->%d\n",
			    mode, _mode);
105 106 107 108 109 110 111 112 113 114

		mode = _mode;
		Update();
	}

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

115
	virtual AudioFormat Open(AudioFormat &af, Error &error) override;
116
	virtual void Close();
117 118
	virtual ConstBuffer<void> FilterPCM(ConstBuffer<void> src,
					    Error &error) override;
119 120
};

121 122
void
ReplayGainFilter::Update()
123
{
124
	unsigned volume = PCM_VOLUME_1;
125
	if (mode != REPLAY_GAIN_OFF) {
126 127 128 129
		const auto &tuple = info.tuples[mode];
		float scale = tuple.CalculateScale(replay_gain_preamp,
						   replay_gain_missing_preamp,
						   replay_gain_limit);
130 131
		FormatDebug(replay_gain_domain,
			    "scale=%f\n", (double)scale);
132

133
		volume = pcm_float_to_volume(scale);
134 135 136
	}

	pv.SetVolume(volume);
137

138
	if (mixer != nullptr) {
139 140
		/* update the hardware mixer volume */

141 142 143
		unsigned _volume = (volume * base) / PCM_VOLUME_1;
		if (_volume > 100)
			_volume = 100;
144

145 146
		Error error;
		if (!mixer_set_volume(mixer, _volume, error))
147
			LogError(error, "Failed to update hardware mixer");
148
	}
149 150
}

151
static Filter *
152
replay_gain_filter_init(gcc_unused const config_param &param,
153
			gcc_unused Error &error)
154
{
155
	return new ReplayGainFilter();
156 157
}

158
AudioFormat
159
ReplayGainFilter::Open(AudioFormat &af, gcc_unused Error &error)
160
{
161 162
	if (!pv.Open(af.format, error))
		return AudioFormat::Undefined();
163

164
	return af;
165 166
}

167 168
void
ReplayGainFilter::Close()
169
{
170
	pv.Close();
171 172
}

173 174
ConstBuffer<void>
ReplayGainFilter::FilterPCM(ConstBuffer<void> src, gcc_unused Error &error)
175
{
176
	return pv.Apply(src);
177 178 179
}

const struct filter_plugin replay_gain_filter_plugin = {
180 181
	"replay_gain",
	replay_gain_filter_init,
182 183
};

184
void
185
replay_gain_filter_set_mixer(Filter *_filter, Mixer *mixer,
186 187
			     unsigned base)
{
188
	ReplayGainFilter *filter = (ReplayGainFilter *)_filter;
189

190
	filter->SetMixer(mixer, base);
191 192
}

193
void
194
replay_gain_filter_set_info(Filter *_filter, const ReplayGainInfo *info)
195
{
196
	ReplayGainFilter *filter = (ReplayGainFilter *)_filter;
197

198
	filter->SetInfo(info);
199
}
200 201

void
202
replay_gain_filter_set_mode(Filter *_filter, ReplayGainMode mode)
203
{
204
	ReplayGainFilter *filter = (ReplayGainFilter *)_filter;
205

206
	filter->SetMode(mode);
207
}