audio merge (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     struct audio_pcm_ops *pcm_ops;
83     LIST_ENTRY (HWVoiceOut) entries;
84 } HWVoiceOut;
85
86 typedef struct HWVoiceIn {
87     int enabled;
88     struct audio_pcm_info info;
89
90     t_sample *conv;
91
92     int wpos;
93     int total_samples_captured;
94     uint64_t ts_helper;
95
96     st_sample_t *conv_buf;
97
98     int samples;
99     LIST_HEAD (sw_in_listhead, SWVoiceIn) sw_head;
100     struct audio_pcm_ops *pcm_ops;
101     LIST_ENTRY (HWVoiceIn) entries;
102 } HWVoiceIn;
103
104 struct SWVoiceOut {
105     struct audio_pcm_info info;
106     t_sample *conv;
107     int64_t ratio;
108     st_sample_t *buf;
109     void *rate;
110     int total_hw_samples_mixed;
111     int active;
112     int empty;
113     HWVoiceOut *hw;
114     char *name;
115     volume_t vol;
116     struct audio_callback callback;
117     LIST_ENTRY (SWVoiceOut) entries;
118 };
119
120 struct SWVoiceIn {
121     int active;
122     struct audio_pcm_info info;
123     int64_t ratio;
124     void *rate;
125     int total_hw_samples_acquired;
126     st_sample_t *buf;
127     f_sample *clip;
128     HWVoiceIn *hw;
129     char *name;
130     volume_t vol;
131     struct audio_callback callback;
132     LIST_ENTRY (SWVoiceIn) entries;
133 };
134
135 struct audio_driver {
136     const char *name;
137     const char *descr;
138     struct audio_option *options;
139     void *(*init) (void);
140     void (*fini) (void *);
141     struct audio_pcm_ops *pcm_ops;
142     int can_be_default;
143     int max_voices_out;
144     int max_voices_in;
145     int voice_size_out;
146     int voice_size_in;
147 };
148
149 struct audio_pcm_ops {
150     int  (*init_out)(HWVoiceOut *hw, audsettings_t *as);
151     void (*fini_out)(HWVoiceOut *hw);
152     int  (*run_out) (HWVoiceOut *hw);
153     int  (*write)   (SWVoiceOut *sw, void *buf, int size);
154     int  (*ctl_out) (HWVoiceOut *hw, int cmd, ...);
155
156     int  (*init_in) (HWVoiceIn *hw, audsettings_t *as);
157     void (*fini_in) (HWVoiceIn *hw);
158     int  (*run_in)  (HWVoiceIn *hw);
159     int  (*read)    (SWVoiceIn *sw, void *buf, int size);
160     int  (*ctl_in)  (HWVoiceIn *hw, int cmd, ...);
161 };
162
163 struct AudioState {
164     struct audio_driver *drv;
165     void *drv_opaque;
166
167     QEMUTimer *ts;
168     LIST_HEAD (card_head, QEMUSoundCard) card_head;
169     LIST_HEAD (hw_in_listhead, HWVoiceIn) hw_head_in;
170     LIST_HEAD (hw_out_listhead, HWVoiceOut) hw_head_out;
171     int nb_hw_voices_out;
172     int nb_hw_voices_in;
173 };
174
175 extern struct audio_driver no_audio_driver;
176 extern struct audio_driver oss_audio_driver;
177 extern struct audio_driver sdl_audio_driver;
178 extern struct audio_driver wav_audio_driver;
179 extern struct audio_driver fmod_audio_driver;
180 extern struct audio_driver alsa_audio_driver;
181 extern struct audio_driver coreaudio_audio_driver;
182 extern struct audio_driver dsound_audio_driver;
183 extern volume_t nominal_volume;
184
185 void audio_pcm_init_info (struct audio_pcm_info *info, audsettings_t *as,
186                           int swap_endian);
187 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len);
188
189 int  audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int len);
190 int  audio_pcm_hw_get_live_in (HWVoiceIn *hw);
191
192 int  audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int len);
193 int  audio_pcm_hw_get_live_out (HWVoiceOut *hw);
194 int  audio_pcm_hw_get_live_out2 (HWVoiceOut *hw, int *nb_live);
195
196 int audio_bug (const char *funcname, int cond);
197 void *audio_calloc (const char *funcname, int nmemb, size_t size);
198
199 #define VOICE_ENABLE 1
200 #define VOICE_DISABLE 2
201
202 static inline int audio_ring_dist (int dst, int src, int len)
203 {
204     return (dst >= src) ? (dst - src) : (len - src + dst);
205 }
206
207 static inline int audio_need_to_swap_endian (int endianness)
208 {
209 #ifdef WORDS_BIGENDIAN
210     return endianness != 1;
211 #else
212     return endianness != 0;
213 #endif
214 }
215
216 #if defined __GNUC__
217 #define GCC_ATTR __attribute__ ((__unused__, __format__ (__printf__, 1, 2)))
218 #define INIT_FIELD(f) . f
219 #define GCC_FMT_ATTR(n, m) __attribute__ ((__format__ (__printf__, n, m)))
220 #else
221 #define GCC_ATTR /**/
222 #define INIT_FIELD(f) /**/
223 #define GCC_FMT_ATTR(n, m)
224 #endif
225
226 static void GCC_ATTR dolog (const char *fmt, ...)
227 {
228     va_list ap;
229
230     va_start (ap, fmt);
231     AUD_vlog (AUDIO_CAP, fmt, ap);
232     va_end (ap);
233 }
234
235 #ifdef DEBUG
236 static void GCC_ATTR ldebug (const char *fmt, ...)
237 {
238     va_list ap;
239
240     va_start (ap, fmt);
241     AUD_vlog (AUDIO_CAP, fmt, ap);
242     va_end (ap);
243 }
244 #else
245 #if defined NDEBUG && defined __GNUC__
246 #define ldebug(...)
247 #elif defined NDEBUG && defined _MSC_VER
248 #define ldebug __noop
249 #else
250 static void GCC_ATTR ldebug (const char *fmt, ...)
251 {
252     (void) fmt;
253 }
254 #endif
255 #endif
256
257 #undef GCC_ATTR
258
259 #define AUDIO_STRINGIFY_(n) #n
260 #define AUDIO_STRINGIFY(n) AUDIO_STRINGIFY_(n)
261
262 #if defined _MSC_VER || defined __GNUC__
263 #define AUDIO_FUNC __FUNCTION__
264 #else
265 #define AUDIO_FUNC __FILE__ ":" AUDIO_STRINGIFY (__LINE__)
266 #endif
267
268 #endif /* audio_int.h */