audio fixes + initial audio capture support (malc)
[qemu] / audio / audio_int.h
1 /*
2  * QEMU Audio subsystem header
3  *
4  * Copyright (c) 2003-2005 Vassili Karpov (malc)
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #ifndef QEMU_AUDIO_INT_H
25 #define QEMU_AUDIO_INT_H
26
27 #ifdef CONFIG_COREAUDIO
28 #define FLOAT_MIXENG
29 /* #define RECIPROCAL */
30 #endif
31 #include "mixeng.h"
32
33 struct audio_pcm_ops;
34
35 typedef enum {
36     AUD_OPT_INT,
37     AUD_OPT_FMT,
38     AUD_OPT_STR,
39     AUD_OPT_BOOL
40 } audio_option_tag_e;
41
42 struct audio_option {
43     const char *name;
44     audio_option_tag_e tag;
45     void *valp;
46     const char *descr;
47     int *overridenp;
48     int overriden;
49 };
50
51 struct audio_callback {
52     void *opaque;
53     audio_callback_fn_t fn;
54 };
55
56 struct audio_pcm_info {
57     int bits;
58     int sign;
59     int freq;
60     int nchannels;
61     int align;
62     int shift;
63     int bytes_per_second;
64     int swap_endian;
65 };
66
67 typedef struct HWVoiceOut {
68     int enabled;
69     int pending_disable;
70     int valid;
71     struct audio_pcm_info info;
72
73     f_sample *clip;
74
75     int rpos;
76     uint64_t ts_helper;
77
78     st_sample_t *mix_buf;
79
80     int samples;
81     LIST_HEAD (sw_out_listhead, SWVoiceOut) sw_head;
82     LIST_HEAD (sw_cap_listhead, SWVoiceOut) sw_cap_head;
83     struct audio_pcm_ops *pcm_ops;
84     LIST_ENTRY (HWVoiceOut) entries;
85 } HWVoiceOut;
86
87 typedef struct HWVoiceIn {
88     int enabled;
89     struct audio_pcm_info info;
90
91     t_sample *conv;
92
93     int wpos;
94     int total_samples_captured;
95     uint64_t ts_helper;
96
97     st_sample_t *conv_buf;
98
99     int samples;
100     LIST_HEAD (sw_in_listhead, SWVoiceIn) sw_head;
101     struct audio_pcm_ops *pcm_ops;
102     LIST_ENTRY (HWVoiceIn) entries;
103 } HWVoiceIn;
104
105 struct SWVoiceOut {
106     struct audio_pcm_info info;
107     t_sample *conv;
108     int64_t ratio;
109     st_sample_t *buf;
110     void *rate;
111     int total_hw_samples_mixed;
112     int active;
113     int empty;
114     HWVoiceOut *hw;
115     char *name;
116     volume_t vol;
117     struct audio_callback callback;
118     LIST_ENTRY (SWVoiceOut) entries;
119     LIST_ENTRY (SWVoiceOut) cap_entries;
120 };
121
122 struct SWVoiceIn {
123     int active;
124     struct audio_pcm_info info;
125     int64_t ratio;
126     void *rate;
127     int total_hw_samples_acquired;
128     st_sample_t *buf;
129     f_sample *clip;
130     HWVoiceIn *hw;
131     char *name;
132     volume_t vol;
133     struct audio_callback callback;
134     LIST_ENTRY (SWVoiceIn) entries;
135 };
136
137 struct audio_driver {
138     const char *name;
139     const char *descr;
140     struct audio_option *options;
141     void *(*init) (void);
142     void (*fini) (void *);
143     struct audio_pcm_ops *pcm_ops;
144     int can_be_default;
145     int max_voices_out;
146     int max_voices_in;
147     int voice_size_out;
148     int voice_size_in;
149 };
150
151 struct audio_pcm_ops {
152     int  (*init_out)(HWVoiceOut *hw, audsettings_t *as);
153     void (*fini_out)(HWVoiceOut *hw);
154     int  (*run_out) (HWVoiceOut *hw);
155     int  (*write)   (SWVoiceOut *sw, void *buf, int size);
156     int  (*ctl_out) (HWVoiceOut *hw, int cmd, ...);
157
158     int  (*init_in) (HWVoiceIn *hw, audsettings_t *as);
159     void (*fini_in) (HWVoiceIn *hw);
160     int  (*run_in)  (HWVoiceIn *hw);
161     int  (*read)    (SWVoiceIn *sw, void *buf, int size);
162     int  (*ctl_in)  (HWVoiceIn *hw, int cmd, ...);
163 };
164
165 struct capture_callback {
166     struct audio_capture_ops ops;
167     void *opaque;
168     LIST_ENTRY (capture_callback) entries;
169 };
170
171 typedef struct CaptureVoiceOut {
172     HWVoiceOut hw;
173     void *buf;
174     LIST_HEAD (cb_listhead, capture_callback) cb_head;
175     LIST_ENTRY (CaptureVoiceOut) entries;
176 } CaptureVoiceOut;
177
178 struct AudioState {
179     struct audio_driver *drv;
180     void *drv_opaque;
181
182     QEMUTimer *ts;
183     LIST_HEAD (card_listhead, QEMUSoundCard) card_head;
184     LIST_HEAD (hw_in_listhead, HWVoiceIn) hw_head_in;
185     LIST_HEAD (hw_out_listhead, HWVoiceOut) hw_head_out;
186     LIST_HEAD (cap_listhead, CaptureVoiceOut) cap_head;
187     int nb_hw_voices_out;
188     int nb_hw_voices_in;
189 };
190
191 extern struct audio_driver no_audio_driver;
192 extern struct audio_driver oss_audio_driver;
193 extern struct audio_driver sdl_audio_driver;
194 extern struct audio_driver wav_audio_driver;
195 extern struct audio_driver fmod_audio_driver;
196 extern struct audio_driver alsa_audio_driver;
197 extern struct audio_driver coreaudio_audio_driver;
198 extern struct audio_driver dsound_audio_driver;
199 extern volume_t nominal_volume;
200
201 void audio_pcm_init_info (struct audio_pcm_info *info, audsettings_t *as,
202                           int swap_endian);
203 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len);
204
205 int  audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int len);
206 int  audio_pcm_hw_get_live_in (HWVoiceIn *hw);
207
208 int  audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int len);
209 int  audio_pcm_hw_get_live_out (HWVoiceOut *hw);
210 int  audio_pcm_hw_get_live_out2 (HWVoiceOut *hw, int *nb_live);
211
212 int audio_bug (const char *funcname, int cond);
213 void *audio_calloc (const char *funcname, int nmemb, size_t size);
214
215 #define VOICE_ENABLE 1
216 #define VOICE_DISABLE 2
217
218 static inline int audio_ring_dist (int dst, int src, int len)
219 {
220     return (dst >= src) ? (dst - src) : (len - src + dst);
221 }
222
223 static inline int audio_need_to_swap_endian (int endianness)
224 {
225 #ifdef WORDS_BIGENDIAN
226     return endianness != 1;
227 #else
228     return endianness != 0;
229 #endif
230 }
231
232 #if defined __GNUC__
233 #define GCC_ATTR __attribute__ ((__unused__, __format__ (__printf__, 1, 2)))
234 #define INIT_FIELD(f) . f
235 #define GCC_FMT_ATTR(n, m) __attribute__ ((__format__ (__printf__, n, m)))
236 #else
237 #define GCC_ATTR /**/
238 #define INIT_FIELD(f) /**/
239 #define GCC_FMT_ATTR(n, m)
240 #endif
241
242 static void GCC_ATTR dolog (const char *fmt, ...)
243 {
244     va_list ap;
245
246     va_start (ap, fmt);
247     AUD_vlog (AUDIO_CAP, fmt, ap);
248     va_end (ap);
249 }
250
251 #ifdef DEBUG
252 static void GCC_ATTR ldebug (const char *fmt, ...)
253 {
254     va_list ap;
255
256     va_start (ap, fmt);
257     AUD_vlog (AUDIO_CAP, fmt, ap);
258     va_end (ap);
259 }
260 #else
261 #if defined NDEBUG && defined __GNUC__
262 #define ldebug(...)
263 #elif defined NDEBUG && defined _MSC_VER
264 #define ldebug __noop
265 #else
266 static void GCC_ATTR ldebug (const char *fmt, ...)
267 {
268     (void) fmt;
269 }
270 #endif
271 #endif
272
273 #undef GCC_ATTR
274
275 #define AUDIO_STRINGIFY_(n) #n
276 #define AUDIO_STRINGIFY(n) AUDIO_STRINGIFY_(n)
277
278 #if defined _MSC_VER || defined __GNUC__
279 #define AUDIO_FUNC __FUNCTION__
280 #else
281 #define AUDIO_FUNC __FILE__ ":" AUDIO_STRINGIFY (__LINE__)
282 #endif
283
284 #endif /* audio_int.h */