audio: Unbreak capturing in mixemu case
[qemu.git] / audio / audio.c
blob1c7738930b639964b8020411579bc7b95b2b979e
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 "monitor.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 */
37 /* #define DEBUG_POLL */
39 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
42 /* Order of CONFIG_AUDIO_DRIVERS is import.
43 The 1st one is the one used by default, that is the reason
44 that we generate the list.
46 static struct audio_driver *drvtab[] = {
47 #ifdef CONFIG_SPICE
48 &spice_audio_driver,
49 #endif
50 CONFIG_AUDIO_DRIVERS
51 &no_audio_driver,
52 &wav_audio_driver
55 struct fixed_settings {
56 int enabled;
57 int nb_voices;
58 int greedy;
59 struct audsettings settings;
62 static struct {
63 struct fixed_settings fixed_out;
64 struct fixed_settings fixed_in;
65 union {
66 int hertz;
67 int64_t ticks;
68 } period;
69 int plive;
70 int log_to_monitor;
71 int try_poll_in;
72 int try_poll_out;
73 } conf = {
74 .fixed_out = { /* DAC fixed settings */
75 .enabled = 1,
76 .nb_voices = 1,
77 .greedy = 1,
78 .settings = {
79 .freq = 44100,
80 .nchannels = 2,
81 .fmt = AUD_FMT_S16,
82 .endianness = AUDIO_HOST_ENDIANNESS,
86 .fixed_in = { /* ADC fixed settings */
87 .enabled = 1,
88 .nb_voices = 1,
89 .greedy = 1,
90 .settings = {
91 .freq = 44100,
92 .nchannels = 2,
93 .fmt = AUD_FMT_S16,
94 .endianness = AUDIO_HOST_ENDIANNESS,
98 .period = { .hertz = 250 },
99 .plive = 0,
100 .log_to_monitor = 0,
101 .try_poll_in = 1,
102 .try_poll_out = 1,
105 static AudioState glob_audio_state;
107 const struct mixeng_volume nominal_volume = {
108 .mute = 0,
109 #ifdef FLOAT_MIXENG
110 .r = 1.0,
111 .l = 1.0,
112 #else
113 .r = 1ULL << 32,
114 .l = 1ULL << 32,
115 #endif
118 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
119 #error No its not
120 #else
121 static void audio_print_options (const char *prefix,
122 struct audio_option *opt);
124 int audio_bug (const char *funcname, int cond)
126 if (cond) {
127 static int shown;
129 AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
130 if (!shown) {
131 struct audio_driver *d;
133 shown = 1;
134 AUD_log (NULL, "Save all your work and restart without audio\n");
135 AUD_log (NULL, "Please send bug report to av1474@comtv.ru\n");
136 AUD_log (NULL, "I am sorry\n");
137 d = glob_audio_state.drv;
138 if (d) {
139 audio_print_options (d->name, d->options);
142 AUD_log (NULL, "Context:\n");
144 #if defined AUDIO_BREAKPOINT_ON_BUG
145 # if defined HOST_I386
146 # if defined __GNUC__
147 __asm__ ("int3");
148 # elif defined _MSC_VER
149 _asm _emit 0xcc;
150 # else
151 abort ();
152 # endif
153 # else
154 abort ();
155 # endif
156 #endif
159 return cond;
161 #endif
163 static inline int audio_bits_to_index (int bits)
165 switch (bits) {
166 case 8:
167 return 0;
169 case 16:
170 return 1;
172 case 32:
173 return 2;
175 default:
176 audio_bug ("bits_to_index", 1);
177 AUD_log (NULL, "invalid bits %d\n", bits);
178 return 0;
182 void *audio_calloc (const char *funcname, int nmemb, size_t size)
184 int cond;
185 size_t len;
187 len = nmemb * size;
188 cond = !nmemb || !size;
189 cond |= nmemb < 0;
190 cond |= len < size;
192 if (audio_bug ("audio_calloc", cond)) {
193 AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
194 funcname);
195 AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
196 return NULL;
199 return g_malloc0 (len);
202 static char *audio_alloc_prefix (const char *s)
204 const char qemu_prefix[] = "QEMU_";
205 size_t len, i;
206 char *r, *u;
208 if (!s) {
209 return NULL;
212 len = strlen (s);
213 r = g_malloc (len + sizeof (qemu_prefix));
215 u = r + sizeof (qemu_prefix) - 1;
217 pstrcpy (r, len + sizeof (qemu_prefix), qemu_prefix);
218 pstrcat (r, len + sizeof (qemu_prefix), s);
220 for (i = 0; i < len; ++i) {
221 u[i] = qemu_toupper(u[i]);
224 return r;
227 static const char *audio_audfmt_to_string (audfmt_e fmt)
229 switch (fmt) {
230 case AUD_FMT_U8:
231 return "U8";
233 case AUD_FMT_U16:
234 return "U16";
236 case AUD_FMT_S8:
237 return "S8";
239 case AUD_FMT_S16:
240 return "S16";
242 case AUD_FMT_U32:
243 return "U32";
245 case AUD_FMT_S32:
246 return "S32";
249 dolog ("Bogus audfmt %d returning S16\n", fmt);
250 return "S16";
253 static audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval,
254 int *defaultp)
256 if (!strcasecmp (s, "u8")) {
257 *defaultp = 0;
258 return AUD_FMT_U8;
260 else if (!strcasecmp (s, "u16")) {
261 *defaultp = 0;
262 return AUD_FMT_U16;
264 else if (!strcasecmp (s, "u32")) {
265 *defaultp = 0;
266 return AUD_FMT_U32;
268 else if (!strcasecmp (s, "s8")) {
269 *defaultp = 0;
270 return AUD_FMT_S8;
272 else if (!strcasecmp (s, "s16")) {
273 *defaultp = 0;
274 return AUD_FMT_S16;
276 else if (!strcasecmp (s, "s32")) {
277 *defaultp = 0;
278 return AUD_FMT_S32;
280 else {
281 dolog ("Bogus audio format `%s' using %s\n",
282 s, audio_audfmt_to_string (defval));
283 *defaultp = 1;
284 return defval;
288 static audfmt_e audio_get_conf_fmt (const char *envname,
289 audfmt_e defval,
290 int *defaultp)
292 const char *var = getenv (envname);
293 if (!var) {
294 *defaultp = 1;
295 return defval;
297 return audio_string_to_audfmt (var, defval, defaultp);
300 static int audio_get_conf_int (const char *key, int defval, int *defaultp)
302 int val;
303 char *strval;
305 strval = getenv (key);
306 if (strval) {
307 *defaultp = 0;
308 val = atoi (strval);
309 return val;
311 else {
312 *defaultp = 1;
313 return defval;
317 static const char *audio_get_conf_str (const char *key,
318 const char *defval,
319 int *defaultp)
321 const char *val = getenv (key);
322 if (!val) {
323 *defaultp = 1;
324 return defval;
326 else {
327 *defaultp = 0;
328 return val;
332 void AUD_vlog (const char *cap, const char *fmt, va_list ap)
334 if (conf.log_to_monitor) {
335 if (cap) {
336 monitor_printf(default_mon, "%s: ", cap);
339 monitor_vprintf(default_mon, fmt, ap);
341 else {
342 if (cap) {
343 fprintf (stderr, "%s: ", cap);
346 vfprintf (stderr, fmt, ap);
350 void AUD_log (const char *cap, const char *fmt, ...)
352 va_list ap;
354 va_start (ap, fmt);
355 AUD_vlog (cap, fmt, ap);
356 va_end (ap);
359 static void audio_print_options (const char *prefix,
360 struct audio_option *opt)
362 char *uprefix;
364 if (!prefix) {
365 dolog ("No prefix specified\n");
366 return;
369 if (!opt) {
370 dolog ("No options\n");
371 return;
374 uprefix = audio_alloc_prefix (prefix);
376 for (; opt->name; opt++) {
377 const char *state = "default";
378 printf (" %s_%s: ", uprefix, opt->name);
380 if (opt->overriddenp && *opt->overriddenp) {
381 state = "current";
384 switch (opt->tag) {
385 case AUD_OPT_BOOL:
387 int *intp = opt->valp;
388 printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
390 break;
392 case AUD_OPT_INT:
394 int *intp = opt->valp;
395 printf ("integer, %s = %d\n", state, *intp);
397 break;
399 case AUD_OPT_FMT:
401 audfmt_e *fmtp = opt->valp;
402 printf (
403 "format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
404 state,
405 audio_audfmt_to_string (*fmtp)
408 break;
410 case AUD_OPT_STR:
412 const char **strp = opt->valp;
413 printf ("string, %s = %s\n",
414 state,
415 *strp ? *strp : "(not set)");
417 break;
419 default:
420 printf ("???\n");
421 dolog ("Bad value tag for option %s_%s %d\n",
422 uprefix, opt->name, opt->tag);
423 break;
425 printf (" %s\n", opt->descr);
428 g_free (uprefix);
431 static void audio_process_options (const char *prefix,
432 struct audio_option *opt)
434 char *optname;
435 const char qemu_prefix[] = "QEMU_";
436 size_t preflen, optlen;
438 if (audio_bug (AUDIO_FUNC, !prefix)) {
439 dolog ("prefix = NULL\n");
440 return;
443 if (audio_bug (AUDIO_FUNC, !opt)) {
444 dolog ("opt = NULL\n");
445 return;
448 preflen = strlen (prefix);
450 for (; opt->name; opt++) {
451 size_t len, i;
452 int def;
454 if (!opt->valp) {
455 dolog ("Option value pointer for `%s' is not set\n",
456 opt->name);
457 continue;
460 len = strlen (opt->name);
461 /* len of opt->name + len of prefix + size of qemu_prefix
462 * (includes trailing zero) + zero + underscore (on behalf of
463 * sizeof) */
464 optlen = len + preflen + sizeof (qemu_prefix) + 1;
465 optname = g_malloc (optlen);
467 pstrcpy (optname, optlen, qemu_prefix);
469 /* copy while upper-casing, including trailing zero */
470 for (i = 0; i <= preflen; ++i) {
471 optname[i + sizeof (qemu_prefix) - 1] = qemu_toupper(prefix[i]);
473 pstrcat (optname, optlen, "_");
474 pstrcat (optname, optlen, opt->name);
476 def = 1;
477 switch (opt->tag) {
478 case AUD_OPT_BOOL:
479 case AUD_OPT_INT:
481 int *intp = opt->valp;
482 *intp = audio_get_conf_int (optname, *intp, &def);
484 break;
486 case AUD_OPT_FMT:
488 audfmt_e *fmtp = opt->valp;
489 *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
491 break;
493 case AUD_OPT_STR:
495 const char **strp = opt->valp;
496 *strp = audio_get_conf_str (optname, *strp, &def);
498 break;
500 default:
501 dolog ("Bad value tag for option `%s' - %d\n",
502 optname, opt->tag);
503 break;
506 if (!opt->overriddenp) {
507 opt->overriddenp = &opt->overridden;
509 *opt->overriddenp = !def;
510 g_free (optname);
514 static void audio_print_settings (struct audsettings *as)
516 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
518 switch (as->fmt) {
519 case AUD_FMT_S8:
520 AUD_log (NULL, "S8");
521 break;
522 case AUD_FMT_U8:
523 AUD_log (NULL, "U8");
524 break;
525 case AUD_FMT_S16:
526 AUD_log (NULL, "S16");
527 break;
528 case AUD_FMT_U16:
529 AUD_log (NULL, "U16");
530 break;
531 case AUD_FMT_S32:
532 AUD_log (NULL, "S32");
533 break;
534 case AUD_FMT_U32:
535 AUD_log (NULL, "U32");
536 break;
537 default:
538 AUD_log (NULL, "invalid(%d)", as->fmt);
539 break;
542 AUD_log (NULL, " endianness=");
543 switch (as->endianness) {
544 case 0:
545 AUD_log (NULL, "little");
546 break;
547 case 1:
548 AUD_log (NULL, "big");
549 break;
550 default:
551 AUD_log (NULL, "invalid");
552 break;
554 AUD_log (NULL, "\n");
557 static int audio_validate_settings (struct audsettings *as)
559 int invalid;
561 invalid = as->nchannels != 1 && as->nchannels != 2;
562 invalid |= as->endianness != 0 && as->endianness != 1;
564 switch (as->fmt) {
565 case AUD_FMT_S8:
566 case AUD_FMT_U8:
567 case AUD_FMT_S16:
568 case AUD_FMT_U16:
569 case AUD_FMT_S32:
570 case AUD_FMT_U32:
571 break;
572 default:
573 invalid = 1;
574 break;
577 invalid |= as->freq <= 0;
578 return invalid ? -1 : 0;
581 static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
583 int bits = 8, sign = 0;
585 switch (as->fmt) {
586 case AUD_FMT_S8:
587 sign = 1;
588 /* fall through */
589 case AUD_FMT_U8:
590 break;
592 case AUD_FMT_S16:
593 sign = 1;
594 /* fall through */
595 case AUD_FMT_U16:
596 bits = 16;
597 break;
599 case AUD_FMT_S32:
600 sign = 1;
601 /* fall through */
602 case AUD_FMT_U32:
603 bits = 32;
604 break;
606 return info->freq == as->freq
607 && info->nchannels == as->nchannels
608 && info->sign == sign
609 && info->bits == bits
610 && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
613 void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
615 int bits = 8, sign = 0, shift = 0;
617 switch (as->fmt) {
618 case AUD_FMT_S8:
619 sign = 1;
620 case AUD_FMT_U8:
621 break;
623 case AUD_FMT_S16:
624 sign = 1;
625 case AUD_FMT_U16:
626 bits = 16;
627 shift = 1;
628 break;
630 case AUD_FMT_S32:
631 sign = 1;
632 case AUD_FMT_U32:
633 bits = 32;
634 shift = 2;
635 break;
638 info->freq = as->freq;
639 info->bits = bits;
640 info->sign = sign;
641 info->nchannels = as->nchannels;
642 info->shift = (as->nchannels == 2) + shift;
643 info->align = (1 << info->shift) - 1;
644 info->bytes_per_second = info->freq << info->shift;
645 info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
648 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
650 if (!len) {
651 return;
654 if (info->sign) {
655 memset (buf, 0x00, len << info->shift);
657 else {
658 switch (info->bits) {
659 case 8:
660 memset (buf, 0x80, len << info->shift);
661 break;
663 case 16:
665 int i;
666 uint16_t *p = buf;
667 int shift = info->nchannels - 1;
668 short s = INT16_MAX;
670 if (info->swap_endianness) {
671 s = bswap16 (s);
674 for (i = 0; i < len << shift; i++) {
675 p[i] = s;
678 break;
680 case 32:
682 int i;
683 uint32_t *p = buf;
684 int shift = info->nchannels - 1;
685 int32_t s = INT32_MAX;
687 if (info->swap_endianness) {
688 s = bswap32 (s);
691 for (i = 0; i < len << shift; i++) {
692 p[i] = s;
695 break;
697 default:
698 AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
699 info->bits);
700 break;
706 * Capture
708 static void noop_conv (struct st_sample *dst, const void *src, int samples)
710 (void) src;
711 (void) dst;
712 (void) samples;
715 static CaptureVoiceOut *audio_pcm_capture_find_specific (
716 struct audsettings *as
719 CaptureVoiceOut *cap;
720 AudioState *s = &glob_audio_state;
722 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
723 if (audio_pcm_info_eq (&cap->hw.info, as)) {
724 return cap;
727 return NULL;
730 static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
732 struct capture_callback *cb;
734 #ifdef DEBUG_CAPTURE
735 dolog ("notification %d sent\n", cmd);
736 #endif
737 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
738 cb->ops.notify (cb->opaque, cmd);
742 static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
744 if (cap->hw.enabled != enabled) {
745 audcnotification_e cmd;
746 cap->hw.enabled = enabled;
747 cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
748 audio_notify_capture (cap, cmd);
752 static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
754 HWVoiceOut *hw = &cap->hw;
755 SWVoiceOut *sw;
756 int enabled = 0;
758 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
759 if (sw->active) {
760 enabled = 1;
761 break;
764 audio_capture_maybe_changed (cap, enabled);
767 static void audio_detach_capture (HWVoiceOut *hw)
769 SWVoiceCap *sc = hw->cap_head.lh_first;
771 while (sc) {
772 SWVoiceCap *sc1 = sc->entries.le_next;
773 SWVoiceOut *sw = &sc->sw;
774 CaptureVoiceOut *cap = sc->cap;
775 int was_active = sw->active;
777 if (sw->rate) {
778 st_rate_stop (sw->rate);
779 sw->rate = NULL;
782 QLIST_REMOVE (sw, entries);
783 QLIST_REMOVE (sc, entries);
784 g_free (sc);
785 if (was_active) {
786 /* We have removed soft voice from the capture:
787 this might have changed the overall status of the capture
788 since this might have been the only active voice */
789 audio_recalc_and_notify_capture (cap);
791 sc = sc1;
795 static int audio_attach_capture (HWVoiceOut *hw)
797 AudioState *s = &glob_audio_state;
798 CaptureVoiceOut *cap;
800 audio_detach_capture (hw);
801 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
802 SWVoiceCap *sc;
803 SWVoiceOut *sw;
804 HWVoiceOut *hw_cap = &cap->hw;
806 sc = audio_calloc (AUDIO_FUNC, 1, sizeof (*sc));
807 if (!sc) {
808 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
809 sizeof (*sc));
810 return -1;
813 sc->cap = cap;
814 sw = &sc->sw;
815 sw->hw = hw_cap;
816 sw->info = hw->info;
817 sw->empty = 1;
818 sw->active = hw->enabled;
819 sw->conv = noop_conv;
820 sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
821 sw->vol = nominal_volume;
822 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
823 if (!sw->rate) {
824 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
825 g_free (sw);
826 return -1;
828 QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
829 QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
830 #ifdef DEBUG_CAPTURE
831 asprintf (&sw->name, "for %p %d,%d,%d",
832 hw, sw->info.freq, sw->info.bits, sw->info.nchannels);
833 dolog ("Added %s active = %d\n", sw->name, sw->active);
834 #endif
835 if (sw->active) {
836 audio_capture_maybe_changed (cap, 1);
839 return 0;
843 * Hard voice (capture)
845 static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
847 SWVoiceIn *sw;
848 int m = hw->total_samples_captured;
850 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
851 if (sw->active) {
852 m = audio_MIN (m, sw->total_hw_samples_acquired);
855 return m;
858 int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
860 int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
861 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
862 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
863 return 0;
865 return live;
868 int audio_pcm_hw_clip_out (HWVoiceOut *hw, void *pcm_buf,
869 int live, int pending)
871 int left = hw->samples - pending;
872 int len = audio_MIN (left, live);
873 int clipped = 0;
875 while (len) {
876 struct st_sample *src = hw->mix_buf + hw->rpos;
877 uint8_t *dst = advance (pcm_buf, hw->rpos << hw->info.shift);
878 int samples_till_end_of_buf = hw->samples - hw->rpos;
879 int samples_to_clip = audio_MIN (len, samples_till_end_of_buf);
881 hw->clip (dst, src, samples_to_clip);
883 hw->rpos = (hw->rpos + samples_to_clip) % hw->samples;
884 len -= samples_to_clip;
885 clipped += samples_to_clip;
887 return clipped;
891 * Soft voice (capture)
893 static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
895 HWVoiceIn *hw = sw->hw;
896 int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
897 int rpos;
899 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
900 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
901 return 0;
904 rpos = hw->wpos - live;
905 if (rpos >= 0) {
906 return rpos;
908 else {
909 return hw->samples + rpos;
913 int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
915 HWVoiceIn *hw = sw->hw;
916 int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
917 struct st_sample *src, *dst = sw->buf;
919 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
921 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
922 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
923 dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
924 return 0;
927 samples = size >> sw->info.shift;
928 if (!live) {
929 return 0;
932 swlim = (live * sw->ratio) >> 32;
933 swlim = audio_MIN (swlim, samples);
935 while (swlim) {
936 src = hw->conv_buf + rpos;
937 isamp = hw->wpos - rpos;
938 /* XXX: <= ? */
939 if (isamp <= 0) {
940 isamp = hw->samples - rpos;
943 if (!isamp) {
944 break;
946 osamp = swlim;
948 if (audio_bug (AUDIO_FUNC, osamp < 0)) {
949 dolog ("osamp=%d\n", osamp);
950 return 0;
953 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
954 swlim -= osamp;
955 rpos = (rpos + isamp) % hw->samples;
956 dst += osamp;
957 ret += osamp;
958 total += isamp;
961 if (!(hw->ctl_caps & VOICE_VOLUME_CAP)) {
962 mixeng_volume (sw->buf, ret, &sw->vol);
965 sw->clip (buf, sw->buf, ret);
966 sw->total_hw_samples_acquired += total;
967 return ret << sw->info.shift;
971 * Hard voice (playback)
973 static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
975 SWVoiceOut *sw;
976 int m = INT_MAX;
977 int nb_live = 0;
979 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
980 if (sw->active || !sw->empty) {
981 m = audio_MIN (m, sw->total_hw_samples_mixed);
982 nb_live += 1;
986 *nb_livep = nb_live;
987 return m;
990 static int audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
992 int smin;
993 int nb_live1;
995 smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
996 if (nb_live) {
997 *nb_live = nb_live1;
1000 if (nb_live1) {
1001 int live = smin;
1003 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1004 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1005 return 0;
1007 return live;
1009 return 0;
1013 * Soft voice (playback)
1015 int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
1017 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
1018 int ret = 0, pos = 0, total = 0;
1020 if (!sw) {
1021 return size;
1024 hwsamples = sw->hw->samples;
1026 live = sw->total_hw_samples_mixed;
1027 if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
1028 dolog ("live=%d hw->samples=%d\n", live, hwsamples);
1029 return 0;
1032 if (live == hwsamples) {
1033 #ifdef DEBUG_OUT
1034 dolog ("%s is full %d\n", sw->name, live);
1035 #endif
1036 return 0;
1039 wpos = (sw->hw->rpos + live) % hwsamples;
1040 samples = size >> sw->info.shift;
1042 dead = hwsamples - live;
1043 swlim = ((int64_t) dead << 32) / sw->ratio;
1044 swlim = audio_MIN (swlim, samples);
1045 if (swlim) {
1046 sw->conv (sw->buf, buf, swlim);
1048 if (!(sw->hw->ctl_caps & VOICE_VOLUME_CAP)) {
1049 mixeng_volume (sw->buf, swlim, &sw->vol);
1053 while (swlim) {
1054 dead = hwsamples - live;
1055 left = hwsamples - wpos;
1056 blck = audio_MIN (dead, left);
1057 if (!blck) {
1058 break;
1060 isamp = swlim;
1061 osamp = blck;
1062 st_rate_flow_mix (
1063 sw->rate,
1064 sw->buf + pos,
1065 sw->hw->mix_buf + wpos,
1066 &isamp,
1067 &osamp
1069 ret += isamp;
1070 swlim -= isamp;
1071 pos += isamp;
1072 live += osamp;
1073 wpos = (wpos + osamp) % hwsamples;
1074 total += osamp;
1077 sw->total_hw_samples_mixed += total;
1078 sw->empty = sw->total_hw_samples_mixed == 0;
1080 #ifdef DEBUG_OUT
1081 dolog (
1082 "%s: write size %d ret %d total sw %d\n",
1083 SW_NAME (sw),
1084 size >> sw->info.shift,
1085 ret,
1086 sw->total_hw_samples_mixed
1088 #endif
1090 return ret << sw->info.shift;
1093 #ifdef DEBUG_AUDIO
1094 static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
1096 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1097 cap, info->bits, info->sign, info->freq, info->nchannels);
1099 #endif
1101 #define DAC
1102 #include "audio_template.h"
1103 #undef DAC
1104 #include "audio_template.h"
1107 * Timer
1109 static int audio_is_timer_needed (void)
1111 HWVoiceIn *hwi = NULL;
1112 HWVoiceOut *hwo = NULL;
1114 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1115 if (!hwo->poll_mode) return 1;
1117 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1118 if (!hwi->poll_mode) return 1;
1120 return 0;
1123 static void audio_reset_timer (AudioState *s)
1125 if (audio_is_timer_needed ()) {
1126 qemu_mod_timer (s->ts, qemu_get_clock_ns (vm_clock) + 1);
1128 else {
1129 qemu_del_timer (s->ts);
1133 static void audio_timer (void *opaque)
1135 audio_run ("timer");
1136 audio_reset_timer (opaque);
1140 * Public API
1142 int AUD_write (SWVoiceOut *sw, void *buf, int size)
1144 int bytes;
1146 if (!sw) {
1147 /* XXX: Consider options */
1148 return size;
1151 if (!sw->hw->enabled) {
1152 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
1153 return 0;
1156 bytes = sw->hw->pcm_ops->write (sw, buf, size);
1157 return bytes;
1160 int AUD_read (SWVoiceIn *sw, void *buf, int size)
1162 int bytes;
1164 if (!sw) {
1165 /* XXX: Consider options */
1166 return size;
1169 if (!sw->hw->enabled) {
1170 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1171 return 0;
1174 bytes = sw->hw->pcm_ops->read (sw, buf, size);
1175 return bytes;
1178 int AUD_get_buffer_size_out (SWVoiceOut *sw)
1180 return sw->hw->samples << sw->hw->info.shift;
1183 void AUD_set_active_out (SWVoiceOut *sw, int on)
1185 HWVoiceOut *hw;
1187 if (!sw) {
1188 return;
1191 hw = sw->hw;
1192 if (sw->active != on) {
1193 AudioState *s = &glob_audio_state;
1194 SWVoiceOut *temp_sw;
1195 SWVoiceCap *sc;
1197 if (on) {
1198 hw->pending_disable = 0;
1199 if (!hw->enabled) {
1200 hw->enabled = 1;
1201 if (s->vm_running) {
1202 hw->pcm_ops->ctl_out (hw, VOICE_ENABLE, conf.try_poll_out);
1203 audio_reset_timer (s);
1207 else {
1208 if (hw->enabled) {
1209 int nb_active = 0;
1211 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1212 temp_sw = temp_sw->entries.le_next) {
1213 nb_active += temp_sw->active != 0;
1216 hw->pending_disable = nb_active == 1;
1220 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1221 sc->sw.active = hw->enabled;
1222 if (hw->enabled) {
1223 audio_capture_maybe_changed (sc->cap, 1);
1226 sw->active = on;
1230 void AUD_set_active_in (SWVoiceIn *sw, int on)
1232 HWVoiceIn *hw;
1234 if (!sw) {
1235 return;
1238 hw = sw->hw;
1239 if (sw->active != on) {
1240 AudioState *s = &glob_audio_state;
1241 SWVoiceIn *temp_sw;
1243 if (on) {
1244 if (!hw->enabled) {
1245 hw->enabled = 1;
1246 if (s->vm_running) {
1247 hw->pcm_ops->ctl_in (hw, VOICE_ENABLE, conf.try_poll_in);
1248 audio_reset_timer (s);
1251 sw->total_hw_samples_acquired = hw->total_samples_captured;
1253 else {
1254 if (hw->enabled) {
1255 int nb_active = 0;
1257 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1258 temp_sw = temp_sw->entries.le_next) {
1259 nb_active += temp_sw->active != 0;
1262 if (nb_active == 1) {
1263 hw->enabled = 0;
1264 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
1268 sw->active = on;
1272 static int audio_get_avail (SWVoiceIn *sw)
1274 int live;
1276 if (!sw) {
1277 return 0;
1280 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1281 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1282 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1283 return 0;
1286 ldebug (
1287 "%s: get_avail live %d ret %" PRId64 "\n",
1288 SW_NAME (sw),
1289 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1292 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1295 static int audio_get_free (SWVoiceOut *sw)
1297 int live, dead;
1299 if (!sw) {
1300 return 0;
1303 live = sw->total_hw_samples_mixed;
1305 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1306 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1307 return 0;
1310 dead = sw->hw->samples - live;
1312 #ifdef DEBUG_OUT
1313 dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
1314 SW_NAME (sw),
1315 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1316 #endif
1318 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1321 static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1323 int n;
1325 if (hw->enabled) {
1326 SWVoiceCap *sc;
1328 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1329 SWVoiceOut *sw = &sc->sw;
1330 int rpos2 = rpos;
1332 n = samples;
1333 while (n) {
1334 int till_end_of_hw = hw->samples - rpos2;
1335 int to_write = audio_MIN (till_end_of_hw, n);
1336 int bytes = to_write << hw->info.shift;
1337 int written;
1339 sw->buf = hw->mix_buf + rpos2;
1340 written = audio_pcm_sw_write (sw, NULL, bytes);
1341 if (written - bytes) {
1342 dolog ("Could not mix %d bytes into a capture "
1343 "buffer, mixed %d\n",
1344 bytes, written);
1345 break;
1347 n -= to_write;
1348 rpos2 = (rpos2 + to_write) % hw->samples;
1353 n = audio_MIN (samples, hw->samples - rpos);
1354 mixeng_clear (hw->mix_buf + rpos, n);
1355 mixeng_clear (hw->mix_buf, samples - n);
1358 static void audio_run_out (AudioState *s)
1360 HWVoiceOut *hw = NULL;
1361 SWVoiceOut *sw;
1363 while ((hw = audio_pcm_hw_find_any_enabled_out (hw))) {
1364 int played;
1365 int live, free, nb_live, cleanup_required, prev_rpos;
1367 live = audio_pcm_hw_get_live_out (hw, &nb_live);
1368 if (!nb_live) {
1369 live = 0;
1372 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1373 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1374 continue;
1377 if (hw->pending_disable && !nb_live) {
1378 SWVoiceCap *sc;
1379 #ifdef DEBUG_OUT
1380 dolog ("Disabling voice\n");
1381 #endif
1382 hw->enabled = 0;
1383 hw->pending_disable = 0;
1384 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
1385 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1386 sc->sw.active = 0;
1387 audio_recalc_and_notify_capture (sc->cap);
1389 continue;
1392 if (!live) {
1393 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1394 if (sw->active) {
1395 free = audio_get_free (sw);
1396 if (free > 0) {
1397 sw->callback.fn (sw->callback.opaque, free);
1401 continue;
1404 prev_rpos = hw->rpos;
1405 played = hw->pcm_ops->run_out (hw, live);
1406 if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1407 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1408 hw->rpos, hw->samples, played);
1409 hw->rpos = 0;
1412 #ifdef DEBUG_OUT
1413 dolog ("played=%d\n", played);
1414 #endif
1416 if (played) {
1417 hw->ts_helper += played;
1418 audio_capture_mix_and_clear (hw, prev_rpos, played);
1421 cleanup_required = 0;
1422 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1423 if (!sw->active && sw->empty) {
1424 continue;
1427 if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1428 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1429 played, sw->total_hw_samples_mixed);
1430 played = sw->total_hw_samples_mixed;
1433 sw->total_hw_samples_mixed -= played;
1435 if (!sw->total_hw_samples_mixed) {
1436 sw->empty = 1;
1437 cleanup_required |= !sw->active && !sw->callback.fn;
1440 if (sw->active) {
1441 free = audio_get_free (sw);
1442 if (free > 0) {
1443 sw->callback.fn (sw->callback.opaque, free);
1448 if (cleanup_required) {
1449 SWVoiceOut *sw1;
1451 sw = hw->sw_head.lh_first;
1452 while (sw) {
1453 sw1 = sw->entries.le_next;
1454 if (!sw->active && !sw->callback.fn) {
1455 #ifdef DEBUG_PLIVE
1456 dolog ("Finishing with old voice\n");
1457 #endif
1458 audio_close_out (sw);
1460 sw = sw1;
1466 static void audio_run_in (AudioState *s)
1468 HWVoiceIn *hw = NULL;
1470 while ((hw = audio_pcm_hw_find_any_enabled_in (hw))) {
1471 SWVoiceIn *sw;
1472 int captured, min;
1474 captured = hw->pcm_ops->run_in (hw);
1476 min = audio_pcm_hw_find_min_in (hw);
1477 hw->total_samples_captured += captured - min;
1478 hw->ts_helper += captured;
1480 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1481 sw->total_hw_samples_acquired -= min;
1483 if (sw->active) {
1484 int avail;
1486 avail = audio_get_avail (sw);
1487 if (avail > 0) {
1488 sw->callback.fn (sw->callback.opaque, avail);
1495 static void audio_run_capture (AudioState *s)
1497 CaptureVoiceOut *cap;
1499 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1500 int live, rpos, captured;
1501 HWVoiceOut *hw = &cap->hw;
1502 SWVoiceOut *sw;
1504 captured = live = audio_pcm_hw_get_live_out (hw, NULL);
1505 rpos = hw->rpos;
1506 while (live) {
1507 int left = hw->samples - rpos;
1508 int to_capture = audio_MIN (live, left);
1509 struct st_sample *src;
1510 struct capture_callback *cb;
1512 src = hw->mix_buf + rpos;
1513 hw->clip (cap->buf, src, to_capture);
1514 mixeng_clear (src, to_capture);
1516 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1517 cb->ops.capture (cb->opaque, cap->buf,
1518 to_capture << hw->info.shift);
1520 rpos = (rpos + to_capture) % hw->samples;
1521 live -= to_capture;
1523 hw->rpos = rpos;
1525 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1526 if (!sw->active && sw->empty) {
1527 continue;
1530 if (audio_bug (AUDIO_FUNC, captured > sw->total_hw_samples_mixed)) {
1531 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1532 captured, sw->total_hw_samples_mixed);
1533 captured = sw->total_hw_samples_mixed;
1536 sw->total_hw_samples_mixed -= captured;
1537 sw->empty = sw->total_hw_samples_mixed == 0;
1542 void audio_run (const char *msg)
1544 AudioState *s = &glob_audio_state;
1546 audio_run_out (s);
1547 audio_run_in (s);
1548 audio_run_capture (s);
1549 #ifdef DEBUG_POLL
1551 static double prevtime;
1552 double currtime;
1553 struct timeval tv;
1555 if (gettimeofday (&tv, NULL)) {
1556 perror ("audio_run: gettimeofday");
1557 return;
1560 currtime = tv.tv_sec + tv.tv_usec * 1e-6;
1561 dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime);
1562 prevtime = currtime;
1564 #endif
1567 static struct audio_option audio_options[] = {
1568 /* DAC */
1570 .name = "DAC_FIXED_SETTINGS",
1571 .tag = AUD_OPT_BOOL,
1572 .valp = &conf.fixed_out.enabled,
1573 .descr = "Use fixed settings for host DAC"
1576 .name = "DAC_FIXED_FREQ",
1577 .tag = AUD_OPT_INT,
1578 .valp = &conf.fixed_out.settings.freq,
1579 .descr = "Frequency for fixed host DAC"
1582 .name = "DAC_FIXED_FMT",
1583 .tag = AUD_OPT_FMT,
1584 .valp = &conf.fixed_out.settings.fmt,
1585 .descr = "Format for fixed host DAC"
1588 .name = "DAC_FIXED_CHANNELS",
1589 .tag = AUD_OPT_INT,
1590 .valp = &conf.fixed_out.settings.nchannels,
1591 .descr = "Number of channels for fixed DAC (1 - mono, 2 - stereo)"
1594 .name = "DAC_VOICES",
1595 .tag = AUD_OPT_INT,
1596 .valp = &conf.fixed_out.nb_voices,
1597 .descr = "Number of voices for DAC"
1600 .name = "DAC_TRY_POLL",
1601 .tag = AUD_OPT_BOOL,
1602 .valp = &conf.try_poll_out,
1603 .descr = "Attempt using poll mode for DAC"
1605 /* ADC */
1607 .name = "ADC_FIXED_SETTINGS",
1608 .tag = AUD_OPT_BOOL,
1609 .valp = &conf.fixed_in.enabled,
1610 .descr = "Use fixed settings for host ADC"
1613 .name = "ADC_FIXED_FREQ",
1614 .tag = AUD_OPT_INT,
1615 .valp = &conf.fixed_in.settings.freq,
1616 .descr = "Frequency for fixed host ADC"
1619 .name = "ADC_FIXED_FMT",
1620 .tag = AUD_OPT_FMT,
1621 .valp = &conf.fixed_in.settings.fmt,
1622 .descr = "Format for fixed host ADC"
1625 .name = "ADC_FIXED_CHANNELS",
1626 .tag = AUD_OPT_INT,
1627 .valp = &conf.fixed_in.settings.nchannels,
1628 .descr = "Number of channels for fixed ADC (1 - mono, 2 - stereo)"
1631 .name = "ADC_VOICES",
1632 .tag = AUD_OPT_INT,
1633 .valp = &conf.fixed_in.nb_voices,
1634 .descr = "Number of voices for ADC"
1637 .name = "ADC_TRY_POLL",
1638 .tag = AUD_OPT_BOOL,
1639 .valp = &conf.try_poll_in,
1640 .descr = "Attempt using poll mode for ADC"
1642 /* Misc */
1644 .name = "TIMER_PERIOD",
1645 .tag = AUD_OPT_INT,
1646 .valp = &conf.period.hertz,
1647 .descr = "Timer period in HZ (0 - use lowest possible)"
1650 .name = "PLIVE",
1651 .tag = AUD_OPT_BOOL,
1652 .valp = &conf.plive,
1653 .descr = "(undocumented)"
1656 .name = "LOG_TO_MONITOR",
1657 .tag = AUD_OPT_BOOL,
1658 .valp = &conf.log_to_monitor,
1659 .descr = "Print logging messages to monitor instead of stderr"
1661 { /* End of list */ }
1664 static void audio_pp_nb_voices (const char *typ, int nb)
1666 switch (nb) {
1667 case 0:
1668 printf ("Does not support %s\n", typ);
1669 break;
1670 case 1:
1671 printf ("One %s voice\n", typ);
1672 break;
1673 case INT_MAX:
1674 printf ("Theoretically supports many %s voices\n", typ);
1675 break;
1676 default:
1677 printf ("Theoretically supports up to %d %s voices\n", nb, typ);
1678 break;
1683 void AUD_help (void)
1685 size_t i;
1687 audio_process_options ("AUDIO", audio_options);
1688 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1689 struct audio_driver *d = drvtab[i];
1690 if (d->options) {
1691 audio_process_options (d->name, d->options);
1695 printf ("Audio options:\n");
1696 audio_print_options ("AUDIO", audio_options);
1697 printf ("\n");
1699 printf ("Available drivers:\n");
1701 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1702 struct audio_driver *d = drvtab[i];
1704 printf ("Name: %s\n", d->name);
1705 printf ("Description: %s\n", d->descr);
1707 audio_pp_nb_voices ("playback", d->max_voices_out);
1708 audio_pp_nb_voices ("capture", d->max_voices_in);
1710 if (d->options) {
1711 printf ("Options:\n");
1712 audio_print_options (d->name, d->options);
1714 else {
1715 printf ("No options\n");
1717 printf ("\n");
1720 printf (
1721 "Options are settable through environment variables.\n"
1722 "Example:\n"
1723 #ifdef _WIN32
1724 " set QEMU_AUDIO_DRV=wav\n"
1725 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1726 #else
1727 " export QEMU_AUDIO_DRV=wav\n"
1728 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1729 "(for csh replace export with setenv in the above)\n"
1730 #endif
1731 " qemu ...\n\n"
1735 static int audio_driver_init (AudioState *s, struct audio_driver *drv)
1737 if (drv->options) {
1738 audio_process_options (drv->name, drv->options);
1740 s->drv_opaque = drv->init ();
1742 if (s->drv_opaque) {
1743 audio_init_nb_voices_out (drv);
1744 audio_init_nb_voices_in (drv);
1745 s->drv = drv;
1746 return 0;
1748 else {
1749 dolog ("Could not init `%s' audio driver\n", drv->name);
1750 return -1;
1754 static void audio_vm_change_state_handler (void *opaque, int running,
1755 RunState state)
1757 AudioState *s = opaque;
1758 HWVoiceOut *hwo = NULL;
1759 HWVoiceIn *hwi = NULL;
1760 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1762 s->vm_running = running;
1763 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1764 hwo->pcm_ops->ctl_out (hwo, op, conf.try_poll_out);
1767 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1768 hwi->pcm_ops->ctl_in (hwi, op, conf.try_poll_in);
1770 audio_reset_timer (s);
1773 static void audio_atexit (void)
1775 AudioState *s = &glob_audio_state;
1776 HWVoiceOut *hwo = NULL;
1777 HWVoiceIn *hwi = NULL;
1779 while ((hwo = audio_pcm_hw_find_any_out (hwo))) {
1780 SWVoiceCap *sc;
1782 if (hwo->enabled) {
1783 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1785 hwo->pcm_ops->fini_out (hwo);
1787 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1788 CaptureVoiceOut *cap = sc->cap;
1789 struct capture_callback *cb;
1791 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1792 cb->ops.destroy (cb->opaque);
1797 while ((hwi = audio_pcm_hw_find_any_in (hwi))) {
1798 if (hwi->enabled) {
1799 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1801 hwi->pcm_ops->fini_in (hwi);
1804 if (s->drv) {
1805 s->drv->fini (s->drv_opaque);
1809 static const VMStateDescription vmstate_audio = {
1810 .name = "audio",
1811 .version_id = 1,
1812 .minimum_version_id = 1,
1813 .minimum_version_id_old = 1,
1814 .fields = (VMStateField []) {
1815 VMSTATE_END_OF_LIST()
1819 static void audio_init (void)
1821 size_t i;
1822 int done = 0;
1823 const char *drvname;
1824 VMChangeStateEntry *e;
1825 AudioState *s = &glob_audio_state;
1827 if (s->drv) {
1828 return;
1831 QLIST_INIT (&s->hw_head_out);
1832 QLIST_INIT (&s->hw_head_in);
1833 QLIST_INIT (&s->cap_head);
1834 atexit (audio_atexit);
1836 s->ts = qemu_new_timer_ns (vm_clock, audio_timer, s);
1837 if (!s->ts) {
1838 hw_error("Could not create audio timer\n");
1841 audio_process_options ("AUDIO", audio_options);
1843 s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1844 s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1846 if (s->nb_hw_voices_out <= 0) {
1847 dolog ("Bogus number of playback voices %d, setting to 1\n",
1848 s->nb_hw_voices_out);
1849 s->nb_hw_voices_out = 1;
1852 if (s->nb_hw_voices_in <= 0) {
1853 dolog ("Bogus number of capture voices %d, setting to 0\n",
1854 s->nb_hw_voices_in);
1855 s->nb_hw_voices_in = 0;
1859 int def;
1860 drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1863 if (drvname) {
1864 int found = 0;
1866 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1867 if (!strcmp (drvname, drvtab[i]->name)) {
1868 done = !audio_driver_init (s, drvtab[i]);
1869 found = 1;
1870 break;
1874 if (!found) {
1875 dolog ("Unknown audio driver `%s'\n", drvname);
1876 dolog ("Run with -audio-help to list available drivers\n");
1880 if (!done) {
1881 for (i = 0; !done && i < ARRAY_SIZE (drvtab); i++) {
1882 if (drvtab[i]->can_be_default) {
1883 done = !audio_driver_init (s, drvtab[i]);
1888 if (!done) {
1889 done = !audio_driver_init (s, &no_audio_driver);
1890 if (!done) {
1891 hw_error("Could not initialize audio subsystem\n");
1893 else {
1894 dolog ("warning: Using timer based audio emulation\n");
1898 if (conf.period.hertz <= 0) {
1899 if (conf.period.hertz < 0) {
1900 dolog ("warning: Timer period is negative - %d "
1901 "treating as zero\n",
1902 conf.period.hertz);
1904 conf.period.ticks = 1;
1905 } else {
1906 conf.period.ticks =
1907 muldiv64 (1, get_ticks_per_sec (), conf.period.hertz);
1910 e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1911 if (!e) {
1912 dolog ("warning: Could not register change state handler\n"
1913 "(Audio can continue looping even after stopping the VM)\n");
1916 QLIST_INIT (&s->card_head);
1917 vmstate_register (NULL, 0, &vmstate_audio, s);
1920 void AUD_register_card (const char *name, QEMUSoundCard *card)
1922 audio_init ();
1923 card->name = g_strdup (name);
1924 memset (&card->entries, 0, sizeof (card->entries));
1925 QLIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries);
1928 void AUD_remove_card (QEMUSoundCard *card)
1930 QLIST_REMOVE (card, entries);
1931 g_free (card->name);
1935 CaptureVoiceOut *AUD_add_capture (
1936 struct audsettings *as,
1937 struct audio_capture_ops *ops,
1938 void *cb_opaque
1941 AudioState *s = &glob_audio_state;
1942 CaptureVoiceOut *cap;
1943 struct capture_callback *cb;
1945 if (audio_validate_settings (as)) {
1946 dolog ("Invalid settings were passed when trying to add capture\n");
1947 audio_print_settings (as);
1948 goto err0;
1951 cb = audio_calloc (AUDIO_FUNC, 1, sizeof (*cb));
1952 if (!cb) {
1953 dolog ("Could not allocate capture callback information, size %zu\n",
1954 sizeof (*cb));
1955 goto err0;
1957 cb->ops = *ops;
1958 cb->opaque = cb_opaque;
1960 cap = audio_pcm_capture_find_specific (as);
1961 if (cap) {
1962 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1963 return cap;
1965 else {
1966 HWVoiceOut *hw;
1967 CaptureVoiceOut *cap;
1969 cap = audio_calloc (AUDIO_FUNC, 1, sizeof (*cap));
1970 if (!cap) {
1971 dolog ("Could not allocate capture voice, size %zu\n",
1972 sizeof (*cap));
1973 goto err1;
1976 hw = &cap->hw;
1977 QLIST_INIT (&hw->sw_head);
1978 QLIST_INIT (&cap->cb_head);
1980 /* XXX find a more elegant way */
1981 hw->samples = 4096 * 4;
1982 hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples,
1983 sizeof (struct st_sample));
1984 if (!hw->mix_buf) {
1985 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1986 hw->samples);
1987 goto err2;
1990 audio_pcm_init_info (&hw->info, as);
1992 cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
1993 if (!cap->buf) {
1994 dolog ("Could not allocate capture buffer "
1995 "(%d samples, each %d bytes)\n",
1996 hw->samples, 1 << hw->info.shift);
1997 goto err3;
2000 hw->clip = mixeng_clip
2001 [hw->info.nchannels == 2]
2002 [hw->info.sign]
2003 [hw->info.swap_endianness]
2004 [audio_bits_to_index (hw->info.bits)];
2006 QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
2007 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
2009 hw = NULL;
2010 while ((hw = audio_pcm_hw_find_any_out (hw))) {
2011 audio_attach_capture (hw);
2013 return cap;
2015 err3:
2016 g_free (cap->hw.mix_buf);
2017 err2:
2018 g_free (cap);
2019 err1:
2020 g_free (cb);
2021 err0:
2022 return NULL;
2026 void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
2028 struct capture_callback *cb;
2030 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
2031 if (cb->opaque == cb_opaque) {
2032 cb->ops.destroy (cb_opaque);
2033 QLIST_REMOVE (cb, entries);
2034 g_free (cb);
2036 if (!cap->cb_head.lh_first) {
2037 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
2039 while (sw) {
2040 SWVoiceCap *sc = (SWVoiceCap *) sw;
2041 #ifdef DEBUG_CAPTURE
2042 dolog ("freeing %s\n", sw->name);
2043 #endif
2045 sw1 = sw->entries.le_next;
2046 if (sw->rate) {
2047 st_rate_stop (sw->rate);
2048 sw->rate = NULL;
2050 QLIST_REMOVE (sw, entries);
2051 QLIST_REMOVE (sc, entries);
2052 g_free (sc);
2053 sw = sw1;
2055 QLIST_REMOVE (cap, entries);
2056 g_free (cap);
2058 return;
2063 void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
2065 if (sw) {
2066 HWVoiceOut *hw = sw->hw;
2068 sw->vol.mute = mute;
2069 sw->vol.l = nominal_volume.l * lvol / 255;
2070 sw->vol.r = nominal_volume.r * rvol / 255;
2072 if (hw->pcm_ops->ctl_out) {
2073 hw->pcm_ops->ctl_out (hw, VOICE_VOLUME, sw);
2078 void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
2080 if (sw) {
2081 HWVoiceIn *hw = sw->hw;
2083 sw->vol.mute = mute;
2084 sw->vol.l = nominal_volume.l * lvol / 255;
2085 sw->vol.r = nominal_volume.r * rvol / 255;
2087 if (hw->pcm_ops->ctl_in) {
2088 hw->pcm_ops->ctl_in (hw, VOICE_VOLUME, sw);