mpd/src/output/Timer.cxx

62 lines
1.5 KiB
C++
Raw Normal View History

/*
2020-01-18 19:22:19 +01:00
* Copyright 2003-2020 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
2013-05-12 15:03:42 +02:00
#include "Timer.hxx"
2020-01-18 20:07:09 +01:00
#include "pcm/AudioFormat.hxx"
#include <assert.h>
2013-08-03 21:00:50 +02:00
Timer::Timer(const AudioFormat af)
2016-12-28 10:11:48 +01:00
:rate(af.sample_rate * af.GetFrameSize())
{
}
2013-05-12 15:03:42 +02:00
void Timer::Start()
{
2016-12-28 10:11:07 +01:00
time = Now();
2013-05-12 15:03:42 +02:00
started = true;
}
2013-05-12 15:03:42 +02:00
void Timer::Reset()
{
2013-05-12 15:03:42 +02:00
started = false;
}
2016-12-28 10:17:29 +01:00
void
Timer::Add(size_t size)
{
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
Timer::GetDelay() const
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
}