]> git.proxmox.com Git - mirror_qemu.git/blob - audio/audio_template.h
audio merge (malc)
[mirror_qemu.git] / audio / audio_template.h
1 /*
2 * QEMU Audio subsystem header
3 *
4 * Copyright (c) 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
25 #ifdef DAC
26 #define TYPE out
27 #define HW glue (HWVoice, Out)
28 #define SW glue (SWVoice, Out)
29 #else
30 #define TYPE in
31 #define HW glue (HWVoice, In)
32 #define SW glue (SWVoice, In)
33 #endif
34
35 static int glue (audio_pcm_hw_init_, TYPE) (
36 HW *hw,
37 audsettings_t *as
38 )
39 {
40 glue (audio_pcm_hw_free_resources_, TYPE) (hw);
41
42 if (glue (hw->pcm_ops->init_, TYPE) (hw, as)) {
43 return -1;
44 }
45
46 if (audio_bug (AUDIO_FUNC, hw->samples <= 0)) {
47 dolog ("hw->samples=%d\n", hw->samples);
48 return -1;
49 }
50
51 LIST_INIT (&hw->sw_head);
52 #ifdef DAC
53 hw->clip =
54 mixeng_clip
55 #else
56 hw->conv =
57 mixeng_conv
58 #endif
59 [hw->info.nchannels == 2]
60 [hw->info.sign]
61 [hw->info.swap_endian]
62 [hw->info.bits == 16];
63
64 if (glue (audio_pcm_hw_alloc_resources_, TYPE) (hw)) {
65 glue (hw->pcm_ops->fini_, TYPE) (hw);
66 return -1;
67 }
68
69 return 0;
70 }
71
72 static void glue (audio_pcm_sw_fini_, TYPE) (SW *sw)
73 {
74 glue (audio_pcm_sw_free_resources_, TYPE) (sw);
75 if (sw->name) {
76 qemu_free (sw->name);
77 sw->name = NULL;
78 }
79 }
80
81 static void glue (audio_pcm_hw_add_sw_, TYPE) (HW *hw, SW *sw)
82 {
83 LIST_INSERT_HEAD (&hw->sw_head, sw, entries);
84 }
85
86 static void glue (audio_pcm_hw_del_sw_, TYPE) (SW *sw)
87 {
88 LIST_REMOVE (sw, entries);
89 }
90
91 static void glue (audio_pcm_hw_gc_, TYPE) (AudioState *s, HW **hwp)
92 {
93 HW *hw = *hwp;
94
95 if (!hw->sw_head.lh_first) {
96 LIST_REMOVE (hw, entries);
97 glue (s->nb_hw_voices_, TYPE) += 1;
98 glue (audio_pcm_hw_free_resources_ ,TYPE) (hw);
99 glue (hw->pcm_ops->fini_, TYPE) (hw);
100 qemu_free (hw);
101 *hwp = NULL;
102 }
103 }
104
105 static HW *glue (audio_pcm_hw_find_any_, TYPE) (AudioState *s, HW *hw)
106 {
107 return hw ? hw->entries.le_next : s->glue (hw_head_, TYPE).lh_first;
108 }
109
110 static HW *glue (audio_pcm_hw_find_any_enabled_, TYPE) (AudioState *s, HW *hw)
111 {
112 while ((hw = glue (audio_pcm_hw_find_any_, TYPE) (s, hw))) {
113 if (hw->enabled) {
114 return hw;
115 }
116 }
117 return NULL;
118 }
119
120 static HW *glue (audio_pcm_hw_find_any_passive_, TYPE) (AudioState *s)
121 {
122 if (glue (s->nb_hw_voices_, TYPE)) {
123 struct audio_driver *drv = s->drv;
124
125 if (audio_bug (AUDIO_FUNC, !drv)) {
126 dolog ("No host audio driver\n");
127 return NULL;
128 }
129
130 HW *hw = audio_calloc (AUDIO_FUNC, 1, glue (drv->voice_size_, TYPE));
131 if (!hw) {
132 dolog ("Can not allocate voice `%s' size %d\n",
133 drv->name, glue (drv->voice_size_, TYPE));
134 }
135
136 LIST_INSERT_HEAD (&s->glue (hw_head_, TYPE), hw, entries);
137 glue (s->nb_hw_voices_, TYPE) -= 1;
138 return hw;
139 }
140
141 return NULL;
142 }
143
144 static HW *glue (audio_pcm_hw_find_specific_, TYPE) (
145 AudioState *s,
146 HW *hw,
147 audsettings_t *as
148 )
149 {
150 while ((hw = glue (audio_pcm_hw_find_any_, TYPE) (s, hw))) {
151 if (audio_pcm_info_eq (&hw->info, as)) {
152 return hw;
153 }
154 }
155 return NULL;
156 }
157
158 static HW *glue (audio_pcm_hw_add_new_, TYPE) (AudioState *s, audsettings_t *as)
159 {
160 HW *hw;
161
162 hw = glue (audio_pcm_hw_find_any_passive_, TYPE) (s);
163 if (hw) {
164 hw->pcm_ops = s->drv->pcm_ops;
165 if (!hw->pcm_ops) {
166 return NULL;
167 }
168
169 if (glue (audio_pcm_hw_init_, TYPE) (hw, as)) {
170 glue (audio_pcm_hw_gc_, TYPE) (s, &hw);
171 return NULL;
172 }
173 else {
174 return hw;
175 }
176 }
177
178 return NULL;
179 }
180
181 static HW *glue (audio_pcm_hw_add_, TYPE) (AudioState *s, audsettings_t *as)
182 {
183 HW *hw;
184
185 if (glue (conf.fixed_, TYPE).enabled && glue (conf.fixed_, TYPE).greedy) {
186 hw = glue (audio_pcm_hw_add_new_, TYPE) (s, as);
187 if (hw) {
188 return hw;
189 }
190 }
191
192 hw = glue (audio_pcm_hw_find_specific_, TYPE) (s, NULL, as);
193 if (hw) {
194 return hw;
195 }
196
197 hw = glue (audio_pcm_hw_add_new_, TYPE) (s, as);
198 if (hw) {
199 return hw;
200 }
201
202 return glue (audio_pcm_hw_find_any_, TYPE) (s, NULL);
203 }
204
205 static SW *glue (audio_pcm_create_voice_pair_, TYPE) (
206 AudioState *s,
207 const char *sw_name,
208 audsettings_t *as
209 )
210 {
211 SW *sw;
212 HW *hw;
213 audsettings_t hw_as;
214
215 if (glue (conf.fixed_, TYPE).enabled) {
216 hw_as = glue (conf.fixed_, TYPE).settings;
217 }
218 else {
219 hw_as = *as;
220 }
221
222 sw = audio_calloc (AUDIO_FUNC, 1, sizeof (*sw));
223 if (!sw) {
224 dolog ("Could not allocate soft voice `%s' (%d bytes)\n",
225 sw_name ? sw_name : "unknown", sizeof (*sw));
226 goto err1;
227 }
228
229 hw = glue (audio_pcm_hw_add_, TYPE) (s, &hw_as);
230 if (!hw) {
231 goto err2;
232 }
233
234 glue (audio_pcm_hw_add_sw_, TYPE) (hw, sw);
235
236 if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, sw_name, as)) {
237 goto err3;
238 }
239
240 return sw;
241
242 err3:
243 glue (audio_pcm_hw_del_sw_, TYPE) (sw);
244 glue (audio_pcm_hw_gc_, TYPE) (s, &hw);
245 err2:
246 qemu_free (sw);
247 err1:
248 return NULL;
249 }
250
251 static void glue (audio_close_, TYPE) (AudioState *s, SW *sw)
252 {
253 glue (audio_pcm_sw_fini_, TYPE) (sw);
254 glue (audio_pcm_hw_del_sw_, TYPE) (sw);
255 glue (audio_pcm_hw_gc_, TYPE) (s, &sw->hw);
256 qemu_free (sw);
257 }
258 void glue (AUD_close_, TYPE) (QEMUSoundCard *card, SW *sw)
259 {
260 if (sw) {
261 if (audio_bug (AUDIO_FUNC, !card || !card->audio)) {
262 dolog ("card=%p card->audio=%p\n",
263 card, card ? card->audio : NULL);
264 return;
265 }
266
267 glue (audio_close_, TYPE) (card->audio, sw);
268 }
269 }
270
271 SW *glue (AUD_open_, TYPE) (
272 QEMUSoundCard *card,
273 SW *sw,
274 const char *name,
275 void *callback_opaque ,
276 audio_callback_fn_t callback_fn,
277 audsettings_t *as
278 )
279 {
280 AudioState *s;
281 #ifdef DAC
282 int live = 0;
283 SW *old_sw = NULL;
284 #endif
285
286 ldebug ("open %s, freq %d, nchannels %d, fmt %d\n",
287 name, as->freq, as->nchannels, as->fmt);
288
289 if (audio_bug (AUDIO_FUNC,
290 !card || !card->audio || !name || !callback_fn || !as)) {
291 dolog ("card=%p card->audio=%p name=%p callback_fn=%p as=%p\n",
292 card, card ? card->audio : NULL, name, callback_fn, as);
293 goto fail;
294 }
295
296 s = card->audio;
297
298 if (audio_bug (AUDIO_FUNC, audio_validate_settigs (as))) {
299 audio_print_settings (as);
300 goto fail;
301 }
302
303 if (audio_bug (AUDIO_FUNC, !s->drv)) {
304 dolog ("Can not open `%s' (no host audio driver)\n", name);
305 goto fail;
306 }
307
308 if (sw && audio_pcm_info_eq (&sw->info, as)) {
309 return sw;
310 }
311
312 #ifdef DAC
313 if (conf.plive && sw && (!sw->active && !sw->empty)) {
314 live = sw->total_hw_samples_mixed;
315
316 #ifdef DEBUG_PLIVE
317 dolog ("Replacing voice %s with %d live samples\n", SW_NAME (sw), live);
318 dolog ("Old %s freq %d, bits %d, channels %d\n",
319 SW_NAME (sw), sw->info.freq, sw->info.bits, sw->info.nchannels);
320 dolog ("New %s freq %d, bits %d, channels %d\n",
321 name,
322 freq,
323 (fmt == AUD_FMT_S16 || fmt == AUD_FMT_U16) ? 16 : 8,
324 nchannels);
325 #endif
326
327 if (live) {
328 old_sw = sw;
329 old_sw->callback.fn = NULL;
330 sw = NULL;
331 }
332 }
333 #endif
334
335 if (!glue (conf.fixed_, TYPE).enabled && sw) {
336 glue (AUD_close_, TYPE) (card, sw);
337 sw = NULL;
338 }
339
340 if (sw) {
341 HW *hw = sw->hw;
342
343 if (!hw) {
344 dolog ("Internal logic error voice `%s' has no hardware store\n",
345 SW_NAME (sw));
346 goto fail;
347 }
348
349 if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, name, as)) {
350 goto fail;
351 }
352 }
353 else {
354 sw = glue (audio_pcm_create_voice_pair_, TYPE) (s, name, as);
355 if (!sw) {
356 dolog ("Failed to create voice `%s'\n", name);
357 goto fail;
358 }
359 }
360
361 if (sw) {
362 sw->vol = nominal_volume;
363 sw->callback.fn = callback_fn;
364 sw->callback.opaque = callback_opaque;
365
366 #ifdef DAC
367 if (live) {
368 int mixed =
369 (live << old_sw->info.shift)
370 * old_sw->info.bytes_per_second
371 / sw->info.bytes_per_second;
372
373 #ifdef DEBUG_PLIVE
374 dolog ("Silence will be mixed %d\n", mixed);
375 #endif
376 sw->total_hw_samples_mixed += mixed;
377 }
378 #endif
379
380 #ifdef DEBUG_AUDIO
381 dolog ("%s\n", name);
382 audio_pcm_print_info ("hw", &sw->hw->info);
383 audio_pcm_print_info ("sw", &sw->info);
384 #endif
385 }
386
387 return sw;
388
389 fail:
390 glue (AUD_close_, TYPE) (card, sw);
391 return NULL;
392 }
393
394 int glue (AUD_is_active_, TYPE) (SW *sw)
395 {
396 return sw ? sw->active : 0;
397 }
398
399 void glue (AUD_init_time_stamp_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts)
400 {
401 if (!sw) {
402 return;
403 }
404
405 ts->old_ts = sw->hw->ts_helper;
406 }
407
408 uint64_t glue (AUD_get_elapsed_usec_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts)
409 {
410 uint64_t delta, cur_ts, old_ts;
411
412 if (!sw) {
413 return 0;
414 }
415
416 cur_ts = sw->hw->ts_helper;
417 old_ts = ts->old_ts;
418 /* dolog ("cur %lld old %lld\n", cur_ts, old_ts); */
419
420 if (cur_ts >= old_ts) {
421 delta = cur_ts - old_ts;
422 }
423 else {
424 delta = UINT64_MAX - old_ts + cur_ts;
425 }
426
427 if (!delta) {
428 return 0;
429 }
430
431 return (delta * sw->hw->info.freq) / 1000000;
432 }
433
434 #undef TYPE
435 #undef HW
436 #undef SW