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