SoftwareMixerPlugin.cxx 2.52 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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"
22
#include "MixerInternal.hxx"
Max Kellermann's avatar
Max Kellermann committed
23 24
#include "FilterPlugin.hxx"
#include "FilterRegistry.hxx"
25
#include "FilterInternal.hxx"
26
#include "filter/VolumeFilterPlugin.hxx"
Max Kellermann's avatar
Max Kellermann committed
27
#include "pcm/PcmVolume.hxx"
28 29 30 31

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

32
struct SoftwareMixer final : public Mixer {
33
	Filter *filter;
34 35

	unsigned volume;
36 37

	SoftwareMixer()
38 39 40
		:Mixer(software_mixer_plugin),
		filter(filter_new(&volume_filter_plugin, nullptr, nullptr)),
		volume(100)
41 42 43 44 45 46 47
	{
		assert(filter != nullptr);
	}

	~SoftwareMixer() {
		delete filter;
	}
48 49
};

50
static Mixer *
51 52 53
software_mixer_init(gcc_unused void *ao,
		    gcc_unused const struct config_param *param,
		    gcc_unused GError **error_r)
54
{
55
	return new SoftwareMixer();
56 57 58
}

static void
59
software_mixer_finish(Mixer *data)
60
{
61
	SoftwareMixer *sm = (SoftwareMixer *)data;
62

63
	delete sm;
64 65 66
}

static int
67
software_mixer_get_volume(Mixer *mixer, gcc_unused GError **error_r)
68
{
69
	SoftwareMixer *sm = (SoftwareMixer *)mixer;
70 71 72 73 74

	return sm->volume;
}

static bool
75
software_mixer_set_volume(Mixer *mixer, unsigned volume,
76
			  gcc_unused GError **error_r)
77
{
78
	SoftwareMixer *sm = (SoftwareMixer *)mixer;
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

	assert(volume <= 100);

	sm->volume = volume;

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

	volume_filter_set(sm->filter, volume);
	return true;
}

const struct mixer_plugin software_mixer_plugin = {
95 96 97 98 99 100 101
	software_mixer_init,
	software_mixer_finish,
	nullptr,
	nullptr,
	software_mixer_get_volume,
	software_mixer_set_volume,
	true,
102 103
};

104
Filter *
105
software_mixer_get_filter(Mixer *mixer)
106
{
107
	SoftwareMixer *sm = (SoftwareMixer *)mixer;
108
	assert(sm->IsPlugin(software_mixer_plugin));
109 110 111

	return sm->filter;
}