ReplayGainMode: add ToString(), FromString()

Move code from ReplayGainConfig.cxx.
This commit is contained in:
Max Kellermann 2016-11-24 17:25:32 +01:00
parent 5f396e824f
commit 0720702c91
6 changed files with 91 additions and 69 deletions

View File

@ -853,7 +853,7 @@ libbasic_a_SOURCES = \
src/CheckAudioFormat.cxx src/CheckAudioFormat.hxx \
src/AudioFormat.cxx src/AudioFormat.hxx \
src/AudioParser.cxx src/AudioParser.hxx \
src/ReplayGainMode.hxx \
src/ReplayGainMode.cxx src/ReplayGainMode.hxx \
src/ReplayGainInfo.cxx src/ReplayGainInfo.hxx
# configuration library

View File

@ -22,10 +22,10 @@
#include "config/Param.hxx"
#include "config/ConfigGlobal.hxx"
#include "system/FatalError.hxx"
#include "util/RuntimeError.hxx"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
ReplayGainMode replay_gain_mode = ReplayGainMode::OFF;
@ -36,54 +36,16 @@ float replay_gain_preamp = 1.0;
float replay_gain_missing_preamp = 1.0;
bool replay_gain_limit = DEFAULT_REPLAYGAIN_LIMIT;
const char *
replay_gain_get_mode_string(void)
{
switch (replay_gain_mode) {
case ReplayGainMode::AUTO:
return "auto";
case ReplayGainMode::OFF:
return "off";
case ReplayGainMode::TRACK:
return "track";
case ReplayGainMode::ALBUM:
return "album";
}
assert(false);
gcc_unreachable();
}
bool
replay_gain_set_mode_string(const char *p)
{
assert(p != nullptr);
if (strcmp(p, "off") == 0)
replay_gain_mode = ReplayGainMode::OFF;
else if (strcmp(p, "track") == 0)
replay_gain_mode = ReplayGainMode::TRACK;
else if (strcmp(p, "album") == 0)
replay_gain_mode = ReplayGainMode::ALBUM;
else if (strcmp(p, "auto") == 0)
replay_gain_mode = ReplayGainMode::AUTO;
else
return false;
return true;
}
void replay_gain_global_init(void)
{
const auto *param = config_get_param(ConfigOption::REPLAYGAIN);
if (param != nullptr &&
!replay_gain_set_mode_string(param->value.c_str())) {
FormatFatalError("replaygain value \"%s\" at line %i is invalid\n",
param->value.c_str(), param->line);
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);

View File

@ -22,7 +22,6 @@
#include "check.h"
#include "ReplayGainMode.hxx"
#include "Compiler.h"
extern ReplayGainMode replay_gain_mode;
extern float replay_gain_preamp;
@ -32,19 +31,4 @@ extern bool replay_gain_limit;
void
replay_gain_global_init();
/**
* Returns the current replay gain mode as a machine-readable string.
*/
gcc_pure
const char *
replay_gain_get_mode_string();
/**
* Sets the replay gain mode, parsed from a string.
*
* @return true on success, false if the string could not be parsed
*/
bool
replay_gain_set_mode_string(const char *p);
#endif

63
src/ReplayGainMode.cxx Normal file
View File

@ -0,0 +1,63 @@
/*
* 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 "ReplayGainMode.hxx"
#include <stdexcept>
#include <assert.h>
#include <string.h>
const char *
ToString(ReplayGainMode mode)
{
switch (mode) {
case ReplayGainMode::AUTO:
return "auto";
case ReplayGainMode::OFF:
return "off";
case ReplayGainMode::TRACK:
return "track";
case ReplayGainMode::ALBUM:
return "album";
}
assert(false);
gcc_unreachable();
}
ReplayGainMode
FromString(const char *s)
{
assert(s != nullptr);
if (strcmp(s, "off") == 0)
return ReplayGainMode::OFF;
else if (strcmp(s, "track") == 0)
return ReplayGainMode::TRACK;
else if (strcmp(s, "album") == 0)
return ReplayGainMode::ALBUM;
else if (strcmp(s, "auto") == 0)
return ReplayGainMode::AUTO;
else
throw std::invalid_argument("Unrecognized replay gain mode");
}

View File

@ -20,6 +20,8 @@
#ifndef MPD_REPLAY_GAIN_MODE_HXX
#define MPD_REPLAY_GAIN_MODE_HXX
#include "Compiler.h"
#include <stdint.h>
enum class ReplayGainMode : uint8_t {
@ -29,4 +31,19 @@ enum class ReplayGainMode : uint8_t {
AUTO,
};
/**
* Return the string representation of a #ReplayGainMode.
*/
gcc_pure
const char *
ToString(ReplayGainMode mode);
/**
* Parse a string to a #ReplayGainMode. Throws std::runtime_error on
* error.
*/
gcc_pure
ReplayGainMode
FromString(const char *s);
#endif

View File

@ -331,13 +331,9 @@ handle_mixrampdelay(Client &client, Request args, gcc_unused Response &r)
}
CommandResult
handle_replay_gain_mode(Client &client, Request args, Response &r)
handle_replay_gain_mode(Client &client, Request args, Response &)
{
if (!replay_gain_set_mode_string(args.front())) {
r.Error(ACK_ERROR_ARG, "Unrecognized replay gain mode");
return CommandResult::ERROR;
}
replay_gain_mode = FromString(args.front());
client.partition.UpdateEffectiveReplayGainMode(replay_gain_mode);
client.partition.EmitIdle(IDLE_OPTIONS);
return CommandResult::OK;
@ -347,6 +343,6 @@ CommandResult
handle_replay_gain_status(gcc_unused Client &client, gcc_unused Request args,
Response &r)
{
r.Format("replay_gain_mode: %s\n", replay_gain_get_mode_string());
r.Format("replay_gain_mode: %s\n", ToString(replay_gain_mode));
return CommandResult::OK;
}