VolumeFilterPlugin.cxx 2.22 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "AudioFormat.hxx"
25
#include "util/ConstBuffer.hxx"
26 27

#include <stdexcept>
28

29
class VolumeFilter final : public Filter {
30
	PcmVolume pv;
31 32

public:
33
	explicit VolumeFilter(const AudioFormat &audio_format)
34
		:Filter(audio_format) {
35
		pv.Open(out_audio_format.format);
36 37
	}

38
	unsigned GetVolume() const noexcept {
39
		return pv.GetVolume();
40 41
	}

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

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

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

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

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

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

74
unsigned
75
volume_filter_get(const Filter *_filter) noexcept
76
{
77 78
	const VolumeFilter *filter =
		(const VolumeFilter *)_filter;
79

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

void
84
volume_filter_set(Filter *_filter, unsigned volume) noexcept
85
{
86
	VolumeFilter *filter = (VolumeFilter *)_filter;
87

88
	filter->SetVolume(volume);
89 90
}