Tag: use SignedSongTime for the song duration

This commit is contained in:
Max Kellermann
2014-08-29 12:14:27 +02:00
parent 8ce30c6a69
commit 7c25d83f1c
25 changed files with 103 additions and 84 deletions

View File

@@ -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;
}

View File

@@ -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) {

View File

@@ -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);
}

View File

@@ -87,7 +87,7 @@ struct LightSong {
}
gcc_pure
double GetDuration() const;
SignedSongTime GetDuration() const;
};
#endif

View File

@@ -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);

View File

@@ -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;
}