MixerAll.cxx 3.03 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 33
#include <assert.h>

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

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

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

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

59 60
	for (auto ao : outputs) {
		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
74
output_mixer_set_volume(AudioOutput &ao, unsigned volume)
75
{
76
	assert(volume <= 100);
77

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

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

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

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

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

	return success;
}
108 109

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

115
	Mixer *mixer = ao.mixer;
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
124
{
125 126
	unsigned ok = 0;
	int total = 0;
127

128 129
	for (auto ao : outputs) {
		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)
144 145 146
{
	assert(volume <= PCM_VOLUME_1);

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

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