VirtualBox

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

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

VMM reorg: Moving the public include files from include/VBox to include/VBox/vmm.

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