VolumeFilterPlugin.cxx 2.41 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
 * 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 "util/Error.hxx"
#include "util/Domain.hxx"
30 31 32 33

#include <assert.h>
#include <string.h>

34
class VolumeFilter final : public Filter {
35
	PcmVolume pv;
36 37 38

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

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

46
	/* virtual methods from class Filter */
47 48
	AudioFormat Open(AudioFormat &af, Error &error) override;
	void Close() override;
49 50
	ConstBuffer<void> FilterPCM(ConstBuffer<void> src,
				    Error &error) override;
51 52
};

53
static constexpr Domain volume_domain("pcm_volume");
54

55
static Filter *
56
volume_filter_init(gcc_unused const ConfigBlock &block,
57
		   gcc_unused Error &error)
58
{
59
	return new VolumeFilter();
60 61
}

62
AudioFormat
63
VolumeFilter::Open(AudioFormat &audio_format, Error &error)
64
{
65 66
	if (!pv.Open(audio_format.format, error))
		return AudioFormat::Undefined();
67

68
	return audio_format;
69 70
}

71 72
void
VolumeFilter::Close()
73
{
74
	pv.Close();
75 76
}

77 78
ConstBuffer<void>
VolumeFilter::FilterPCM(ConstBuffer<void> src, gcc_unused Error &error)
79
{
80
	return pv.Apply(src);
81 82 83
}

const struct filter_plugin volume_filter_plugin = {
84 85
	"volume",
	volume_filter_init,
86
};
87 88

unsigned
89
volume_filter_get(const Filter *_filter)
90
{
91 92
	const VolumeFilter *filter =
		(const VolumeFilter *)_filter;
93

94
	return filter->GetVolume();
95 96 97
}

void
98
volume_filter_set(Filter *_filter, unsigned volume)
99
{
100
	VolumeFilter *filter = (VolumeFilter *)_filter;
101

102
	filter->SetVolume(volume);
103 104
}