Timer.cxx 1.52 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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 "pcm/AudioFormat.hxx"
22

23
#include <cassert>
24

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

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

36
void Timer::Reset()
37
{
38
	started = false;
39 40
}

41 42
void
Timer::Add(size_t size)
43
{
44
	assert(started);
45

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

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

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

60
	return std::chrono::duration_cast<std::chrono::steady_clock::duration>(delay);
61
}