2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2007-05-30 20:16:35 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2007-05-30 20:16:35 +02:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-05-12 15:03:42 +02:00
|
|
|
#include "Timer.hxx"
|
2013-08-03 21:00:50 +02:00
|
|
|
#include "AudioFormat.hxx"
|
2008-10-08 10:49:29 +02:00
|
|
|
|
2013-10-17 21:53:19 +02:00
|
|
|
#include <limits>
|
|
|
|
|
2008-10-08 10:49:29 +02:00
|
|
|
#include <assert.h>
|
2007-05-30 20:16:35 +02:00
|
|
|
|
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())
|
2007-05-30 20:16:35 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-05-12 15:03:42 +02:00
|
|
|
void Timer::Start()
|
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
|
|
|
}
|
|
|
|
|
2013-05-12 15:03:42 +02:00
|
|
|
void Timer::Reset()
|
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
|
|
|
|
Timer::Add(size_t size)
|
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
|
|
|
|
Timer::GetDelay() const
|
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
|
|
|
}
|