-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_decoder.h
211 lines (188 loc) · 5.28 KB
/
audio_decoder.h
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//SPDX-License-Identifier: GPL-2.0-or-later
/*
Copyright (C) 2007-2024 Cyril Hrubis <[email protected]>
*/
#ifndef AUDIO_DECODER_H
#define AUDIO_DECODER_H
#include <stddef.h>
/**
* @brief Maximum softvolume gain is 130%
*/
#define AUDIO_DECODER_SOFTVOL_MAX 130
/**
* @brief Audio decoder control.
*/
enum audio_decoder_ctrl {
/** @brief Resume playback after a pause. */
AUDIO_DECODER_PLAY,
/** @brief Pause playback. */
AUDIO_DECODER_PAUSE,
};
/**
* @brief Software volume control.
*/
enum audio_decoder_softvol_op {
/** @brief Gets soft volume value. */
AUDIO_DECODER_SOFTVOL_GET,
/** @brief Sets soft volume value. */
AUDIO_DECODER_SOFTVOL_SET,
};
/**
* @brief Audio decoder options.
*
* This structure should be returned by the audio decoder init function.
*/
struct audio_decoder_ops {
/**
* @brief Loads a new track.
*
* @param path A path to the file.
* @return Zero on success.
*/
int (*track_load)(const char *path);
/**
* @brief Seeks in the current track.
*
* @param whence A seek whence.
* @param offset_ms An offset in miliseconds.
*
* @return Zero on success.
*/
int (*track_seek)(long offset_ms);
/**
* @brief Playback control.
*
* @param playback A playback operation.
*/
int (*track_ctrl)(enum audio_decoder_ctrl ctrl);
/**
* @brief Software volume control.
*
* @param vol A new volume for the set op.
*
* @return Zero for set, current volume for cur and maximal volume for max.
*/
unsigned long (*softvol)(enum audio_decoder_softvol_op op, unsigned long vol);
/**
* @brief Processes events, fills buffers, etc.
*
* This has to be called regularly to process events, fill buffers, etc.
*
* @return An interval before next call to this function in miliseconds.
*/
unsigned long (*tick)(void);
};
static inline int audio_decoder_track_load(const struct audio_decoder_ops *ops, const char *path)
{
return ops->track_load(path);
}
static inline unsigned long audio_decoder_tick(const struct audio_decoder_ops *ops)
{
return ops->tick();
}
static inline void audio_decoder_track_pause(const struct audio_decoder_ops *ops)
{
ops->track_ctrl(AUDIO_DECODER_PAUSE);
}
static inline void audio_decoder_track_play(const struct audio_decoder_ops *ops)
{
ops->track_ctrl(AUDIO_DECODER_PLAY);
}
static inline void audio_decoder_track_seek(const struct audio_decoder_ops *ops, unsigned long offset_ms)
{
ops->track_seek(offset_ms);
}
static inline unsigned long audio_decoder_softvol_max(const struct audio_decoder_ops *ops)
{
return ops->softvol(AUDIO_DECODER_SOFTVOL_MAX, 0);
}
static inline unsigned long audio_decoder_softvol_get(const struct audio_decoder_ops *ops)
{
return ops->softvol(AUDIO_DECODER_SOFTVOL_GET, 0);
}
static inline unsigned long audio_decoder_softvol_set(const struct audio_decoder_ops *ops, unsigned long vol)
{
return ops->softvol(AUDIO_DECODER_SOFTVOL_SET, vol);
}
/**
* @brief A decoder callbacks.
*
* This structure is filled in by the application and callbacks are called by the decoder.
*/
struct audio_decoder_callbacks {
/**
* @brief Audio track info callback.
*
* This is called when track is loaded to set or clear the track info.
*/
void (*track_info)(const char *artist, const char *album, const char *title);
/**
* @brief Audio track duration callback.
*
* @param duration_ms A duration of currently plaing song. If zero
* currently plaing song had finished.
*/
void (*track_duration)(long duration_ms);
/**
* @brief Audio track art callback.
*
* This is called when track is loaded to set or clear the track cover art.
*
* @param buf A bufer with image that was embedded in the audio file.
* @param buf_len A buffer lenght in bytes.
*/
void (*track_art)(void *buf, size_t buf_len);
/**
* @brief Updates current song offset from the start.
*
* @param offset_ms Current offset from the start of the song in miliseconds.
*/
void (*track_pos)(long offset_ms);
};
/**
* @brief An audio decoder initialization.
*/
struct audio_decoder {
/** @brief An audio decoder name. */
const char *name;
/**
* @brief An audio decoder init funciton.
*
* @param cbs An audio decoder app callbacks.
*
* @return An audio decoder ops.
*/
const struct audio_decoder_ops *(*init)(const struct audio_decoder_callbacks *cbs);
};
/**
* @brief A NULL name terminated array of available decoders.
*/
extern const struct audio_decoder audio_decoders[];
/**
* @brief Initialize audio decoder by name.
*
* The function falls back to first available decoder if decoder with a name
* wasn't found.
*
* @param name An audio decoder name.
* @param cbs An audio decoder app callbacks.
*/
const struct audio_decoder_ops *audio_decoder_init(const char *name,
const struct audio_decoder_callbacks *cbs);
/**
* @brief A mpg123 init function.
*
* @param cbs An audio decoder app callbacks.
*
* @return An mpg123 decoder ops or NULL if mpg123 is not compiled in.
*/
const struct audio_decoder_ops *audio_decoder_mpg123(const struct audio_decoder_callbacks *cbs);
/**
* @brief A mpv init function.
*
* @param cbs An audio decoder app callbacks.
*
* @return An mpv decoder ops or NULL if mpv is not compiled in.
*/
const struct audio_decoder_ops *audio_decoder_mpv(const struct audio_decoder_callbacks *cbs);
#endif /* AUDIO_DECODER_H */