From 7c25d83f1cc4c7db2d2d3f4506525dd056b885e8 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 29 Aug 2014 12:14:27 +0200 Subject: Tag: use SignedSongTime for the song duration --- src/db/Count.cxx | 9 ++++++--- src/db/Helpers.cxx | 4 ++-- src/db/LightSong.cxx | 14 ++++++++------ src/db/LightSong.hxx | 2 +- src/db/plugins/ProxyDatabasePlugin.cxx | 5 ++++- src/db/plugins/upnp/Directory.cxx | 12 ++++++------ 6 files changed, 27 insertions(+), 19 deletions(-) (limited to 'src/db') diff --git a/src/db/Count.cxx b/src/db/Count.cxx index 4fd53a73b..fecd901e5 100644 --- a/src/db/Count.cxx +++ b/src/db/Count.cxx @@ -64,7 +64,10 @@ static bool stats_visitor_song(SearchStats &stats, const LightSong &song) { stats.n_songs++; - stats.total_time_s += song.GetDuration(); + + const auto duration = song.GetDuration(); + if (!duration.IsNegative()) + stats.total_time_s += duration.ToS(); return true; } @@ -79,8 +82,8 @@ CollectGroupCounts(TagCountMap &map, TagType group, const Tag &tag) SearchStats())); SearchStats &s = r.first->second; ++s.n_songs; - if (tag.time > 0) - s.total_time_s += tag.time; + if (!tag.duration.IsNegative()) + s.total_time_s += tag.duration.ToS(); found = true; } diff --git a/src/db/Helpers.cxx b/src/db/Helpers.cxx index 089b2b17d..8df6c265c 100644 --- a/src/db/Helpers.cxx +++ b/src/db/Helpers.cxx @@ -40,8 +40,8 @@ static void StatsVisitTag(DatabaseStats &stats, StringSet &artists, StringSet &albums, const Tag &tag) { - if (tag.time > 0) - stats.total_duration += tag.time; + if (!tag.duration.IsNegative()) + stats.total_duration += tag.duration.ToS(); for (const auto &item : tag) { switch (item.type) { diff --git a/src/db/LightSong.cxx b/src/db/LightSong.cxx index 3e0f361d8..5cdebc133 100644 --- a/src/db/LightSong.cxx +++ b/src/db/LightSong.cxx @@ -20,14 +20,16 @@ #include "LightSong.hxx" #include "tag/Tag.hxx" -double +SignedSongTime LightSong::GetDuration() const { - if (end_time.IsPositive()) - return (end_time - start_time).ToDoubleS(); + SongTime a = start_time, b = end_time; + if (!b.IsPositive()) { + if (tag->duration.IsNegative()) + return tag->duration; - if (tag->time <= 0) - return 0; + b = SongTime(tag->duration); + } - return tag->time - start_time.ToDoubleS(); + return SignedSongTime(b - a); } diff --git a/src/db/LightSong.hxx b/src/db/LightSong.hxx index 26f68d18c..bbd449fbe 100644 --- a/src/db/LightSong.hxx +++ b/src/db/LightSong.hxx @@ -87,7 +87,7 @@ struct LightSong { } gcc_pure - double GetDuration() const; + SignedSongTime GetDuration() const; }; #endif diff --git a/src/db/plugins/ProxyDatabasePlugin.cxx b/src/db/plugins/ProxyDatabasePlugin.cxx index c6b57d248..97b544203 100644 --- a/src/db/plugins/ProxyDatabasePlugin.cxx +++ b/src/db/plugins/ProxyDatabasePlugin.cxx @@ -199,7 +199,10 @@ ProxySong::ProxySong(const mpd_song *song) #endif TagBuilder tag_builder; - tag_builder.SetTime(mpd_song_get_duration(song)); + + const unsigned duration = mpd_song_get_duration(song); + if (duration > 0) + tag_builder.SetDuration(SignedSongTime::FromS(duration)); for (const auto *i = &tag_table[0]; i->d != TAG_NUM_OF_ITEM_TYPES; ++i) Copy(tag_builder, i->d, song, i->s); diff --git a/src/db/plugins/upnp/Directory.cxx b/src/db/plugins/upnp/Directory.cxx index e43cd48a6..683022a10 100644 --- a/src/db/plugins/upnp/Directory.cxx +++ b/src/db/plugins/upnp/Directory.cxx @@ -59,28 +59,28 @@ ParseItemClass(const char *name, size_t length) } gcc_pure -static int +static SignedSongTime ParseDuration(const char *duration) { char *endptr; unsigned result = ParseUnsigned(duration, &endptr); if (endptr == duration || *endptr != ':') - return 0; + return SignedSongTime::Negative(); result *= 60; duration = endptr + 1; result += ParseUnsigned(duration, &endptr); if (endptr == duration || *endptr != ':') - return 0; + return SignedSongTime::Negative(); result *= 60; duration = endptr + 1; result += ParseUnsigned(duration, &endptr); if (endptr == duration || *endptr != 0) - return 0; + return SignedSongTime::Negative(); - return result; + return SignedSongTime::FromS(result); } /** @@ -183,7 +183,7 @@ protected: const char *duration = GetAttribute(attrs, "duration"); if (duration != nullptr) - tag.SetTime(ParseDuration(duration)); + tag.SetDuration(ParseDuration(duration)); state = RES; } -- cgit v1.2.3