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

29 30
#include <stdexcept>

31 32
#include <assert.h>

33
gcc_pure
34
static int
35
output_mixer_get_volume(const AudioOutput &ao) noexcept
36
{
37
	if (!ao.enabled)
38 39
		return -1;

40
	Mixer *mixer = ao.mixer;
41
	if (mixer == nullptr)
42 43
		return -1;

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

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

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

	if (ok == 0)
		return -1;

	return total / ok;
}

74
static bool
75
output_mixer_set_volume(AudioOutput &ao, unsigned volume) noexcept
76
{
77
	assert(volume <= 100);
78

79
	if (!ao.enabled)
80 81
		return false;

82
	Mixer *mixer = ao.mixer;
83
	if (mixer == nullptr)
84 85
		return false;

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

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

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

	return success;
}
109 110

static int
111
output_mixer_get_software_volume(const AudioOutput &ao) noexcept
112
{
113
	if (!ao.enabled)
114 115
		return -1;

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

120
	return mixer_get_volume(mixer);
121 122 123
}

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

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

	if (ok == 0)
		return -1;

	return total / ok;
}

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

148 149 150 151
	for (auto ao : outputs) {
		const auto mixer = ao->mixer;

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