VolumeFilterPlugin.cxx 2.3 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 24
#include "filter/FilterPlugin.hxx"
#include "filter/FilterInternal.hxx"
#include "filter/FilterRegistry.hxx"
25
#include "pcm/Volume.hxx"
26
#include "AudioFormat.hxx"
27
#include "util/ConstBuffer.hxx"
28 29

#include <stdexcept>
30

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

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

40
	unsigned GetVolume() const {
41
		return pv.GetVolume();
42 43 44
	}

	void SetVolume(unsigned _volume) {
45
		pv.SetVolume(_volume);
46 47
	}

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

52 53 54 55 56
class PreparedVolumeFilter final : public PreparedFilter {
	PcmVolume pv;

public:
	/* virtual methods from class Filter */
57
	Filter *Open(AudioFormat &af) override;
58 59 60
};

static PreparedFilter *
61
volume_filter_init(gcc_unused const ConfigBlock &block)
62
{
63
	return new PreparedVolumeFilter();
64 65
}

66
Filter *
67
PreparedVolumeFilter::Open(AudioFormat &audio_format)
68
{
69
	return new VolumeFilter(audio_format);
70 71
}

72
ConstBuffer<void>
73
VolumeFilter::FilterPCM(ConstBuffer<void> src)
74
{
75
	return pv.Apply(src);
76 77
}

78
const FilterPlugin volume_filter_plugin = {
79 80
	"volume",
	volume_filter_init,
81
};
82 83

unsigned
84
volume_filter_get(const Filter *_filter)
85
{
86 87
	const VolumeFilter *filter =
		(const VolumeFilter *)_filter;
88

89
	return filter->GetVolume();
90 91 92
}

void
93
volume_filter_set(Filter *_filter, unsigned volume)
94
{
95
	VolumeFilter *filter = (VolumeFilter *)_filter;
96

97
	filter->SetVolume(volume);
98 99
}