Timer.cxx 1.56 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "config.h"
21
#include "Timer.hxx"
22
#include "AudioFormat.hxx"
23

24 25
#include <limits>

26
#include <assert.h>
27

28
Timer::Timer(const AudioFormat af)
29
	:rate(af.sample_rate * af.GetFrameSize())
30 31 32
{
}

33
void Timer::Start()
34
{
35
	time = Now();
36
	started = true;
37 38
}

39
void Timer::Reset()
40
{
41
	started = false;
42 43
}

44 45
void
Timer::Add(size_t size)
46
{
47
	assert(started);
48

49 50
	// (size samples) / (rate samples per second) = duration seconds
	// duration seconds * 1000000 = duration us
51
	time += Time(((uint64_t)size * Time::period::den) / (Time::period::num * rate));
52 53
}

54 55
std::chrono::steady_clock::duration
Timer::GetDelay() const
56
{
57 58
	assert(started);

59 60 61
	const auto delay = time - Now();
	if (delay < Time::zero())
		return Time::zero();
62

63
	return std::chrono::duration_cast<std::chrono::steady_clock::duration>(delay);
64
}