From 77c1f5487668569abb24dc458c40f019539dda52 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 25 Nov 2016 11:13:08 +0100 Subject: ReplayGainConfig: add struct ReplayGainConfig, move globals to ReplayGainGlobal.cxx --- Makefile.am | 7 ++- src/Main.cxx | 2 +- src/ReplayGainConfig.cxx | 87 --------------------------- src/ReplayGainConfig.hxx | 15 ++--- src/ReplayGainGlobal.cxx | 83 +++++++++++++++++++++++++ src/ReplayGainGlobal.hxx | 35 +++++++++++ src/ReplayGainInfo.cxx | 10 +-- src/ReplayGainInfo.hxx | 5 +- src/command/PlayerCommands.cxx | 2 +- src/decoder/Bridge.cxx | 6 +- src/filter/plugins/ReplayGainFilterPlugin.cxx | 6 +- test/FakeReplayGainConfig.cxx | 25 -------- test/FakeReplayGainGlobal.cxx | 24 ++++++++ 13 files changed, 168 insertions(+), 139 deletions(-) delete mode 100644 src/ReplayGainConfig.cxx create mode 100644 src/ReplayGainGlobal.cxx create mode 100644 src/ReplayGainGlobal.hxx delete mode 100644 test/FakeReplayGainConfig.cxx create mode 100644 test/FakeReplayGainGlobal.cxx diff --git a/Makefile.am b/Makefile.am index 978ea1053..b35c63347 100644 --- a/Makefile.am +++ b/Makefile.am @@ -166,7 +166,7 @@ libmpd_a_SOURCES = \ src/queue/PlaylistState.cxx src/queue/PlaylistState.hxx \ src/queue/Listener.hxx \ src/PluginUnavailable.hxx \ - src/ReplayGainConfig.cxx src/ReplayGainConfig.hxx \ + src/ReplayGainGlobal.cxx src/ReplayGainGlobal.hxx \ src/DetachedSong.cxx src/DetachedSong.hxx \ src/LocateUri.cxx src/LocateUri.hxx \ src/SongUpdate.cxx \ @@ -853,6 +853,7 @@ libbasic_a_SOURCES = \ src/CheckAudioFormat.cxx src/CheckAudioFormat.hxx \ src/AudioFormat.cxx src/AudioFormat.hxx \ src/AudioParser.cxx src/AudioParser.hxx \ + src/ReplayGainConfig.hxx \ src/ReplayGainMode.cxx src/ReplayGainMode.hxx \ src/ReplayGainInfo.cxx src/ReplayGainInfo.hxx @@ -1978,7 +1979,7 @@ test_run_filter_LDADD = \ libsystem.a \ libutil.a test_run_filter_SOURCES = test/run_filter.cxx \ - test/FakeReplayGainConfig.cxx \ + test/FakeReplayGainGlobal.cxx \ src/Log.cxx src/LogBackend.cxx \ src/filter/FilterPlugin.cxx src/filter/FilterRegistry.cxx @@ -2073,7 +2074,7 @@ test_run_output_LDADD = $(MPD_LIBS) \ libthread.a \ libutil.a test_run_output_SOURCES = test/run_output.cxx \ - test/FakeReplayGainConfig.cxx \ + test/FakeReplayGainGlobal.cxx \ test/ScopeIOThread.hxx \ src/Log.cxx src/LogBackend.cxx \ src/IOThread.cxx \ diff --git a/src/Main.cxx b/src/Main.cxx index e87eebd8e..e157f7c46 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -33,7 +33,7 @@ #include "command/AllCommands.hxx" #include "Partition.hxx" #include "tag/TagConfig.hxx" -#include "ReplayGainConfig.hxx" +#include "ReplayGainGlobal.hxx" #include "Idle.hxx" #include "Log.hxx" #include "LogInit.hxx" diff --git a/src/ReplayGainConfig.cxx b/src/ReplayGainConfig.cxx deleted file mode 100644 index 9f7841632..000000000 --- a/src/ReplayGainConfig.cxx +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright 2003-2016 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 "config.h" -#include "ReplayGainConfig.hxx" -#include "config/Param.hxx" -#include "config/ConfigGlobal.hxx" -#include "util/RuntimeError.hxx" - -#include -#include -#include - -ReplayGainMode replay_gain_mode = ReplayGainMode::OFF; - -static constexpr bool DEFAULT_REPLAYGAIN_LIMIT = true; - -float replay_gain_preamp = 1.0; -float replay_gain_missing_preamp = 1.0; -bool replay_gain_limit = DEFAULT_REPLAYGAIN_LIMIT; - -static float -ParsePreamp(const char *s) -{ - assert(s != nullptr); - - char *endptr; - float f = strtod(s, &endptr); - if (endptr == s || *endptr != '\0') - throw std::invalid_argument("Not a numeric value"); - - if (f < -15 || f > 15) - throw std::invalid_argument("Number must be between -15 and 15"); - - return pow(10, f / 20.0); -} - -static float -ParsePreamp(const ConfigParam &p) -{ - try { - return ParsePreamp(p.value.c_str()); - } catch (...) { - std::throw_with_nested(FormatRuntimeError("Failed to parse line %i", - p.line)); - } -} - -void replay_gain_global_init(void) -{ - const auto *param = config_get_param(ConfigOption::REPLAYGAIN); - - try { - if (param != nullptr) - replay_gain_mode = FromString(param->value.c_str()); - } catch (...) { - std::throw_with_nested(FormatRuntimeError("Failed to parse line %i", - param->line)); - } - - param = config_get_param(ConfigOption::REPLAYGAIN_PREAMP); - if (param) - replay_gain_preamp = ParsePreamp(*param); - - param = config_get_param(ConfigOption::REPLAYGAIN_MISSING_PREAMP); - if (param) - replay_gain_missing_preamp = ParsePreamp(*param); - - replay_gain_limit = config_get_bool(ConfigOption::REPLAYGAIN_LIMIT, - DEFAULT_REPLAYGAIN_LIMIT); -} diff --git a/src/ReplayGainConfig.hxx b/src/ReplayGainConfig.hxx index 3d3154c3f..f5d560ecc 100644 --- a/src/ReplayGainConfig.hxx +++ b/src/ReplayGainConfig.hxx @@ -21,14 +21,15 @@ #define MPD_REPLAY_GAIN_CONFIG_HXX #include "check.h" -#include "ReplayGainMode.hxx" -extern ReplayGainMode replay_gain_mode; -extern float replay_gain_preamp; -extern float replay_gain_missing_preamp; -extern bool replay_gain_limit; +struct ReplayGainConfig { + static constexpr bool DEFAULT_LIMIT = true; -void -replay_gain_global_init(); + float preamp = 1.0; + + float missing_preamp = 1.0; + + bool limit = DEFAULT_LIMIT; +}; #endif diff --git a/src/ReplayGainGlobal.cxx b/src/ReplayGainGlobal.cxx new file mode 100644 index 000000000..a345c82a0 --- /dev/null +++ b/src/ReplayGainGlobal.cxx @@ -0,0 +1,83 @@ +/* + * Copyright 2003-2016 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 "config.h" +#include "ReplayGainGlobal.hxx" +#include "ReplayGainConfig.hxx" +#include "config/Param.hxx" +#include "config/ConfigGlobal.hxx" +#include "util/RuntimeError.hxx" + +#include +#include +#include + +ReplayGainMode replay_gain_mode = ReplayGainMode::OFF; +ReplayGainConfig replay_gain_config; + +static float +ParsePreamp(const char *s) +{ + assert(s != nullptr); + + char *endptr; + float f = strtod(s, &endptr); + if (endptr == s || *endptr != '\0') + throw std::invalid_argument("Not a numeric value"); + + if (f < -15 || f > 15) + throw std::invalid_argument("Number must be between -15 and 15"); + + return pow(10, f / 20.0); +} + +static float +ParsePreamp(const ConfigParam &p) +{ + try { + return ParsePreamp(p.value.c_str()); + } catch (...) { + std::throw_with_nested(FormatRuntimeError("Failed to parse line %i", + p.line)); + } +} + +void replay_gain_global_init(void) +{ + const auto *param = config_get_param(ConfigOption::REPLAYGAIN); + + try { + if (param != nullptr) + replay_gain_mode = FromString(param->value.c_str()); + } catch (...) { + std::throw_with_nested(FormatRuntimeError("Failed to parse line %i", + param->line)); + } + + param = config_get_param(ConfigOption::REPLAYGAIN_PREAMP); + if (param) + replay_gain_config.preamp = ParsePreamp(*param); + + param = config_get_param(ConfigOption::REPLAYGAIN_MISSING_PREAMP); + if (param) + replay_gain_config.missing_preamp = ParsePreamp(*param); + + replay_gain_config.limit = config_get_bool(ConfigOption::REPLAYGAIN_LIMIT, + ReplayGainConfig::DEFAULT_LIMIT); +} diff --git a/src/ReplayGainGlobal.hxx b/src/ReplayGainGlobal.hxx new file mode 100644 index 000000000..9f73472e6 --- /dev/null +++ b/src/ReplayGainGlobal.hxx @@ -0,0 +1,35 @@ +/* + * Copyright 2003-2016 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_REPLAY_GAIN_GLOBAL_HXX +#define MPD_REPLAY_GAIN_GLOBAL_HXX + +#include "check.h" +#include "ReplayGainMode.hxx" + +struct ReplayGainConfig; + +extern ReplayGainMode replay_gain_mode; + +extern ReplayGainConfig replay_gain_config; + +void +replay_gain_global_init(); + +#endif diff --git a/src/ReplayGainInfo.cxx b/src/ReplayGainInfo.cxx index 3524811a0..9e2644778 100644 --- a/src/ReplayGainInfo.cxx +++ b/src/ReplayGainInfo.cxx @@ -19,25 +19,25 @@ #include "config.h" #include "ReplayGainInfo.hxx" +#include "ReplayGainConfig.hxx" #include float -ReplayGainTuple::CalculateScale(float preamp, float missing_preamp, - bool peak_limit) const +ReplayGainTuple::CalculateScale(const ReplayGainConfig &config) const { float scale; if (IsDefined()) { scale = pow(10.0, gain / 20.0); - scale *= preamp; + scale *= config.preamp; if (scale > 15.0) scale = 15.0; - if (peak_limit && scale * peak > 1.0) + if (config.limit && scale * peak > 1.0) scale = 1.0 / peak; } else - scale = missing_preamp; + scale = config.missing_preamp; return scale; } diff --git a/src/ReplayGainInfo.hxx b/src/ReplayGainInfo.hxx index ae14635ad..3e5549960 100644 --- a/src/ReplayGainInfo.hxx +++ b/src/ReplayGainInfo.hxx @@ -24,6 +24,8 @@ #include "Compiler.h" #include "ReplayGainMode.hxx" +struct ReplayGainConfig; + struct ReplayGainTuple { float gain; float peak; @@ -38,8 +40,7 @@ struct ReplayGainTuple { } gcc_pure - float CalculateScale(float preamp, float missing_preamp, - bool peak_limit) const; + float CalculateScale(const ReplayGainConfig &config) const; }; struct ReplayGainInfo { diff --git a/src/command/PlayerCommands.cxx b/src/command/PlayerCommands.cxx index f6f2af2fc..76da257e1 100644 --- a/src/command/PlayerCommands.cxx +++ b/src/command/PlayerCommands.cxx @@ -30,7 +30,7 @@ #include "Instance.hxx" #include "Idle.hxx" #include "AudioFormat.hxx" -#include "ReplayGainConfig.hxx" +#include "ReplayGainGlobal.hxx" #include "util/ScopeExit.hxx" #ifdef ENABLE_DATABASE diff --git a/src/decoder/Bridge.cxx b/src/decoder/Bridge.cxx index f878a6708..4bd5e5c8b 100644 --- a/src/decoder/Bridge.cxx +++ b/src/decoder/Bridge.cxx @@ -30,7 +30,7 @@ #include "pcm/PcmConvert.hxx" #include "tag/Tag.hxx" #include "AudioConfig.hxx" -#include "ReplayGainConfig.hxx" +#include "ReplayGainGlobal.hxx" #include "Log.hxx" #include "input/InputStream.hxx" #include "util/ConstBuffer.hxx" @@ -599,9 +599,7 @@ DecoderBridge::SubmitReplayGain(const ReplayGainInfo *new_replay_gain_info) const auto &tuple = new_replay_gain_info->Get(rgm); const auto scale = - tuple.CalculateScale(replay_gain_preamp, - replay_gain_missing_preamp, - replay_gain_limit); + tuple.CalculateScale(replay_gain_config); dc.replay_gain_db = 20.0 * log10f(scale); } diff --git a/src/filter/plugins/ReplayGainFilterPlugin.cxx b/src/filter/plugins/ReplayGainFilterPlugin.cxx index df98db151..f51e50b68 100644 --- a/src/filter/plugins/ReplayGainFilterPlugin.cxx +++ b/src/filter/plugins/ReplayGainFilterPlugin.cxx @@ -24,7 +24,7 @@ #include "filter/FilterRegistry.hxx" #include "AudioFormat.hxx" #include "ReplayGainInfo.hxx" -#include "ReplayGainConfig.hxx" +#include "ReplayGainGlobal.hxx" #include "mixer/MixerControl.hxx" #include "pcm/Volume.hxx" #include "util/ConstBuffer.hxx" @@ -140,9 +140,7 @@ ReplayGainFilter::Update() unsigned volume = PCM_VOLUME_1; if (mode != ReplayGainMode::OFF) { const auto &tuple = info.Get(mode); - float scale = tuple.CalculateScale(replay_gain_preamp, - replay_gain_missing_preamp, - replay_gain_limit); + float scale = tuple.CalculateScale(replay_gain_config); FormatDebug(replay_gain_domain, "scale=%f\n", (double)scale); diff --git a/test/FakeReplayGainConfig.cxx b/test/FakeReplayGainConfig.cxx deleted file mode 100644 index e4bd7175e..000000000 --- a/test/FakeReplayGainConfig.cxx +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2003-2016 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 "config.h" -#include "ReplayGainConfig.hxx" - -float replay_gain_preamp = 1.0; -float replay_gain_missing_preamp = 1.0; -bool replay_gain_limit = true; diff --git a/test/FakeReplayGainGlobal.cxx b/test/FakeReplayGainGlobal.cxx new file mode 100644 index 000000000..9726d67a1 --- /dev/null +++ b/test/FakeReplayGainGlobal.cxx @@ -0,0 +1,24 @@ +/* + * Copyright 2003-2016 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 "config.h" +#include "ReplayGainGlobal.hxx" +#include "ReplayGainConfig.hxx" + +ReplayGainConfig replay_gain_config; -- cgit v1.2.3