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

#include <stdexcept>
29

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

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

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

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

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

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

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

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

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

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

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

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

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