VirtualBox

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

Last change on this file since 55344 was 51639, checked in by vboxsync, 10 years ago

LogRel cosmetics/consistency.

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