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
frame_count.hpp
Go to the documentation of this file.
1
/**
2
* @file frame_count.hpp
3
* @brief The audio driver's frame count and the frame stamp
4
* @ingroup hal
5
*
6
* FrameCount counts the stereo frames the audio driver has delivered to the
7
* codec since boot. It is driver state, not a clock: control time is
8
* control/clock.hpp over the system timer. One writer, the audio driver,
9
* which stamps before each block's callback and advances after it; any
10
* context may read. Readers take unsigned deltas, correct across the wrap
11
* every 2^32 frames (about 24.8 h at 48 kHz).
12
*
13
* The frame stamp pairs the count with the system timer: at each driver
14
* period, before the app callback, the driver records `{frames, ticks}`,
15
* `frames` being the first frame of the block the callback computes and
16
* `ticks` the system timer's count then. It is written in the audio ISR and
17
* read in the audio callback - one context, so the two words cannot tear. A
18
* reader in another context would need a sequence counter, which the stamp
19
* does not have.
20
*
21
* The counter is a relaxed atomic: race-free between the audio thread and
22
* the control thread on the workbench, plain LDR/STR on Cortex-M7. It
23
* publishes no other data, so no ordering is needed.
24
*/
25
26
#ifndef SBL_HW_HAL_AUDIO_FRAME_COUNT_HPP_
27
#define SBL_HW_HAL_AUDIO_FRAME_COUNT_HPP_
28
29
#include <atomic>
30
#include <cstdint>
31
32
namespace
sbl::hal::audio
{
33
34
/** A block's stamp: its first frame and the system timer's count when the driver recorded it. */
35
struct
FrameStamp
{
36
uint32_t frames = 0;
37
uint32_t ticks = 0;
38
bool
valid
=
false
;
/**< false until the driver's first stamp, and after reset_stamp() */
39
};
40
41
class
FrameCount
{
42
public
:
43
/** Frames delivered since boot (wraps; take unsigned deltas). */
44
static
uint32_t
frames
() {
return
frames_.load(std::memory_order_relaxed); }
45
46
/**
47
* Audio driver only, after a block's callback: add its frames. Single
48
* writer, so a load/store pair replaces a read-modify-write (no LDREX/STREX).
49
*/
50
static
void
advance
(uint32_t
frames
) {
51
frames_.store(frames_.load(std::memory_order_relaxed) +
frames
, std::memory_order_relaxed);
52
}
53
54
/** Audio driver only, before a block's callback: stamp the block it is about to compute. */
55
static
void
stamp
(uint32_t ticks) { stamp_ = {
frames
(), ticks,
true
}; }
56
57
/** The latest stamp, for the audio callback's context (see the file comment). */
58
static
FrameStamp
latest_stamp
() {
return
stamp_; }
59
60
/** Audio driver, on stop: no block is pending, so no stamp is current. The count keeps its value. */
61
static
void
reset_stamp
() { stamp_ = {}; }
62
63
private
:
64
static_assert
(std::atomic<uint32_t>::is_always_lock_free,
"FrameCount needs a lock-free 32-bit atomic"
);
65
66
static
inline
std::atomic<uint32_t> frames_{0};
67
static
inline
FrameStamp stamp_{};
68
};
69
70
}
// namespace sbl::hal::audio
71
72
#endif
// SBL_HW_HAL_AUDIO_FRAME_COUNT_HPP_
sbl::hal::audio::FrameCount
Definition
frame_count.hpp:41
sbl::hal::audio::FrameCount::frames
static uint32_t frames()
Definition
frame_count.hpp:44
sbl::hal::audio::FrameCount::reset_stamp
static void reset_stamp()
Definition
frame_count.hpp:61
sbl::hal::audio::FrameCount::latest_stamp
static FrameStamp latest_stamp()
Definition
frame_count.hpp:58
sbl::hal::audio::FrameCount::advance
static void advance(uint32_t frames)
Definition
frame_count.hpp:50
sbl::hal::audio::FrameCount::stamp
static void stamp(uint32_t ticks)
Definition
frame_count.hpp:55
sbl::hal::audio
Audio streams and the sample clock.
Definition
frame_count.hpp:32
sbl::hal::audio::FrameStamp
Definition
frame_count.hpp:35
sbl::hal::audio::FrameStamp::valid
bool valid
Definition
frame_count.hpp:38
src
sbl
hw
hal
audio
frame_count.hpp
Generated by
1.9.8