kvm: configure: per architecture top level dependencies
[qemu-kvm/fedora.git] / audio / audio.c
blob5e9d88bb1a1ea42ee6a0a7c50ede1473b206a088
1 /*
2 * QEMU Audio subsystem
4 * Copyright (c) 2003-2005 Vassili Karpov (malc)
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "hw/hw.h"
25 #include "audio.h"
26 #include "console.h"
27 #include "qemu-timer.h"
28 #include "sysemu.h"
30 #define AUDIO_CAP "audio"
31 #include "audio_int.h"
33 /* #define DEBUG_PLIVE */
34 /* #define DEBUG_LIVE */
35 /* #define DEBUG_OUT */
36 /* #define DEBUG_CAPTURE */
38 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
40 static struct audio_driver *drvtab[] = {
41 #ifdef CONFIG_OSS
42 &oss_audio_driver,
43 #endif
44 #ifdef CONFIG_ALSA
45 &alsa_audio_driver,
46 #endif
47 #ifdef CONFIG_COREAUDIO
48 &coreaudio_audio_driver,
49 #endif
50 #ifdef CONFIG_DSOUND
51 &dsound_audio_driver,
52 #endif
53 #ifdef CONFIG_FMOD
54 &fmod_audio_driver,
55 #endif
56 #ifdef CONFIG_SDL
57 &sdl_audio_driver,
58 #endif
59 &no_audio_driver,
60 &wav_audio_driver
63 struct fixed_settings {
64 int enabled;
65 int nb_voices;
66 int greedy;
67 audsettings_t settings;
70 static struct {
71 struct fixed_settings fixed_out;
72 struct fixed_settings fixed_in;
73 union {
74 int hz;
75 int64_t ticks;
76 } period;
77 int plive;
78 int log_to_monitor;
79 } conf = {
80 { /* DAC fixed settings */
81 1, /* enabled */
82 1, /* nb_voices */
83 1, /* greedy */
85 44100, /* freq */
86 2, /* nchannels */
87 AUD_FMT_S16, /* fmt */
88 AUDIO_HOST_ENDIANNESS
92 { /* ADC fixed settings */
93 1, /* enabled */
94 1, /* nb_voices */
95 1, /* greedy */
97 44100, /* freq */
98 2, /* nchannels */
99 AUD_FMT_S16, /* fmt */
100 AUDIO_HOST_ENDIANNESS
104 { 0 }, /* period */
105 0, /* plive */
106 0 /* log_to_monitor */
109 static AudioState glob_audio_state;
111 volume_t nominal_volume = {
113 #ifdef FLOAT_MIXENG
114 1.0,
116 #else
117 UINT_MAX,
118 UINT_MAX
119 #endif
122 /* http://www.df.lth.se/~john_e/gems/gem002d.html */
123 /* http://www.multi-platforms.com/Tips/PopCount.htm */
124 uint32_t popcount (uint32_t u)
126 u = ((u&0x55555555) + ((u>>1)&0x55555555));
127 u = ((u&0x33333333) + ((u>>2)&0x33333333));
128 u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
129 u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
130 u = ( u&0x0000ffff) + (u>>16);
131 return u;
134 inline uint32_t lsbindex (uint32_t u)
136 return popcount ((u&-u)-1);
139 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
140 #error No its not
141 #else
142 int audio_bug (const char *funcname, int cond)
144 if (cond) {
145 static int shown;
147 AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
148 if (!shown) {
149 shown = 1;
150 AUD_log (NULL, "Save all your work and restart without audio\n");
151 AUD_log (NULL, "Please send bug report to malc@pulsesoft.com\n");
152 AUD_log (NULL, "I am sorry\n");
154 AUD_log (NULL, "Context:\n");
156 #if defined AUDIO_BREAKPOINT_ON_BUG
157 # if defined HOST_I386
158 # if defined __GNUC__
159 __asm__ ("int3");
160 # elif defined _MSC_VER
161 _asm _emit 0xcc;
162 # else
163 abort ();
164 # endif
165 # else
166 abort ();
167 # endif
168 #endif
171 return cond;
173 #endif
175 static inline int audio_bits_to_index (int bits)
177 switch (bits) {
178 case 8:
179 return 0;
181 case 16:
182 return 1;
184 case 32:
185 return 2;
187 default:
188 audio_bug ("bits_to_index", 1);
189 AUD_log (NULL, "invalid bits %d\n", bits);
190 return 0;
194 void *audio_calloc (const char *funcname, int nmemb, size_t size)
196 int cond;
197 size_t len;
199 len = nmemb * size;
200 cond = !nmemb || !size;
201 cond |= nmemb < 0;
202 cond |= len < size;
204 if (audio_bug ("audio_calloc", cond)) {
205 AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
206 funcname);
207 AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
208 return NULL;
211 return qemu_mallocz (len);
214 static char *audio_alloc_prefix (const char *s)
216 const char qemu_prefix[] = "QEMU_";
217 size_t len;
218 char *r;
220 if (!s) {
221 return NULL;
224 len = strlen (s);
225 r = qemu_malloc (len + sizeof (qemu_prefix));
227 if (r) {
228 size_t i;
229 char *u = r + sizeof (qemu_prefix) - 1;
231 strcpy (r, qemu_prefix);
232 strcat (r, s);
234 for (i = 0; i < len; ++i) {
235 u[i] = toupper (u[i]);
238 return r;
241 static const char *audio_audfmt_to_string (audfmt_e fmt)
243 switch (fmt) {
244 case AUD_FMT_U8:
245 return "U8";
247 case AUD_FMT_U16:
248 return "U16";
250 case AUD_FMT_S8:
251 return "S8";
253 case AUD_FMT_S16:
254 return "S16";
256 case AUD_FMT_U32:
257 return "U32";
259 case AUD_FMT_S32:
260 return "S32";
263 dolog ("Bogus audfmt %d returning S16\n", fmt);
264 return "S16";
267 static audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval,
268 int *defaultp)
270 if (!strcasecmp (s, "u8")) {
271 *defaultp = 0;
272 return AUD_FMT_U8;
274 else if (!strcasecmp (s, "u16")) {
275 *defaultp = 0;
276 return AUD_FMT_U16;
278 else if (!strcasecmp (s, "u32")) {
279 *defaultp = 0;
280 return AUD_FMT_U32;
282 else if (!strcasecmp (s, "s8")) {
283 *defaultp = 0;
284 return AUD_FMT_S8;
286 else if (!strcasecmp (s, "s16")) {
287 *defaultp = 0;
288 return AUD_FMT_S16;
290 else if (!strcasecmp (s, "s32")) {
291 *defaultp = 0;
292 return AUD_FMT_S32;
294 else {
295 dolog ("Bogus audio format `%s' using %s\n",
296 s, audio_audfmt_to_string (defval));
297 *defaultp = 1;
298 return defval;
302 static audfmt_e audio_get_conf_fmt (const char *envname,
303 audfmt_e defval,
304 int *defaultp)
306 const char *var = getenv (envname);
307 if (!var) {
308 *defaultp = 1;
309 return defval;
311 return audio_string_to_audfmt (var, defval, defaultp);
314 static int audio_get_conf_int (const char *key, int defval, int *defaultp)
316 int val;
317 char *strval;
319 strval = getenv (key);
320 if (strval) {
321 *defaultp = 0;
322 val = atoi (strval);
323 return val;
325 else {
326 *defaultp = 1;
327 return defval;
331 static const char *audio_get_conf_str (const char *key,
332 const char *defval,
333 int *defaultp)
335 const char *val = getenv (key);
336 if (!val) {
337 *defaultp = 1;
338 return defval;
340 else {
341 *defaultp = 0;
342 return val;
346 void AUD_vlog (const char *cap, const char *fmt, va_list ap)
348 if (conf.log_to_monitor) {
349 if (cap) {
350 term_printf ("%s: ", cap);
353 term_vprintf (fmt, ap);
355 else {
356 if (cap) {
357 fprintf (stderr, "%s: ", cap);
360 vfprintf (stderr, fmt, ap);
364 void AUD_log (const char *cap, const char *fmt, ...)
366 va_list ap;
368 va_start (ap, fmt);
369 AUD_vlog (cap, fmt, ap);
370 va_end (ap);
373 static void audio_print_options (const char *prefix,
374 struct audio_option *opt)
376 char *uprefix;
378 if (!prefix) {
379 dolog ("No prefix specified\n");
380 return;
383 if (!opt) {
384 dolog ("No options\n");
385 return;
388 uprefix = audio_alloc_prefix (prefix);
390 for (; opt->name; opt++) {
391 const char *state = "default";
392 printf (" %s_%s: ", uprefix, opt->name);
394 if (opt->overriddenp && *opt->overriddenp) {
395 state = "current";
398 switch (opt->tag) {
399 case AUD_OPT_BOOL:
401 int *intp = opt->valp;
402 printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
404 break;
406 case AUD_OPT_INT:
408 int *intp = opt->valp;
409 printf ("integer, %s = %d\n", state, *intp);
411 break;
413 case AUD_OPT_FMT:
415 audfmt_e *fmtp = opt->valp;
416 printf (
417 "format, %s = %s, (one of: U8 S8 U16 S16)\n",
418 state,
419 audio_audfmt_to_string (*fmtp)
422 break;
424 case AUD_OPT_STR:
426 const char **strp = opt->valp;
427 printf ("string, %s = %s\n",
428 state,
429 *strp ? *strp : "(not set)");
431 break;
433 default:
434 printf ("???\n");
435 dolog ("Bad value tag for option %s_%s %d\n",
436 uprefix, opt->name, opt->tag);
437 break;
439 printf (" %s\n", opt->descr);
442 qemu_free (uprefix);
445 static void audio_process_options (const char *prefix,
446 struct audio_option *opt)
448 char *optname;
449 const char qemu_prefix[] = "QEMU_";
450 size_t preflen;
452 if (audio_bug (AUDIO_FUNC, !prefix)) {
453 dolog ("prefix = NULL\n");
454 return;
457 if (audio_bug (AUDIO_FUNC, !opt)) {
458 dolog ("opt = NULL\n");
459 return;
462 preflen = strlen (prefix);
464 for (; opt->name; opt++) {
465 size_t len, i;
466 int def;
468 if (!opt->valp) {
469 dolog ("Option value pointer for `%s' is not set\n",
470 opt->name);
471 continue;
474 len = strlen (opt->name);
475 /* len of opt->name + len of prefix + size of qemu_prefix
476 * (includes trailing zero) + zero + underscore (on behalf of
477 * sizeof) */
478 optname = qemu_malloc (len + preflen + sizeof (qemu_prefix) + 1);
479 if (!optname) {
480 dolog ("Could not allocate memory for option name `%s'\n",
481 opt->name);
482 continue;
485 strcpy (optname, qemu_prefix);
487 /* copy while upper-casing, including trailing zero */
488 for (i = 0; i <= preflen; ++i) {
489 optname[i + sizeof (qemu_prefix) - 1] = toupper (prefix[i]);
491 strcat (optname, "_");
492 strcat (optname, opt->name);
494 def = 1;
495 switch (opt->tag) {
496 case AUD_OPT_BOOL:
497 case AUD_OPT_INT:
499 int *intp = opt->valp;
500 *intp = audio_get_conf_int (optname, *intp, &def);
502 break;
504 case AUD_OPT_FMT:
506 audfmt_e *fmtp = opt->valp;
507 *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
509 break;
511 case AUD_OPT_STR:
513 const char **strp = opt->valp;
514 *strp = audio_get_conf_str (optname, *strp, &def);
516 break;
518 default:
519 dolog ("Bad value tag for option `%s' - %d\n",
520 optname, opt->tag);
521 break;
524 if (!opt->overriddenp) {
525 opt->overriddenp = &opt->overridden;
527 *opt->overriddenp = !def;
528 qemu_free (optname);
532 static void audio_print_settings (audsettings_t *as)
534 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
536 switch (as->fmt) {
537 case AUD_FMT_S8:
538 AUD_log (NULL, "S8");
539 break;
540 case AUD_FMT_U8:
541 AUD_log (NULL, "U8");
542 break;
543 case AUD_FMT_S16:
544 AUD_log (NULL, "S16");
545 break;
546 case AUD_FMT_U16:
547 AUD_log (NULL, "U16");
548 break;
549 default:
550 AUD_log (NULL, "invalid(%d)", as->fmt);
551 break;
554 AUD_log (NULL, " endianness=");
555 switch (as->endianness) {
556 case 0:
557 AUD_log (NULL, "little");
558 break;
559 case 1:
560 AUD_log (NULL, "big");
561 break;
562 default:
563 AUD_log (NULL, "invalid");
564 break;
566 AUD_log (NULL, "\n");
569 static int audio_validate_settings (audsettings_t *as)
571 int invalid;
573 invalid = as->nchannels != 1 && as->nchannels != 2;
574 invalid |= as->endianness != 0 && as->endianness != 1;
576 switch (as->fmt) {
577 case AUD_FMT_S8:
578 case AUD_FMT_U8:
579 case AUD_FMT_S16:
580 case AUD_FMT_U16:
581 case AUD_FMT_S32:
582 case AUD_FMT_U32:
583 break;
584 default:
585 invalid = 1;
586 break;
589 invalid |= as->freq <= 0;
590 return invalid ? -1 : 0;
593 static int audio_pcm_info_eq (struct audio_pcm_info *info, audsettings_t *as)
595 int bits = 8, sign = 0;
597 switch (as->fmt) {
598 case AUD_FMT_S8:
599 sign = 1;
600 case AUD_FMT_U8:
601 break;
603 case AUD_FMT_S16:
604 sign = 1;
605 case AUD_FMT_U16:
606 bits = 16;
607 break;
609 case AUD_FMT_S32:
610 sign = 1;
611 case AUD_FMT_U32:
612 bits = 32;
613 break;
615 return info->freq == as->freq
616 && info->nchannels == as->nchannels
617 && info->sign == sign
618 && info->bits == bits
619 && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
622 void audio_pcm_init_info (struct audio_pcm_info *info, audsettings_t *as)
624 int bits = 8, sign = 0, shift = 0;
626 switch (as->fmt) {
627 case AUD_FMT_S8:
628 sign = 1;
629 case AUD_FMT_U8:
630 break;
632 case AUD_FMT_S16:
633 sign = 1;
634 case AUD_FMT_U16:
635 bits = 16;
636 shift = 1;
637 break;
639 case AUD_FMT_S32:
640 sign = 1;
641 case AUD_FMT_U32:
642 bits = 32;
643 shift = 2;
644 break;
647 info->freq = as->freq;
648 info->bits = bits;
649 info->sign = sign;
650 info->nchannels = as->nchannels;
651 info->shift = (as->nchannels == 2) + shift;
652 info->align = (1 << info->shift) - 1;
653 info->bytes_per_second = info->freq << info->shift;
654 info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
657 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
659 if (!len) {
660 return;
663 if (info->sign) {
664 memset (buf, 0x00, len << info->shift);
666 else {
667 switch (info->bits) {
668 case 8:
669 memset (buf, 0x80, len << info->shift);
670 break;
672 case 16:
674 int i;
675 uint16_t *p = buf;
676 int shift = info->nchannels - 1;
677 short s = INT16_MAX;
679 if (info->swap_endianness) {
680 s = bswap16 (s);
683 for (i = 0; i < len << shift; i++) {
684 p[i] = s;
687 break;
689 case 32:
691 int i;
692 uint32_t *p = buf;
693 int shift = info->nchannels - 1;
694 int32_t s = INT32_MAX;
696 if (info->swap_endianness) {
697 s = bswap32 (s);
700 for (i = 0; i < len << shift; i++) {
701 p[i] = s;
704 break;
706 default:
707 AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
708 info->bits);
709 break;
715 * Capture
717 static void noop_conv (st_sample_t *dst, const void *src,
718 int samples, volume_t *vol)
720 (void) src;
721 (void) dst;
722 (void) samples;
723 (void) vol;
726 static CaptureVoiceOut *audio_pcm_capture_find_specific (
727 AudioState *s,
728 audsettings_t *as
731 CaptureVoiceOut *cap;
733 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
734 if (audio_pcm_info_eq (&cap->hw.info, as)) {
735 return cap;
738 return NULL;
741 static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
743 struct capture_callback *cb;
745 #ifdef DEBUG_CAPTURE
746 dolog ("notification %d sent\n", cmd);
747 #endif
748 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
749 cb->ops.notify (cb->opaque, cmd);
753 static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
755 if (cap->hw.enabled != enabled) {
756 audcnotification_e cmd;
757 cap->hw.enabled = enabled;
758 cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
759 audio_notify_capture (cap, cmd);
763 static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
765 HWVoiceOut *hw = &cap->hw;
766 SWVoiceOut *sw;
767 int enabled = 0;
769 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
770 if (sw->active) {
771 enabled = 1;
772 break;
775 audio_capture_maybe_changed (cap, enabled);
778 static void audio_detach_capture (HWVoiceOut *hw)
780 SWVoiceCap *sc = hw->cap_head.lh_first;
782 while (sc) {
783 SWVoiceCap *sc1 = sc->entries.le_next;
784 SWVoiceOut *sw = &sc->sw;
785 CaptureVoiceOut *cap = sc->cap;
786 int was_active = sw->active;
788 if (sw->rate) {
789 st_rate_stop (sw->rate);
790 sw->rate = NULL;
793 LIST_REMOVE (sw, entries);
794 LIST_REMOVE (sc, entries);
795 qemu_free (sc);
796 if (was_active) {
797 /* We have removed soft voice from the capture:
798 this might have changed the overall status of the capture
799 since this might have been the only active voice */
800 audio_recalc_and_notify_capture (cap);
802 sc = sc1;
806 static int audio_attach_capture (AudioState *s, HWVoiceOut *hw)
808 CaptureVoiceOut *cap;
810 audio_detach_capture (hw);
811 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
812 SWVoiceCap *sc;
813 SWVoiceOut *sw;
814 HWVoiceOut *hw_cap = &cap->hw;
816 sc = audio_calloc (AUDIO_FUNC, 1, sizeof (*sc));
817 if (!sc) {
818 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
819 sizeof (*sc));
820 return -1;
823 sc->cap = cap;
824 sw = &sc->sw;
825 sw->hw = hw_cap;
826 sw->info = hw->info;
827 sw->empty = 1;
828 sw->active = hw->enabled;
829 sw->conv = noop_conv;
830 sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
831 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
832 if (!sw->rate) {
833 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
834 qemu_free (sw);
835 return -1;
837 LIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
838 LIST_INSERT_HEAD (&hw->cap_head, sc, entries);
839 #ifdef DEBUG_CAPTURE
840 asprintf (&sw->name, "for %p %d,%d,%d",
841 hw, sw->info.freq, sw->info.bits, sw->info.nchannels);
842 dolog ("Added %s active = %d\n", sw->name, sw->active);
843 #endif
844 if (sw->active) {
845 audio_capture_maybe_changed (cap, 1);
848 return 0;
852 * Hard voice (capture)
854 static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
856 SWVoiceIn *sw;
857 int m = hw->total_samples_captured;
859 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
860 if (sw->active) {
861 m = audio_MIN (m, sw->total_hw_samples_acquired);
864 return m;
867 int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
869 int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
870 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
871 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
872 return 0;
874 return live;
878 * Soft voice (capture)
880 static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
882 HWVoiceIn *hw = sw->hw;
883 int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
884 int rpos;
886 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
887 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
888 return 0;
891 rpos = hw->wpos - live;
892 if (rpos >= 0) {
893 return rpos;
895 else {
896 return hw->samples + rpos;
900 int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
902 HWVoiceIn *hw = sw->hw;
903 int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
904 st_sample_t *src, *dst = sw->buf;
906 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
908 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
909 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
910 dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
911 return 0;
914 samples = size >> sw->info.shift;
915 if (!live) {
916 return 0;
919 swlim = (live * sw->ratio) >> 32;
920 swlim = audio_MIN (swlim, samples);
922 while (swlim) {
923 src = hw->conv_buf + rpos;
924 isamp = hw->wpos - rpos;
925 /* XXX: <= ? */
926 if (isamp <= 0) {
927 isamp = hw->samples - rpos;
930 if (!isamp) {
931 break;
933 osamp = swlim;
935 if (audio_bug (AUDIO_FUNC, osamp < 0)) {
936 dolog ("osamp=%d\n", osamp);
937 return 0;
940 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
941 swlim -= osamp;
942 rpos = (rpos + isamp) % hw->samples;
943 dst += osamp;
944 ret += osamp;
945 total += isamp;
948 sw->clip (buf, sw->buf, ret);
949 sw->total_hw_samples_acquired += total;
950 return ret << sw->info.shift;
954 * Hard voice (playback)
956 static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
958 SWVoiceOut *sw;
959 int m = INT_MAX;
960 int nb_live = 0;
962 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
963 if (sw->active || !sw->empty) {
964 m = audio_MIN (m, sw->total_hw_samples_mixed);
965 nb_live += 1;
969 *nb_livep = nb_live;
970 return m;
973 int audio_pcm_hw_get_live_out2 (HWVoiceOut *hw, int *nb_live)
975 int smin;
977 smin = audio_pcm_hw_find_min_out (hw, nb_live);
979 if (!*nb_live) {
980 return 0;
982 else {
983 int live = smin;
985 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
986 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
987 return 0;
989 return live;
993 int audio_pcm_hw_get_live_out (HWVoiceOut *hw)
995 int nb_live;
996 int live;
998 live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
999 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1000 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1001 return 0;
1003 return live;
1007 * Soft voice (playback)
1009 int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
1011 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
1012 int ret = 0, pos = 0, total = 0;
1014 if (!sw) {
1015 return size;
1018 hwsamples = sw->hw->samples;
1020 live = sw->total_hw_samples_mixed;
1021 if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
1022 dolog ("live=%d hw->samples=%d\n", live, hwsamples);
1023 return 0;
1026 if (live == hwsamples) {
1027 #ifdef DEBUG_OUT
1028 dolog ("%s is full %d\n", sw->name, live);
1029 #endif
1030 return 0;
1033 wpos = (sw->hw->rpos + live) % hwsamples;
1034 samples = size >> sw->info.shift;
1036 dead = hwsamples - live;
1037 swlim = ((int64_t) dead << 32) / sw->ratio;
1038 swlim = audio_MIN (swlim, samples);
1039 if (swlim) {
1040 sw->conv (sw->buf, buf, swlim, &sw->vol);
1043 while (swlim) {
1044 dead = hwsamples - live;
1045 left = hwsamples - wpos;
1046 blck = audio_MIN (dead, left);
1047 if (!blck) {
1048 break;
1050 isamp = swlim;
1051 osamp = blck;
1052 st_rate_flow_mix (
1053 sw->rate,
1054 sw->buf + pos,
1055 sw->hw->mix_buf + wpos,
1056 &isamp,
1057 &osamp
1059 ret += isamp;
1060 swlim -= isamp;
1061 pos += isamp;
1062 live += osamp;
1063 wpos = (wpos + osamp) % hwsamples;
1064 total += osamp;
1067 sw->total_hw_samples_mixed += total;
1068 sw->empty = sw->total_hw_samples_mixed == 0;
1070 #ifdef DEBUG_OUT
1071 dolog (
1072 "%s: write size %d ret %d total sw %d\n",
1073 SW_NAME (sw),
1074 size >> sw->info.shift,
1075 ret,
1076 sw->total_hw_samples_mixed
1078 #endif
1080 return ret << sw->info.shift;
1083 #ifdef DEBUG_AUDIO
1084 static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
1086 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1087 cap, info->bits, info->sign, info->freq, info->nchannels);
1089 #endif
1091 #define DAC
1092 #include "audio_template.h"
1093 #undef DAC
1094 #include "audio_template.h"
1096 int AUD_write (SWVoiceOut *sw, void *buf, int size)
1098 int bytes;
1100 if (!sw) {
1101 /* XXX: Consider options */
1102 return size;
1105 if (!sw->hw->enabled) {
1106 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
1107 return 0;
1110 bytes = sw->hw->pcm_ops->write (sw, buf, size);
1111 return bytes;
1114 int AUD_read (SWVoiceIn *sw, void *buf, int size)
1116 int bytes;
1118 if (!sw) {
1119 /* XXX: Consider options */
1120 return size;
1123 if (!sw->hw->enabled) {
1124 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1125 return 0;
1128 bytes = sw->hw->pcm_ops->read (sw, buf, size);
1129 return bytes;
1132 int AUD_get_buffer_size_out (SWVoiceOut *sw)
1134 return sw->hw->samples << sw->hw->info.shift;
1137 void AUD_set_active_out (SWVoiceOut *sw, int on)
1139 HWVoiceOut *hw;
1141 if (!sw) {
1142 return;
1145 hw = sw->hw;
1146 if (sw->active != on) {
1147 SWVoiceOut *temp_sw;
1148 SWVoiceCap *sc;
1150 if (on) {
1151 hw->pending_disable = 0;
1152 if (!hw->enabled) {
1153 hw->enabled = 1;
1154 hw->pcm_ops->ctl_out (hw, VOICE_ENABLE);
1157 else {
1158 if (hw->enabled) {
1159 int nb_active = 0;
1161 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1162 temp_sw = temp_sw->entries.le_next) {
1163 nb_active += temp_sw->active != 0;
1166 hw->pending_disable = nb_active == 1;
1170 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1171 sc->sw.active = hw->enabled;
1172 if (hw->enabled) {
1173 audio_capture_maybe_changed (sc->cap, 1);
1176 sw->active = on;
1180 void AUD_set_active_in (SWVoiceIn *sw, int on)
1182 HWVoiceIn *hw;
1184 if (!sw) {
1185 return;
1188 hw = sw->hw;
1189 if (sw->active != on) {
1190 SWVoiceIn *temp_sw;
1192 if (on) {
1193 if (!hw->enabled) {
1194 hw->enabled = 1;
1195 hw->pcm_ops->ctl_in (hw, VOICE_ENABLE);
1197 sw->total_hw_samples_acquired = hw->total_samples_captured;
1199 else {
1200 if (hw->enabled) {
1201 int nb_active = 0;
1203 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1204 temp_sw = temp_sw->entries.le_next) {
1205 nb_active += temp_sw->active != 0;
1208 if (nb_active == 1) {
1209 hw->enabled = 0;
1210 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
1214 sw->active = on;
1218 static int audio_get_avail (SWVoiceIn *sw)
1220 int live;
1222 if (!sw) {
1223 return 0;
1226 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1227 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1228 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1229 return 0;
1232 ldebug (
1233 "%s: get_avail live %d ret %" PRId64 "\n",
1234 SW_NAME (sw),
1235 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1238 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1241 static int audio_get_free (SWVoiceOut *sw)
1243 int live, dead;
1245 if (!sw) {
1246 return 0;
1249 live = sw->total_hw_samples_mixed;
1251 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1252 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1253 return 0;
1256 dead = sw->hw->samples - live;
1258 #ifdef DEBUG_OUT
1259 dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
1260 SW_NAME (sw),
1261 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1262 #endif
1264 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1267 static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1269 int n;
1271 if (hw->enabled) {
1272 SWVoiceCap *sc;
1274 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1275 SWVoiceOut *sw = &sc->sw;
1276 int rpos2 = rpos;
1278 n = samples;
1279 while (n) {
1280 int till_end_of_hw = hw->samples - rpos2;
1281 int to_write = audio_MIN (till_end_of_hw, n);
1282 int bytes = to_write << hw->info.shift;
1283 int written;
1285 sw->buf = hw->mix_buf + rpos2;
1286 written = audio_pcm_sw_write (sw, NULL, bytes);
1287 if (written - bytes) {
1288 dolog ("Could not mix %d bytes into a capture "
1289 "buffer, mixed %d\n",
1290 bytes, written);
1291 break;
1293 n -= to_write;
1294 rpos2 = (rpos2 + to_write) % hw->samples;
1299 n = audio_MIN (samples, hw->samples - rpos);
1300 mixeng_clear (hw->mix_buf + rpos, n);
1301 mixeng_clear (hw->mix_buf, samples - n);
1304 static void audio_run_out (AudioState *s)
1306 HWVoiceOut *hw = NULL;
1307 SWVoiceOut *sw;
1309 while ((hw = audio_pcm_hw_find_any_enabled_out (s, hw))) {
1310 int played;
1311 int live, free, nb_live, cleanup_required, prev_rpos;
1313 live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
1314 if (!nb_live) {
1315 live = 0;
1318 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1319 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1320 continue;
1323 if (hw->pending_disable && !nb_live) {
1324 SWVoiceCap *sc;
1325 #ifdef DEBUG_OUT
1326 dolog ("Disabling voice\n");
1327 #endif
1328 hw->enabled = 0;
1329 hw->pending_disable = 0;
1330 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
1331 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1332 sc->sw.active = 0;
1333 audio_recalc_and_notify_capture (sc->cap);
1335 continue;
1338 if (!live) {
1339 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1340 if (sw->active) {
1341 free = audio_get_free (sw);
1342 if (free > 0) {
1343 sw->callback.fn (sw->callback.opaque, free);
1347 continue;
1350 prev_rpos = hw->rpos;
1351 played = hw->pcm_ops->run_out (hw);
1352 if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1353 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1354 hw->rpos, hw->samples, played);
1355 hw->rpos = 0;
1358 #ifdef DEBUG_OUT
1359 dolog ("played=%d\n", played);
1360 #endif
1362 if (played) {
1363 hw->ts_helper += played;
1364 audio_capture_mix_and_clear (hw, prev_rpos, played);
1367 cleanup_required = 0;
1368 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1369 if (!sw->active && sw->empty) {
1370 continue;
1373 if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1374 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1375 played, sw->total_hw_samples_mixed);
1376 played = sw->total_hw_samples_mixed;
1379 sw->total_hw_samples_mixed -= played;
1381 if (!sw->total_hw_samples_mixed) {
1382 sw->empty = 1;
1383 cleanup_required |= !sw->active && !sw->callback.fn;
1386 if (sw->active) {
1387 free = audio_get_free (sw);
1388 if (free > 0) {
1389 sw->callback.fn (sw->callback.opaque, free);
1394 if (cleanup_required) {
1395 SWVoiceOut *sw1;
1397 sw = hw->sw_head.lh_first;
1398 while (sw) {
1399 sw1 = sw->entries.le_next;
1400 if (!sw->active && !sw->callback.fn) {
1401 #ifdef DEBUG_PLIVE
1402 dolog ("Finishing with old voice\n");
1403 #endif
1404 audio_close_out (s, sw);
1406 sw = sw1;
1412 static void audio_run_in (AudioState *s)
1414 HWVoiceIn *hw = NULL;
1416 while ((hw = audio_pcm_hw_find_any_enabled_in (s, hw))) {
1417 SWVoiceIn *sw;
1418 int captured, min;
1420 captured = hw->pcm_ops->run_in (hw);
1422 min = audio_pcm_hw_find_min_in (hw);
1423 hw->total_samples_captured += captured - min;
1424 hw->ts_helper += captured;
1426 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1427 sw->total_hw_samples_acquired -= min;
1429 if (sw->active) {
1430 int avail;
1432 avail = audio_get_avail (sw);
1433 if (avail > 0) {
1434 sw->callback.fn (sw->callback.opaque, avail);
1441 static void audio_run_capture (AudioState *s)
1443 CaptureVoiceOut *cap;
1445 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1446 int live, rpos, captured;
1447 HWVoiceOut *hw = &cap->hw;
1448 SWVoiceOut *sw;
1450 captured = live = audio_pcm_hw_get_live_out (hw);
1451 rpos = hw->rpos;
1452 while (live) {
1453 int left = hw->samples - rpos;
1454 int to_capture = audio_MIN (live, left);
1455 st_sample_t *src;
1456 struct capture_callback *cb;
1458 src = hw->mix_buf + rpos;
1459 hw->clip (cap->buf, src, to_capture);
1460 mixeng_clear (src, to_capture);
1462 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1463 cb->ops.capture (cb->opaque, cap->buf,
1464 to_capture << hw->info.shift);
1466 rpos = (rpos + to_capture) % hw->samples;
1467 live -= to_capture;
1469 hw->rpos = rpos;
1471 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1472 if (!sw->active && sw->empty) {
1473 continue;
1476 if (audio_bug (AUDIO_FUNC, captured > sw->total_hw_samples_mixed)) {
1477 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1478 captured, sw->total_hw_samples_mixed);
1479 captured = sw->total_hw_samples_mixed;
1482 sw->total_hw_samples_mixed -= captured;
1483 sw->empty = sw->total_hw_samples_mixed == 0;
1488 static void audio_timer (void *opaque)
1490 AudioState *s = opaque;
1492 audio_run_out (s);
1493 audio_run_in (s);
1494 audio_run_capture (s);
1496 qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);
1499 static struct audio_option audio_options[] = {
1500 /* DAC */
1501 {"DAC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_out.enabled,
1502 "Use fixed settings for host DAC", NULL, 0},
1504 {"DAC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_out.settings.freq,
1505 "Frequency for fixed host DAC", NULL, 0},
1507 {"DAC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_out.settings.fmt,
1508 "Format for fixed host DAC", NULL, 0},
1510 {"DAC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_out.settings.nchannels,
1511 "Number of channels for fixed DAC (1 - mono, 2 - stereo)", NULL, 0},
1513 {"DAC_VOICES", AUD_OPT_INT, &conf.fixed_out.nb_voices,
1514 "Number of voices for DAC", NULL, 0},
1516 /* ADC */
1517 {"ADC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_in.enabled,
1518 "Use fixed settings for host ADC", NULL, 0},
1520 {"ADC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_in.settings.freq,
1521 "Frequency for fixed host ADC", NULL, 0},
1523 {"ADC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_in.settings.fmt,
1524 "Format for fixed host ADC", NULL, 0},
1526 {"ADC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_in.settings.nchannels,
1527 "Number of channels for fixed ADC (1 - mono, 2 - stereo)", NULL, 0},
1529 {"ADC_VOICES", AUD_OPT_INT, &conf.fixed_in.nb_voices,
1530 "Number of voices for ADC", NULL, 0},
1532 /* Misc */
1533 {"TIMER_PERIOD", AUD_OPT_INT, &conf.period.hz,
1534 "Timer period in HZ (0 - use lowest possible)", NULL, 0},
1536 {"PLIVE", AUD_OPT_BOOL, &conf.plive,
1537 "(undocumented)", NULL, 0},
1539 {"LOG_TO_MONITOR", AUD_OPT_BOOL, &conf.log_to_monitor,
1540 "print logging messages to montior instead of stderr", NULL, 0},
1542 {NULL, 0, NULL, NULL, NULL, 0}
1545 static void audio_pp_nb_voices (const char *typ, int nb)
1547 switch (nb) {
1548 case 0:
1549 printf ("Does not support %s\n", typ);
1550 break;
1551 case 1:
1552 printf ("One %s voice\n", typ);
1553 break;
1554 case INT_MAX:
1555 printf ("Theoretically supports many %s voices\n", typ);
1556 break;
1557 default:
1558 printf ("Theoretically supports upto %d %s voices\n", nb, typ);
1559 break;
1564 void AUD_help (void)
1566 size_t i;
1568 audio_process_options ("AUDIO", audio_options);
1569 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1570 struct audio_driver *d = drvtab[i];
1571 if (d->options) {
1572 audio_process_options (d->name, d->options);
1576 printf ("Audio options:\n");
1577 audio_print_options ("AUDIO", audio_options);
1578 printf ("\n");
1580 printf ("Available drivers:\n");
1582 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1583 struct audio_driver *d = drvtab[i];
1585 printf ("Name: %s\n", d->name);
1586 printf ("Description: %s\n", d->descr);
1588 audio_pp_nb_voices ("playback", d->max_voices_out);
1589 audio_pp_nb_voices ("capture", d->max_voices_in);
1591 if (d->options) {
1592 printf ("Options:\n");
1593 audio_print_options (d->name, d->options);
1595 else {
1596 printf ("No options\n");
1598 printf ("\n");
1601 printf (
1602 "Options are settable through environment variables.\n"
1603 "Example:\n"
1604 #ifdef _WIN32
1605 " set QEMU_AUDIO_DRV=wav\n"
1606 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1607 #else
1608 " export QEMU_AUDIO_DRV=wav\n"
1609 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1610 "(for csh replace export with setenv in the above)\n"
1611 #endif
1612 " qemu ...\n\n"
1616 static int audio_driver_init (AudioState *s, struct audio_driver *drv)
1618 if (drv->options) {
1619 audio_process_options (drv->name, drv->options);
1621 s->drv_opaque = drv->init ();
1623 if (s->drv_opaque) {
1624 audio_init_nb_voices_out (s, drv);
1625 audio_init_nb_voices_in (s, drv);
1626 s->drv = drv;
1627 return 0;
1629 else {
1630 dolog ("Could not init `%s' audio driver\n", drv->name);
1631 return -1;
1635 static void audio_vm_change_state_handler (void *opaque, int running)
1637 AudioState *s = opaque;
1638 HWVoiceOut *hwo = NULL;
1639 HWVoiceIn *hwi = NULL;
1640 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1642 while ((hwo = audio_pcm_hw_find_any_enabled_out (s, hwo))) {
1643 hwo->pcm_ops->ctl_out (hwo, op);
1646 while ((hwi = audio_pcm_hw_find_any_enabled_in (s, hwi))) {
1647 hwi->pcm_ops->ctl_in (hwi, op);
1651 static void audio_atexit (void)
1653 AudioState *s = &glob_audio_state;
1654 HWVoiceOut *hwo = NULL;
1655 HWVoiceIn *hwi = NULL;
1657 while ((hwo = audio_pcm_hw_find_any_enabled_out (s, hwo))) {
1658 SWVoiceCap *sc;
1660 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1661 hwo->pcm_ops->fini_out (hwo);
1663 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1664 CaptureVoiceOut *cap = sc->cap;
1665 struct capture_callback *cb;
1667 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1668 cb->ops.destroy (cb->opaque);
1673 while ((hwi = audio_pcm_hw_find_any_enabled_in (s, hwi))) {
1674 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1675 hwi->pcm_ops->fini_in (hwi);
1678 if (s->drv) {
1679 s->drv->fini (s->drv_opaque);
1683 static void audio_save (QEMUFile *f, void *opaque)
1685 (void) f;
1686 (void) opaque;
1689 static int audio_load (QEMUFile *f, void *opaque, int version_id)
1691 (void) f;
1692 (void) opaque;
1694 if (version_id != 1) {
1695 return -EINVAL;
1698 return 0;
1701 void AUD_register_card (AudioState *s, const char *name, QEMUSoundCard *card)
1703 card->audio = s;
1704 card->name = qemu_strdup (name);
1705 memset (&card->entries, 0, sizeof (card->entries));
1706 LIST_INSERT_HEAD (&s->card_head, card, entries);
1709 void AUD_remove_card (QEMUSoundCard *card)
1711 LIST_REMOVE (card, entries);
1712 card->audio = NULL;
1713 qemu_free (card->name);
1716 AudioState *AUD_init (void)
1718 size_t i;
1719 int done = 0;
1720 const char *drvname;
1721 AudioState *s = &glob_audio_state;
1723 LIST_INIT (&s->hw_head_out);
1724 LIST_INIT (&s->hw_head_in);
1725 LIST_INIT (&s->cap_head);
1726 atexit (audio_atexit);
1728 s->ts = qemu_new_timer (vm_clock, audio_timer, s);
1729 if (!s->ts) {
1730 dolog ("Could not create audio timer\n");
1731 return NULL;
1734 audio_process_options ("AUDIO", audio_options);
1736 s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1737 s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1739 if (s->nb_hw_voices_out <= 0) {
1740 dolog ("Bogus number of playback voices %d, setting to 1\n",
1741 s->nb_hw_voices_out);
1742 s->nb_hw_voices_out = 1;
1745 if (s->nb_hw_voices_in <= 0) {
1746 dolog ("Bogus number of capture voices %d, setting to 0\n",
1747 s->nb_hw_voices_in);
1748 s->nb_hw_voices_in = 0;
1752 int def;
1753 drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1756 if (drvname) {
1757 int found = 0;
1759 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1760 if (!strcmp (drvname, drvtab[i]->name)) {
1761 done = !audio_driver_init (s, drvtab[i]);
1762 found = 1;
1763 break;
1767 if (!found) {
1768 dolog ("Unknown audio driver `%s'\n", drvname);
1769 dolog ("Run with -audio-help to list available drivers\n");
1773 if (!done) {
1774 for (i = 0; !done && i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1775 if (drvtab[i]->can_be_default) {
1776 done = !audio_driver_init (s, drvtab[i]);
1781 if (!done) {
1782 done = !audio_driver_init (s, &no_audio_driver);
1783 if (!done) {
1784 dolog ("Could not initialize audio subsystem\n");
1786 else {
1787 dolog ("warning: Using timer based audio emulation\n");
1791 if (done) {
1792 VMChangeStateEntry *e;
1794 if (conf.period.hz <= 0) {
1795 if (conf.period.hz < 0) {
1796 dolog ("warning: Timer period is negative - %d "
1797 "treating as zero\n",
1798 conf.period.hz);
1800 conf.period.ticks = 1;
1802 else {
1803 conf.period.ticks = ticks_per_sec / conf.period.hz;
1806 e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1807 if (!e) {
1808 dolog ("warning: Could not register change state handler\n"
1809 "(Audio can continue looping even after stopping the VM)\n");
1812 else {
1813 qemu_del_timer (s->ts);
1814 return NULL;
1817 LIST_INIT (&s->card_head);
1818 register_savevm ("audio", 0, 1, audio_save, audio_load, s);
1819 qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);
1820 return s;
1823 CaptureVoiceOut *AUD_add_capture (
1824 AudioState *s,
1825 audsettings_t *as,
1826 struct audio_capture_ops *ops,
1827 void *cb_opaque
1830 CaptureVoiceOut *cap;
1831 struct capture_callback *cb;
1833 if (!s) {
1834 /* XXX suppress */
1835 s = &glob_audio_state;
1838 if (audio_validate_settings (as)) {
1839 dolog ("Invalid settings were passed when trying to add capture\n");
1840 audio_print_settings (as);
1841 goto err0;
1844 cb = audio_calloc (AUDIO_FUNC, 1, sizeof (*cb));
1845 if (!cb) {
1846 dolog ("Could not allocate capture callback information, size %zu\n",
1847 sizeof (*cb));
1848 goto err0;
1850 cb->ops = *ops;
1851 cb->opaque = cb_opaque;
1853 cap = audio_pcm_capture_find_specific (s, as);
1854 if (cap) {
1855 LIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1856 return cap;
1858 else {
1859 HWVoiceOut *hw;
1860 CaptureVoiceOut *cap;
1862 cap = audio_calloc (AUDIO_FUNC, 1, sizeof (*cap));
1863 if (!cap) {
1864 dolog ("Could not allocate capture voice, size %zu\n",
1865 sizeof (*cap));
1866 goto err1;
1869 hw = &cap->hw;
1870 LIST_INIT (&hw->sw_head);
1871 LIST_INIT (&cap->cb_head);
1873 /* XXX find a more elegant way */
1874 hw->samples = 4096 * 4;
1875 hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples,
1876 sizeof (st_sample_t));
1877 if (!hw->mix_buf) {
1878 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1879 hw->samples);
1880 goto err2;
1883 audio_pcm_init_info (&hw->info, as);
1885 cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
1886 if (!cap->buf) {
1887 dolog ("Could not allocate capture buffer "
1888 "(%d samples, each %d bytes)\n",
1889 hw->samples, 1 << hw->info.shift);
1890 goto err3;
1893 hw->clip = mixeng_clip
1894 [hw->info.nchannels == 2]
1895 [hw->info.sign]
1896 [hw->info.swap_endianness]
1897 [audio_bits_to_index (hw->info.bits)];
1899 LIST_INSERT_HEAD (&s->cap_head, cap, entries);
1900 LIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1902 hw = NULL;
1903 while ((hw = audio_pcm_hw_find_any_out (s, hw))) {
1904 audio_attach_capture (s, hw);
1906 return cap;
1908 err3:
1909 qemu_free (cap->hw.mix_buf);
1910 err2:
1911 qemu_free (cap);
1912 err1:
1913 qemu_free (cb);
1914 err0:
1915 return NULL;
1919 void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
1921 struct capture_callback *cb;
1923 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1924 if (cb->opaque == cb_opaque) {
1925 cb->ops.destroy (cb_opaque);
1926 LIST_REMOVE (cb, entries);
1927 qemu_free (cb);
1929 if (!cap->cb_head.lh_first) {
1930 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
1932 while (sw) {
1933 SWVoiceCap *sc = (SWVoiceCap *) sw;
1934 #ifdef DEBUG_CAPTURE
1935 dolog ("freeing %s\n", sw->name);
1936 #endif
1938 sw1 = sw->entries.le_next;
1939 if (sw->rate) {
1940 st_rate_stop (sw->rate);
1941 sw->rate = NULL;
1943 LIST_REMOVE (sw, entries);
1944 LIST_REMOVE (sc, entries);
1945 qemu_free (sc);
1946 sw = sw1;
1948 LIST_REMOVE (cap, entries);
1949 qemu_free (cap);
1951 return;