MixerAll.cxx 3.12 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 "output/MultipleOutputs.hxx"
21 22 23
#include "MixerControl.hxx"
#include "MixerInternal.hxx"
#include "MixerList.hxx"
24
#include "output/Filtered.hxx"
25
#include "pcm/Volume.hxx"
26
#include "Log.hxx"
27

28 29
#include <stdexcept>

30 31
#include <assert.h>

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

39
	auto *mixer = ao.GetMixer();
40
	if (mixer == nullptr)
41 42
		return -1;

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

53
int
54
MultipleOutputs::GetVolume() const noexcept
55
{
56 57
	unsigned ok = 0;
	int total = 0;
58

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

	if (ok == 0)
		return -1;

	return total / ok;
}

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

78
	if (!ao.IsEnabled())
79 80
		return false;

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

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

96
bool
97
MultipleOutputs::SetVolume(unsigned volume) noexcept
98
{
99 100
	assert(volume <= 100);

101
	bool success = false;
102
	for (auto *ao : outputs)
103
		success = output_mixer_set_volume(*ao, volume)
104
			|| success;
105 106 107

	return success;
}
108 109

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

115
	auto *mixer = ao.GetMixer();
116
	if (mixer == nullptr || !mixer->IsPlugin(software_mixer_plugin))
117 118
		return -1;

119
	return mixer_get_volume(mixer);
120 121 122
}

int
123
MultipleOutputs::GetSoftwareVolume() const noexcept
124
{
125 126
	unsigned ok = 0;
	int total = 0;
127

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

	if (ok == 0)
		return -1;

	return total / ok;
}

void
143
MultipleOutputs::SetSoftwareVolume(unsigned volume) noexcept
144 145 146
{
	assert(volume <= PCM_VOLUME_1);

147 148
	for (auto *ao : outputs) {
		auto *mixer = ao->GetMixer();
149 150

		if (mixer != nullptr &&
151 152
		    (&mixer->plugin == &software_mixer_plugin ||
		     &mixer->plugin == &null_mixer_plugin))
153
			mixer_set_volume(mixer, volume);
154 155
	}
}