Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
clock.hpp
Go to the documentation of this file.
1/**
2 * @file clock.hpp
3 * @brief The control clock: the app's one control timebase
4 * @ingroup control
5 *
6 * Clock<Source> reads a source with `now()` (ticks, wrapping at 2^32) and a
7 * constexpr `TICKS_PER_SECOND`, and adds what control code needs: elapsed
8 * ticks between two readings and the conversion of a time to ticks when the
9 * time is set, in 64-bit arithmetic so no rate truncates. The
10 * template is unit-tested over a fake source; target code uses the alias in
11 * target_clock.hpp, whose source is chosen per target at compile time.
12 *
13 * The scheduler passes `now()` to run(); subscribers receive elapsed ticks.
14 * `dsp/` never sees the clock; it counts frames in `process(n)`.
15 */
16
17#ifndef SBL_CONTROL_CLOCK_HPP_
18#define SBL_CONTROL_CLOCK_HPP_
19
20#include <cstdint>
21
22namespace sbl::control {
23
24template<class Source>
25class Clock {
26public:
27 static constexpr uint32_t TICKS_PER_SECOND = Source::TICKS_PER_SECOND;
28 static_assert(TICKS_PER_SECOND > 0, "a clock source needs a rate");
29
30 /** The tick now. Wraps at 2^32; take unsigned deltas. */
31 static uint32_t now() { return Source::now(); }
32
33 /** Ticks from `earlier` to `later`, correct across one wrap. */
34 static constexpr uint32_t elapsed(uint32_t earlier, uint32_t later) { return later - earlier; }
35
36 /** A time in milliseconds as ticks, rounded down. Beyond 2^32 ticks the result wraps. */
37 static constexpr uint32_t ticks_from_ms(uint32_t ms) {
38 return static_cast<uint32_t>(static_cast<uint64_t>(ms) * TICKS_PER_SECOND / 1000u);
39 }
40
41 /** A time in microseconds as ticks, rounded down. Beyond 2^32 ticks the result wraps. */
42 static constexpr uint32_t ticks_from_us(uint32_t us) {
43 return static_cast<uint32_t>(static_cast<uint64_t>(us) * TICKS_PER_SECOND / 1'000'000u);
44 }
45};
46
47} // namespace sbl::control
48
49#endif // SBL_CONTROL_CLOCK_HPP_
static constexpr uint32_t elapsed(uint32_t earlier, uint32_t later)
Definition clock.hpp:34
static constexpr uint32_t ticks_from_us(uint32_t us)
Definition clock.hpp:42
static uint32_t now()
Definition clock.hpp:31
static constexpr uint32_t ticks_from_ms(uint32_t ms)
Definition clock.hpp:37
The control stack: scheduler, event queue, merge, CC mapping, calibration.