MixerAll.cxx 3.08 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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
#include "Log.hxx"
29

30 31 32
#include <assert.h>

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

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

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

	return volume;
50 51
}

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

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

	if (ok == 0)
		return -1;

	return total / ok;
}

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

77
	if (!ao.enabled)
78 79
		return false;

80
	Mixer *mixer = ao.mixer;
81
	if (mixer == nullptr)
82 83
		return false;

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

	return success;
92 93
}

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

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

	return success;
}
106 107

static int
108
output_mixer_get_software_volume(const AudioOutput &ao)
109
{
110
	if (!ao.enabled)
111 112
		return -1;

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

117
	return mixer_get_volume(mixer, IgnoreError());
118 119 120
}

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

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

	if (ok == 0)
		return -1;

	return total / ok;
}

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

145 146 147 148
	for (auto ao : outputs) {
		const auto mixer = ao->mixer;

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