VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/audio.c@ 32673

Last change on this file since 32673 was 32673, checked in by vboxsync, 14 years ago

Devices/Audio: minor fix (free alloc'd string).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 51.1 KB
Line 
1/*
2 * QEMU Audio subsystem
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#define LOG_GROUP LOG_GROUP_DEV_AUDIO
25#include <VBox/pdm.h>
26#include <VBox/err.h>
27#include <VBox/mm.h>
28
29#include <VBox/log.h>
30#include <iprt/asm-math.h>
31#include <iprt/assert.h>
32#include <iprt/uuid.h>
33#include <iprt/string.h>
34#include <iprt/alloc.h>
35
36#include "Builtins.h"
37#include "../../vl_vbox.h"
38
39#include <ctype.h>
40#include <stdlib.h>
41
42#define AUDIO_CAP "audio"
43#include "audio.h"
44#include "audio_int.h"
45
46#ifdef RT_OS_WINDOWS
47#define strcasecmp stricmp
48#endif
49
50/* #define DEBUG_PLIVE */
51/* #define DEBUG_LIVE */
52/* #define DEBUG_OUT */
53/* #define DEBUG_CAPTURE */
54
55#define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
56
57/**
58 * @implements PDMIAUDIOCONNECTOR
59 */
60typedef struct DRVAUDIO
61{
62 /** The audio interface. */
63 PDMIAUDIOCONNECTOR IAudioConnector;
64 /** Pointer to the driver instance. */
65 PPDMDRVINS pDrvIns;
66} DRVAUDIO, *PDRVAUDIO;
67
68static struct audio_driver *drvtab[] = {
69#if defined (RT_OS_LINUX) || defined (RT_OS_FREEBSD) || defined(VBOX_WITH_SOLARIS_OSS)
70 &oss_audio_driver,
71#endif
72#ifdef RT_OS_LINUX
73# ifdef VBOX_WITH_PULSE
74 &pulse_audio_driver,
75# endif
76# ifdef VBOX_WITH_ALSA
77 &alsa_audio_driver,
78# endif
79#endif /* RT_OS_LINUX */
80#ifdef RT_OS_FREEBSD
81# ifdef VBOX_WITH_PULSE
82 &pulse_audio_driver,
83# endif
84#endif
85#ifdef RT_OS_DARWIN
86 &coreaudio_audio_driver,
87#endif
88#ifdef RT_OS_WINDOWS
89 &dsound_audio_driver,
90#endif
91#ifdef RT_OS_L4
92 &oss_audio_driver,
93#endif
94#ifdef RT_OS_SOLARIS
95 &solaudio_audio_driver,
96#endif
97 &no_audio_driver
98};
99
100static char *audio_streamname;
101
102const char *audio_get_stream_name(void)
103{
104 return audio_streamname;
105}
106
107struct fixed_settings {
108 int enabled;
109 int nb_voices;
110 int greedy;
111 audsettings_t settings;
112};
113
114static struct {
115 struct fixed_settings fixed_out;
116 struct fixed_settings fixed_in;
117 union {
118 int hz;
119 int64_t ticks;
120 } period;
121 int plive;
122} conf = {
123 { /* DAC fixed settings */
124 1, /* enabled */
125 1, /* nb_voices */
126 1, /* greedy */
127 {
128 44100, /* freq */
129 2, /* nchannels */
130 AUD_FMT_S16 /* fmt */
131 }
132 },
133
134 { /* ADC fixed settings */
135 1, /* enabled */
136 1, /* nb_voices */
137 1, /* greedy */
138 {
139 44100, /* freq */
140 2, /* nchannels */
141 AUD_FMT_S16 /* fmt */
142 }
143 },
144
145 { 200 }, /* frequency (in Hz) */
146 0, /* plive */
147};
148
149static AudioState glob_audio_state;
150
151volume_t nominal_volume = {
152 0,
153#ifdef FLOAT_MIXENG
154 1.0,
155 1.0
156#else
157#ifndef VBOX
158 UINT_MAX,
159 UINT_MAX
160#else
161 INT_MAX,
162 INT_MAX
163#endif
164#endif
165};
166
167#ifdef VBOX
168volume_t sum_out_volume =
169{
170 0,
171 INT_MAX,
172 INT_MAX
173};
174volume_t master_out_volume =
175{
176 0,
177 INT_MAX,
178 INT_MAX
179};
180volume_t pcm_out_volume =
181{
182 0,
183 INT_MAX,
184 INT_MAX
185};
186volume_t pcm_in_volume =
187{
188 0,
189 INT_MAX,
190 INT_MAX
191};
192#endif
193
194/* http://www.df.lth.se/~john_e/gems/gem002d.html */
195/* http://www.multi-platforms.com/Tips/PopCount.htm */
196uint32_t popcount (uint32_t u)
197{
198 u = ((u&0x55555555) + ((u>>1)&0x55555555));
199 u = ((u&0x33333333) + ((u>>2)&0x33333333));
200 u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
201 u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
202 u = ( u&0x0000ffff) + (u>>16);
203 return u;
204}
205
206uint32_t lsbindex (uint32_t u)
207{
208 return popcount ((u&-u)-1);
209}
210
211uint64_t audio_get_clock (void)
212{
213 AudioState *s;
214
215 s = &glob_audio_state;
216 return PDMDrvHlpTMGetVirtualTime (s->pDrvIns);
217}
218
219uint64_t audio_get_ticks_per_sec (void)
220{
221 AudioState *s;
222
223 s = &glob_audio_state;
224 return PDMDrvHlpTMGetVirtualFreq (s->pDrvIns);
225}
226
227#ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
228#error No its not
229#else
230int audio_bug (const char *funcname, int cond)
231{
232 if (cond) {
233 static int shown;
234
235 AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
236 if (!shown) {
237 shown = 1;
238 AUD_log (NULL, "Save all your work and restart without audio\n");
239 AUD_log (NULL, "Please send a bug, see www.virtualbox.org\n");
240 AUD_log (NULL, "I am sorry\n");
241 }
242 AUD_log (NULL, "Context:\n");
243
244#if defined AUDIO_BREAKPOINT_ON_BUG
245# if defined HOST_I386
246# if defined __GNUC__
247 __asm__ ("int3");
248# elif defined _MSC_VER
249 _asm _emit 0xcc;
250# else
251 abort ();
252# endif
253# else
254 abort ();
255# endif
256#endif
257 }
258
259 return cond;
260}
261#endif
262
263static inline int audio_bits_to_index (int bits)
264{
265 switch (bits) {
266 case 8:
267 return 0;
268
269 case 16:
270 return 1;
271
272 case 32:
273 return 2;
274
275 default:
276 audio_bug ("bits_to_index", 1);
277 AUD_log (NULL, "invalid bits %d\n", bits);
278 return 0;
279 }
280}
281
282void *audio_calloc (const char *funcname, int nmemb, size_t size)
283{
284 int cond;
285 size_t len;
286
287 len = nmemb * size;
288 cond = !nmemb || !size;
289 cond |= nmemb < 0;
290 cond |= len < size;
291
292 if (audio_bug ("audio_calloc", cond)) {
293 AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
294 funcname);
295 AUD_log (NULL, "nmemb=%d size=%" FMTZ "u (len=%" FMTZ "u)\n",
296 nmemb, size, len);
297 return NULL;
298 }
299
300 return qemu_mallocz (len);
301}
302
303static const char *audio_audfmt_to_string (audfmt_e fmt)
304{
305 switch (fmt) {
306 case AUD_FMT_U8:
307 return "U8";
308
309 case AUD_FMT_U16:
310 return "U16";
311
312 case AUD_FMT_U32:
313 return "U32";
314
315 case AUD_FMT_S8:
316 return "S8";
317
318 case AUD_FMT_S16:
319 return "S16";
320
321 case AUD_FMT_S32:
322 return "S32";
323 }
324
325 dolog ("Bogus audfmt %d returning S16\n", fmt);
326 return "S16";
327}
328
329static audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval,
330 int *defaultp)
331{
332 if (!strcasecmp (s, "u8")) {
333 *defaultp = 0;
334 return AUD_FMT_U8;
335 }
336 else if (!strcasecmp (s, "u16")) {
337 *defaultp = 0;
338 return AUD_FMT_U16;
339 }
340 else if (!strcasecmp (s, "u32")) {
341 *defaultp = 0;
342 return AUD_FMT_U32;
343 }
344 else if (!strcasecmp (s, "s8")) {
345 *defaultp = 0;
346 return AUD_FMT_S8;
347 }
348 else if (!strcasecmp (s, "s16")) {
349 *defaultp = 0;
350 return AUD_FMT_S16;
351 }
352 else if (!strcasecmp (s, "s32")) {
353 *defaultp = 0;
354 return AUD_FMT_S32;
355 }
356 else {
357 dolog ("Bogus audio format `%s' using %s\n",
358 s, audio_audfmt_to_string (defval));
359 *defaultp = 1;
360 return defval;
361 }
362}
363
364static audfmt_e audio_get_conf_fmt (const char *envname,
365 audfmt_e defval,
366 int *defaultp)
367{
368 const char *var = getenv (envname);
369 if (!var) {
370 *defaultp = 1;
371 return defval;
372 }
373 return audio_string_to_audfmt (var, defval, defaultp);
374}
375
376static int audio_get_conf_int (const char *key, int defval, int *defaultp)
377{
378 int val;
379 char *strval;
380
381 strval = getenv (key);
382 if (strval) {
383 *defaultp = 0;
384 val = atoi (strval);
385 return val;
386 }
387 else {
388 *defaultp = 1;
389 return defval;
390 }
391}
392
393static const char *audio_get_conf_str (const char *key,
394 const char *defval,
395 int *defaultp)
396{
397 const char *val = getenv (key);
398 if (!val) {
399 *defaultp = 1;
400 return defval;
401 }
402 else {
403 *defaultp = 0;
404 return val;
405 }
406}
407
408void AUD_vlog (const char *cap, const char *fmt, va_list va)
409{
410 va_list va2;
411 va_copy (va2, va); /* Have to make a copy here or GCC will break. */
412 if (cap) {
413 Log (("%s: %N", cap, fmt, &va2));
414 }
415 else {
416 Log (("%N", fmt, &va2));
417 }
418 va_end (va2);
419}
420
421void AUD_log (const char *cap, const char *fmt, ...)
422{
423 va_list va;
424
425 va_start (va, fmt);
426 AUD_vlog (cap, fmt, va);
427 va_end (va);
428}
429
430static void audio_process_options (const char *prefix,
431 struct audio_option *opt)
432{
433 char *optname;
434 const char vbox_prefix[] = "VBOX_";
435 size_t preflen;
436
437 if (audio_bug (AUDIO_FUNC, !prefix)) {
438 dolog ("prefix = NULL\n");
439 return;
440 }
441
442 if (audio_bug (AUDIO_FUNC, !opt)) {
443 dolog ("opt = NULL\n");
444 return;
445 }
446
447 preflen = strlen (prefix);
448
449 for (; opt->name; opt++) {
450 size_t len, i;
451 int def;
452
453 if (!opt->valp) {
454 dolog ("Option value pointer for `%s' is not set\n",
455 opt->name);
456 continue;
457 }
458
459 len = strlen (opt->name);
460 /* len of opt->name + len of prefix + size of vbox_prefix
461 * (includes trailing zero) + zero + underscore (on behalf of
462 * sizeof) */
463 optname = qemu_malloc (len + preflen + sizeof (vbox_prefix) + 1);
464 if (!optname) {
465 dolog ("Could not allocate memory for option name `%s'\n",
466 opt->name);
467 continue;
468 }
469
470 strcpy (optname, vbox_prefix);
471
472 /* copy while upcasing, including trailing zero */
473 for (i = 0; i <= preflen; ++i) {
474 optname[i + sizeof (vbox_prefix) - 1] = toupper (prefix[i]);
475 }
476 strcat (optname, "_");
477 strcat (optname, opt->name);
478
479 def = 1;
480 switch (opt->tag) {
481 case AUD_OPT_BOOL:
482 case AUD_OPT_INT:
483 {
484 int *intp = opt->valp;
485 *intp = audio_get_conf_int (optname, *intp, &def);
486 }
487 break;
488
489 case AUD_OPT_FMT:
490 {
491 audfmt_e *fmtp = opt->valp;
492 *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
493 }
494 break;
495
496 case AUD_OPT_STR:
497 {
498 const char **strp = opt->valp;
499 *strp = audio_get_conf_str (optname, *strp, &def);
500 }
501 break;
502
503 default:
504 dolog ("Bad value tag for option `%s' - %d\n",
505 optname, opt->tag);
506 break;
507 }
508
509 if (!opt->overridenp) {
510 opt->overridenp = &opt->overriden;
511 }
512 *opt->overridenp = !def;
513 qemu_free (optname);
514 }
515}
516
517static void audio_print_settings (audsettings_t *as)
518{
519 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
520
521 switch (as->fmt) {
522 case AUD_FMT_S8:
523 AUD_log (NULL, "S8");
524 break;
525 case AUD_FMT_U8:
526 AUD_log (NULL, "U8");
527 break;
528 case AUD_FMT_S16:
529 AUD_log (NULL, "S16");
530 break;
531 case AUD_FMT_U16:
532 AUD_log (NULL, "U16");
533 break;
534 case AUD_FMT_S32:
535 AUD_log (NULL, "S32");
536 break;
537 case AUD_FMT_U32:
538 AUD_log (NULL, "U32");
539 break;
540 default:
541 AUD_log (NULL, "invalid(%d)", as->fmt);
542 break;
543 }
544
545 AUD_log (NULL, " endianness=");
546 switch (as->endianness) {
547 case 0:
548 AUD_log (NULL, "little");
549 break;
550 case 1:
551 AUD_log (NULL, "big");
552 break;
553 default:
554 AUD_log (NULL, "invalid");
555 break;
556 }
557 AUD_log (NULL, "\n");
558}
559
560static int audio_validate_settings (audsettings_t *as)
561{
562 int invalid;
563
564 invalid = as->nchannels != 1 && as->nchannels != 2;
565 invalid |= as->endianness != 0 && as->endianness != 1;
566
567 switch (as->fmt) {
568 case AUD_FMT_S8:
569 case AUD_FMT_U8:
570 case AUD_FMT_S16:
571 case AUD_FMT_U16:
572 case AUD_FMT_S32:
573 case AUD_FMT_U32:
574 break;
575 default:
576 invalid = 1;
577 break;
578 }
579
580 invalid |= as->freq <= 0;
581 return invalid ? -1 : 0;
582}
583
584static int audio_pcm_info_eq (struct audio_pcm_info *info, audsettings_t *as)
585{
586 int bits = 8, sign = 0;
587
588 switch (as->fmt) {
589 case AUD_FMT_S8:
590 sign = 1;
591 case AUD_FMT_U8:
592 break;
593
594 case AUD_FMT_S16:
595 sign = 1;
596 case AUD_FMT_U16:
597 bits = 16;
598 break;
599
600 case AUD_FMT_S32:
601 sign = 1;
602 case AUD_FMT_U32:
603 bits = 32;
604 break;
605 }
606 return info->freq == as->freq
607 && info->nchannels == as->nchannels
608 && info->sign == sign
609 && info->bits == bits
610 && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
611}
612
613void audio_pcm_init_info (struct audio_pcm_info *info, audsettings_t *as)
614{
615 int bits = 8, sign = 0, shift = 0;
616
617 switch (as->fmt) {
618 case AUD_FMT_S8:
619 sign = 1;
620 case AUD_FMT_U8:
621 break;
622
623 case AUD_FMT_S16:
624 sign = 1;
625 case AUD_FMT_U16:
626 bits = 16;
627 shift = 1;
628 break;
629
630 case AUD_FMT_S32:
631 sign = 1;
632 case AUD_FMT_U32:
633 bits = 32;
634 shift = 2;
635 break;
636 }
637
638 info->freq = as->freq;
639 info->bits = bits;
640 info->sign = sign;
641 info->nchannels = as->nchannels;
642 info->shift = (as->nchannels == 2) + shift;
643 info->align = (1 << info->shift) - 1;
644 info->bytes_per_second = info->freq << info->shift;
645 info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
646}
647
648void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
649{
650 if (!len) {
651 return;
652 }
653
654 if (info->sign) {
655 memset (buf, 0x00, len << info->shift);
656 }
657 else {
658 switch (info->bits) {
659 case 8:
660 memset (buf, 0x80, len << info->shift);
661 break;
662
663 case 16:
664 {
665 int i;
666 uint16_t *p = buf;
667 int shift = info->nchannels - 1;
668 short s = INT16_MAX;
669
670 if (info->swap_endianness) {
671 s = bswap16 (s);
672 }
673
674 for (i = 0; i < len << shift; i++) {
675 p[i] = s;
676 }
677 }
678 break;
679
680 case 32:
681 {
682 int i;
683 uint32_t *p = buf;
684 int shift = info->nchannels - 1;
685 int32_t s = INT32_MAX;
686
687 if (info->swap_endianness) {
688 s = bswap32 (s);
689 }
690
691 for (i = 0; i < len << shift; i++) {
692 p[i] = s;
693 }
694 }
695 break;
696
697 default:
698 AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
699 info->bits);
700 break;
701 }
702 }
703}
704
705/*
706 * Capture
707 */
708static void noop_conv (st_sample_t *dst, const void *src,
709 int samples, volume_t *vol)
710{
711 (void) src;
712 (void) dst;
713 (void) samples;
714 (void) vol;
715}
716
717static CaptureVoiceOut *audio_pcm_capture_find_specific (
718 AudioState *s,
719 audsettings_t *as
720 )
721{
722 CaptureVoiceOut *cap;
723
724 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
725 if (audio_pcm_info_eq (&cap->hw.info, as)) {
726 return cap;
727 }
728 }
729 return NULL;
730}
731
732static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
733{
734 struct capture_callback *cb;
735
736#ifdef DEBUG_CAPTURE
737 dolog ("notification %d sent\n", cmd);
738#endif
739 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
740 cb->ops.notify (cb->opaque, cmd);
741 }
742}
743
744static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
745{
746 if (cap->hw.enabled != enabled) {
747 audcnotification_e cmd;
748 cap->hw.enabled = enabled;
749 cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
750 audio_notify_capture (cap, cmd);
751 }
752}
753
754static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
755{
756 HWVoiceOut *hw = &cap->hw;
757 SWVoiceOut *sw;
758 int enabled = 0;
759
760 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
761 if (sw->active) {
762 enabled = 1;
763 break;
764 }
765 }
766 audio_capture_maybe_changed (cap, enabled);
767}
768
769static void audio_detach_capture (HWVoiceOut *hw)
770{
771 SWVoiceCap *sc = hw->cap_head.lh_first;
772
773 while (sc) {
774 SWVoiceCap *sc1 = sc->entries.le_next;
775 SWVoiceOut *sw = &sc->sw;
776 CaptureVoiceOut *cap = sc->cap;
777 int was_active = sw->active;
778
779 if (sw->rate) {
780 st_rate_stop (sw->rate);
781 sw->rate = NULL;
782 }
783
784 LIST_REMOVE (sw, entries);
785 LIST_REMOVE (sc, entries);
786 qemu_free (sc);
787 if (was_active) {
788 /* We have removed soft voice from the capture:
789 this might have changed the overall status of the capture
790 since this might have been the only active voice */
791 audio_recalc_and_notify_capture (cap);
792 }
793 sc = sc1;
794 }
795}
796
797static int audio_attach_capture (AudioState *s, HWVoiceOut *hw)
798{
799 CaptureVoiceOut *cap;
800
801 audio_detach_capture (hw);
802 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
803 SWVoiceCap *sc;
804 SWVoiceOut *sw;
805 HWVoiceOut *hw_cap = &cap->hw;
806
807 sc = audio_calloc (AUDIO_FUNC, 1, sizeof (*sc));
808 if (!sc) {
809 dolog ("Could not allocate soft capture voice (%u bytes)\n",
810 sizeof (*sc));
811 return -1;
812 }
813
814 sc->cap = cap;
815 sw = &sc->sw;
816 sw->hw = hw_cap;
817 sw->info = hw->info;
818 sw->empty = 1;
819 sw->active = hw->enabled;
820 sw->conv = noop_conv;
821 sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
822 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
823 if (!sw->rate) {
824 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
825 qemu_free (sw);
826 return -1;
827 }
828 LIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
829 LIST_INSERT_HEAD (&hw->cap_head, sc, entries);
830#ifdef DEBUG_CAPTURE
831 asprintf (&sw->name, "for %p %d,%d,%d",
832 hw, sw->info.freq, sw->info.bits, sw->info.nchannels);
833 dolog ("Added %s active = %d\n", sw->name, sw->active);
834#endif
835 if (sw->active) {
836 audio_capture_maybe_changed (cap, 1);
837 }
838 }
839 return 0;
840}
841
842/*
843 * Hard voice (capture)
844 */
845static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
846{
847 SWVoiceIn *sw;
848 int m = hw->total_samples_captured;
849
850 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
851 if (sw->active) {
852 m = audio_MIN (m, sw->total_hw_samples_acquired);
853 }
854 }
855 return m;
856}
857
858int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
859{
860 int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
861 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
862 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
863 return 0;
864 }
865 return live;
866}
867
868/*
869 * Soft voice (capture)
870 */
871static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
872{
873 HWVoiceIn *hw = sw->hw;
874 int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
875 int rpos;
876
877 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
878 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
879 return 0;
880 }
881
882 rpos = hw->wpos - live;
883 if (rpos >= 0) {
884 return rpos;
885 }
886 else {
887 return hw->samples + rpos;
888 }
889}
890
891int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
892{
893 HWVoiceIn *hw = sw->hw;
894 int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
895 st_sample_t *src, *dst = sw->buf;
896
897 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
898
899 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
900 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
901 dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
902 return 0;
903 }
904
905 samples = size >> sw->info.shift;
906 if (!live) {
907 return 0;
908 }
909
910 swlim = (live * sw->ratio) >> 32;
911 swlim = audio_MIN (swlim, samples);
912
913 while (swlim) {
914 src = hw->conv_buf + rpos;
915 isamp = hw->wpos - rpos;
916 /* XXX: <= ? */
917 if (isamp <= 0) {
918 isamp = hw->samples - rpos;
919 }
920
921 if (!isamp) {
922 break;
923 }
924 osamp = swlim;
925
926 if (audio_bug (AUDIO_FUNC, osamp < 0)) {
927 dolog ("osamp=%d\n", osamp);
928 return 0;
929 }
930
931 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
932 swlim -= osamp;
933 rpos = (rpos + isamp) % hw->samples;
934 dst += osamp;
935 ret += osamp;
936 total += isamp;
937 }
938
939 sw->clip (buf, sw->buf, ret);
940 sw->total_hw_samples_acquired += total;
941 return ret << sw->info.shift;
942}
943
944/*
945 * Hard voice (playback)
946 */
947static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
948{
949 SWVoiceOut *sw;
950 int m = INT_MAX;
951 int nb_live = 0;
952
953 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
954 if (sw->active || !sw->empty) {
955 m = audio_MIN (m, sw->total_hw_samples_mixed);
956 nb_live += 1;
957 }
958 }
959
960 *nb_livep = nb_live;
961 return m;
962}
963
964int audio_pcm_hw_get_live_out2 (HWVoiceOut *hw, int *nb_live)
965{
966 int smin;
967
968 smin = audio_pcm_hw_find_min_out (hw, nb_live);
969
970 if (!*nb_live) {
971 return 0;
972 }
973 else {
974 int live = smin;
975
976 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
977 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
978 return 0;
979 }
980 return live;
981 }
982}
983
984int audio_pcm_hw_get_live_out (HWVoiceOut *hw)
985{
986 int nb_live;
987 int live;
988
989 live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
990 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
991 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
992 return 0;
993 }
994 return live;
995}
996
997/*
998 * Soft voice (playback)
999 */
1000int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
1001{
1002 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
1003 int ret = 0, pos = 0, total = 0;
1004
1005 if (!sw) {
1006 return size;
1007 }
1008
1009 hwsamples = sw->hw->samples;
1010
1011 live = sw->total_hw_samples_mixed;
1012 if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
1013 dolog ("live=%d hw->samples=%d\n", live, hwsamples);
1014 return 0;
1015 }
1016
1017 if (live == hwsamples) {
1018#ifdef DEBUG_OUT
1019 dolog ("%s is full %d\n", sw->name, live);
1020#endif
1021 return 0;
1022 }
1023
1024 wpos = (sw->hw->rpos + live) % hwsamples;
1025 samples = size >> sw->info.shift;
1026
1027 dead = hwsamples - live;
1028 swlim = ((int64_t) dead << 32) / sw->ratio;
1029 swlim = audio_MIN (swlim, samples);
1030 if (swlim) {
1031#ifndef VBOX
1032 sw->conv (sw->buf, buf, swlim, &sw->vol);
1033#else
1034 sw->conv (sw->buf, buf, swlim, &sum_out_volume);
1035#endif
1036 }
1037
1038 while (swlim) {
1039 dead = hwsamples - live;
1040 left = hwsamples - wpos;
1041 blck = audio_MIN (dead, left);
1042 if (!blck) {
1043 break;
1044 }
1045 isamp = swlim;
1046 osamp = blck;
1047 st_rate_flow_mix (
1048 sw->rate,
1049 sw->buf + pos,
1050 sw->hw->mix_buf + wpos,
1051 &isamp,
1052 &osamp
1053 );
1054 ret += isamp;
1055 swlim -= isamp;
1056 pos += isamp;
1057 live += osamp;
1058 wpos = (wpos + osamp) % hwsamples;
1059 total += osamp;
1060 }
1061
1062 sw->total_hw_samples_mixed += total;
1063 sw->empty = sw->total_hw_samples_mixed == 0;
1064
1065#ifdef DEBUG_OUT
1066 dolog (
1067 "%s: write size %d ret %d total sw %d\n",
1068 SW_NAME (sw),
1069 size >> sw->info.shift,
1070 ret,
1071 sw->total_hw_samples_mixed
1072 );
1073#endif
1074
1075 return ret << sw->info.shift;
1076}
1077
1078#ifdef DEBUG_AUDIO
1079static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
1080{
1081 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1082 cap, info->bits, info->sign, info->freq, info->nchannels);
1083}
1084#endif
1085
1086#define DAC
1087#include "audio_template.h"
1088#undef DAC
1089#include "audio_template.h"
1090
1091int AUD_write (SWVoiceOut *sw, void *buf, int size)
1092{
1093 int bytes;
1094
1095 if (!sw) {
1096 /* XXX: Consider options */
1097 return size;
1098 }
1099
1100 if (!sw->hw->enabled) {
1101 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
1102 return 0;
1103 }
1104
1105 bytes = sw->hw->pcm_ops->write (sw, buf, size);
1106 return bytes;
1107}
1108
1109int AUD_read (SWVoiceIn *sw, void *buf, int size)
1110{
1111 int bytes;
1112
1113 if (!sw) {
1114 /* XXX: Consider options */
1115 return size;
1116 }
1117
1118 if (!sw->hw->enabled) {
1119 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1120 return 0;
1121 }
1122
1123 bytes = sw->hw->pcm_ops->read (sw, buf, size);
1124 return bytes;
1125}
1126
1127int AUD_get_buffer_size_out (SWVoiceOut *sw)
1128{
1129 return sw->hw->samples << sw->hw->info.shift;
1130}
1131
1132void AUD_set_active_out (SWVoiceOut *sw, int on)
1133{
1134 HWVoiceOut *hw;
1135
1136 if (!sw) {
1137 return;
1138 }
1139
1140 hw = sw->hw;
1141 if (sw->active != on) {
1142 SWVoiceOut *temp_sw;
1143 SWVoiceCap *sc;
1144
1145 if (on) {
1146 hw->pending_disable = 0;
1147 if (!hw->enabled) {
1148 hw->enabled = 1;
1149 hw->pcm_ops->ctl_out (hw, VOICE_ENABLE);
1150 }
1151 }
1152 else {
1153 if (hw->enabled) {
1154 int nb_active = 0;
1155
1156 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1157 temp_sw = temp_sw->entries.le_next) {
1158 nb_active += temp_sw->active != 0;
1159 }
1160
1161 hw->pending_disable = nb_active == 1;
1162 }
1163 }
1164
1165 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1166 sc->sw.active = hw->enabled;
1167 if (hw->enabled) {
1168 audio_capture_maybe_changed (sc->cap, 1);
1169 }
1170 }
1171 sw->active = on;
1172 }
1173}
1174
1175void AUD_set_active_in (SWVoiceIn *sw, int on)
1176{
1177 HWVoiceIn *hw;
1178
1179 if (!sw) {
1180 return;
1181 }
1182
1183 hw = sw->hw;
1184 if (sw->active != on) {
1185 SWVoiceIn *temp_sw;
1186
1187 if (on) {
1188 if (!hw->enabled) {
1189 hw->enabled = 1;
1190 hw->pcm_ops->ctl_in (hw, VOICE_ENABLE);
1191 }
1192 sw->total_hw_samples_acquired = hw->total_samples_captured;
1193 }
1194 else {
1195 if (hw->enabled) {
1196 int nb_active = 0;
1197
1198 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1199 temp_sw = temp_sw->entries.le_next) {
1200 nb_active += temp_sw->active != 0;
1201 }
1202
1203 if (nb_active == 1) {
1204 hw->enabled = 0;
1205 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
1206 }
1207 }
1208 }
1209 sw->active = on;
1210 }
1211}
1212
1213static int audio_get_avail (SWVoiceIn *sw)
1214{
1215 int live;
1216
1217 if (!sw) {
1218 return 0;
1219 }
1220
1221 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1222 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1223 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1224 return 0;
1225 }
1226
1227 ldebug (
1228 "%s: get_avail live %d ret %lld\n",
1229 SW_NAME (sw),
1230 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1231 );
1232
1233 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1234}
1235
1236static int audio_get_free (SWVoiceOut *sw)
1237{
1238 int live, dead;
1239
1240 if (!sw) {
1241 return 0;
1242 }
1243
1244 live = sw->total_hw_samples_mixed;
1245
1246 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1247 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1248 return 0;
1249 }
1250
1251 dead = sw->hw->samples - live;
1252
1253#ifdef DEBUG_OUT
1254 dolog ("%s: get_free live %d dead %d ret %lld\n",
1255 SW_NAME (sw),
1256 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1257#endif
1258
1259 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1260}
1261
1262static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1263{
1264 int n;
1265
1266 if (hw->enabled) {
1267 SWVoiceCap *sc;
1268
1269 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1270 SWVoiceOut *sw = &sc->sw;
1271 int rpos2 = rpos;
1272
1273 n = samples;
1274 while (n) {
1275 int till_end_of_hw = hw->samples - rpos2;
1276 int to_write = audio_MIN (till_end_of_hw, n);
1277 int bytes = to_write << hw->info.shift;
1278 int written;
1279
1280 sw->buf = hw->mix_buf + rpos2;
1281 written = audio_pcm_sw_write (sw, NULL, bytes);
1282 if (written - bytes) {
1283 dolog ("Could not mix %d bytes into a capture "
1284 "buffer, mixed %d\n",
1285 bytes, written);
1286 break;
1287 }
1288 n -= to_write;
1289 rpos2 = (rpos2 + to_write) % hw->samples;
1290 }
1291 }
1292 }
1293
1294 n = audio_MIN (samples, hw->samples - rpos);
1295 mixeng_sniff_and_clear (hw, hw->mix_buf + rpos, n);
1296 mixeng_sniff_and_clear (hw, hw->mix_buf, samples - n);
1297}
1298
1299static void audio_run_out (AudioState *s)
1300{
1301 HWVoiceOut *hw = NULL;
1302 SWVoiceOut *sw;
1303
1304 while ((hw = audio_pcm_hw_find_any_enabled_out (s, hw))) {
1305 int played;
1306 int live, myfree, nb_live, cleanup_required, prev_rpos;
1307
1308 live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
1309 if (!nb_live) {
1310 live = 0;
1311 }
1312
1313 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1314 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1315 continue;
1316 }
1317
1318 if (hw->pending_disable && !nb_live) {
1319 SWVoiceCap *sc;
1320#ifdef DEBUG_OUT
1321 dolog ("Disabling voice\n");
1322#endif
1323 hw->enabled = 0;
1324 hw->pending_disable = 0;
1325 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
1326 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1327 sc->sw.active = 0;
1328 audio_recalc_and_notify_capture (sc->cap);
1329 }
1330 continue;
1331 }
1332
1333 if (!live) {
1334 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1335 if (sw->active) {
1336 myfree = audio_get_free (sw);
1337 if (myfree > 0) {
1338 sw->callback.fn (sw->callback.opaque, myfree);
1339 }
1340 }
1341 }
1342 continue;
1343 }
1344
1345 prev_rpos = hw->rpos;
1346 played = hw->pcm_ops->run_out (hw);
1347 if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1348 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1349 hw->rpos, hw->samples, played);
1350 hw->rpos = 0;
1351 }
1352
1353#ifdef DEBUG_OUT
1354 dolog ("played=%d\n", played);
1355#endif
1356
1357 if (played) {
1358 hw->ts_helper += played;
1359 audio_capture_mix_and_clear (hw, prev_rpos, played);
1360 }
1361
1362 cleanup_required = 0;
1363 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1364 if (!sw->active && sw->empty) {
1365 continue;
1366 }
1367
1368 if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1369 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1370 played, sw->total_hw_samples_mixed);
1371 played = sw->total_hw_samples_mixed;
1372 }
1373
1374 sw->total_hw_samples_mixed -= played;
1375
1376 if (!sw->total_hw_samples_mixed) {
1377 sw->empty = 1;
1378 cleanup_required |= !sw->active && !sw->callback.fn;
1379 }
1380
1381 if (sw->active) {
1382 myfree = audio_get_free (sw);
1383 if (myfree > 0) {
1384 sw->callback.fn (sw->callback.opaque, myfree);
1385 }
1386 }
1387 }
1388
1389 if (cleanup_required) {
1390 SWVoiceOut *sw1;
1391
1392 sw = hw->sw_head.lh_first;
1393 while (sw) {
1394 sw1 = sw->entries.le_next;
1395 if (!sw->active && !sw->callback.fn) {
1396#ifdef DEBUG_PLIVE
1397 dolog ("Finishing with old voice\n");
1398#endif
1399 audio_close_out (s, sw);
1400 }
1401 sw = sw1;
1402 }
1403 }
1404 }
1405}
1406
1407static void audio_run_in (AudioState *s)
1408{
1409 HWVoiceIn *hw = NULL;
1410
1411 while ((hw = audio_pcm_hw_find_any_enabled_in (s, hw))) {
1412 SWVoiceIn *sw;
1413 int captured, min;
1414
1415 captured = hw->pcm_ops->run_in (hw);
1416
1417 min = audio_pcm_hw_find_min_in (hw);
1418 hw->total_samples_captured += captured - min;
1419 hw->ts_helper += captured;
1420
1421 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1422 sw->total_hw_samples_acquired -= min;
1423
1424 if (sw->active) {
1425 int avail;
1426
1427 avail = audio_get_avail (sw);
1428 if (avail > 0) {
1429 sw->callback.fn (sw->callback.opaque, avail);
1430 }
1431 }
1432 }
1433 }
1434}
1435
1436static void audio_run_capture (AudioState *s)
1437{
1438 CaptureVoiceOut *cap;
1439
1440 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1441 int live, rpos, captured;
1442 HWVoiceOut *hw = &cap->hw;
1443 SWVoiceOut *sw;
1444
1445 captured = live = audio_pcm_hw_get_live_out (hw);
1446 rpos = hw->rpos;
1447 while (live) {
1448 int left = hw->samples - rpos;
1449 int to_capture = audio_MIN (live, left);
1450 st_sample_t *src;
1451 struct capture_callback *cb;
1452
1453 src = hw->mix_buf + rpos;
1454 hw->clip (cap->buf, src, to_capture);
1455 mixeng_sniff_and_clear (hw, src, to_capture);
1456
1457 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1458 cb->ops.capture (cb->opaque, cap->buf,
1459 to_capture << hw->info.shift);
1460 }
1461 rpos = (rpos + to_capture) % hw->samples;
1462 live -= to_capture;
1463 }
1464 hw->rpos = rpos;
1465
1466 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1467 if (!sw->active && sw->empty) {
1468 continue;
1469 }
1470
1471 if (audio_bug (AUDIO_FUNC, captured > sw->total_hw_samples_mixed)) {
1472 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1473 captured, sw->total_hw_samples_mixed);
1474 captured = sw->total_hw_samples_mixed;
1475 }
1476
1477 sw->total_hw_samples_mixed -= captured;
1478 sw->empty = sw->total_hw_samples_mixed == 0;
1479 }
1480 }
1481}
1482
1483static void audio_timer (void *opaque)
1484{
1485 AudioState *s = opaque;
1486
1487 audio_run_out (s);
1488 audio_run_in (s);
1489 audio_run_capture (s);
1490
1491 TMTimerSet (s->ts, TMTimerGet (s->ts) + conf.period.ticks);
1492}
1493
1494static struct audio_option audio_options[] = {
1495 /* DAC */
1496 {"DAC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_out.enabled,
1497 "Use fixed settings for host DAC", NULL, 0},
1498
1499 {"DAC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_out.settings.freq,
1500 "Frequency for fixed host DAC", NULL, 0},
1501
1502 {"DAC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_out.settings.fmt,
1503 "Format for fixed host DAC", NULL, 0},
1504
1505 {"DAC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_out.settings.nchannels,
1506 "Number of channels for fixed DAC (1 - mono, 2 - stereo)", NULL, 0},
1507
1508 {"DAC_VOICES", AUD_OPT_INT, &conf.fixed_out.nb_voices,
1509 "Number of voices for DAC", NULL, 0},
1510
1511 /* ADC */
1512 {"ADC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_in.enabled,
1513 "Use fixed settings for host ADC", NULL, 0},
1514
1515 {"ADC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_in.settings.freq,
1516 "Frequency for fixed host ADC", NULL, 0},
1517
1518 {"ADC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_in.settings.fmt,
1519 "Format for fixed host ADC", NULL, 0},
1520
1521 {"ADC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_in.settings.nchannels,
1522 "Number of channels for fixed ADC (1 - mono, 2 - stereo)", NULL, 0},
1523
1524 {"ADC_VOICES", AUD_OPT_INT, &conf.fixed_in.nb_voices,
1525 "Number of voices for ADC", NULL, 0},
1526
1527 /* Misc */
1528 {"TIMER_FREQ", AUD_OPT_INT, &conf.period.hz,
1529 "Timer frequency in Hz (0 - use lowest possible)", NULL, 0},
1530
1531 {"PLIVE", AUD_OPT_BOOL, &conf.plive,
1532 "(undocumented)", NULL, 0},
1533
1534 {NULL, 0, NULL, NULL, NULL, 0}
1535};
1536
1537static int audio_driver_init (AudioState *s, struct audio_driver *drv)
1538{
1539 if (drv->options) {
1540 audio_process_options (drv->name, drv->options);
1541 }
1542 s->drv_opaque = drv->init ();
1543
1544 if (s->drv_opaque) {
1545 audio_init_nb_voices_out (s, drv);
1546 audio_init_nb_voices_in (s, drv);
1547 s->drv = drv;
1548 return 0;
1549 }
1550 else {
1551 dolog ("Could not init `%s' audio driver\n", drv->name);
1552 return -1;
1553 }
1554}
1555
1556static void audio_vm_change_state_handler (void *opaque, int running)
1557{
1558 AudioState *s = opaque;
1559 HWVoiceOut *hwo = NULL;
1560 HWVoiceIn *hwi = NULL;
1561 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1562
1563 while ((hwo = audio_pcm_hw_find_any_enabled_out (s, hwo))) {
1564 hwo->pcm_ops->ctl_out (hwo, op);
1565 }
1566
1567 while ((hwi = audio_pcm_hw_find_any_enabled_in (s, hwi))) {
1568 hwi->pcm_ops->ctl_in (hwi, op);
1569 }
1570}
1571
1572static void audio_atexit (void)
1573{
1574 AudioState *s = &glob_audio_state;
1575 HWVoiceOut *hwo = NULL;
1576 HWVoiceIn *hwi = NULL;
1577
1578 /* VBox change: audio_pcm_hw_find_any_enabled_out => audio_pcm_hw_find_any_out */
1579 while ((hwo = audio_pcm_hw_find_any_out (s, hwo))) {
1580 SWVoiceCap *sc;
1581
1582 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1583 hwo->pcm_ops->fini_out (hwo);
1584
1585 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1586 CaptureVoiceOut *cap = sc->cap;
1587 struct capture_callback *cb;
1588
1589 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1590 cb->ops.destroy (cb->opaque);
1591 }
1592 }
1593 }
1594
1595 /* VBox change: audio_pcm_hw_find_any_enabled_in => audio_pcm_hw_find_any_in */
1596 while ((hwi = audio_pcm_hw_find_any_in (s, hwi))) {
1597 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1598 hwi->pcm_ops->fini_in (hwi);
1599 }
1600
1601 if (s->drv) {
1602 s->drv->fini (s->drv_opaque);
1603 }
1604}
1605
1606void AUD_register_card (const char *name, QEMUSoundCard *card)
1607{
1608 AudioState *s = &glob_audio_state;
1609 card->audio = s;
1610 card->name = qemu_strdup (name);
1611 memset (&card->entries, 0, sizeof (card->entries));
1612 LIST_INSERT_HEAD (&s->card_head, card, entries);
1613}
1614
1615void AUD_remove_card (QEMUSoundCard *card)
1616{
1617 LIST_REMOVE (card, entries);
1618 card->audio = NULL;
1619 qemu_free (card->name);
1620}
1621
1622static DECLCALLBACK(void) audio_timer_helper (PPDMDRVINS pDrvIns, PTMTIMER pTimer, void *pvUser)
1623{
1624 AudioState *s = (AudioState *)pvUser;
1625 audio_timer (s);
1626}
1627
1628static int AUD_init (PPDMDRVINS pDrvIns, const char *drvname)
1629{
1630 size_t i;
1631 int done = 0;
1632 AudioState *s = &glob_audio_state;
1633 int rc;
1634
1635 LIST_INIT (&s->hw_head_out);
1636 LIST_INIT (&s->hw_head_in);
1637 LIST_INIT (&s->cap_head);
1638
1639 rc = PDMDrvHlpTMTimerCreate (pDrvIns, TMCLOCK_VIRTUAL, audio_timer_helper,
1640 &glob_audio_state, 0, "Audio timer", &s->ts);
1641 if (RT_FAILURE (rc))
1642 return rc;
1643
1644 audio_process_options ("AUDIO", audio_options);
1645
1646 s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1647 s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1648
1649 if (s->nb_hw_voices_out <= 0) {
1650 dolog ("Bogus number of playback voices %d, setting to 1\n",
1651 s->nb_hw_voices_out);
1652 s->nb_hw_voices_out = 1;
1653 }
1654
1655 if (s->nb_hw_voices_in <= 0) {
1656 dolog ("Bogus number of capture voices %d, setting to 0\n",
1657 s->nb_hw_voices_in);
1658 s->nb_hw_voices_in = 0;
1659 }
1660
1661 LogRel(("Audio: Trying driver '%s'.\n", drvname));
1662
1663 if (drvname) {
1664 int found = 0;
1665
1666 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1667 if (!strcmp (drvname, drvtab[i]->name)) {
1668 done = !audio_driver_init (s, drvtab[i]);
1669 found = 1;
1670 break;
1671 }
1672 }
1673
1674 if (!found) {
1675 dolog ("Unknown audio driver `%s'\n", drvname);
1676 }
1677 }
1678
1679 if (!done) {
1680 for (i = 0; !done && i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1681 if (drvtab[i]->can_be_default) {
1682 LogRel(("Audio: Initialization of driver '%s' failed, trying '%s'.\n",
1683 drvname, drvtab[i]->name));
1684 drvname = drvtab[i]->name;
1685 done = !audio_driver_init (s, drvtab[i]);
1686 }
1687 }
1688 }
1689
1690 if (!done) {
1691 done = !audio_driver_init (s, &no_audio_driver);
1692 if (!done) {
1693 dolog ("Could not initialize audio subsystem\n");
1694 }
1695 else {
1696 LogRel(("Audio: Initialization of driver '%s' failed, using NULL driver.\n", drvname));
1697 dolog ("warning: Using timer based audio emulation\n");
1698 }
1699 }
1700
1701 if (done) {
1702 if (conf.period.hz <= 0) {
1703 if (conf.period.hz < 0) {
1704 dolog ("warning: Timer period is negative - %d "
1705 "treating as zero\n",
1706 conf.period.hz);
1707 }
1708 conf.period.ticks = 1;
1709 }
1710 else {
1711 conf.period.ticks = PDMDrvHlpTMGetVirtualFreq (pDrvIns)
1712 / conf.period.hz;
1713 }
1714 }
1715 else {
1716 /* XXX */
1717 rc = TMR3TimerDestroy (s->ts);
1718 return rc;
1719 }
1720
1721 LIST_INIT (&s->card_head);
1722 TMTimerSet (s->ts, TMTimerGet (s->ts) + conf.period.ticks);
1723 return VINF_SUCCESS;
1724}
1725
1726int AUD_init_null(void)
1727{
1728 AudioState *s = &glob_audio_state;
1729
1730#ifdef VBOX
1731 if (s->drv)
1732 s->drv->fini (s->drv_opaque);
1733#endif
1734
1735 LogRel(("Audio: Using NULL audio driver\n"));
1736 return audio_driver_init (s, &no_audio_driver);
1737}
1738
1739CaptureVoiceOut *AUD_add_capture (
1740 AudioState *s,
1741 audsettings_t *as,
1742 struct audio_capture_ops *ops,
1743 void *cb_opaque
1744 )
1745{
1746 CaptureVoiceOut *cap;
1747 struct capture_callback *cb;
1748
1749 if (!s) {
1750 /* XXX suppress */
1751 s = &glob_audio_state;
1752 }
1753
1754 if (audio_validate_settings (as)) {
1755 dolog ("Invalid settings were passed when trying to add capture\n");
1756 audio_print_settings (as);
1757 goto err0;
1758 }
1759
1760 cb = audio_calloc (AUDIO_FUNC, 1, sizeof (*cb));
1761 if (!cb) {
1762 dolog ("Could not allocate capture callback information, size %u\n",
1763 sizeof (*cb));
1764 goto err0;
1765 }
1766 cb->ops = *ops;
1767 cb->opaque = cb_opaque;
1768
1769 cap = audio_pcm_capture_find_specific (s, as);
1770 if (cap) {
1771 LIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1772 return cap;
1773 }
1774 else {
1775 HWVoiceOut *hw;
1776#ifndef VBOX
1777 CaptureVoiceOut *cap;
1778#endif
1779
1780 cap = audio_calloc (AUDIO_FUNC, 1, sizeof (*cap));
1781 if (!cap) {
1782 dolog ("Could not allocate capture voice, size %u\n",
1783 sizeof (*cap));
1784 goto err1;
1785 }
1786
1787 hw = &cap->hw;
1788 LIST_INIT (&hw->sw_head);
1789 LIST_INIT (&cap->cb_head);
1790
1791 /* XXX find a more elegant way */
1792 hw->samples = 4096 * 4;
1793 hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples,
1794 sizeof (st_sample_t));
1795 if (!hw->mix_buf) {
1796 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1797 hw->samples);
1798 goto err2;
1799 }
1800
1801 audio_pcm_init_info (&hw->info, as);
1802
1803 cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
1804 if (!cap->buf) {
1805 dolog ("Could not allocate capture buffer "
1806 "(%d samples, each %d bytes)\n",
1807 hw->samples, 1 << hw->info.shift);
1808 goto err3;
1809 }
1810
1811 hw->clip = mixeng_clip
1812 [hw->info.nchannels == 2]
1813 [hw->info.sign]
1814 [hw->info.swap_endianness]
1815 [audio_bits_to_index (hw->info.bits)];
1816
1817 LIST_INSERT_HEAD (&s->cap_head, cap, entries);
1818 LIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1819
1820 hw = NULL;
1821 while ((hw = audio_pcm_hw_find_any_out (s, hw))) {
1822 audio_attach_capture (s, hw);
1823 }
1824 return cap;
1825
1826 err3:
1827 qemu_free (cap->hw.mix_buf);
1828 err2:
1829 qemu_free (cap);
1830 err1:
1831 qemu_free (cb);
1832 err0:
1833 return NULL;
1834 }
1835}
1836
1837void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
1838{
1839 struct capture_callback *cb;
1840
1841 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1842 if (cb->opaque == cb_opaque) {
1843 cb->ops.destroy (cb_opaque);
1844 LIST_REMOVE (cb, entries);
1845 qemu_free (cb);
1846
1847 if (!cap->cb_head.lh_first) {
1848 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
1849
1850 while (sw) {
1851 SWVoiceCap *sc = (SWVoiceCap *) sw;
1852#ifdef DEBUG_CAPTURE
1853 dolog ("freeing %s\n", sw->name);
1854#endif
1855
1856 sw1 = sw->entries.le_next;
1857 if (sw->rate) {
1858 st_rate_stop (sw->rate);
1859 sw->rate = NULL;
1860 }
1861 LIST_REMOVE (sw, entries);
1862 LIST_REMOVE (sc, entries);
1863 qemu_free (sc);
1864 sw = sw1;
1865 }
1866 LIST_REMOVE (cap, entries);
1867 qemu_free (cap);
1868 }
1869 return;
1870 }
1871 }
1872}
1873
1874void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
1875{
1876 if (sw)
1877 {
1878 sw->vol.mute = mute;
1879 sw->vol.l = (uint32_t)lvol * 0x808080; /* maximum is INT_MAX = 0x7fffffff */
1880 sw->vol.r = (uint32_t)rvol * 0x808080; /* maximum is INT_MAX = 0x7fffffff */
1881 }
1882}
1883
1884void AUD_set_volume (audmixerctl_t mt, int *mute, uint8_t *lvol, uint8_t *rvol)
1885{
1886 volume_t *vol = NULL;
1887 const char *name;
1888
1889 switch (mt)
1890 {
1891 case AUD_MIXER_VOLUME:
1892 name = "MASTER";
1893 vol = &master_out_volume;
1894 break;
1895 case AUD_MIXER_PCM:
1896 name = "PCM_OUT";
1897 vol = &pcm_out_volume;
1898 break;
1899 case AUD_MIXER_LINE_IN:
1900 name = "LINE_IN";
1901 vol = &pcm_in_volume;
1902 break;
1903 default:
1904 return;
1905
1906 }
1907
1908 if (vol)
1909 {
1910 uint32_t u32VolumeLeft = (uint32_t)*lvol;
1911 uint32_t u32VolumeRight = (uint32_t)*rvol;
1912 /* 0x00..0xff => 0x01..0x100 */
1913 if (u32VolumeLeft)
1914 u32VolumeLeft++;
1915 if (u32VolumeRight)
1916 u32VolumeRight++;
1917 vol->mute = *mute;
1918 vol->l = u32VolumeLeft * 0x800000; /* maximum is 0x80000000 */
1919 vol->r = u32VolumeRight * 0x800000; /* maximum is 0x80000000 */
1920 }
1921 sum_out_volume.mute = master_out_volume.mute || pcm_out_volume.mute;
1922 sum_out_volume.l = ASMMultU64ByU32DivByU32(master_out_volume.l, pcm_out_volume.l, 0x80000000U);
1923 sum_out_volume.r = ASMMultU64ByU32DivByU32(master_out_volume.r, pcm_out_volume.r, 0x80000000U);
1924}
1925
1926void AUD_set_record_source (audrecsource_t *ars, audrecsource_t *als)
1927{
1928 LogRel(("Audio: set_record_source ars=%d als=%d (not implemented)\n", *ars, *als));
1929}
1930
1931/**
1932 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1933 */
1934static DECLCALLBACK(void *) drvAudioQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1935{
1936 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
1937 PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
1938 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
1939 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIAUDIOCONNECTOR, &pThis->IAudioConnector);
1940 return NULL;
1941}
1942
1943/**
1944 * Power Off notification.
1945 *
1946 * @param pDrvIns The driver instance data.
1947 */
1948static DECLCALLBACK(void) drvAudioPowerOff(PPDMDRVINS pDrvIns)
1949{
1950 AudioState *s = &glob_audio_state;
1951 audio_vm_change_state_handler (s, 0);
1952}
1953
1954/**
1955 * Destruct a driver instance.
1956 *
1957 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
1958 * resources can be freed correctly.
1959 *
1960 * @param pDrvIns The driver instance data.
1961 */
1962static DECLCALLBACK(void) drvAudioDestruct(PPDMDRVINS pDrvIns)
1963{
1964 LogFlow(("drvAUDIODestruct:\n"));
1965 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
1966
1967 if (audio_streamname)
1968 {
1969 MMR3HeapFree(audio_streamname);
1970 audio_streamname = NULL;
1971 }
1972
1973 audio_atexit ();
1974}
1975
1976/**
1977 * Construct an AUDIO driver instance.
1978 *
1979 * @copydoc FNPDMDRVCONSTRUCT
1980 */
1981static DECLCALLBACK(int) drvAudioConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle, uint32_t fFlags)
1982{
1983 PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
1984 char *drvname;
1985 int rc;
1986
1987 LogFlow(("drvAUDIOConstruct:\n"));
1988 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
1989
1990 /*
1991 * Validate the config.
1992 */
1993 if (!CFGMR3AreValuesValid(pCfgHandle, "AudioDriver\0StreamName\0"))
1994 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
1995
1996 /*
1997 * Init the static parts.
1998 */
1999 pThis->pDrvIns = pDrvIns;
2000 /* IBase */
2001 pDrvIns->IBase.pfnQueryInterface = drvAudioQueryInterface;
2002 /* IAudio */
2003 /* pThis->IAudioConnector.pfn; */
2004
2005 glob_audio_state.pDrvIns = pDrvIns;
2006
2007 rc = CFGMR3QueryStringAlloc (pCfgHandle, "AudioDriver", &drvname);
2008 if (RT_FAILURE (rc))
2009 return rc;
2010
2011 rc = CFGMR3QueryStringAlloc (pCfgHandle, "StreamName", &audio_streamname);
2012 if (RT_FAILURE (rc))
2013 audio_streamname = NULL;
2014
2015 rc = AUD_init (pDrvIns, drvname);
2016 if (RT_FAILURE (rc))
2017 return rc;
2018
2019 MMR3HeapFree (drvname);
2020
2021 return VINF_SUCCESS;
2022}
2023
2024/**
2025 * Suspend notification.
2026 *
2027 * @returns VBox status.
2028 * @param pDrvIns The driver instance data.
2029 */
2030static DECLCALLBACK(void) drvAudioSuspend(PPDMDRVINS pDrvIns)
2031{
2032 AudioState *s = &glob_audio_state;
2033 audio_vm_change_state_handler (s, 0);
2034}
2035
2036/**
2037 * Resume notification.
2038 *
2039 * @returns VBox status.
2040 * @param pDrvIns The driver instance data.
2041 */
2042static DECLCALLBACK(void) audioResume(PPDMDRVINS pDrvIns)
2043{
2044 AudioState *s = &glob_audio_state;
2045 audio_vm_change_state_handler (s, 1);
2046}
2047
2048/**
2049 * Audio driver registration record.
2050 */
2051const PDMDRVREG g_DrvAUDIO =
2052{
2053 /* u32Version */
2054 PDM_DRVREG_VERSION,
2055 /* szName */
2056 "AUDIO",
2057 /* szRCMod */
2058 "",
2059 /* szR0Mod */
2060 "",
2061 /* pszDescription */
2062 "AUDIO Driver",
2063 /* fFlags */
2064 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
2065 /* fClass. */
2066 PDM_DRVREG_CLASS_AUDIO,
2067 /* cMaxInstances */
2068 1,
2069 /* cbInstance */
2070 sizeof(DRVAUDIO),
2071 /* pfnConstruct */
2072 drvAudioConstruct,
2073 /* pfnDestruct */
2074 drvAudioDestruct,
2075 /* pfnRelocate */
2076 NULL,
2077 /* pfnIOCtl */
2078 NULL,
2079 /* pfnPowerOn */
2080 NULL,
2081 /* pfnReset */
2082 NULL,
2083 /* pfnSuspend */
2084 drvAudioSuspend,
2085 /* pfnResume */
2086 audioResume,
2087 /* pfnAttach */
2088 NULL,
2089 /* pfnDetach */
2090 NULL,
2091 /* pfnPowerOff */
2092 drvAudioPowerOff,
2093 /* pfnSoftReset */
2094 NULL,
2095 /* u32EndVersion */
2096 PDM_DRVREG_VERSION
2097};
2098
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