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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
internal void audio_ring_init(Arena *arena, AudioRing *ring, u64 capacity_frames_pow2)
{
Assert(is_pow(capacity_frames_pow2));
MemSetZero(ring, sizeof(*ring));
ring->capacity_frames = capacity_frames_pow2;
ring->mask = capacity_frames_pow2 - 1;
ring->samples = PushArrayAligned(arena, f32, capacity_frames_pow2 * AUDIO_CHANNELS);
}
//- producer
internal u64 audio_ring_writable(AudioRing *ring, f32 **out_pointer, u64 *out_free)
{
// NOTE: refresh the cached read position as soon as it claims we're low on space, not only
// when it claims we're completely full. the last write of a track is a partial chunk that can
// leave the ring *nearly* full; waiting for "completely full" then never happens and the
// decoder would wait forever on a ring that has long been drained. one atomic load per
// decoded chunk (~4096 frames) when it fires.
u64 write = ring->write_frame; // we own it, plain read is fine
u64 used = write - ring->producer_cached_read;
if(ring->capacity_frames - used < ring->capacity_frames / 4) {
ring->producer_cached_read = AtomicLoad(&ring->read_frame);
used = write - ring->producer_cached_read;
}
u64 free_frames = ring->capacity_frames - used;
if(out_free) *out_free = free_frames;
u64 slot = write & ring->mask;
u64 until_wrap = ring->capacity_frames - slot;
*out_pointer = ring->samples + slot * AUDIO_CHANNELS;
return Min(free_frames, until_wrap);
}
internal void audio_ring_commit(AudioRing *ring, u64 frames)
{
AtomicStore(&ring->write_frame, ring->write_frame + frames);
}
internal u64 audio_ring_fill(AudioRing *ring)
{
ring->producer_cached_read = AtomicLoad(&ring->read_frame);
return ring->write_frame - ring->producer_cached_read;
}
internal void audio_ring_flush(AudioRing *ring)
{
AtomicStoreRelaxed(&ring->flush_to_frame, ring->write_frame);
// NOTE: release orders the flush_to store before the request becomes visible
AtomicStore(&ring->flush_request, ring->flush_request + 1);
}
internal b32 audio_ring_push_marker(AudioRing *ring, AudioMarker marker)
{
u32 write = ring->marker_write;
u32 read = AtomicLoad(&ring->marker_read);
if(write - read >= AUDIO_MARKER_CAPACITY) {
return false;
}
ring->markers[write % AUDIO_MARKER_CAPACITY] = marker;
AtomicStore(&ring->marker_write, write + 1);
return true;
}
//- consumer
internal u64 audio_ring_readable(AudioRing *ring, f32 **out_pointer)
{
// NOTE: a flush moves read_frame forward to the flush point, which can be past our cached copy
// of write_frame. so refresh whenever the cache isn't strictly ahead of the reader, and never
// subtract the other way round: an underflow here reads as "billions of frames available" and
// replays stale ring memory forever (the "stuck looping the last second" bug).
u64 read = ring->read_frame;
if(ring->consumer_cached_write <= read) {
ring->consumer_cached_write = AtomicLoad(&ring->write_frame);
}
u64 available = (ring->consumer_cached_write > read) ? ring->consumer_cached_write - read : 0;
u64 slot = read & ring->mask;
u64 until_wrap = ring->capacity_frames - slot;
*out_pointer = ring->samples + slot * AUDIO_CHANNELS;
return Min(available, until_wrap);
}
internal void audio_ring_release(AudioRing *ring, u64 frames)
{
AtomicStore(&ring->read_frame, ring->read_frame + frames);
}
internal b32 audio_ring_peek_marker(AudioRing *ring, AudioMarker *out)
{
u32 read = ring->marker_read;
u32 write = AtomicLoad(&ring->marker_write);
if(read == write) {
return false;
}
*out = ring->markers[read % AUDIO_MARKER_CAPACITY];
return true;
}
internal void audio_ring_pop_marker(AudioRing *ring)
{
AtomicStore(&ring->marker_read, ring->marker_read + 1);
}
|