ReplayGainFilterPlugin.cxx 4.85 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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 116 117
	/* virtual methods from class Filter */
	AudioFormat Open(AudioFormat &af, Error &error) override;
	void Close() override;
118 119
	ConstBuffer<void> FilterPCM(ConstBuffer<void> src,
				    Error &error) override;
120 121
};

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

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

	pv.SetVolume(volume);
138

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

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

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

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

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

165
	return af;
166 167
}

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

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

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

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

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

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

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

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

207
	filter->SetMode(mode);
208
}