mpd/src/output/Timer.cxx

48 lines
886 B
C++
Raw Normal View History

// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project
2013-05-12 15:03:42 +02:00
#include "Timer.hxx"
2020-01-18 20:07:09 +01:00
#include "pcm/AudioFormat.hxx"
#include <cassert>
2022-07-08 22:57:27 +02:00
Timer::Timer(const AudioFormat af) noexcept
2016-12-28 10:11:48 +01:00
:rate(af.sample_rate * af.GetFrameSize())
{
}
2022-07-08 22:57:27 +02:00
void
Timer::Start() noexcept
{
2016-12-28 10:11:07 +01:00
time = Now();
2013-05-12 15:03:42 +02:00
started = true;
}
2022-07-08 22:57:27 +02:00
void
Timer::Reset() noexcept
{
2013-05-12 15:03:42 +02:00
started = false;
}
2016-12-28 10:17:29 +01:00
void
2022-07-08 22:57:27 +02:00
Timer::Add(size_t size) noexcept
{
2013-05-12 15:03:42 +02:00
assert(started);
// (size samples) / (rate samples per second) = duration seconds
// duration seconds * 1000000 = duration us
2016-12-28 10:11:07 +01:00
time += Time(((uint64_t)size * Time::period::den) / (Time::period::num * rate));
}
2016-12-28 10:11:07 +01:00
std::chrono::steady_clock::duration
2022-07-08 22:57:27 +02:00
Timer::GetDelay() const noexcept
2010-11-05 09:39:50 +01:00
{
assert(started);
2016-12-28 10:11:07 +01:00
const auto delay = time - Now();
if (delay < Time::zero())
return Time::zero();
2010-11-05 09:39:50 +01:00
2016-12-28 10:11:07 +01:00
return std::chrono::duration_cast<std::chrono::steady_clock::duration>(delay);
2010-11-05 09:39:50 +01:00
}