Volume.cxx 2.95 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3
 * http://www.musicpd.org
Warren Dukes's avatar
Warren Dukes committed
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
Warren Dukes's avatar
Warren Dukes committed
18
 */
19

Max Kellermann's avatar
Max Kellermann committed
20
#include "Volume.hxx"
21
#include "output/MultipleOutputs.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "Idle.hxx"
23
#include "util/StringCompare.hxx"
24
#include "util/Domain.hxx"
25
#include "system/PeriodClock.hxx"
26
#include "fs/io/BufferedOutputStream.hxx"
27
#include "Log.hxx"
Max Kellermann's avatar
Max Kellermann committed
28

29
#include <assert.h>
30
#include <stdlib.h>
Max Kellermann's avatar
Max Kellermann committed
31

32
#define SW_VOLUME_STATE                         "sw_volume: "
Warren Dukes's avatar
Warren Dukes committed
33

34 35
static constexpr Domain volume_domain("volume");

36
static unsigned volume_software_set = 100;
37

38 39 40
/** the cached hardware mixer value; invalid if negative */
static int last_hardware_volume = -1;
/** the age of #last_hardware_volume */
41
static PeriodClock hardware_volume_clock;
42

43
void
44
InvalidateHardwareVolume() noexcept
45 46 47
{
	/* flush the hardware volume cache */
	last_hardware_volume = -1;
Warren Dukes's avatar
Warren Dukes committed
48 49
}

50
int
51
volume_level_get(const MultipleOutputs &outputs) noexcept
Avuton Olrich's avatar
Avuton Olrich committed
52
{
53
	if (last_hardware_volume >= 0 &&
54
	    !hardware_volume_clock.CheckUpdate(std::chrono::seconds(1)))
55 56 57
		/* throttle access to hardware mixers */
		return last_hardware_volume;

58
	last_hardware_volume = outputs.GetVolume();
59
	return last_hardware_volume;
Warren Dukes's avatar
Warren Dukes committed
60 61
}

62 63
static bool
software_volume_change(MultipleOutputs &outputs, unsigned volume)
Avuton Olrich's avatar
Avuton Olrich committed
64
{
65
	assert(volume <= 100);
Warren Dukes's avatar
Warren Dukes committed
66

67
	volume_software_set = volume;
68
	outputs.SetSoftwareVolume(volume);
Warren Dukes's avatar
Warren Dukes committed
69

70
	return true;
Warren Dukes's avatar
Warren Dukes committed
71 72
}

73 74
static bool
hardware_volume_change(MultipleOutputs &outputs, unsigned volume)
75
{
76 77 78
	/* reset the cache */
	last_hardware_volume = -1;

79
	return outputs.SetVolume(volume);
80 81
}

82 83
bool
volume_level_change(MultipleOutputs &outputs, unsigned volume)
Avuton Olrich's avatar
Avuton Olrich committed
84
{
85 86
	assert(volume <= 100);

87 88
	volume_software_set = volume;

89 90
	idle_add(IDLE_MIXER);

91
	return hardware_volume_change(outputs, volume);
Warren Dukes's avatar
Warren Dukes committed
92
}
93

94
bool
95
read_sw_volume_state(const char *line, MultipleOutputs &outputs)
96
{
97
	char *end = nullptr;
98 99
	long int sv;

100 101
	line = StringAfterPrefix(line, SW_VOLUME_STATE);
	if (line == nullptr)
102 103 104 105
		return false;

	sv = strtol(line, &end, 10);
	if (*end == 0 && sv >= 0 && sv <= 100)
106
		software_volume_change(outputs, sv);
107
	else
108 109
		FormatWarning(volume_domain,
			      "Can't parse software volume: %s", line);
110
	return true;
111 112
}

113 114
void
save_sw_volume_state(BufferedOutputStream &os)
115
{
116
	os.Format(SW_VOLUME_STATE "%u\n", volume_software_set);
117
}
118 119

unsigned
120
sw_volume_state_get_hash() noexcept
121 122 123
{
	return volume_software_set;
}