SoftwareMixerPlugin.cxx 2.54 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 "SoftwareMixerPlugin.hxx"
Max Kellermann's avatar
Max Kellermann committed
21
#include "mixer/MixerInternal.hxx"
22
#include "filter/plugins/VolumeFilterPlugin.hxx"
23
#include "pcm/Volume.hxx"
24 25 26 27

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

28
class SoftwareMixer final : public Mixer {
29
	Filter *filter = nullptr;
30

31 32 33
	/**
	 * The current volume in percent (0..100).
	 */
34
	unsigned volume = 100;
35

36
public:
37
	SoftwareMixer(MixerListener &_listener)
38
		:Mixer(software_mixer_plugin, _listener)
39 40
	{
	}
41

42
	void SetFilter(Filter *_filter) noexcept;
43 44

	/* virtual methods from class Mixer */
45
	void Open() override {
46 47
	}

48
	void Close() noexcept override {
49 50
	}

51
	int GetVolume() override {
52 53 54
		return volume;
	}

55
	void SetVolume(unsigned volume) override;
56 57
};

58
static Mixer *
59
software_mixer_init(gcc_unused EventLoop &event_loop,
60
		    gcc_unused AudioOutput &ao,
61
		    MixerListener &listener,
62
		    gcc_unused const ConfigBlock &block)
63
{
64
	return new SoftwareMixer(listener);
65 66
}

67 68
gcc_const
static unsigned
69
PercentVolumeToSoftwareVolume(unsigned volume) noexcept
70 71 72 73 74 75 76 77 78 79 80 81
{
	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;
}

82 83
void
SoftwareMixer::SetVolume(unsigned new_volume)
84
{
85
	assert(new_volume <= 100);
86

87
	volume = new_volume;
88 89 90

	if (filter != nullptr)
		volume_filter_set(filter, PercentVolumeToSoftwareVolume(new_volume));
91 92
}

93
const MixerPlugin software_mixer_plugin = {
94 95
	software_mixer_init,
	true,
96 97
};

98
inline void
99
SoftwareMixer::SetFilter(Filter *_filter) noexcept
100
{
101
	filter = _filter;
102

103 104 105
	if (filter != nullptr)
		volume_filter_set(filter,
				  PercentVolumeToSoftwareVolume(volume));
106 107
}

108
void
109
software_mixer_set_filter(Mixer &mixer, Filter *filter) noexcept
110
{
111 112
	SoftwareMixer &sm = (SoftwareMixer &)mixer;
	sm.SetFilter(filter);
113
}