VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/audio_template.h@ 6290

Last change on this file since 6290 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.5 KB
Line 
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 NAME "playback"
27#define HWBUF hw->mix_buf
28#define TYPE out
29#define HW HWVoiceOut
30#define SW SWVoiceOut
31#else
32#define NAME "capture"
33#define TYPE in
34#define HW HWVoiceIn
35#define SW SWVoiceIn
36#define HWBUF hw->conv_buf
37#endif
38
39static void glue (audio_init_nb_voices_, TYPE) (
40 AudioState *s,
41 struct audio_driver *drv
42 )
43{
44 int max_voices = glue (drv->max_voices_, TYPE);
45 int voice_size = glue (drv->voice_size_, TYPE);
46
47 if (glue (s->nb_hw_voices_, TYPE) > max_voices) {
48 if (!max_voices) {
49#ifdef DAC
50 dolog ("Driver `%s' does not support " NAME "\n", drv->name);
51#endif
52 }
53 else {
54 dolog ("Driver `%s' does not support %d " NAME " voices, max %d\n",
55 drv->name,
56 glue (s->nb_hw_voices_, TYPE),
57 max_voices);
58 }
59 glue (s->nb_hw_voices_, TYPE) = max_voices;
60 }
61
62 if (audio_bug (AUDIO_FUNC, !voice_size && max_voices)) {
63 dolog ("drv=`%s' voice_size=0 max_voices=%d\n",
64 drv->name, max_voices);
65 glue (s->nb_hw_voices_, TYPE) = 0;
66 }
67
68 if (audio_bug (AUDIO_FUNC, voice_size && !max_voices)) {
69 dolog ("drv=`%s' voice_size=%d max_voices=0\n",
70 drv->name, voice_size);
71 }
72}
73
74static void glue (audio_pcm_hw_free_resources_, TYPE) (HW *hw)
75{
76 if (HWBUF) {
77 qemu_free (HWBUF);
78 }
79
80 HWBUF = NULL;
81}
82
83static int glue (audio_pcm_hw_alloc_resources_, TYPE) (HW *hw)
84{
85 HWBUF = audio_calloc (AUDIO_FUNC, hw->samples, sizeof (st_sample_t));
86 if (!HWBUF) {
87 dolog ("Could not allocate " NAME " buffer (%d samples)\n",
88 hw->samples);
89 return -1;
90 }
91
92 return 0;
93}
94
95static void glue (audio_pcm_sw_free_resources_, TYPE) (SW *sw)
96{
97 if (sw->buf) {
98 qemu_free (sw->buf);
99 }
100
101 if (sw->rate) {
102 st_rate_stop (sw->rate);
103 }
104
105 sw->buf = NULL;
106 sw->rate = NULL;
107}
108
109static int glue (audio_pcm_sw_alloc_resources_, TYPE) (SW *sw)
110{
111 int samples;
112
113#ifdef DAC
114 samples = sw->hw->samples;
115#else
116 samples = ((int64_t) sw->hw->samples << 32) / sw->ratio;
117#endif
118
119 sw->buf = audio_calloc (AUDIO_FUNC, samples, sizeof (st_sample_t));
120 if (!sw->buf) {
121 dolog ("Could not allocate buffer for `%s' (%d samples)\n",
122 SW_NAME (sw), samples);
123 return -1;
124 }
125
126#ifdef DAC
127 sw->rate = st_rate_start (sw->info.freq, sw->hw->info.freq);
128#else
129 sw->rate = st_rate_start (sw->hw->info.freq, sw->info.freq);
130#endif
131 if (!sw->rate) {
132 qemu_free (sw->buf);
133 sw->buf = NULL;
134 return -1;
135 }
136 return 0;
137}
138
139static int glue (audio_pcm_sw_init_, TYPE) (
140 SW *sw,
141 HW *hw,
142 const char *name,
143 audsettings_t *as
144 )
145{
146 int err;
147
148 audio_pcm_init_info (&sw->info, as);
149 sw->hw = hw;
150 sw->active = 0;
151#ifdef DAC
152 sw->ratio = ((int64_t) sw->hw->info.freq << 32) / sw->info.freq;
153 sw->total_hw_samples_mixed = 0;
154 sw->empty = 1;
155#else
156 sw->ratio = ((int64_t) sw->info.freq << 32) / sw->hw->info.freq;
157#endif
158
159#ifdef DAC
160 sw->conv = mixeng_conv
161#else
162 sw->clip = mixeng_clip
163#endif
164 [sw->info.nchannels == 2]
165 [sw->info.sign]
166 [sw->info.swap_endianness]
167 [sw->info.bits == 16];
168
169 sw->name = qemu_strdup (name);
170 err = glue (audio_pcm_sw_alloc_resources_, TYPE) (sw);
171 if (err) {
172 qemu_free (sw->name);
173 sw->name = NULL;
174 }
175 return err;
176}
177
178static void glue (audio_pcm_sw_fini_, TYPE) (SW *sw)
179{
180 glue (audio_pcm_sw_free_resources_, TYPE) (sw);
181 if (sw->name) {
182 qemu_free (sw->name);
183 sw->name = NULL;
184 }
185}
186
187static void glue (audio_pcm_hw_add_sw_, TYPE) (HW *hw, SW *sw)
188{
189 LIST_INSERT_HEAD (&hw->sw_head, sw, entries);
190}
191
192static void glue (audio_pcm_hw_del_sw_, TYPE) (SW *sw)
193{
194 LIST_REMOVE (sw, entries);
195}
196
197static void glue (audio_pcm_hw_gc_, TYPE) (AudioState *s, HW **hwp)
198{
199 HW *hw = *hwp;
200
201 if (!hw->sw_head.lh_first) {
202#ifdef DAC
203 audio_detach_capture (hw);
204#endif
205 LIST_REMOVE (hw, entries);
206 glue (s->nb_hw_voices_, TYPE) += 1;
207 glue (audio_pcm_hw_free_resources_ ,TYPE) (hw);
208 glue (hw->pcm_ops->fini_, TYPE) (hw);
209 qemu_free (hw);
210 *hwp = NULL;
211 }
212}
213
214static HW *glue (audio_pcm_hw_find_any_, TYPE) (AudioState *s, HW *hw)
215{
216 return hw ? hw->entries.le_next : s->glue (hw_head_, TYPE).lh_first;
217}
218
219static HW *glue (audio_pcm_hw_find_any_enabled_, TYPE) (AudioState *s, HW *hw)
220{
221 while ((hw = glue (audio_pcm_hw_find_any_, TYPE) (s, hw))) {
222 if (hw->enabled) {
223 return hw;
224 }
225 }
226 return NULL;
227}
228
229static HW *glue (audio_pcm_hw_find_specific_, TYPE) (
230 AudioState *s,
231 HW *hw,
232 audsettings_t *as
233 )
234{
235 while ((hw = glue (audio_pcm_hw_find_any_, TYPE) (s, hw))) {
236 if (audio_pcm_info_eq (&hw->info, as)) {
237 return hw;
238 }
239 }
240 return NULL;
241}
242
243static HW *glue (audio_pcm_hw_add_new_, TYPE) (AudioState *s, audsettings_t *as)
244{
245 HW *hw;
246 struct audio_driver *drv = s->drv;
247
248 if (!glue (s->nb_hw_voices_, TYPE)) {
249 return NULL;
250 }
251
252 if (audio_bug (AUDIO_FUNC, !drv)) {
253 dolog ("No host audio driver\n");
254 return NULL;
255 }
256
257 if (audio_bug (AUDIO_FUNC, !drv->pcm_ops)) {
258 dolog ("Host audio driver without pcm_ops\n");
259 return NULL;
260 }
261
262 hw = audio_calloc (AUDIO_FUNC, 1, glue (drv->voice_size_, TYPE));
263 if (!hw) {
264 dolog ("Can not allocate voice `%s' size %d\n",
265 drv->name, glue (drv->voice_size_, TYPE));
266 return NULL;
267 }
268
269 hw->pcm_ops = drv->pcm_ops;
270 LIST_INIT (&hw->sw_head);
271
272#ifdef DAC
273 LIST_INIT (&hw->cap_head);
274#endif
275 if (glue (hw->pcm_ops->init_, TYPE) (hw, as)) {
276 goto err0;
277 }
278
279 if (audio_bug (AUDIO_FUNC, hw->samples <= 0)) {
280 dolog ("hw->samples=%d\n", hw->samples);
281 goto err1;
282 }
283
284#ifdef DAC
285 hw->clip = mixeng_clip
286#else
287 hw->conv = mixeng_conv
288#endif
289 [hw->info.nchannels == 2]
290 [hw->info.sign]
291 [hw->info.swap_endianness]
292 [hw->info.bits == 16];
293
294 if (glue (audio_pcm_hw_alloc_resources_, TYPE) (hw)) {
295 goto err1;
296 }
297
298 LIST_INSERT_HEAD (&s->glue (hw_head_, TYPE), hw, entries);
299 glue (s->nb_hw_voices_, TYPE) -= 1;
300#ifdef DAC
301 audio_attach_capture (s, hw);
302#endif
303 return hw;
304
305 err1:
306 glue (hw->pcm_ops->fini_, TYPE) (hw);
307 err0:
308 qemu_free (hw);
309 return NULL;
310}
311
312static HW *glue (audio_pcm_hw_add_, TYPE) (AudioState *s, audsettings_t *as)
313{
314 HW *hw;
315
316 if (glue (conf.fixed_, TYPE).enabled && glue (conf.fixed_, TYPE).greedy) {
317 hw = glue (audio_pcm_hw_add_new_, TYPE) (s, as);
318 if (hw) {
319 return hw;
320 }
321 }
322
323 hw = glue (audio_pcm_hw_find_specific_, TYPE) (s, NULL, as);
324 if (hw) {
325 return hw;
326 }
327
328 hw = glue (audio_pcm_hw_add_new_, TYPE) (s, as);
329 if (hw) {
330 return hw;
331 }
332
333 return glue (audio_pcm_hw_find_any_, TYPE) (s, NULL);
334}
335
336static SW *glue (audio_pcm_create_voice_pair_, TYPE) (
337 AudioState *s,
338 const char *sw_name,
339 audsettings_t *as
340 )
341{
342 SW *sw;
343 HW *hw;
344 audsettings_t hw_as;
345
346 if (glue (conf.fixed_, TYPE).enabled) {
347 hw_as = glue (conf.fixed_, TYPE).settings;
348 }
349 else {
350 hw_as = *as;
351 }
352
353 sw = audio_calloc (AUDIO_FUNC, 1, sizeof (*sw));
354 if (!sw) {
355#if defined __STDC_VERSION__ && __STDC_VERSION__ > 199901L
356 dolog ("Could not allocate soft voice `%s' (%zu bytes)\n",
357 sw_name ? sw_name : "unknown", sizeof (*sw));
358#else
359 dolog ("Could not allocate soft voice `%s' (%u bytes)\n",
360 sw_name ? sw_name : "unknown", sizeof (*sw));
361#endif
362 goto err1;
363 }
364
365 hw = glue (audio_pcm_hw_add_, TYPE) (s, &hw_as);
366 if (!hw) {
367 goto err2;
368 }
369
370 glue (audio_pcm_hw_add_sw_, TYPE) (hw, sw);
371
372 if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, sw_name, as)) {
373 goto err3;
374 }
375
376 return sw;
377
378err3:
379 glue (audio_pcm_hw_del_sw_, TYPE) (sw);
380 glue (audio_pcm_hw_gc_, TYPE) (s, &hw);
381err2:
382 qemu_free (sw);
383err1:
384 return NULL;
385}
386
387static void glue (audio_close_, TYPE) (AudioState *s, SW *sw)
388{
389 glue (audio_pcm_sw_fini_, TYPE) (sw);
390 glue (audio_pcm_hw_del_sw_, TYPE) (sw);
391 glue (audio_pcm_hw_gc_, TYPE) (s, &sw->hw);
392 qemu_free (sw);
393}
394
395void glue (AUD_close_, TYPE) (QEMUSoundCard *card, SW *sw)
396{
397 if (sw) {
398 if (audio_bug (AUDIO_FUNC, !card || !card->audio)) {
399 dolog ("card=%p card->audio=%p\n",
400 (void *) card, card ? (void *) card->audio : NULL);
401 return;
402 }
403
404 glue (audio_close_, TYPE) (card->audio, sw);
405 }
406}
407
408SW *glue (AUD_open_, TYPE) (
409 QEMUSoundCard *card,
410 SW *sw,
411 const char *name,
412 void *callback_opaque ,
413 audio_callback_fn_t callback_fn,
414 audsettings_t *as
415 )
416{
417 AudioState *s;
418#ifdef DAC
419 int live = 0;
420 SW *old_sw = NULL;
421#endif
422
423 ldebug ("open %s, freq %d, nchannels %d, fmt %d\n",
424 name, as->freq, as->nchannels, as->fmt);
425
426 if (audio_bug (AUDIO_FUNC,
427 !card || !card->audio || !name || !callback_fn || !as)) {
428 dolog ("card=%p card->audio=%p name=%p callback_fn=%p as=%p\n",
429 (void *) card, card ? (void *) card->audio : NULL,
430 name,
431 (void *) callback_fn,
432 (void *) as);
433 goto fail;
434 }
435
436 s = card->audio;
437
438 if (audio_bug (AUDIO_FUNC, audio_validate_settings (as))) {
439 audio_print_settings (as);
440 goto fail;
441 }
442
443 if (audio_bug (AUDIO_FUNC, !s->drv)) {
444 dolog ("Can not open `%s' (no host audio driver)\n", name);
445 goto fail;
446 }
447
448 if (sw && audio_pcm_info_eq (&sw->info, as)) {
449 return sw;
450 }
451
452#ifdef DAC
453 if (conf.plive && sw && (!sw->active && !sw->empty)) {
454 live = sw->total_hw_samples_mixed;
455
456#ifdef DEBUG_PLIVE
457 dolog ("Replacing voice %s with %d live samples\n", SW_NAME (sw), live);
458 dolog ("Old %s freq %d, bits %d, channels %d\n",
459 SW_NAME (sw), sw->info.freq, sw->info.bits, sw->info.nchannels);
460 dolog ("New %s freq %d, bits %d, channels %d\n",
461 name,
462 freq,
463 (fmt == AUD_FMT_S16 || fmt == AUD_FMT_U16) ? 16 : 8,
464 nchannels);
465#endif
466
467 if (live) {
468 old_sw = sw;
469 old_sw->callback.fn = NULL;
470 sw = NULL;
471 }
472 }
473#endif
474
475 if (!glue (conf.fixed_, TYPE).enabled && sw) {
476 glue (AUD_close_, TYPE) (card, sw);
477 sw = NULL;
478 }
479
480 if (sw) {
481 HW *hw = sw->hw;
482
483 if (!hw) {
484 dolog ("Internal logic error voice `%s' has no hardware store\n",
485 SW_NAME (sw));
486 goto fail;
487 }
488
489 glue (audio_pcm_sw_fini_, TYPE) (sw);
490 if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, name, as)) {
491 goto fail;
492 }
493 }
494 else {
495 sw = glue (audio_pcm_create_voice_pair_, TYPE) (s, name, as);
496 if (!sw) {
497 dolog ("Failed to create voice `%s'\n", name);
498 return NULL;
499 }
500 }
501
502 if (sw) {
503 sw->vol = nominal_volume;
504 sw->callback.fn = callback_fn;
505 sw->callback.opaque = callback_opaque;
506
507#ifdef DAC
508 if (live) {
509 int mixed =
510 (live << old_sw->info.shift)
511 * old_sw->info.bytes_per_second
512 / sw->info.bytes_per_second;
513
514#ifdef DEBUG_PLIVE
515 dolog ("Silence will be mixed %d\n", mixed);
516#endif
517 sw->total_hw_samples_mixed += mixed;
518 }
519#endif
520
521#ifdef DEBUG_AUDIO
522 dolog ("%s\n", name);
523 audio_pcm_print_info ("hw", &sw->hw->info);
524 audio_pcm_print_info ("sw", &sw->info);
525#endif
526 }
527
528 return sw;
529
530 fail:
531 glue (AUD_close_, TYPE) (card, sw);
532 return NULL;
533}
534
535int glue (AUD_is_active_, TYPE) (SW *sw)
536{
537 return sw ? sw->active : 0;
538}
539
540void glue (AUD_init_time_stamp_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts)
541{
542 if (!sw) {
543 return;
544 }
545
546 ts->old_ts = sw->hw->ts_helper;
547}
548
549uint64_t glue (AUD_get_elapsed_usec_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts)
550{
551 uint64_t delta, cur_ts, old_ts;
552
553 if (!sw) {
554 return 0;
555 }
556
557 cur_ts = sw->hw->ts_helper;
558 old_ts = ts->old_ts;
559 /* dolog ("cur %lld old %lld\n", cur_ts, old_ts); */
560
561 if (cur_ts >= old_ts) {
562 delta = cur_ts - old_ts;
563 }
564 else {
565 delta = UINT64_MAX - old_ts + cur_ts;
566 }
567
568 if (!delta) {
569 return 0;
570 }
571
572 return (delta * sw->hw->info.freq) / 1000000;
573}
574
575#undef TYPE
576#undef HW
577#undef SW
578#undef HWBUF
579#undef NAME
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette