VolumeFilterPlugin.cxx 2.24 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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 "VolumeFilterPlugin.hxx"
21 22
#include "filter/Filter.hxx"
#include "filter/Prepared.hxx"
23
#include "pcm/Volume.hxx"
24
#include "pcm/AudioFormat.hxx"
25
#include "util/ConstBuffer.hxx"
26

27
class VolumeFilter final : public Filter {
28
	PcmVolume pv;
29 30

public:
31
	explicit VolumeFilter(const AudioFormat &audio_format)
32
		:Filter(audio_format) {
33 34
		out_audio_format.format = pv.Open(out_audio_format.format,
						  true);
35 36
	}

37
	[[nodiscard]] unsigned GetVolume() const noexcept {
38
		return pv.GetVolume();
39 40
	}

41
	void SetVolume(unsigned _volume) noexcept {
42
		pv.SetVolume(_volume);
43 44
	}

45
	/* virtual methods from class Filter */
46
	ConstBuffer<void> FilterPCM(ConstBuffer<void> src) override;
47 48
};

49 50 51
class PreparedVolumeFilter final : public PreparedFilter {
public:
	/* virtual methods from class Filter */
52
	std::unique_ptr<Filter> Open(AudioFormat &af) override;
53 54
};

55
std::unique_ptr<Filter>
56
PreparedVolumeFilter::Open(AudioFormat &audio_format)
57
{
58
	return std::make_unique<VolumeFilter>(audio_format);
59 60
}

61
ConstBuffer<void>
62
VolumeFilter::FilterPCM(ConstBuffer<void> src)
63
{
64
	return pv.Apply(src);
65 66
}

67
std::unique_ptr<PreparedFilter>
68 69
volume_filter_prepare() noexcept
{
70
	return std::make_unique<PreparedVolumeFilter>();
71 72
}

73
unsigned
74
volume_filter_get(const Filter *_filter) noexcept
75
{
Max Kellermann's avatar
Max Kellermann committed
76
	const auto *filter =
77
		(const VolumeFilter *)_filter;
78

79
	return filter->GetVolume();
80 81 82
}

void
83
volume_filter_set(Filter *_filter, unsigned volume) noexcept
84
{
Max Kellermann's avatar
Max Kellermann committed
85
	auto *filter = (VolumeFilter *)_filter;
86

87
	filter->SetVolume(volume);
88
}