ReplayGain.cxx 2.25 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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 "ReplayGain.hxx"
21
#include "VorbisComment.hxx"
22 23
#include "ReplayGainInfo.hxx"
#include "util/ASCII.hxx"
24
#include "util/NumberParser.hxx"
25 26 27

#include <assert.h>

28 29 30
template<typename T>
static bool
ParseReplayGainTagTemplate(ReplayGainInfo &info, const T t)
31
{
32
	const char *value;
33

34
	if ((value = t["replaygain_track_gain"]) != nullptr) {
35
		info.track.gain = ParseFloat(value);
36
		return true;
37
	} else if ((value = t["replaygain_album_gain"]) != nullptr) {
38
		info.album.gain = ParseFloat(value);
39
		return true;
40
	} else if ((value = t["replaygain_track_peak"]) != nullptr) {
41
		info.track.peak = ParseFloat(value);
42
		return true;
43
	} else if ((value = t["replaygain_album_peak"]) != nullptr) {
44
		info.album.peak = ParseFloat(value);
45 46 47
		return true;
	} else
		return false;
48 49 50 51 52 53 54 55 56 57 58 59 60 61

}

bool
ParseReplayGainTag(ReplayGainInfo &info, const char *name, const char *value)
{
	assert(name != nullptr);
	assert(value != nullptr);

	struct NameValue {
		const char *name;
		const char *value;

		gcc_pure
62
		const char *operator[](const char *n) const noexcept {
63 64 65 66 67 68 69
			return StringEqualsCaseASCII(name, n)
				? value
				: nullptr;
		}
	};

	return ParseReplayGainTagTemplate(info, NameValue{name, value});
70
}
71 72

bool
73
ParseReplayGainVorbis(ReplayGainInfo &info, StringView entry)
74 75
{
	struct VorbisCommentEntry {
76
		StringView entry;
77 78

		gcc_pure
79 80
		const char *operator[](StringView n) const noexcept {
			return GetVorbisCommentValue(entry, n).data;
81 82 83 84 85
		}
	};

	return ParseReplayGainTagTemplate(info, VorbisCommentEntry{entry});
}