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
|
#ifndef AUDIO_PIPEWIRE_H
#define AUDIO_PIPEWIRE_H
// NOTE: native pipewire output. a pw_stream in F32 stereo at the file's own sample rate, with
// node.rate hinted so the graph switches to it when the config allows (bit perfect, no
// resampling). if the graph can't switch, pipewire's own high quality resampler does it.
//
// the process callback runs on pipewire's realtime data thread (PW_STREAM_FLAG_RT_PROCESS).
// it only touches the ring, a few atomics and its own state. no locks, no allocation, no syscalls.
#define AUDIO_TAP_FRAMES 4096 // last played audio, for the spectrum
typedef enum PlaybackFlags {
PLAYBACK_HAS_TRACK = 1 << 0,
PLAYBACK_STARVED = 1 << 1, // ring ran dry while playing (still downloading / decoding)
PLAYBACK_ENDED = 1 << 2,
} PlaybackFlags;
typedef struct PlaybackPosition PlaybackPosition;
struct PlaybackPosition {
u32 track_key;
u32 rate;
u64 frame;
u64 total_frames;
u32 flags;
};
typedef struct AudioOutput AudioOutput;
struct AudioOutput {
struct pw_thread_loop *loop;
struct pw_stream *stream;
AudioRing *ring;
u32 rate;
b32 active;
b32 connected;
//- controls, written by main, read by the rt thread
CacheAligned u32 volume_bits; // f32
u32 paused;
//- rt thread private
CacheAligned f32 gain; // what was applied at the end of the last buffer
u32 flush_seen;
AudioMarker marker; // the marker the current audio belongs to
b32 has_marker;
b32 ended;
b32 starved;
//- published by the rt thread, seqlock protected
CacheAligned u32 position_sequence;
PlaybackPosition position;
u32 underruns; // audio missing while a track was already playing
u32 switch_gaps; // callbacks waiting for a new track's first samples (seek, play, open)
u64 frames_since_start; // rt private: frames played since the last marker or flush
//- profiling, written by the rt thread only (plain stores, read racily for display)
CacheAligned u64 callbacks;
u32 callback_frames; // frames pipewire asked for last time
u64 callback_ns_total;
u64 callback_ns_max;
b32 rt_registered;
//- spectrum tap, written by the rt thread, read (racily, it's just visuals) by main
CacheAligned u64 tap_write;
f32 *tap;
};
internal b32 audio_output_init(Arena *arena, AudioOutput *output, AudioRing *ring, u32 rate);
internal void audio_output_shutdown(AudioOutput *output);
internal void audio_output_set_rate(AudioOutput *output, u32 rate); // decoder thread
internal void audio_output_set_volume(AudioOutput *output, f32 volume); // linear 0..1
internal void audio_output_set_paused(AudioOutput *output, b32 paused);
internal void audio_output_set_active(AudioOutput *output, b32 active); // suspend when idle
internal PlaybackPosition audio_output_position(AudioOutput *output);
internal void audio_output_read_tap(AudioOutput *output, f32 *out_stereo, u32 frames);
#endif // AUDIO_PIPEWIRE_H
|