mpd/src/mixer/plugins/SoftwareMixerPlugin.cxx

115 lines
2.5 KiB
C++
Raw Normal View History

/*
2020-01-18 19:22:19 +01:00
* Copyright 2003-2020 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.
*/
#include "SoftwareMixerPlugin.hxx"
2014-01-24 16:25:21 +01:00
#include "mixer/MixerInternal.hxx"
2014-01-24 16:31:52 +01:00
#include "filter/plugins/VolumeFilterPlugin.hxx"
2013-12-22 23:24:42 +01:00
#include "pcm/Volume.hxx"
#include <cassert>
#include <math.h>
class SoftwareMixer final : public Mixer {
Filter *filter = nullptr;
2013-10-30 22:47:24 +01:00
/**
* The current volume in percent (0..100).
*/
2016-07-01 14:06:08 +02:00
unsigned volume = 100;
2013-04-16 21:25:27 +02:00
public:
2020-02-01 13:59:55 +01:00
explicit SoftwareMixer(MixerListener &_listener)
:Mixer(software_mixer_plugin, _listener)
2013-04-16 21:25:27 +02:00
{
}
void SetFilter(Filter *_filter) noexcept;
/* virtual methods from class Mixer */
2016-09-09 12:52:51 +02:00
void Open() override {
}
2017-06-08 21:42:12 +02:00
void Close() noexcept override {
}
2016-09-09 12:52:51 +02:00
int GetVolume() override {
return volume;
}
2016-09-09 12:52:51 +02:00
void SetVolume(unsigned volume) override;
};
2013-04-16 21:33:25 +02:00
static Mixer *
software_mixer_init(gcc_unused EventLoop &event_loop,
gcc_unused AudioOutput &ao,
MixerListener &listener,
2016-09-09 12:52:51 +02:00
gcc_unused const ConfigBlock &block)
{
return new SoftwareMixer(listener);
}
gcc_const
static unsigned
PercentVolumeToSoftwareVolume(unsigned volume) noexcept
{
assert(volume <= 100);
if (volume >= 100)
return PCM_VOLUME_1;
else if (volume > 0)
return pcm_float_to_volume((exp(volume / 25.0) - 1) /
(54.5981500331F - 1));
else
return 0;
}
2016-09-09 12:52:51 +02:00
void
SoftwareMixer::SetVolume(unsigned new_volume)
{
assert(new_volume <= 100);
volume = new_volume;
if (filter != nullptr)
volume_filter_set(filter, PercentVolumeToSoftwareVolume(new_volume));
}
const MixerPlugin software_mixer_plugin = {
software_mixer_init,
true,
};
inline void
SoftwareMixer::SetFilter(Filter *_filter) noexcept
{
filter = _filter;
if (filter != nullptr)
volume_filter_set(filter,
PercentVolumeToSoftwareVolume(volume));
}
void
software_mixer_set_filter(Mixer &mixer, Filter *filter) noexcept
{
2020-02-01 13:55:08 +01:00
auto &sm = (SoftwareMixer &)mixer;
sm.SetFilter(filter);
}