song info: show replay gain info if available (flac/ogg only)

This commit is contained in:
Andrzej Rybczak
2013-05-28 00:08:14 +02:00
parent 4db97e5502
commit 1feb244dce
3 changed files with 80 additions and 1 deletions

View File

@@ -22,6 +22,7 @@
#include "helpers.h"
#include "song_info.h"
#include "tag_editor.h"
#include "tags.h"
#include "title.h"
#include "screen_switcher.h"
@@ -124,7 +125,17 @@ void SongInfo::PrepareSong(MPD::Song &s)
}
w << NC::Format::Bold << "Bitrate: " << NC::Format::NoBold << Config.color2 << f.audioProperties()->bitrate() << " kbps\n" << NC::Color::End;
w << NC::Format::Bold << "Sample rate: " << NC::Format::NoBold << Config.color2 << f.audioProperties()->sampleRate() << " Hz\n" << NC::Color::End;
w << NC::Format::Bold << "Channels: " << NC::Format::NoBold << Config.color2 << channels << '\n' << NC::Color::Default;
w << NC::Format::Bold << "Channels: " << NC::Format::NoBold << Config.color2 << channels << NC::Color::End << "\n";
auto rginfo = Tags::readReplayGain(f.file());
if (!rginfo.empty())
{
w << NC::Format::Bold << "\nReference loudness: " << NC::Format::NoBold << Config.color2 << rginfo.referenceLoudness() << NC::Color::End << "\n";
w << NC::Format::Bold << "Track gain: " << NC::Format::NoBold << Config.color2 << rginfo.trackGain() << NC::Color::End << "\n";
w << NC::Format::Bold << "Track peak: " << NC::Format::NoBold << Config.color2 << rginfo.trackPeak() << NC::Color::End << "\n";
w << NC::Format::Bold << "Album gain: " << NC::Format::NoBold << Config.color2 << rginfo.albumGain() << NC::Color::End << "\n";
w << NC::Format::Bold << "Album peak: " << NC::Format::NoBold << Config.color2 << rginfo.albumPeak() << NC::Color::End << "\n";
}
}
}
# endif // HAVE_TAGLIB_H

View File

@@ -187,6 +187,24 @@ void writeXiphComments(const MPD::MutableSong &s, TagLib::Ogg::XiphComment *tag)
writeXiph("COMMENT", tagList(s, &MPD::Song::getComment));
}
Tags::ReplayGainInfo getReplayGain(TagLib::Ogg::XiphComment *tag)
{
auto first_or_empty = [](const TagLib::StringList &list) {
std::string result;
if (!list.isEmpty())
result = list.front().to8Bit(true);
return result;
};
auto &fields = tag->fieldListMap();
return Tags::ReplayGainInfo(
first_or_empty(fields["REPLAYGAIN_REFERENCE_LOUDNESS"]),
first_or_empty(fields["REPLAYGAIN_TRACK_GAIN"]),
first_or_empty(fields["REPLAYGAIN_TRACK_PEAK"]),
first_or_empty(fields["REPLAYGAIN_ALBUM_GAIN"]),
first_or_empty(fields["REPLAYGAIN_ALBUM_PEAK"])
);
}
}
namespace Tags {//
@@ -198,6 +216,22 @@ bool extendedSetSupported(const TagLib::File *f)
|| dynamic_cast<const TagLib::FLAC::File *>(f);
}
ReplayGainInfo readReplayGain(TagLib::File *f)
{
ReplayGainInfo result;
if (auto ogg_file = dynamic_cast<TagLib::Ogg::Vorbis::File *>(f))
{
if (auto xiph = ogg_file->tag())
result = getReplayGain(xiph);
}
else if (auto flac_file = dynamic_cast<TagLib::FLAC::File *>(f))
{
if (auto xiph = flac_file->xiphComment())
result = getReplayGain(xiph);
}
return result;
}
void read(MPD::MutableSong &s)
{
TagLib::FileRef f(s.getURI().c_str());

View File

@@ -30,6 +30,40 @@
namespace Tags {//
struct ReplayGainInfo
{
ReplayGainInfo() { }
ReplayGainInfo(std::string reference_loudness, std::string track_gain,
std::string track_peak, std::string album_gain,
std::string album_peak)
: m_reference_loudness(reference_loudness), m_track_gain(track_gain)
, m_track_peak(track_peak), m_album_gain(album_gain), m_album_peak(album_peak) { }
bool empty() const
{
return m_reference_loudness.empty()
&& m_track_gain.empty()
&& m_track_peak.empty()
&& m_album_gain.empty()
&& m_album_peak.empty();
}
const std::string &referenceLoudness() const { return m_reference_loudness; }
const std::string &trackGain() const { return m_track_gain; }
const std::string &trackPeak() const { return m_track_peak; }
const std::string &albumGain() const { return m_album_gain; }
const std::string &albumPeak() const { return m_album_peak; }
private:
std::string m_reference_loudness;
std::string m_track_gain;
std::string m_track_peak;
std::string m_album_gain;
std::string m_album_peak;
};
ReplayGainInfo readReplayGain(TagLib::File *f);
bool extendedSetSupported(const TagLib::File *f);
void read(MPD::MutableSong &);