~ubuntu-branches/ubuntu/utopic/mpd/utopic-proposed

« back to all changes in this revision

Viewing changes to src/ReplayGainConfig.cxx

  • Committer: Package Import Robot
  • Author(s): Steve Kowalik
  • Date: 2013-11-12 18:17:40 UTC
  • mfrom: (2.2.36 sid)
  • Revision ID: package-import@ubuntu.com-20131112181740-72aa4zihehoobedp
Tags: 0.18.3-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - Add libmp3lame-dev to Build-Depends, and enable LAME.
  - Read the user for the daemon from the config file in the init script.
  - Move avahi-daemon from Suggests to Recommends.
  - Added apport hook to include user configuration file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
 
3
 * http://www.musicpd.org
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License along
 
16
 * with this program; if not, write to the Free Software Foundation, Inc.,
 
17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
18
 */
 
19
 
 
20
#include "config.h"
 
21
#include "ReplayGainConfig.hxx"
 
22
#include "Idle.hxx"
 
23
#include "ConfigData.hxx"
 
24
#include "ConfigGlobal.hxx"
 
25
#include "Playlist.hxx"
 
26
#include "system/FatalError.hxx"
 
27
 
 
28
#include <assert.h>
 
29
#include <stdlib.h>
 
30
#include <string.h>
 
31
 
 
32
ReplayGainMode replay_gain_mode = REPLAY_GAIN_OFF;
 
33
 
 
34
static constexpr bool DEFAULT_REPLAYGAIN_LIMIT = true;
 
35
 
 
36
float replay_gain_preamp = 1.0;
 
37
float replay_gain_missing_preamp = 1.0;
 
38
bool replay_gain_limit = DEFAULT_REPLAYGAIN_LIMIT;
 
39
 
 
40
const char *
 
41
replay_gain_get_mode_string(void)
 
42
{
 
43
        switch (replay_gain_mode) {
 
44
        case REPLAY_GAIN_AUTO:
 
45
                return "auto";
 
46
 
 
47
        case REPLAY_GAIN_OFF:
 
48
                return "off";
 
49
 
 
50
        case REPLAY_GAIN_TRACK:
 
51
                return "track";
 
52
 
 
53
        case REPLAY_GAIN_ALBUM:
 
54
                return "album";
 
55
        }
 
56
 
 
57
        assert(false);
 
58
        gcc_unreachable();
 
59
}
 
60
 
 
61
bool
 
62
replay_gain_set_mode_string(const char *p)
 
63
{
 
64
        assert(p != nullptr);
 
65
 
 
66
        if (strcmp(p, "off") == 0)
 
67
                replay_gain_mode = REPLAY_GAIN_OFF;
 
68
        else if (strcmp(p, "track") == 0)
 
69
                replay_gain_mode = REPLAY_GAIN_TRACK;
 
70
        else if (strcmp(p, "album") == 0)
 
71
                replay_gain_mode = REPLAY_GAIN_ALBUM;
 
72
        else if (strcmp(p, "auto") == 0)
 
73
                replay_gain_mode = REPLAY_GAIN_AUTO;
 
74
        else
 
75
                return false;
 
76
 
 
77
        idle_add(IDLE_OPTIONS);
 
78
 
 
79
        return true;
 
80
}
 
81
 
 
82
void replay_gain_global_init(void)
 
83
{
 
84
        const struct config_param *param = config_get_param(CONF_REPLAYGAIN);
 
85
 
 
86
        if (param != nullptr &&
 
87
            !replay_gain_set_mode_string(param->value.c_str())) {
 
88
                FormatFatalError("replaygain value \"%s\" at line %i is invalid\n",
 
89
                                 param->value.c_str(), param->line);
 
90
        }
 
91
 
 
92
        param = config_get_param(CONF_REPLAYGAIN_PREAMP);
 
93
 
 
94
        if (param) {
 
95
                char *test;
 
96
                float f = strtod(param->value.c_str(), &test);
 
97
 
 
98
                if (*test != '\0') {
 
99
                        FormatFatalError("Replaygain preamp \"%s\" is not a number at "
 
100
                                         "line %i\n",
 
101
                                         param->value.c_str(), param->line);
 
102
                }
 
103
 
 
104
                if (f < -15 || f > 15) {
 
105
                        FormatFatalError("Replaygain preamp \"%s\" is not between -15 and"
 
106
                                         "15 at line %i\n",
 
107
                                         param->value.c_str(), param->line);
 
108
                }
 
109
 
 
110
                replay_gain_preamp = pow(10, f / 20.0);
 
111
        }
 
112
 
 
113
        param = config_get_param(CONF_REPLAYGAIN_MISSING_PREAMP);
 
114
 
 
115
        if (param) {
 
116
                char *test;
 
117
                float f = strtod(param->value.c_str(), &test);
 
118
 
 
119
                if (*test != '\0') {
 
120
                        FormatFatalError("Replaygain missing preamp \"%s\" is not a number at "
 
121
                                         "line %i\n",
 
122
                                         param->value.c_str(), param->line);
 
123
                }
 
124
 
 
125
                if (f < -15 || f > 15) {
 
126
                        FormatFatalError("Replaygain missing preamp \"%s\" is not between -15 and"
 
127
                                         "15 at line %i\n",
 
128
                                         param->value.c_str(), param->line);
 
129
                }
 
130
 
 
131
                replay_gain_missing_preamp = pow(10, f / 20.0);
 
132
        }
 
133
 
 
134
        replay_gain_limit = config_get_bool(CONF_REPLAYGAIN_LIMIT, DEFAULT_REPLAYGAIN_LIMIT);
 
135
}
 
136
 
 
137
ReplayGainMode
 
138
replay_gain_get_real_mode(bool random_mode)
 
139
{
 
140
        ReplayGainMode rgm;
 
141
 
 
142
        rgm = replay_gain_mode;
 
143
 
 
144
        if (rgm == REPLAY_GAIN_AUTO)
 
145
            rgm = random_mode ? REPLAY_GAIN_TRACK : REPLAY_GAIN_ALBUM;
 
146
 
 
147
        return rgm;
 
148
}