diff options
author | Andrzej Rybczak <electricityispower@gmail.com> | 2013-05-28 00:08:14 +0200 |
---|---|---|
committer | Andrzej Rybczak <electricityispower@gmail.com> | 2013-05-28 00:11:27 +0200 |
commit | 1feb244dce282abfa7838ec7a7d7acb977487338 (patch) | |
tree | d97406f83d7e7a9bd7cd08eb212cb7e2dd334043 | |
parent | 4db97e55025e3eb33746aa9afdf2f2f4da132f9b (diff) |
song info: show replay gain info if available (flac/ogg only)
-rw-r--r-- | src/song_info.cpp | 13 | ||||
-rw-r--r-- | src/tags.cpp | 34 | ||||
-rw-r--r-- | src/tags.h | 34 |
3 files changed, 80 insertions, 1 deletions
diff --git a/src/song_info.cpp b/src/song_info.cpp index ca2d9062..e7ebad73 100644 --- a/src/song_info.cpp +++ b/src/song_info.cpp @@ -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 diff --git a/src/tags.cpp b/src/tags.cpp index 4fa866d0..8042ca95 100644 --- a/src/tags.cpp +++ b/src/tags.cpp @@ -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()); @@ -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 &); |