aboutsummaryrefslogtreecommitdiffstats
path: root/source/audio/audio_decode.h
blob: 614730744a16a98d23809f77fde0d3d2a2752d88 (plain) (blame)
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
#ifndef AUDIO_DECODE_H
#define AUDIO_DECODE_H

// NOTE: the decoder thread. owns the mpg123 handle, fills the ring, follows commands from the
// main thread. mpg123 decodes straight into the ring's memory (no intermediate copy), in f32
// at the file's native rate, gapless (encoder delay/padding trimmed via the lame header).
//
// mpg123 reads through our own reader callbacks instead of a plain fd. the reader knows how
// much of the file has been downloaded and blocks on the store's futex until the bytes it needs
// exist, so the decoder can start on a file that is still coming in.

typedef enum DecoderCommandKind {
	DECODER_PLAY = 1,
	DECODER_SEEK,
	DECODER_STOP,
	DECODER_SET_AUTOPLAY, // frame = AutoplayMode
	DECODER_QUEUE_CLEAR,
	DECODER_QUEUE_ADD,    // track_key appended to the play queue
} DecoderCommandKind;

typedef enum AutoplayMode {
	AUTOPLAY_OFF = 0,
	AUTOPLAY_NEXT,            // next surah, streaming it if needed
	AUTOPLAY_NEXT_DOWNLOADED, // next surah that is on disk (offline listening)
} AutoplayMode;

typedef struct DecoderCommand DecoderCommand;
struct DecoderCommand {
	u32 kind;
	u32 track_key;
	u64 frame;
};

typedef enum DecoderStatus {
	DECODER_IDLE = 0,
	DECODER_BUFFERING, // waiting for the network
	DECODER_DECODING,
	DECODER_ERROR,
} DecoderStatus;

#define DECODER_COMMAND_CAPACITY 256 // a whole surah list queued at once never makes the ui wait
#define DECODER_QUEUE_CAPACITY   512
#define DECODER_CHUNK_FRAMES     4096

typedef struct TrackReader TrackReader;
struct TrackReader {
	struct Decoder *decoder;
	TrackStore     *store;
	TrackEntry     *entry;
	int             fd;
	u32             key;
	s64             offset;
	b32             interrupted;
	u8             *map;       // finished files are mapped: reads are memcpy, not a syscall each
	u64             map_size;
	s64             map_trimmed_at; // offset of the last rss trim, see reader_read
};

typedef struct Decoder Decoder;
struct Decoder {
	//- command queue, main -> decoder (spsc)
	CacheAligned u32 command_write;
	CacheAligned u32 command_read;
	DecoderCommand commands[DECODER_COMMAND_CAPACITY];
	Signal wake;
	u32    quit;

	//- published for the ui
	CacheAligned u32 status;
	u32 status_key;
	u64 published_total; // track length in frames once known (streams learn it late)
	const char *decoder_name; // which mpg123 synth it picked (neon64, ...), for the profiler
	u64 decoded_frames;  // profiling: frames decoded and time spent decoding them
	u64 decode_ns;

	//- decoder thread private
	CacheAligned AudioRing *ring;
	AudioOutput   *output;
	TrackStore    *store;
	mpg123_handle *handle;
	TrackReader    reader;
	u32            key;
	b32            open;
	b32            decoding;
	u32            autoplay; // AutoplayMode
	// NOTE: an explicit play queue (multi selection). while the playing track is in it, the queue
	// decides what comes next instead of autoplay. a plain single play clears it.
	u32            queue[DECODER_QUEUE_CAPACITY];
	u32            queue_count;
	u32            rate;
	u64            total_frames;
	Thread         thread;
};

internal b32  decoder_init(Arena *arena, Decoder *decoder, AudioRing *ring, AudioOutput *output, TrackStore *store);
internal void decoder_shutdown(Decoder *decoder);
internal void decoder_command(Decoder *decoder, DecoderCommandKind kind, u32 track_key, u64 frame); // main thread

#endif // AUDIO_DECODE_H