1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
#ifndef AUDIO_RING_H
#define AUDIO_RING_H
// NOTE: single producer (decoder thread) single consumer (pipewire realtime thread) ring of
// interleaved stereo f32 frames.
//
// - read/write positions are monotonically increasing u64 frame counters, never wrapped. the
// slot is (position & mask). that makes "is the marker behind the read head" a plain compare.
// - the producer's and consumer's positions live on separate cache lines, each side also keeps
// a cached copy of the other side's position so it only touches the shared line when its
// cached view says the ring looks full/empty.
// - the consumer never blocks, never allocates, never makes a syscall.
//
// flushing (seek / new track) is done without the producer ever touching the read position:
// the producer records flush_to_frame = write_frame and bumps flush_request. the consumer fades
// out whatever it is playing, then jumps read_frame to flush_to_frame. everything the producer
// writes after the flush is already past that point.
#define AUDIO_CHANNELS 2
typedef enum AudioMarkerKind {
AUDIO_MARKER_TRACK_START = 1,
AUDIO_MARKER_END_OF_STREAM,
} AudioMarkerKind;
// NOTE: tells the consumer "from ring frame X on, you are playing track T at track frame F".
// that's how the ui position stays exact across gapless track changes and seeks.
typedef struct AudioMarker AudioMarker;
struct AudioMarker {
u64 ring_frame;
u64 track_frame;
u64 track_total_frames;
u32 track_key;
u32 rate;
u32 kind;
u32 padding;
};
#define AUDIO_MARKER_CAPACITY 32
typedef struct AudioRing AudioRing;
struct AudioRing {
//- producer owned line
CacheAligned u64 write_frame;
u64 producer_cached_read;
u32 marker_write;
//- consumer owned line
CacheAligned u64 read_frame;
u64 consumer_cached_write;
u32 marker_read;
//- flush handshake (producer writes, consumer reads)
CacheAligned u64 flush_to_frame;
u32 flush_request;
//- immutable after init
CacheAligned f32 *samples;
u64 capacity_frames;
u64 mask;
AudioMarker markers[AUDIO_MARKER_CAPACITY];
};
internal void audio_ring_init(Arena *arena, AudioRing *ring, u64 capacity_frames_pow2);
//- producer side
internal u64 audio_ring_writable(AudioRing *ring, f32 **out_pointer, u64 *out_free); // contiguous frames free (and the total)
internal void audio_ring_commit(AudioRing *ring, u64 frames);
internal u64 audio_ring_fill(AudioRing *ring); // frames queued
internal void audio_ring_flush(AudioRing *ring);
internal b32 audio_ring_push_marker(AudioRing *ring, AudioMarker marker);
//- consumer side
internal u64 audio_ring_readable(AudioRing *ring, f32 **out_pointer); // contiguous frames queued
internal void audio_ring_release(AudioRing *ring, u64 frames);
internal b32 audio_ring_peek_marker(AudioRing *ring, AudioMarker *out);
internal void audio_ring_pop_marker(AudioRing *ring);
#endif // AUDIO_RING_H
|