mixer/Volume: refactor to class MixerMemento, per partition

Eliminate global variables, convert them to MixerMemento fields.

Closes https://github.com/MusicPlayerDaemon/MPD/issues/1583
This commit is contained in:
Max Kellermann
2022-08-08 23:15:09 +02:00
parent 615c301961
commit 4b4f47002b
13 changed files with 141 additions and 116 deletions

View File

@@ -17,11 +17,10 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "Volume.hxx"
#include "Memento.hxx"
#include "output/MultipleOutputs.hxx"
#include "Idle.hxx"
#include "util/StringCompare.hxx"
#include "system/PeriodClock.hxx"
#include "io/BufferedOutputStream.hxx"
#include <cassert>
@@ -30,22 +29,8 @@
#define SW_VOLUME_STATE "sw_volume: "
static unsigned volume_software_set = 100;
/** the cached hardware mixer value; invalid if negative */
static int last_hardware_volume = -1;
/** the age of #last_hardware_volume */
static PeriodClock hardware_volume_clock;
void
InvalidateHardwareVolume() noexcept
{
/* flush the hardware volume cache */
last_hardware_volume = -1;
}
int
volume_level_get(const MultipleOutputs &outputs) noexcept
MixerMemento::GetVolume(const MultipleOutputs &outputs) noexcept
{
if (last_hardware_volume >= 0 &&
!hardware_volume_clock.CheckUpdate(std::chrono::seconds(1)))
@@ -56,8 +41,8 @@ volume_level_get(const MultipleOutputs &outputs) noexcept
return last_hardware_volume;
}
static bool
software_volume_change(MultipleOutputs &outputs, unsigned volume)
inline bool
MixerMemento::SetSoftwareVolume(MultipleOutputs &outputs, unsigned volume)
{
assert(volume <= 100);
@@ -67,8 +52,8 @@ software_volume_change(MultipleOutputs &outputs, unsigned volume)
return true;
}
static void
hardware_volume_change(MultipleOutputs &outputs, unsigned volume)
inline void
MixerMemento::SetHardwareVolume(MultipleOutputs &outputs, unsigned volume)
{
/* reset the cache */
last_hardware_volume = -1;
@@ -77,7 +62,7 @@ hardware_volume_change(MultipleOutputs &outputs, unsigned volume)
}
void
volume_level_change(MultipleOutputs &outputs, unsigned volume)
MixerMemento::SetVolume(MultipleOutputs &outputs, unsigned volume)
{
assert(volume <= 100);
@@ -85,11 +70,11 @@ volume_level_change(MultipleOutputs &outputs, unsigned volume)
idle_add(IDLE_MIXER);
hardware_volume_change(outputs, volume);
SetHardwareVolume(outputs, volume);
}
bool
read_sw_volume_state(const char *line, MultipleOutputs &outputs)
MixerMemento::LoadSoftwareVolumeState(const char *line, MultipleOutputs &outputs)
{
char *end = nullptr;
long int sv;
@@ -100,19 +85,13 @@ read_sw_volume_state(const char *line, MultipleOutputs &outputs)
sv = strtol(line, &end, 10);
if (*end == 0 && sv >= 0 && sv <= 100)
software_volume_change(outputs, sv);
SetSoftwareVolume(outputs, sv);
return true;
}
void
save_sw_volume_state(BufferedOutputStream &os)
MixerMemento::SaveSoftwareVolumeState(BufferedOutputStream &os) const
{
os.Format(SW_VOLUME_STATE "%u\n", volume_software_set);
}
unsigned
sw_volume_state_get_hash() noexcept
{
return volume_software_set;
}

73
src/mixer/Memento.hxx Normal file
View File

@@ -0,0 +1,73 @@
/*
* Copyright 2003-2021 The Music Player Daemon Project
* 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.
*/
#pragma once
#include "system/PeriodClock.hxx"
class MultipleOutputs;
class BufferedOutputStream;
/**
* Cache for hardware/software volume levels.
*/
class MixerMemento {
unsigned volume_software_set = 100;
/** the cached hardware mixer value; invalid if negative */
int last_hardware_volume = -1;
/** the age of #last_hardware_volume */
PeriodClock hardware_volume_clock;
public:
/**
* Flush the hardware volume cache.
*/
void InvalidateHardwareVolume() noexcept {
last_hardware_volume = -1;
}
[[gnu::pure]]
int GetVolume(const MultipleOutputs &outputs) noexcept;
/**
* Throws on error.
*/
void SetVolume(MultipleOutputs &outputs, unsigned volume);
bool LoadSoftwareVolumeState(const char *line, MultipleOutputs &outputs);
void SaveSoftwareVolumeState(BufferedOutputStream &os) const;
/**
* Generates a hash number for the current state of the software
* volume control. This is used by timer_save_state_file() to
* determine whether the state has changed and the state file should
* be saved.
*/
[[gnu::pure]]
unsigned GetSoftwareVolumeStateHash() const noexcept {
return volume_software_set;
}
private:
bool SetSoftwareVolume(MultipleOutputs &outputs, unsigned volume);
void SetHardwareVolume(MultipleOutputs &outputs, unsigned volume);
};

View File

@@ -1,55 +0,0 @@
/*
* Copyright 2003-2021 The Music Player Daemon Project
* 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.
*/
#ifndef MPD_VOLUME_HXX
#define MPD_VOLUME_HXX
class MultipleOutputs;
class BufferedOutputStream;
void
InvalidateHardwareVolume() noexcept;
[[gnu::pure]]
int
volume_level_get(const MultipleOutputs &outputs) noexcept;
/**
* Throws on error.
*/
void
volume_level_change(MultipleOutputs &outputs, unsigned volume);
bool
read_sw_volume_state(const char *line, MultipleOutputs &outputs);
void
save_sw_volume_state(BufferedOutputStream &os);
/**
* Generates a hash number for the current state of the software
* volume control. This is used by timer_save_state_file() to
* determine whether the state has changed and the state file should
* be saved.
*/
[[gnu::pure]]
unsigned
sw_volume_state_get_hash() noexcept;
#endif