MixerAll.cxx 3.11 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 "util/Error.hxx"
28 29
#include "util/Domain.hxx"
#include "Log.hxx"
30

31 32
#include <assert.h>

33
static constexpr Domain mixer_domain("mixer");
34

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

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

45
	Error error;
46
	int volume = mixer_get_volume(mixer, error);
47
	if (volume < 0 && error.IsDefined())
48 49
		FormatError(error,
			    "Failed to read mixer for '%s'",
50
			    ao.name);
51 52

	return volume;
53 54
}

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

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

	if (ok == 0)
		return -1;

	return total / ok;
}

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

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

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

87
	Error error;
88
	bool success = mixer_set_volume(mixer, volume, error);
89
	if (!success && error.IsDefined())
90 91
		FormatError(error,
			    "Failed to set mixer for '%s'",
92
			    ao.name);
93 94

	return success;
95 96
}

97
bool
98
MultipleOutputs::SetVolume(unsigned volume)
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)
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, IgnoreError());
121 122 123
}

int
124
MultipleOutputs::GetSoftwareVolume() const
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)
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
		    &mixer->plugin == &software_mixer_plugin)
153
			mixer_set_volume(mixer, volume, IgnoreError());
154 155
	}
}