mpd/src/output/Timer.hxx

41 lines
781 B
C++
Raw Normal View History

// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project
2013-05-12 19:03:42 +06:00
#ifndef MPD_TIMER_HXX
#define MPD_TIMER_HXX
2016-12-28 10:11:07 +01:00
#include <chrono>
2013-08-03 21:00:50 +02:00
struct AudioFormat;
2013-05-12 19:03:42 +06:00
class Timer {
2016-12-28 10:11:07 +01:00
typedef std::chrono::microseconds Time;
Time time;
2016-12-28 10:11:48 +01:00
bool started = false;
2013-05-12 19:03:42 +06:00
const int rate;
public:
2022-07-08 22:57:27 +02:00
explicit Timer(AudioFormat af) noexcept;
2013-01-15 18:58:02 +01:00
2022-07-08 22:57:27 +02:00
bool IsStarted() const noexcept { return started; }
2022-07-08 22:57:27 +02:00
void Start() noexcept;
void Reset() noexcept;
2022-07-08 22:57:27 +02:00
void Add(size_t size) noexcept;
2013-05-12 19:03:42 +06:00
/**
2016-12-28 10:11:07 +01:00
* Returns the duration to sleep to get back to sync.
2013-05-12 19:03:42 +06:00
*/
2022-07-08 22:57:27 +02:00
[[gnu::pure]]
std::chrono::steady_clock::duration GetDelay() const noexcept;
2016-12-28 10:11:07 +01:00
private:
2022-07-08 22:57:27 +02:00
[[gnu::pure]]
static Time Now() noexcept {
2016-12-28 10:11:07 +01:00
return std::chrono::duration_cast<Time>(std::chrono::steady_clock::now().time_since_epoch());
}
2013-05-12 19:03:42 +06:00
};
2013-01-15 18:58:02 +01:00
#endif