MixerAll.cxx 3.09 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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 "output/MultipleOutputs.hxx"
21 22 23
#include "MixerControl.hxx"
#include "MixerInternal.hxx"
#include "MixerList.hxx"
24
#include "pcm/Volume.hxx"
25
#include "Log.hxx"
26

27
#include <cassert>
28

29
gcc_pure
30
static int
Max Kellermann's avatar
Max Kellermann committed
31
output_mixer_get_volume(const AudioOutputControl &ao) noexcept
32
{
33
	if (!ao.IsEnabled())
34 35
		return -1;

36
	auto *mixer = ao.GetMixer();
37
	if (mixer == nullptr)
38 39
		return -1;

40 41
	try {
		return mixer_get_volume(mixer);
42 43
	} catch (...) {
		FormatError(std::current_exception(),
44
			    "Failed to read mixer for '%s'",
45
			    ao.GetName());
46 47
		return -1;
	}
48 49
}

50
int
51
MultipleOutputs::GetVolume() const noexcept
52
{
53 54
	unsigned ok = 0;
	int total = 0;
55

56
	for (const auto &ao : outputs) {
57
		int volume = output_mixer_get_volume(*ao);
58
		if (volume >= 0) {
59 60 61 62 63 64 65 66 67 68 69
			total += volume;
			++ok;
		}
	}

	if (ok == 0)
		return -1;

	return total / ok;
}

70
static bool
Max Kellermann's avatar
Max Kellermann committed
71
output_mixer_set_volume(AudioOutputControl &ao, unsigned volume) noexcept
72
{
73
	assert(volume <= 100);
74

75
	if (!ao.IsEnabled())
76 77
		return false;

78
	auto *mixer = ao.GetMixer();
79
	if (mixer == nullptr)
80 81
		return false;

82 83 84
	try {
		mixer_set_volume(mixer, volume);
		return true;
85 86
	} catch (...) {
		FormatError(std::current_exception(),
87
			    "Failed to set mixer for '%s'",
88
			    ao.GetName());
89 90
		return false;
	}
91 92
}

93
bool
94
MultipleOutputs::SetVolume(unsigned volume) noexcept
95
{
96 97
	assert(volume <= 100);

98
	bool success = false;
99
	for (const auto &ao : outputs)
100
		success = output_mixer_set_volume(*ao, volume)
101
			|| success;
102 103 104

	return success;
}
105 106

static int
Max Kellermann's avatar
Max Kellermann committed
107
output_mixer_get_software_volume(const AudioOutputControl &ao) noexcept
108
{
109
	if (!ao.IsEnabled())
110 111
		return -1;

112
	auto *mixer = ao.GetMixer();
113
	if (mixer == nullptr || !mixer->IsPlugin(software_mixer_plugin))
114 115
		return -1;

116
	return mixer_get_volume(mixer);
117 118 119
}

int
120
MultipleOutputs::GetSoftwareVolume() const noexcept
121
{
122 123
	unsigned ok = 0;
	int total = 0;
124

125
	for (const auto &ao : outputs) {
126
		int volume = output_mixer_get_software_volume(*ao);
127 128 129 130 131 132 133 134 135 136 137 138 139
		if (volume >= 0) {
			total += volume;
			++ok;
		}
	}

	if (ok == 0)
		return -1;

	return total / ok;
}

void
140
MultipleOutputs::SetSoftwareVolume(unsigned volume) noexcept
141 142 143
{
	assert(volume <= PCM_VOLUME_1);

144
	for (const auto &ao : outputs) {
145
		auto *mixer = ao->GetMixer();
146 147

		if (mixer != nullptr &&
148 149
		    (&mixer->plugin == &software_mixer_plugin ||
		     &mixer->plugin == &null_mixer_plugin))
150
			mixer_set_volume(mixer, volume);
151 152
	}
}