Configurable base/exp for software mixer

This commit is contained in:
badaix 2020-05-04 10:14:12 +02:00
parent 5df647d89e
commit 3b3632474c
4 changed files with 32 additions and 10 deletions

View file

@ -41,10 +41,10 @@ AlsaPlayer::AlsaPlayer(boost::asio::io_context& io_context, const ClientSettings
if (settings_.mixer.parameter.empty()) if (settings_.mixer.parameter.empty())
mixer_name_ = DEFAULT_MIXER; mixer_name_ = DEFAULT_MIXER;
else else
utils::string::split_left(settings_.mixer.parameter, ':', mixer_name_, tmp); mixer_name_ = utils::string::split_left(settings_.mixer.parameter, ':', tmp);
// default:CARD=ALSA => default // default:CARD=ALSA => default
utils::string::split_left(settings_.pcm_device.name, ':', mixer_device_, tmp); mixer_device_ = utils::string::split_left(settings_.pcm_device.name, ':', tmp);
LOG(DEBUG, LOG_TAG) << "Mixer: " << mixer_name_ << ", device: " << mixer_device_ << "\n"; LOG(DEBUG, LOG_TAG) << "Mixer: " << mixer_name_ << ", device: " << mixer_device_ << "\n";
} }
} }
@ -229,6 +229,7 @@ void AlsaPlayer::initMixer()
void AlsaPlayer::initAlsa() void AlsaPlayer::initAlsa()
{ {
std::lock_guard<std::mutex> lock(mutex_);
unsigned int tmp, rate; unsigned int tmp, rate;
int err, channels; int err, channels;
snd_pcm_hw_params_t* params; snd_pcm_hw_params_t* params;

View file

@ -24,6 +24,7 @@
#include "common/aixlog.hpp" #include "common/aixlog.hpp"
#include "common/snap_exception.hpp" #include "common/snap_exception.hpp"
#include "common/str_compat.hpp" #include "common/str_compat.hpp"
#include "common/utils/string_utils.hpp"
#include "player.hpp" #include "player.hpp"
@ -51,7 +52,8 @@ void Player::start()
playerThread_ = thread(&Player::worker, this); playerThread_ = thread(&Player::worker, this);
// If hardware mixer is used, send the initial volume to the server, because this is // If hardware mixer is used, send the initial volume to the server, because this is
// the volume that is configured by the user on his local device // the volume that is configured by the user on his local device, so we shouldn't change it
// on client start up
if (settings_.mixer.mode == ClientSettings::Mixer::Mode::hardware) if (settings_.mixer.mode == ClientSettings::Mixer::Mode::hardware)
{ {
double volume; double volume;
@ -128,7 +130,7 @@ void Player::adjustVolume(char* buffer, size_t frames)
void Player::setVolume_poly(double volume, double exp) void Player::setVolume_poly(double volume, double exp)
{ {
volume_ = std::pow(volume, exp); volume_ = std::pow(volume, exp);
LOG(DEBUG, LOG_TAG) << "setVolume poly: " << volume << " => " << volume_ << "\n"; LOG(DEBUG, LOG_TAG) << "setVolume poly with exp " << exp << ": " << volume << " => " << volume_ << "\n";
} }
@ -138,7 +140,7 @@ void Player::setVolume_exp(double volume, double base)
// double base = M_E; // double base = M_E;
// double base = 10.; // double base = 10.;
volume_ = (pow(base, volume) - 1) / (base - 1); volume_ = (pow(base, volume) - 1) / (base - 1);
LOG(DEBUG, LOG_TAG) << "setVolume exp: " << volume << " => " << volume_ << "\n"; LOG(DEBUG, LOG_TAG) << "setVolume exp with base " << base << ": " << volume << " => " << volume_ << "\n";
} }
@ -152,10 +154,22 @@ void Player::setVolume(double volume, bool mute)
} }
else if (settings_.mixer.mode == ClientSettings::Mixer::Mode::software) else if (settings_.mixer.mode == ClientSettings::Mixer::Mode::software)
{ {
if (settings_.mixer.parameter == "poly") string param;
setVolume_poly(volume, 3.); string mode = utils::string::split_left(settings_.mixer.parameter, ':', param);
double dparam;
try
{
if (!param.empty())
dparam = cpt::stod(param);
}
catch(...)
{
throw SnapException("Invalid mixer param: " + param);
}
if (mode == "poly")
setVolume_poly(volume, param.empty() ? 3. : dparam);
else else
setVolume_exp(volume, 10.); setVolume_exp(volume, param.empty() ? 10. : dparam);
} }
else if (settings_.mixer.mode == ClientSettings::Mixer::Mode::script) else if (settings_.mixer.mode == ClientSettings::Mixer::Mode::script)
{ {

View file

@ -310,8 +310,7 @@ int main(int argc, char** argv)
settings.player.mixer.mode = ClientSettings::Mixer::Mode::software; settings.player.mixer.mode = ClientSettings::Mixer::Mode::software;
if (mixer_mode->is_set()) if (mixer_mode->is_set())
{ {
string mode; string mode = utils::string::split_left(mixer_mode->value(), ':', settings.player.mixer.parameter);
utils::string::split_left(mixer_mode->value(), ':', mode, settings.player.mixer.parameter);
if (mode == "software") if (mode == "software")
settings.player.mixer.mode = ClientSettings::Mixer::Mode::software; settings.player.mixer.mode = ClientSettings::Mixer::Mode::software;
else if ((mode == "hardware") && hw_mixer_supported) else if ((mode == "hardware") && hw_mixer_supported)

View file

@ -114,6 +114,14 @@ static void split_left(const std::string& s, char delim, std::string& left, std:
} }
static std::string split_left(const std::string& s, char delim, std::string& right)
{
std::string left;
split_left(s, delim, left, right);
return left;
}
static std::vector<std::string>& split(const std::string& s, char delim, std::vector<std::string>& elems) static std::vector<std::string>& split(const std::string& s, char delim, std::vector<std::string>& elems)
{ {