Volume.cxx 2.89 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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/StringUtil.hxx"
25
#include "util/Domain.hxx"
26
#include "system/PeriodClock.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 44
void
InvalidateHardwareVolume()
45 46 47
{
	/* flush the hardware volume cache */
	last_hardware_volume = -1;
Warren Dukes's avatar
Warren Dukes committed
48 49
}

50 51
int
volume_level_get(const MultipleOutputs &outputs)
Avuton Olrich's avatar
Avuton Olrich committed
52
{
53
	if (last_hardware_volume >= 0 &&
54
	    !hardware_volume_clock.CheckUpdate(1000))
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
	if (!StringStartsWith(line, SW_VOLUME_STATE))
101 102 103 104 105
		return false;

	line += sizeof(SW_VOLUME_STATE) - 1;
	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(FILE *fp)
{
115
	fprintf(fp, SW_VOLUME_STATE "%u\n", volume_software_set);
116
}
117 118 119 120 121 122

unsigned
sw_volume_state_get_hash(void)
{
	return volume_software_set;
}