chiark / gitweb /
speaker: protocol structure now has a union for different arg types
[disorder] / lib / uaudio.h
CommitLineData
7a2c7068
RK
1/*
2 * This file is part of DisOrder.
5db8461a 3 * Copyright (C) 2009, 2013 Richard Kettlewell
7a2c7068
RK
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/** @file lib/uaudio.h
20 * @brief Uniform audio interface
21 */
22
23#ifndef UAUDIO_H
24#define UAUDIO_H
25
4fd38868
RK
26extern int uaudio_rate;
27extern int uaudio_bits;
28extern int uaudio_channels;
29extern int uaudio_signed;
30extern size_t uaudio_sample_size;
5db8461a 31extern int uaudio_buffer;
4fd38868 32
7a2c7068
RK
33/** @brief Callback to get audio data
34 * @param buffer Where to put audio data
35 * @param max_samples How many samples to supply
36 * @param userdata As passed to uaudio_open()
37 * @return Number of samples filled
d4170ca7
RK
38 *
39 * This function should not block if possible (better to fill the buffer with
40 * 0s) and should definitely not block indefinitely. This great caution with
41 * any locks or syscalls! In particular avoid it taking a lock that may be
42 * held while any of the @ref uaudio members are called.
43 *
44 * If it's more convenient, it's OK to return less than the maximum number of
45 * samples (including 0) provided you expect to be called again for more
46 * samples immediately.
7a2c7068 47 */
4fd38868 48typedef size_t uaudio_callback(void *buffer,
7a2c7068
RK
49 size_t max_samples,
50 void *userdata);
51
4fd38868
RK
52/** @brief Callback to play audio data
53 * @param buffer Pointer to audio buffer
54 * @param samples Number of samples to play
b1f6ca8c 55 * @param flags Flags word
4fd38868
RK
56 * @return Number of samples played
57 *
58 * Used with uaudio_thread_start() etc.
b1f6ca8c
RK
59 *
60 * @p flags is a bitmap giving the current pause state and transitions:
61 * - @ref UAUDIO_PAUSE if this is the first call of a pause
62 * - @ref UAUDIO_RESUME if this is the first call of a resumse
63 * - @ref UAUDIO_PLAYING if this is outside a pause
64 * - @ref UAUDIO_PAUSED if this is in a pause
65 *
66 * During a pause, the sample data is guaranteed to be 0.
4fd38868 67 */
b1f6ca8c
RK
68typedef size_t uaudio_playcallback(void *buffer, size_t samples,
69 unsigned flags);
70
71/** @brief Start of a pause */
72#define UAUDIO_PAUSE 0x0001
73
74/** @brief End of a pause */
75#define UAUDIO_RESUME 0x0002
76
77/** @brief Currently playing */
78#define UAUDIO_PLAYING 0x0004
79
80/** @brief Currently paused */
81#define UAUDIO_PAUSED 0x0008
4fd38868 82
7a2c7068
RK
83/** @brief Audio API definition */
84struct uaudio {
85 /** @brief Name of this API */
86 const char *name;
87
88 /** @brief List of options, terminated by NULL */
89 const char *const *options;
90
91 /** @brief Do slow setup
92 * @param ua Handle returned by uaudio_open()
93 * @param callback Called for audio data
94 * @param userdata Passed to @p callback
95 *
96 * This does resource-intensive setup for the output device.
97 *
98 * For instance it might open mixable audio devices or network sockets. It
99 * will create any background thread required. However, it must not exclude
100 * other processes from outputting sound.
101 */
102 void (*start)(uaudio_callback *callback,
103 void *userdata);
104
105 /** @brief Tear down
106 * @param ua Handle returned by uaudio_open()
107 *
108 * This undoes the effect of @c start.
109 */
110 void (*stop)(void);
111
112 /** @brief Enable output
113 *
114 * A background thread will start calling @c callback as set by @c
115 * start and playing the audio data received from it.
116 */
117 void (*activate)(void);
118
119 /** @brief Disable output
120 *
121 * The background thread will stop calling @c callback.
122 */
123 void (*deactivate)(void);
124
b50cfb8a
RK
125 /** @brief Open mixer device */
126 void (*open_mixer)(void);
127
128 /** @brief Closer mixer device */
129 void (*close_mixer)(void);
130
131 /** @brief Get volume
132 * @param left Where to put the left-channel value
133 * @param right Where to put the right-channel value
134 *
135 * 0 is silent and 100 is maximum volume.
136 */
137 void (*get_volume)(int *left, int *right);
138
139 /** @brief Set volume
140 * @param left Pointer to left-channel value (updated)
141 * @param right Pointer to right-channel value (updated)
142 *
143 * The values are updated with those actually set by the underlying system
144 * call.
145 *
146 * 0 is silent and 100 is maximum volume.
147 */
148 void (*set_volume)(int *left, int *right);
ba70caca
RK
149
150 /** @brief Set configuration */
151 void (*configure)(void);
06385470
RK
152
153 /** @brief Descriptive flags */
154 unsigned flags;
7a2c7068 155};
4fd38868 156
06385470
RK
157/** @brief API is suitable for clients */
158#define UAUDIO_API_CLIENT 0x0001
159
160/** @brief API is suitable for servers */
161#define UAUDIO_API_SERVER 0x0002
162
4fd38868 163void uaudio_set_format(int rate, int channels, int samplesize, int signed_);
7a2c7068 164void uaudio_set(const char *name, const char *value);
b50cfb8a 165char *uaudio_get(const char *name, const char *default_value);
4fd38868
RK
166void uaudio_thread_start(uaudio_callback *callback,
167 void *userdata,
168 uaudio_playcallback *playcallback,
169 size_t min,
63761c19
RK
170 size_t max,
171 unsigned flags);
172
4fd38868
RK
173void uaudio_thread_stop(void);
174void uaudio_thread_activate(void);
175void uaudio_thread_deactivate(void);
b1f6ca8c 176uint32_t uaudio_schedule_sync(void);
1c140a81 177void uaudio_schedule_sent(size_t nsamples_sent);
ec57f6c9 178void uaudio_schedule_init(void);
b50cfb8a 179const struct uaudio *uaudio_find(const char *name);
06385470
RK
180const struct uaudio *uaudio_default(const struct uaudio *const *apis,
181 unsigned context);
ec57f6c9
RK
182
183extern uint64_t uaudio_schedule_timestamp;
184extern int uaudio_schedule_reactivated;
7a2c7068
RK
185
186#if HAVE_COREAUDIO_AUDIOHARDWARE_H
187extern const struct uaudio uaudio_coreaudio;
188#endif
189
190#if HAVE_ALSA_ASOUNDLIB_H
191extern const struct uaudio uaudio_alsa;
192#endif
193
194#if HAVE_SYS_SOUNDCARD_H || EMPEG_HOST
195extern const struct uaudio uaudio_oss;
196#endif
197
de0ef46e
RK
198#if HAVE_PULSEAUDIO
199extern const struct uaudio uaudio_pulseaudio;
200#endif
201
7a2c7068
RK
202extern const struct uaudio uaudio_rtp;
203
66288757 204extern const struct uaudio uaudio_command;
7a2c7068 205
5b053666 206extern const struct uaudio *const uaudio_apis[];
efd23a80 207
7a2c7068
RK
208#endif /* UAUDIO_H */
209
210/*
211Local Variables:
212c-basic-offset:2
213comment-column:40
214fill-column:79
215indent-tabs-mode:nil
216End:
217*/