SoftwareMixerPlugin.cxx 2.66 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 "SoftwareMixerPlugin.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "mixer/MixerInternal.hxx"
23 24 25 26
#include "filter/FilterPlugin.hxx"
#include "filter/FilterRegistry.hxx"
#include "filter/FilterInternal.hxx"
#include "filter/plugins/VolumeFilterPlugin.hxx"
27
#include "pcm/Volume.hxx"
28
#include "config/Block.hxx"
29 30 31 32

#include <assert.h>
#include <math.h>

33
class SoftwareMixer final : public Mixer {
34
	Filter *filter = nullptr;
35

36 37 38
	/**
	 * The current volume in percent (0..100).
	 */
39
	unsigned volume = 100;
40

41
public:
42
	SoftwareMixer(MixerListener &_listener)
43
		:Mixer(software_mixer_plugin, _listener)
44 45
	{
	}
46

47
	void SetFilter(Filter *_filter);
48 49

	/* virtual methods from class Mixer */
50
	void Open() override {
51 52 53 54 55
	}

	virtual void Close() override {
	}

56
	int GetVolume() override {
57 58 59
		return volume;
	}

60
	void SetVolume(unsigned volume) override;
61 62
};

63
static Mixer *
64 65
software_mixer_init(gcc_unused EventLoop &event_loop,
		    gcc_unused AudioOutput &ao,
66
		    MixerListener &listener,
67
		    gcc_unused const ConfigBlock &block)
68
{
69
	return new SoftwareMixer(listener);
70 71
}

72 73
gcc_const
static unsigned
74
PercentVolumeToSoftwareVolume(unsigned volume) noexcept
75 76 77 78 79 80 81 82 83 84 85 86
{
	assert(volume <= 100);

	if (volume >= 100)
		return PCM_VOLUME_1;
	else if (volume > 0)
		return pcm_float_to_volume((exp(volume / 25.0) - 1) /
					   (54.5981500331F - 1));
	else
		return 0;
}

87 88
void
SoftwareMixer::SetVolume(unsigned new_volume)
89
{
90
	assert(new_volume <= 100);
91

92
	volume = new_volume;
93 94 95

	if (filter != nullptr)
		volume_filter_set(filter, PercentVolumeToSoftwareVolume(new_volume));
96 97
}

98
const MixerPlugin software_mixer_plugin = {
99 100
	software_mixer_init,
	true,
101 102
};

103 104
inline void
SoftwareMixer::SetFilter(Filter *_filter)
105
{
106
	filter = _filter;
107

108 109 110
	if (filter != nullptr)
		volume_filter_set(filter,
				  PercentVolumeToSoftwareVolume(volume));
111 112
}

113 114
void
software_mixer_set_filter(Mixer &mixer, Filter *filter)
115
{
116 117
	SoftwareMixer &sm = (SoftwareMixer &)mixer;
	sm.SetFilter(filter);
118
}