2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2007-05-30 20:16:35 +02:00
|
|
|
|
2013-05-12 15:03:42 +02:00
|
|
|
#include "Timer.hxx"
|
2020-01-18 20:07:09 +01:00
|
|
|
#include "pcm/AudioFormat.hxx"
|
2008-10-08 10:49:29 +02:00
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2007-05-30 20:16:35 +02:00
|
|
|
|
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())
|
2007-05-30 20:16:35 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-07-08 22:57:27 +02:00
|
|
|
void
|
|
|
|
Timer::Start() noexcept
|
2007-05-30 20:16:35 +02:00
|
|
|
{
|
2016-12-28 10:11:07 +01:00
|
|
|
time = Now();
|
2013-05-12 15:03:42 +02:00
|
|
|
started = true;
|
2007-05-30 20:16:35 +02:00
|
|
|
}
|
|
|
|
|
2022-07-08 22:57:27 +02:00
|
|
|
void
|
|
|
|
Timer::Reset() noexcept
|
2007-05-30 20:16:35 +02:00
|
|
|
{
|
2013-05-12 15:03:42 +02:00
|
|
|
started = false;
|
2007-05-30 20:16:35 +02:00
|
|
|
}
|
|
|
|
|
2016-12-28 10:17:29 +01:00
|
|
|
void
|
2022-07-08 22:57:27 +02:00
|
|
|
Timer::Add(size_t size) noexcept
|
2007-05-30 20:16:35 +02:00
|
|
|
{
|
2013-05-12 15:03:42 +02:00
|
|
|
assert(started);
|
2007-05-30 20:16:35 +02:00
|
|
|
|
2013-02-24 18:19:55 +01:00
|
|
|
// (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));
|
2007-05-30 20:16:35 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2016-12-28 10:16:11 +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
|
|
|
}
|