MixerAll.cxx 3.46 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 "lib/fmt/ExceptionFormatter.hxx"
25
#include "pcm/Volume.hxx"
26
#include "util/Domain.hxx"
27
#include "Log.hxx"
28

29
#include <cassert>
30

31 32
static constexpr Domain mixer_domain("mixer");

33
gcc_pure
34
static int
Max Kellermann's avatar
Max Kellermann committed
35
output_mixer_get_volume(const AudioOutputControl &ao) noexcept
36
{
37
	auto *mixer = ao.GetMixer();
38
	if (mixer == nullptr)
39 40
		return -1;

41 42 43 44 45
	/* software mixers are always considered, even if they are
	   disabled */
	if (!ao.IsEnabled() && !mixer->IsPlugin(software_mixer_plugin))
		return -1;

46 47
	try {
		return mixer_get_volume(mixer);
48
	} catch (...) {
49 50 51
		FmtError(mixer_domain,
			 "Failed to read mixer for '{}': {}",
			 ao.GetName(), std::current_exception());
52 53
		return -1;
	}
54 55
}

56
int
57
MultipleOutputs::GetVolume() const noexcept
58
{
59 60
	unsigned ok = 0;
	int total = 0;
61

62
	for (const auto &ao : outputs) {
63
		int volume = output_mixer_get_volume(*ao);
64
		if (volume >= 0) {
65 66 67 68 69 70 71 72 73 74 75
			total += volume;
			++ok;
		}
	}

	if (ok == 0)
		return -1;

	return total / ok;
}

76
static bool
Max Kellermann's avatar
Max Kellermann committed
77
output_mixer_set_volume(AudioOutputControl &ao, unsigned volume) noexcept
78
{
79
	assert(volume <= 100);
80

81
	auto *mixer = ao.GetMixer();
82
	if (mixer == nullptr)
83 84
		return false;

85 86
	/* software mixers are always updated, even if they are
	   disabled */
87
	if (!ao.IsReallyEnabled() && !mixer->IsPlugin(software_mixer_plugin))
88 89
		return false;

90 91 92
	try {
		mixer_set_volume(mixer, volume);
		return true;
93
	} catch (...) {
94 95 96
		FmtError(mixer_domain,
			 "Failed to set mixer for '{}': {}",
			 ao.GetName(), std::current_exception());
97 98
		return false;
	}
99 100
}

101
bool
102
MultipleOutputs::SetVolume(unsigned volume) noexcept
103
{
104 105
	assert(volume <= 100);

106
	bool success = false;
107
	for (const auto &ao : outputs)
108
		success = output_mixer_set_volume(*ao, volume)
109
			|| success;
110 111 112

	return success;
}
113 114

static int
Max Kellermann's avatar
Max Kellermann committed
115
output_mixer_get_software_volume(const AudioOutputControl &ao) noexcept
116
{
117
	if (!ao.IsEnabled())
118 119
		return -1;

120
	auto *mixer = ao.GetMixer();
121
	if (mixer == nullptr || !mixer->IsPlugin(software_mixer_plugin))
122 123
		return -1;

124
	return mixer_get_volume(mixer);
125 126 127
}

int
128
MultipleOutputs::GetSoftwareVolume() const noexcept
129
{
130 131
	unsigned ok = 0;
	int total = 0;
132

133
	for (const auto &ao : outputs) {
134
		int volume = output_mixer_get_software_volume(*ao);
135 136 137 138 139 140 141 142 143 144 145 146 147
		if (volume >= 0) {
			total += volume;
			++ok;
		}
	}

	if (ok == 0)
		return -1;

	return total / ok;
}

void
148
MultipleOutputs::SetSoftwareVolume(unsigned volume) noexcept
149 150 151
{
	assert(volume <= PCM_VOLUME_1);

152
	for (const auto &ao : outputs) {
153
		auto *mixer = ao->GetMixer();
154 155

		if (mixer != nullptr &&
156 157
		    (&mixer->plugin == &software_mixer_plugin ||
		     &mixer->plugin == &null_mixer_plugin))
158
			mixer_set_volume(mixer, volume);
159 160
	}
}