/*
* Copyright (C) 2016 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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 3 of the License, or
* (at your option) any later version.
*
* LMS 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 LMS. If not, see .
*/
#include "AudioFileParser.hpp"
#include
#include
#include
#include "core/ILogger.hpp"
#include "core/PartialDateTime.hpp"
#include "core/String.hpp"
#include "metadata/Exception.hpp"
#include "AvFormatImageReader.hpp"
#include "AvFormatTagReader.hpp"
#include "TagLibImageReader.hpp"
#include "TagLibTagReader.hpp"
#include "Utils.hpp"
namespace lms::metadata
{
namespace
{
void visitTagValues(const ITagReader& tagReader, std::string_view tagType, std::span tagDelimiters, ITagReader::TagValueVisitor visitor)
{
tagReader.visitTagValues(tagType, [&](std::string_view value) {
auto visitTagIfNonEmpty{ [&](std::string_view tag) {
tag = core::stringUtils::stringTrim(tag);
if (!tag.empty())
visitor(tag);
} };
for (std::string_view tagDelimiter : tagDelimiters)
{
if (value.find(tagDelimiter) != std::string_view::npos)
{
for (std::string_view splitTag : core::stringUtils::splitString(value, tagDelimiters))
visitTagIfNonEmpty(splitTag);
return;
}
}
// no delimiter found, or no delimiter to be used
visitTagIfNonEmpty(value);
});
}
template
void addTagIfNonEmpty(std::vector& res, std::string_view tag)
{
if (tag.empty())
return;
if (std::optional val{ core::stringUtils::readAs(tag) })
res.emplace_back(std::move(*val));
}
template
std::vector getTagValuesFirstMatchAs(const ITagReader& tagReader, std::initializer_list tagTypes, std::span tagDelimiters, const WhiteList* whitelist = nullptr)
{
std::vector res;
for (const TagType tagType : tagTypes)
{
tagReader.visitTagValues(tagType, [&](std::string_view value) {
value = core::stringUtils::stringTrim(value);
// short path: no custom delimiter
if (tagDelimiters.empty())
{
addTagIfNonEmpty(res, value);
return;
}
// Algo:
// 1. replace whitelist entries by placeholders
// 2. apply delimiters
// 3. replace whitelist entries back
constexpr std::string_view substitutionPrefix{ "__LMS_ENTRY__" };
std::unordered_map substitutionMap;
std::string strToSplit{ value };
if (whitelist)
{
std::size_t counter{};
for (std::string_view whiteListEntry : *whitelist)
{
whiteListEntry = core::stringUtils::stringTrim(whiteListEntry);
const std::string::size_type pos{ strToSplit.find(whiteListEntry) };
if (pos == std::string::npos)
continue;
std::string substitutionStr{ std::string{ substitutionPrefix } + std::to_string(counter++) };
strToSplit.replace(pos, whiteListEntry.size(), substitutionStr);
substitutionMap.emplace(std::move(substitutionStr), whiteListEntry);
}
}
for (std::string_view strSplit : core::stringUtils::splitString(strToSplit, tagDelimiters))
{
std::string str{ core::stringUtils::stringTrim(strSplit) };
while (true)
{
std::string::size_type prefixPos{ str.find(substitutionPrefix) };
if (prefixPos == std::string::npos)
break;
std::string::size_type counterEnd{ prefixPos + substitutionPrefix.size() };
while (std::isdigit(str[counterEnd]))
counterEnd++;
std::string substitutionStr{ str.substr(prefixPos, counterEnd - prefixPos) };
auto it{ substitutionMap.find(substitutionStr) };
if (it != std::cend(substitutionMap))
str.replace(prefixPos, counterEnd - prefixPos, it->second);
}
addTagIfNonEmpty(res, str);
}
});
if (!res.empty())
break;
}
return res;
}
template
std::optional getTagValueFirstMatchAs(const ITagReader& tagReader, std::initializer_list tagTypes)
{
std::optional res;
std::vector values{ getTagValuesFirstMatchAs(tagReader, tagTypes, {} /* don't expect multiple values here */) };
if (!values.empty())
res = std::move(values.front());
return res;
}
template
std::vector getTagValuesAs(const ITagReader& tagReader, TagType tagType, std::span tagDelimiters)
{
return getTagValuesFirstMatchAs(tagReader, { tagType }, tagDelimiters);
}
template
std::optional getTagValueAs(const ITagReader& tagReader, TagType tagType)
{
return getTagValueFirstMatchAs(tagReader, { tagType });
}
std::vector getLyrics(const ITagReader& tagReader)
{
std::vector res;
tagReader.visitLyricsTags([&](std::string_view language, std::string_view lyricsText) {
std::istringstream iss{ std::string{ lyricsText } }; // TODO avoid copies (ispanstream?)
try
{
Lyrics lyrics{ parseLyrics(iss) };
if (lyrics.language.empty())
lyrics.language = language;
res.emplace_back(std::move(lyrics));
}
catch (const LyricsException& e)
{
LMS_LOG(METADATA, ERROR, "Failed to parse lyrics: " + std::string{ e.what() });
}
});
return res;
}
std::vector getArtists(const ITagReader& tagReader,
std::initializer_list artistTagNames,
std::initializer_list artistSortTagNames,
std::initializer_list artistMBIDTagNames,
const AudioFileParserParameters& params)
{
std::vector artistNames{ getTagValuesFirstMatchAs(tagReader, artistTagNames, params.artistTagDelimiters, ¶ms.artistsToNotSplit) };
if (artistNames.empty())
return {};
std::vector artistSortNames{ getTagValuesFirstMatchAs(tagReader, artistSortTagNames, params.artistTagDelimiters, ¶ms.artistsToNotSplit) };
std::vector artistMBIDs{ getTagValuesFirstMatchAs(tagReader, artistMBIDTagNames, params.defaultTagDelimiters) };
std::vector artists;
artists.reserve(artistNames.size());
for (std::size_t i{}; i < artistNames.size(); ++i)
{
Artist& artist{ artists.emplace_back(std::move(artistNames[i])) };
if (artistNames.size() == artistSortNames.size())
artist.sortName = std::move(artistSortNames[i]);
if (artistNames.size() == artistMBIDs.size())
artist.mbid = std::move(artistMBIDs[i]);
}
return artists;
}
PerformerContainer getPerformerArtists(const ITagReader& tagReader)
{
PerformerContainer performers;
tagReader.visitPerformerTags([&](std::string_view role, std::string_view name) {
// picard stores like this: (see https://picard-docs.musicbrainz.org/en/appendices/tag_mapping.html#performer)
// We consider we may hit both styles for the same track
if (role.empty())
{
// "PERFORMER" "artist (role)"
utils::PerformerArtist performer{ utils::extractPerformerAndRole(name) };
core::stringUtils::capitalize(performer.role);
performers[performer.role].push_back(std::move(performer.artist));
}
else
{
// "PERFORMER:role", "artist" (MP3)
std::string roleCapitalized{ core::stringUtils::stringToLower(role) };
core::stringUtils::capitalize(roleCapitalized);
performers[roleCapitalized].push_back(Artist{ name });
}
});
return performers;
}
bool strIsMatchingArtistNames(std::string_view str, std::span artistNames)
{
std::string_view::size_type currentOffset{};
for (const std::string_view artistName : artistNames)
{
std::string_view::size_type newPos{ str.find(artistName, currentOffset) };
if (newPos == std::string_view::npos)
return false;
currentOffset = newPos + artistName.size();
}
return true;
}
bool strIsContainingAny(std::string_view str, std::span subStrs)
{
return std::any_of(std::cbegin(subStrs), std::cend(subStrs), [&str](const std::string& subStr) { return str.find(subStr) != std::string_view::npos; });
}
std::string computeArtistDisplayName(std::span artists, const std::optional& artistTag, std::span artistTagDelimiters)
{
std::string artistDisplayName;
if (artists.size() == 1)
artistDisplayName = artists.front().name;
else if (artists.size() > 1)
{
std::vector artistNames;
std::transform(std::cbegin(artists), std::cend(artists), std::back_inserter(artistNames), [](const Artist& artist) -> std::string_view { return artist.name; });
// Picard use case: if we manage to match all artists in the "artist" tag (considered single-valued), and if no custom delimiter is hit, we use it as the display name
// Otherwise, we reconstruct the string using a standard, hardcoded, join
if (artistTag && strIsMatchingArtistNames(*artistTag, artistNames))
{
// Limitation: this test does not take the whitelist into account
if (!strIsContainingAny(*artistTag, artistTagDelimiters))
artistDisplayName = *artistTag;
}
if (artistDisplayName.empty())
artistDisplayName = core::stringUtils::joinStrings(artistNames, ", ");
}
return artistDisplayName;
}
std::optional getAdvisory(const ITagReader& tagReader)
{
if (const auto value{ getTagValueAs(tagReader, TagType::Advisory) })
{
switch (*value)
{
case 1:
case 4:
return Track::Advisory::Explicit;
case 2:
return Track::Advisory::Clean;
case 0:
return Track::Advisory::Unknown;
}
}
return std::nullopt;
}
} // namespace
std::unique_ptr createAudioFileParser(const AudioFileParserParameters& params)
{
return std::make_unique(params);
}
AudioFileParser::AudioFileParser(const AudioFileParserParameters& params)
: _params{ params }
{
switch (_params.backend)
{
case ParserBackend::TagLib:
LMS_LOG(METADATA, INFO, "Using TagLib parser with read style = " << utils::readStyleToString(_params.readStyle));
break;
case ParserBackend::AvFormat:
LMS_LOG(METADATA, INFO, "Using AvFormat parser");
break;
}
}
std::span AudioFileParser::getSupportedExtensions() const
{
// TODO: use backend capability to retrieve supported formats
static const std::array fileExtensions{
".aac",
".alac",
".aif",
".aiff",
".ape",
".dsf",
".flac",
".m4a",
".m4b",
".mp3",
".mpc",
".oga",
".ogg",
".opus",
".shn",
".wav",
".wma",
".wv",
};
return fileExtensions;
}
std::unique_ptr