From a83f7146adf2a7c281c2852de3d91528c54a8200 Mon Sep 17 00:00:00 2001 From: Lefucjusz Date: Tue, 17 Jan 2023 10:11:30 +0100 Subject: [PATCH] [MOS-767] Fix full path displayed for invalid audio file Fix of the issue that whole file path was displayed instead of just filename in music player for invalid audio file for which Taglib failed to obtain tags. --- module-audio/tags_fetcher/TagsFetcher.cpp | 107 +++++++++++----------- module-audio/tags_fetcher/TagsFetcher.hpp | 12 +-- pure_changelog.md | 1 + 3 files changed, 59 insertions(+), 61 deletions(-) diff --git a/module-audio/tags_fetcher/TagsFetcher.cpp b/module-audio/tags_fetcher/TagsFetcher.cpp index b0e01e9b2eec909852351aa91175a70dc6b5999f..db3ec261cf01d5e9594079cf9d9bacc596353f4e 100644 --- a/module-audio/tags_fetcher/TagsFetcher.cpp +++ b/module-audio/tags_fetcher/TagsFetcher.cpp @@ -1,79 +1,76 @@ -// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #include "TagsFetcher.hpp" - -#include #include - #include "fileref.h" -#include "tag.h" -#include "tfilestream.h" namespace tags::fetcher { - std::optional fetchTagsInternal(std::string filePath) + namespace { - TagLib::FileRef const tagReader(filePath.c_str()); - if (!tagReader.isNull() && (tagReader.tag() != nullptr)) { - TagLib::Tag *tags = tagReader.tag(); - TagLib::AudioProperties *properties = tagReader.audioProperties(); + std::string getTitleFromFilePath(const std::string &path) + { + const auto pos = path.rfind('/'); + if (pos != std::string::npos) { + return path.substr(pos + 1); + } + return path; + } - constexpr auto unicode = true; + std::optional fetchTagsInternal(const std::string &filePath) + { + const TagLib::FileRef tagReader(filePath.c_str()); + const auto tags = tagReader.tag(); + if (!tagReader.isNull() && (tags != nullptr)) { + const auto properties = tagReader.audioProperties(); - auto getTitle = [&]() -> std::string { - const auto title = tags->title().to8Bit(unicode); - // If title tag empty fill it with raw file name + constexpr bool unicode = true; + + const auto artist = tags->artist().to8Bit(unicode); + const auto album = tags->album().to8Bit(unicode); + const auto genre = tags->genre().to8Bit(unicode); + const auto year = tags->year(); + const auto comment = tags->comment().to8Bit(unicode); + const auto track = tags->track(); + + auto title = tags->title().to8Bit(unicode); if (title.empty()) { - if (const auto pos = filePath.rfind('/'); pos == std::string::npos) { - return filePath; - } - else { - return &filePath[pos + 1]; - } + title = getTitleFromFilePath(filePath); } - return title; - }; - const auto artist = tags->artist().to8Bit(unicode); - const auto album = tags->album().to8Bit(unicode); - const auto genre = tags->genre().to8Bit(unicode); - const auto year = tags->year(); - const auto comment = tags->comment().to8Bit(unicode); - const auto track = tags->track(); + const uint32_t total_duration_s = properties->length(); + const uint32_t duration_min = total_duration_s / utils::secondsInMinute; + const uint32_t duration_hour = duration_min / utils::secondsInMinute; + const uint32_t duration_sec = total_duration_s % utils::secondsInMinute; + const uint32_t sample_rate = properties->sampleRate(); + const uint32_t num_channel = properties->channels(); + const uint32_t bitrate = properties->bitrate(); - const uint32_t total_duration_s = properties->length(); - const uint32_t duration_min = total_duration_s / utils::secondsInMinute; - const uint32_t duration_hour = duration_min / utils::secondsInMinute; - const uint32_t duration_sec = total_duration_s % utils::secondsInMinute; - const uint32_t sample_rate = properties->sampleRate(); - const uint32_t num_channel = properties->channels(); - const uint32_t bitrate = properties->bitrate(); - const auto title = getTitle(); + return Tags{total_duration_s, + duration_hour, + duration_min, + duration_sec, + sample_rate, + num_channel, + bitrate, + artist, + genre, + title, + album, + year, + filePath, + comment, + track}; + } - return Tags{total_duration_s, - duration_hour, - duration_min, - duration_sec, - sample_rate, - num_channel, - bitrate, - artist, - genre, - title, - album, - year, - filePath, - comment, - track}; + return {}; } - - return {}; } Tags fetchTags(const std::string &filePath) { - return fetchTagsInternal(filePath).value_or(Tags{filePath}); + return fetchTagsInternal(filePath).value_or(Tags{filePath, getTitleFromFilePath(filePath)}); } } // namespace tags::fetcher diff --git a/module-audio/tags_fetcher/TagsFetcher.hpp b/module-audio/tags_fetcher/TagsFetcher.hpp index 11be9be8ac6a7ca34d69af96649f0d615982cf38..7871d3a5bc5b73b6fe008d1bbf946e1cbcffd2a4 100644 --- a/module-audio/tags_fetcher/TagsFetcher.hpp +++ b/module-audio/tags_fetcher/TagsFetcher.hpp @@ -1,5 +1,5 @@ -// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #pragma once @@ -7,12 +7,12 @@ #include #include #include +#include namespace tags::fetcher { struct Tags { - /* Total audio duration in seconds */ uint32_t total_duration_s = 0; /* Audio duration - hours part */ @@ -55,12 +55,12 @@ namespace tags::fetcher std::string comment, uint32_t track) : total_duration_s{total_duration_s}, duration_hour{duration_hour}, duration_min{duration_min}, - duration_sec{duration_sec}, sample_rate{sample_rate}, - num_channel{num_channel}, bitrate{bitrate}, artist{artist}, genre{genre}, album{album}, year{year}, - filePath{filePath}, title{title}, comment{comment}, track{track} + duration_sec{duration_sec}, sample_rate{sample_rate}, num_channel{num_channel}, bitrate{bitrate}, + artist{std::move(artist)}, genre{std::move(genre)}, album{std::move(album)}, year{year}, + filePath{std::move(filePath)}, title{std::move(title)}, comment{std::move(comment)}, track{track} {} - explicit Tags(std::string filePath) : filePath{filePath}, title{filePath} + Tags(std::string filePath, std::string title) : filePath{std::move(filePath)}, title{std::move(title)} {} }; diff --git a/pure_changelog.md b/pure_changelog.md index c876ea2009722a1b57c6b52120e0b51414fff055..cdaf4c784428ca58c21e9059c9ad88809f6f8536 100644 --- a/pure_changelog.md +++ b/pure_changelog.md @@ -30,6 +30,7 @@ * Fixed crash when syncing with Mudita Center * Fixed inactive alarms after timezone change and reboot. * Fixed no sound when Bluetooth audio device connected/disconnected during call +* Fixed full filesystem path displayed in music player for invalid files instead of just filename ### Added