Commit 6ad93398 authored by Max Kellermann's avatar Max Kellermann

DetachedSong: use std::chrono::duration for start_ms and end_ms

parent 854258f3
...@@ -28,11 +28,12 @@ DetachedSong::DetachedSong(const LightSong &other) ...@@ -28,11 +28,12 @@ DetachedSong::DetachedSong(const LightSong &other)
real_uri(other.real_uri != nullptr ? other.real_uri : ""), real_uri(other.real_uri != nullptr ? other.real_uri : ""),
tag(*other.tag), tag(*other.tag),
mtime(other.mtime), mtime(other.mtime),
start_ms(other.start_ms), end_ms(other.end_ms) {} start_time(SongTime::FromMS(other.start_ms)),
end_time(SongTime::FromMS(other.end_ms)) {}
DetachedSong::~DetachedSong() DetachedSong::~DetachedSong()
{ {
/* this destructor exists here just so it won't get inlined */ /* this destructor exists here just so it won't inlined */
} }
bool bool
...@@ -60,8 +61,8 @@ DetachedSong::IsInDatabase() const ...@@ -60,8 +61,8 @@ DetachedSong::IsInDatabase() const
double double
DetachedSong::GetDuration() const DetachedSong::GetDuration() const
{ {
if (end_ms > 0) if (end_time.IsPositive())
return (end_ms - start_ms) / 1000.0; return (end_time - start_time).ToDoubleS();
return tag.time - start_ms / 1000.0; return tag.time - start_time.ToDoubleS();
} }
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "check.h" #include "check.h"
#include "tag/Tag.hxx" #include "tag/Tag.hxx"
#include "Chrono.hxx"
#include "Compiler.h" #include "Compiler.h"
#include <string> #include <string>
...@@ -65,15 +66,15 @@ class DetachedSong { ...@@ -65,15 +66,15 @@ class DetachedSong {
time_t mtime; time_t mtime;
/** /**
* Start of this sub-song within the file in milliseconds. * Start of this sub-song within the file.
*/ */
unsigned start_ms; SongTime start_time;
/** /**
* End of this sub-song within the file in milliseconds. * End of this sub-song within the file.
* Unused if zero. * Unused if zero.
*/ */
unsigned end_ms; SongTime end_time;
explicit DetachedSong(const LightSong &other); explicit DetachedSong(const LightSong &other);
...@@ -82,21 +83,25 @@ public: ...@@ -82,21 +83,25 @@ public:
explicit DetachedSong(const char *_uri) explicit DetachedSong(const char *_uri)
:uri(_uri), :uri(_uri),
mtime(0), start_ms(0), end_ms(0) {} mtime(0),
start_time(SongTime::zero()), end_time(SongTime::zero()) {}
explicit DetachedSong(const std::string &_uri) explicit DetachedSong(const std::string &_uri)
:uri(_uri), :uri(_uri),
mtime(0), start_ms(0), end_ms(0) {} mtime(0),
start_time(SongTime::zero()), end_time(SongTime::zero()) {}
explicit DetachedSong(std::string &&_uri) explicit DetachedSong(std::string &&_uri)
:uri(std::move(_uri)), :uri(std::move(_uri)),
mtime(0), start_ms(0), end_ms(0) {} mtime(0),
start_time(SongTime::zero()), end_time(SongTime::zero()) {}
template<typename U> template<typename U>
DetachedSong(U &&_uri, Tag &&_tag) DetachedSong(U &&_uri, Tag &&_tag)
:uri(std::forward<U>(_uri)), :uri(std::forward<U>(_uri)),
tag(std::move(_tag)), tag(std::move(_tag)),
mtime(0), start_ms(0), end_ms(0) {} mtime(0),
start_time(SongTime::zero()), end_time(SongTime::zero()) {}
DetachedSong(DetachedSong &&) = default; DetachedSong(DetachedSong &&) = default;
...@@ -191,20 +196,20 @@ public: ...@@ -191,20 +196,20 @@ public:
mtime = _value; mtime = _value;
} }
unsigned GetStartMS() const { SongTime GetStartTime() const {
return start_ms; return start_time;
} }
void SetStartMS(unsigned _value) { void SetStartTime(SongTime _value) {
start_ms = _value; start_time = _value;
} }
unsigned GetEndMS() const { SongTime GetEndTime() const {
return end_ms; return end_time;
} }
void SetEndMS(unsigned _value) { void SetEndTime(SongTime _value) {
end_ms = _value; end_time = _value;
} }
gcc_pure gcc_pure
......
...@@ -291,12 +291,12 @@ Player::StartDecoder(MusicPipe &_pipe) ...@@ -291,12 +291,12 @@ Player::StartDecoder(MusicPipe &_pipe)
assert(queued || pc.command == PlayerCommand::SEEK); assert(queued || pc.command == PlayerCommand::SEEK);
assert(pc.next_song != nullptr); assert(pc.next_song != nullptr);
unsigned start_ms = pc.next_song->GetStartMS(); SongTime start_time = pc.next_song->GetStartTime();
if (pc.command == PlayerCommand::SEEK) if (pc.command == PlayerCommand::SEEK)
start_ms += pc.seek_time.ToMS(); start_time += pc.seek_time;
dc.Start(new DetachedSong(*pc.next_song), dc.Start(new DetachedSong(*pc.next_song),
start_ms, pc.next_song->GetEndMS(), start_time.ToMS(), pc.next_song->GetEndTime().ToMS(),
buffer, _pipe); buffer, _pipe);
} }
...@@ -376,13 +376,13 @@ real_song_duration(const DetachedSong &song, double decoder_duration) ...@@ -376,13 +376,13 @@ real_song_duration(const DetachedSong &song, double decoder_duration)
back to Song::GetDuration() */ back to Song::GetDuration() */
return song.GetDuration(); return song.GetDuration();
const unsigned start_ms = song.GetStartMS(); const SongTime start_time = song.GetStartTime();
const unsigned end_ms = song.GetEndMS(); const SongTime end_time = song.GetEndTime();
if (end_ms > 0 && end_ms / 1000.0 < decoder_duration) if (end_time.IsPositive() && end_time.ToDoubleS() < decoder_duration)
return (end_ms - start_ms) / 1000.0; return (end_time - start_time).ToDoubleS();
return decoder_duration - start_ms / 1000.0; return decoder_duration - start_time.ToDoubleS();
} }
bool bool
...@@ -518,7 +518,7 @@ Player::SeekDecoder() ...@@ -518,7 +518,7 @@ Player::SeekDecoder()
{ {
assert(pc.next_song != nullptr); assert(pc.next_song != nullptr);
const unsigned start_ms = pc.next_song->GetStartMS(); const SongTime start_time = pc.next_song->GetStartTime();
if (!dc.LockIsCurrentSong(*pc.next_song)) { if (!dc.LockIsCurrentSong(*pc.next_song)) {
/* the decoder is already decoding the "next" song - /* the decoder is already decoding the "next" song -
...@@ -568,7 +568,7 @@ Player::SeekDecoder() ...@@ -568,7 +568,7 @@ Player::SeekDecoder()
where = total_time; where = total_time;
} }
if (!dc.Seek(where + SongTime::FromMS(start_ms))) { if (!dc.Seek(where + start_time)) {
/* decoder failure */ /* decoder failure */
player_command_finished(pc); player_command_finished(pc);
return false; return false;
......
...@@ -97,8 +97,8 @@ song_print_info(Client &client, const DetachedSong &song, bool base) ...@@ -97,8 +97,8 @@ song_print_info(Client &client, const DetachedSong &song, bool base)
{ {
song_print_uri(client, song, base); song_print_uri(client, song, base);
const unsigned start_ms = song.GetStartMS(); const unsigned start_ms = song.GetStartTime().ToMS();
const unsigned end_ms = song.GetEndMS(); const unsigned end_ms = song.GetEndTime().ToMS();
if (end_ms > 0) if (end_ms > 0)
client_printf(client, "Range: %u.%03u-%u.%03u\n", client_printf(client, "Range: %u.%03u-%u.%03u\n",
......
...@@ -65,7 +65,7 @@ song_save(BufferedOutputStream &os, const DetachedSong &song) ...@@ -65,7 +65,7 @@ song_save(BufferedOutputStream &os, const DetachedSong &song)
{ {
os.Format(SONG_BEGIN "%s\n", song.GetURI()); os.Format(SONG_BEGIN "%s\n", song.GetURI());
range_save(os, song.GetStartMS(), song.GetEndMS()); range_save(os, song.GetStartTime().ToMS(), song.GetEndTime().ToMS());
tag_save(os, song.GetTag()); tag_save(os, song.GetTag());
...@@ -113,8 +113,8 @@ song_load(TextFile &file, const char *uri, ...@@ -113,8 +113,8 @@ song_load(TextFile &file, const char *uri,
? strtoul(endptr + 1, nullptr, 10) ? strtoul(endptr + 1, nullptr, 10)
: 0; : 0;
song->SetStartMS(start_ms); song->SetStartTime(SongTime::FromMS(start_ms));
song->SetEndMS(end_ms); song->SetEndTime(SongTime::FromMS(end_ms));
} else { } else {
delete song; delete song;
......
...@@ -59,8 +59,8 @@ Song::NewFrom(DetachedSong &&other, Directory &parent) ...@@ -59,8 +59,8 @@ Song::NewFrom(DetachedSong &&other, Directory &parent)
Song *song = song_alloc(other.GetURI(), parent); Song *song = song_alloc(other.GetURI(), parent);
song->tag = std::move(other.WritableTag()); song->tag = std::move(other.WritableTag());
song->mtime = other.GetLastModified(); song->mtime = other.GetLastModified();
song->start_ms = other.GetStartMS(); song->start_ms = other.GetStartTime().ToMS();
song->end_ms = other.GetEndMS(); song->end_ms = other.GetEndTime().ToMS();
return song; return song;
} }
......
...@@ -521,7 +521,7 @@ decoder_data(Decoder &decoder, ...@@ -521,7 +521,7 @@ decoder_data(Decoder &decoder,
const auto dest = const auto dest =
chunk->Write(dc.out_audio_format, chunk->Write(dc.out_audio_format,
decoder.timestamp - decoder.timestamp -
dc.song->GetStartMS() / 1000.0, dc.song->GetStartTime().ToDoubleS(),
kbit_rate); kbit_rate);
if (dest.IsNull()) { if (dest.IsNull()) {
/* the chunk is full, flush it */ /* the chunk is full, flush it */
......
...@@ -267,12 +267,12 @@ CueParser::Feed2(char *p) ...@@ -267,12 +267,12 @@ CueParser::Feed2(char *p)
return; return;
if (!last_updated && previous != nullptr && if (!last_updated && previous != nullptr &&
previous->GetStartMS() < (unsigned)position_ms) { previous->GetStartTime().ToMS() < (unsigned)position_ms) {
last_updated = true; last_updated = true;
previous->SetEndMS(position_ms); previous->SetEndTime(SongTime::FromMS(position_ms));
} }
current->SetStartMS(position_ms); current->SetStartTime(SongTime::FromMS(position_ms));
} }
} }
......
...@@ -479,8 +479,8 @@ playlist::SetSongIdRange(PlayerControl &pc, unsigned id, ...@@ -479,8 +479,8 @@ playlist::SetSongIdRange(PlayerControl &pc, unsigned id,
} }
/* edit it */ /* edit it */
song.SetStartMS(start_ms); song.SetStartTime(SongTime::FromMS(start_ms));
song.SetEndMS(end_ms); song.SetEndTime(SongTime::FromMS(end_ms));
/* announce the change to all interested subsystems */ /* announce the change to all interested subsystems */
UpdateQueuedSong(pc, nullptr); UpdateQueuedSong(pc, nullptr);
......
...@@ -53,7 +53,7 @@ static void ...@@ -53,7 +53,7 @@ static void
queue_save_song(BufferedOutputStream &os, int idx, const DetachedSong &song) queue_save_song(BufferedOutputStream &os, int idx, const DetachedSong &song)
{ {
if (song.IsInDatabase() && if (song.IsInDatabase() &&
song.GetStartMS() == 0 && song.GetEndMS() == 0) song.GetStartTime().IsZero() && song.GetEndTime().IsZero())
/* use the brief format (just the URI) for "full" /* use the brief format (just the URI) for "full"
database songs */ database songs */
queue_save_database_song(os, idx, song); queue_save_database_song(os, idx, song);
......
...@@ -128,8 +128,8 @@ int main(int argc, char **argv) ...@@ -128,8 +128,8 @@ int main(int argc, char **argv)
while ((song = playlist->NextSong()) != NULL) { while ((song = playlist->NextSong()) != NULL) {
printf("%s\n", song->GetURI()); printf("%s\n", song->GetURI());
const unsigned start_ms = song->GetStartMS(); const unsigned start_ms = song->GetStartTime().ToMS();
const unsigned end_ms = song->GetEndMS(); const unsigned end_ms = song->GetEndTime().ToMS();
if (end_ms > 0) if (end_ms > 0)
printf("range: %u:%02u..%u:%02u\n", printf("range: %u:%02u..%u:%02u\n",
......
...@@ -185,15 +185,15 @@ ToString(const DetachedSong &song) ...@@ -185,15 +185,15 @@ ToString(const DetachedSong &song)
result.push_back('|'); result.push_back('|');
if (song.GetStartMS() > 0) { if (song.GetStartTime().IsPositive()) {
sprintf(buffer, "%u", song.GetStartMS()); sprintf(buffer, "%u", song.GetStartTime().ToMS());
result.append(buffer); result.append(buffer);
} }
result.push_back('-'); result.push_back('-');
if (song.GetEndMS() > 0) { if (song.GetEndTime().IsPositive()) {
sprintf(buffer, "%u", song.GetEndMS()); sprintf(buffer, "%u", song.GetEndTime().ToMS());
result.append(buffer); result.append(buffer);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment