Timer.cxx 1.54 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "Timer.hxx"
21
#include "AudioFormat.hxx"
22

23 24
#include <limits>

25
#include <assert.h>
26

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

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

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

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

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

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

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

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