Sound Byte Libs 0.5.1-121-g3358a44
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
sample_clock.hpp
Go to the documentation of this file.
1/**
2 * @file sample_clock.hpp
3 * @brief Audio-clock frame counter — the elapsed-samples source for control code
4 * @ingroup hal
5 *
6 * Counts stereo frames the audio driver has handed to the codec since boot.
7 * The count advances only with the audio clock, so control code that needs
8 * elapsed time (the FDP-073 scheduler's `tick(elapsed_samples)`) reads it
9 * instead of assuming a timer rate: the 1 kHz control timer is not
10 * sample-locked to the codec.
11 *
12 * One writer: the audio driver calls advance() once per block, after the
13 * application callback returns. Any context may call now(). Consumers take
14 * unsigned deltas, which stay correct across the wrap every 2^32 frames
15 * (~24.8 h at 48 kHz).
16 *
17 * The counter is a relaxed atomic: race-free between the audio thread and
18 * the control thread on the workbench, plain LDR/STR on Cortex-M7. It
19 * publishes no other data, so no ordering is needed.
20 */
21
22#ifndef SBL_HW_HAL_AUDIO_SAMPLE_CLOCK_HPP_
23#define SBL_HW_HAL_AUDIO_SAMPLE_CLOCK_HPP_
24
25#include <atomic>
26#include <cstdint>
27
28namespace sbl {
29namespace hal {
30namespace audio {
31
33public:
34 /// Frames clocked out since boot (wraps; take unsigned deltas).
35 static uint32_t now() {
36 return frames_.load(std::memory_order_relaxed);
37 }
38
39 /// Add one block's frames. Audio driver only — single writer, so a
40 /// load/store pair replaces a read-modify-write (no LDREX/STREX).
41 static void advance(uint32_t frames) {
42 frames_.store(frames_.load(std::memory_order_relaxed) + frames,
43 std::memory_order_relaxed);
44 }
45
46private:
47 static_assert(std::atomic<uint32_t>::is_always_lock_free,
48 "SampleClock needs a lock-free 32-bit atomic");
49
50 static inline std::atomic<uint32_t> frames_{0};
51};
52
53} // namespace audio
54} // namespace hal
55} // namespace sbl
56
57#endif // SBL_HW_HAL_AUDIO_SAMPLE_CLOCK_HPP_
static void advance(uint32_t frames)
static uint32_t now()
Frames clocked out since boot (wraps; take unsigned deltas).
Root namespace for all Sound Byte Libs code.
Definition assert.hpp:51