Volume.cxx 2.95 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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

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

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

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

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

37
static unsigned volume_software_set = 100;
38

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

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

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

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

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

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

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

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

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

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

88 89
	volume_software_set = volume;

90 91
	idle_add(IDLE_MIXER);

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

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

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

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

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

unsigned
sw_volume_state_get_hash(void)
{
	return volume_software_set;
}