pci-host/prep: Set isa_mem_base in the PCI host bridge
[qemu/ar7.git] / audio / audio.c
blob02bb8861f89ae4e7ba969e4a4b478f8cad28a99c
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/monitor.h"
27 #include "qemu/timer.h"
28 #include "sysemu/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 sw->name = g_strdup_printf ("for %p %d,%d,%d",
832 hw, sw->info.freq, sw->info.bits,
833 sw->info.nchannels);
834 dolog ("Added %s active = %d\n", sw->name, sw->active);
835 #endif
836 if (sw->active) {
837 audio_capture_maybe_changed (cap, 1);
840 return 0;
844 * Hard voice (capture)
846 static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
848 SWVoiceIn *sw;
849 int m = hw->total_samples_captured;
851 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
852 if (sw->active) {
853 m = audio_MIN (m, sw->total_hw_samples_acquired);
856 return m;
859 int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
861 int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
862 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
863 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
864 return 0;
866 return live;
869 int audio_pcm_hw_clip_out (HWVoiceOut *hw, void *pcm_buf,
870 int live, int pending)
872 int left = hw->samples - pending;
873 int len = audio_MIN (left, live);
874 int clipped = 0;
876 while (len) {
877 struct st_sample *src = hw->mix_buf + hw->rpos;
878 uint8_t *dst = advance (pcm_buf, hw->rpos << hw->info.shift);
879 int samples_till_end_of_buf = hw->samples - hw->rpos;
880 int samples_to_clip = audio_MIN (len, samples_till_end_of_buf);
882 hw->clip (dst, src, samples_to_clip);
884 hw->rpos = (hw->rpos + samples_to_clip) % hw->samples;
885 len -= samples_to_clip;
886 clipped += samples_to_clip;
888 return clipped;
892 * Soft voice (capture)
894 static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
896 HWVoiceIn *hw = sw->hw;
897 int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
898 int rpos;
900 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
901 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
902 return 0;
905 rpos = hw->wpos - live;
906 if (rpos >= 0) {
907 return rpos;
909 else {
910 return hw->samples + rpos;
914 int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
916 HWVoiceIn *hw = sw->hw;
917 int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
918 struct st_sample *src, *dst = sw->buf;
920 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
922 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
923 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
924 dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
925 return 0;
928 samples = size >> sw->info.shift;
929 if (!live) {
930 return 0;
933 swlim = (live * sw->ratio) >> 32;
934 swlim = audio_MIN (swlim, samples);
936 while (swlim) {
937 src = hw->conv_buf + rpos;
938 isamp = hw->wpos - rpos;
939 /* XXX: <= ? */
940 if (isamp <= 0) {
941 isamp = hw->samples - rpos;
944 if (!isamp) {
945 break;
947 osamp = swlim;
949 if (audio_bug (AUDIO_FUNC, osamp < 0)) {
950 dolog ("osamp=%d\n", osamp);
951 return 0;
954 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
955 swlim -= osamp;
956 rpos = (rpos + isamp) % hw->samples;
957 dst += osamp;
958 ret += osamp;
959 total += isamp;
962 if (!(hw->ctl_caps & VOICE_VOLUME_CAP)) {
963 mixeng_volume (sw->buf, ret, &sw->vol);
966 sw->clip (buf, sw->buf, ret);
967 sw->total_hw_samples_acquired += total;
968 return ret << sw->info.shift;
972 * Hard voice (playback)
974 static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
976 SWVoiceOut *sw;
977 int m = INT_MAX;
978 int nb_live = 0;
980 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
981 if (sw->active || !sw->empty) {
982 m = audio_MIN (m, sw->total_hw_samples_mixed);
983 nb_live += 1;
987 *nb_livep = nb_live;
988 return m;
991 static int audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
993 int smin;
994 int nb_live1;
996 smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
997 if (nb_live) {
998 *nb_live = nb_live1;
1001 if (nb_live1) {
1002 int live = smin;
1004 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1005 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1006 return 0;
1008 return live;
1010 return 0;
1014 * Soft voice (playback)
1016 int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
1018 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
1019 int ret = 0, pos = 0, total = 0;
1021 if (!sw) {
1022 return size;
1025 hwsamples = sw->hw->samples;
1027 live = sw->total_hw_samples_mixed;
1028 if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
1029 dolog ("live=%d hw->samples=%d\n", live, hwsamples);
1030 return 0;
1033 if (live == hwsamples) {
1034 #ifdef DEBUG_OUT
1035 dolog ("%s is full %d\n", sw->name, live);
1036 #endif
1037 return 0;
1040 wpos = (sw->hw->rpos + live) % hwsamples;
1041 samples = size >> sw->info.shift;
1043 dead = hwsamples - live;
1044 swlim = ((int64_t) dead << 32) / sw->ratio;
1045 swlim = audio_MIN (swlim, samples);
1046 if (swlim) {
1047 sw->conv (sw->buf, buf, swlim);
1049 if (!(sw->hw->ctl_caps & VOICE_VOLUME_CAP)) {
1050 mixeng_volume (sw->buf, swlim, &sw->vol);
1054 while (swlim) {
1055 dead = hwsamples - live;
1056 left = hwsamples - wpos;
1057 blck = audio_MIN (dead, left);
1058 if (!blck) {
1059 break;
1061 isamp = swlim;
1062 osamp = blck;
1063 st_rate_flow_mix (
1064 sw->rate,
1065 sw->buf + pos,
1066 sw->hw->mix_buf + wpos,
1067 &isamp,
1068 &osamp
1070 ret += isamp;
1071 swlim -= isamp;
1072 pos += isamp;
1073 live += osamp;
1074 wpos = (wpos + osamp) % hwsamples;
1075 total += osamp;
1078 sw->total_hw_samples_mixed += total;
1079 sw->empty = sw->total_hw_samples_mixed == 0;
1081 #ifdef DEBUG_OUT
1082 dolog (
1083 "%s: write size %d ret %d total sw %d\n",
1084 SW_NAME (sw),
1085 size >> sw->info.shift,
1086 ret,
1087 sw->total_hw_samples_mixed
1089 #endif
1091 return ret << sw->info.shift;
1094 #ifdef DEBUG_AUDIO
1095 static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
1097 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1098 cap, info->bits, info->sign, info->freq, info->nchannels);
1100 #endif
1102 #define DAC
1103 #include "audio_template.h"
1104 #undef DAC
1105 #include "audio_template.h"
1108 * Timer
1110 static int audio_is_timer_needed (void)
1112 HWVoiceIn *hwi = NULL;
1113 HWVoiceOut *hwo = NULL;
1115 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1116 if (!hwo->poll_mode) return 1;
1118 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1119 if (!hwi->poll_mode) return 1;
1121 return 0;
1124 static void audio_reset_timer (AudioState *s)
1126 if (audio_is_timer_needed ()) {
1127 qemu_mod_timer (s->ts, qemu_get_clock_ns (vm_clock) + 1);
1129 else {
1130 qemu_del_timer (s->ts);
1134 static void audio_timer (void *opaque)
1136 audio_run ("timer");
1137 audio_reset_timer (opaque);
1141 * Public API
1143 int AUD_write (SWVoiceOut *sw, void *buf, int size)
1145 int bytes;
1147 if (!sw) {
1148 /* XXX: Consider options */
1149 return size;
1152 if (!sw->hw->enabled) {
1153 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
1154 return 0;
1157 bytes = sw->hw->pcm_ops->write (sw, buf, size);
1158 return bytes;
1161 int AUD_read (SWVoiceIn *sw, void *buf, int size)
1163 int bytes;
1165 if (!sw) {
1166 /* XXX: Consider options */
1167 return size;
1170 if (!sw->hw->enabled) {
1171 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1172 return 0;
1175 bytes = sw->hw->pcm_ops->read (sw, buf, size);
1176 return bytes;
1179 int AUD_get_buffer_size_out (SWVoiceOut *sw)
1181 return sw->hw->samples << sw->hw->info.shift;
1184 void AUD_set_active_out (SWVoiceOut *sw, int on)
1186 HWVoiceOut *hw;
1188 if (!sw) {
1189 return;
1192 hw = sw->hw;
1193 if (sw->active != on) {
1194 AudioState *s = &glob_audio_state;
1195 SWVoiceOut *temp_sw;
1196 SWVoiceCap *sc;
1198 if (on) {
1199 hw->pending_disable = 0;
1200 if (!hw->enabled) {
1201 hw->enabled = 1;
1202 if (s->vm_running) {
1203 hw->pcm_ops->ctl_out (hw, VOICE_ENABLE, conf.try_poll_out);
1204 audio_reset_timer (s);
1208 else {
1209 if (hw->enabled) {
1210 int nb_active = 0;
1212 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1213 temp_sw = temp_sw->entries.le_next) {
1214 nb_active += temp_sw->active != 0;
1217 hw->pending_disable = nb_active == 1;
1221 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1222 sc->sw.active = hw->enabled;
1223 if (hw->enabled) {
1224 audio_capture_maybe_changed (sc->cap, 1);
1227 sw->active = on;
1231 void AUD_set_active_in (SWVoiceIn *sw, int on)
1233 HWVoiceIn *hw;
1235 if (!sw) {
1236 return;
1239 hw = sw->hw;
1240 if (sw->active != on) {
1241 AudioState *s = &glob_audio_state;
1242 SWVoiceIn *temp_sw;
1244 if (on) {
1245 if (!hw->enabled) {
1246 hw->enabled = 1;
1247 if (s->vm_running) {
1248 hw->pcm_ops->ctl_in (hw, VOICE_ENABLE, conf.try_poll_in);
1249 audio_reset_timer (s);
1252 sw->total_hw_samples_acquired = hw->total_samples_captured;
1254 else {
1255 if (hw->enabled) {
1256 int nb_active = 0;
1258 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1259 temp_sw = temp_sw->entries.le_next) {
1260 nb_active += temp_sw->active != 0;
1263 if (nb_active == 1) {
1264 hw->enabled = 0;
1265 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
1269 sw->active = on;
1273 static int audio_get_avail (SWVoiceIn *sw)
1275 int live;
1277 if (!sw) {
1278 return 0;
1281 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1282 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1283 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1284 return 0;
1287 ldebug (
1288 "%s: get_avail live %d ret %" PRId64 "\n",
1289 SW_NAME (sw),
1290 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1293 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1296 static int audio_get_free (SWVoiceOut *sw)
1298 int live, dead;
1300 if (!sw) {
1301 return 0;
1304 live = sw->total_hw_samples_mixed;
1306 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1307 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1308 return 0;
1311 dead = sw->hw->samples - live;
1313 #ifdef DEBUG_OUT
1314 dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
1315 SW_NAME (sw),
1316 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1317 #endif
1319 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1322 static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1324 int n;
1326 if (hw->enabled) {
1327 SWVoiceCap *sc;
1329 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1330 SWVoiceOut *sw = &sc->sw;
1331 int rpos2 = rpos;
1333 n = samples;
1334 while (n) {
1335 int till_end_of_hw = hw->samples - rpos2;
1336 int to_write = audio_MIN (till_end_of_hw, n);
1337 int bytes = to_write << hw->info.shift;
1338 int written;
1340 sw->buf = hw->mix_buf + rpos2;
1341 written = audio_pcm_sw_write (sw, NULL, bytes);
1342 if (written - bytes) {
1343 dolog ("Could not mix %d bytes into a capture "
1344 "buffer, mixed %d\n",
1345 bytes, written);
1346 break;
1348 n -= to_write;
1349 rpos2 = (rpos2 + to_write) % hw->samples;
1354 n = audio_MIN (samples, hw->samples - rpos);
1355 mixeng_clear (hw->mix_buf + rpos, n);
1356 mixeng_clear (hw->mix_buf, samples - n);
1359 static void audio_run_out (AudioState *s)
1361 HWVoiceOut *hw = NULL;
1362 SWVoiceOut *sw;
1364 while ((hw = audio_pcm_hw_find_any_enabled_out (hw))) {
1365 int played;
1366 int live, free, nb_live, cleanup_required, prev_rpos;
1368 live = audio_pcm_hw_get_live_out (hw, &nb_live);
1369 if (!nb_live) {
1370 live = 0;
1373 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1374 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1375 continue;
1378 if (hw->pending_disable && !nb_live) {
1379 SWVoiceCap *sc;
1380 #ifdef DEBUG_OUT
1381 dolog ("Disabling voice\n");
1382 #endif
1383 hw->enabled = 0;
1384 hw->pending_disable = 0;
1385 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
1386 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1387 sc->sw.active = 0;
1388 audio_recalc_and_notify_capture (sc->cap);
1390 continue;
1393 if (!live) {
1394 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1395 if (sw->active) {
1396 free = audio_get_free (sw);
1397 if (free > 0) {
1398 sw->callback.fn (sw->callback.opaque, free);
1402 continue;
1405 prev_rpos = hw->rpos;
1406 played = hw->pcm_ops->run_out (hw, live);
1407 if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1408 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1409 hw->rpos, hw->samples, played);
1410 hw->rpos = 0;
1413 #ifdef DEBUG_OUT
1414 dolog ("played=%d\n", played);
1415 #endif
1417 if (played) {
1418 hw->ts_helper += played;
1419 audio_capture_mix_and_clear (hw, prev_rpos, played);
1422 cleanup_required = 0;
1423 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1424 if (!sw->active && sw->empty) {
1425 continue;
1428 if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1429 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1430 played, sw->total_hw_samples_mixed);
1431 played = sw->total_hw_samples_mixed;
1434 sw->total_hw_samples_mixed -= played;
1436 if (!sw->total_hw_samples_mixed) {
1437 sw->empty = 1;
1438 cleanup_required |= !sw->active && !sw->callback.fn;
1441 if (sw->active) {
1442 free = audio_get_free (sw);
1443 if (free > 0) {
1444 sw->callback.fn (sw->callback.opaque, free);
1449 if (cleanup_required) {
1450 SWVoiceOut *sw1;
1452 sw = hw->sw_head.lh_first;
1453 while (sw) {
1454 sw1 = sw->entries.le_next;
1455 if (!sw->active && !sw->callback.fn) {
1456 #ifdef DEBUG_PLIVE
1457 dolog ("Finishing with old voice\n");
1458 #endif
1459 audio_close_out (sw);
1461 sw = sw1;
1467 static void audio_run_in (AudioState *s)
1469 HWVoiceIn *hw = NULL;
1471 while ((hw = audio_pcm_hw_find_any_enabled_in (hw))) {
1472 SWVoiceIn *sw;
1473 int captured, min;
1475 captured = hw->pcm_ops->run_in (hw);
1477 min = audio_pcm_hw_find_min_in (hw);
1478 hw->total_samples_captured += captured - min;
1479 hw->ts_helper += captured;
1481 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1482 sw->total_hw_samples_acquired -= min;
1484 if (sw->active) {
1485 int avail;
1487 avail = audio_get_avail (sw);
1488 if (avail > 0) {
1489 sw->callback.fn (sw->callback.opaque, avail);
1496 static void audio_run_capture (AudioState *s)
1498 CaptureVoiceOut *cap;
1500 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1501 int live, rpos, captured;
1502 HWVoiceOut *hw = &cap->hw;
1503 SWVoiceOut *sw;
1505 captured = live = audio_pcm_hw_get_live_out (hw, NULL);
1506 rpos = hw->rpos;
1507 while (live) {
1508 int left = hw->samples - rpos;
1509 int to_capture = audio_MIN (live, left);
1510 struct st_sample *src;
1511 struct capture_callback *cb;
1513 src = hw->mix_buf + rpos;
1514 hw->clip (cap->buf, src, to_capture);
1515 mixeng_clear (src, to_capture);
1517 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1518 cb->ops.capture (cb->opaque, cap->buf,
1519 to_capture << hw->info.shift);
1521 rpos = (rpos + to_capture) % hw->samples;
1522 live -= to_capture;
1524 hw->rpos = rpos;
1526 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1527 if (!sw->active && sw->empty) {
1528 continue;
1531 if (audio_bug (AUDIO_FUNC, captured > sw->total_hw_samples_mixed)) {
1532 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1533 captured, sw->total_hw_samples_mixed);
1534 captured = sw->total_hw_samples_mixed;
1537 sw->total_hw_samples_mixed -= captured;
1538 sw->empty = sw->total_hw_samples_mixed == 0;
1543 void audio_run (const char *msg)
1545 AudioState *s = &glob_audio_state;
1547 audio_run_out (s);
1548 audio_run_in (s);
1549 audio_run_capture (s);
1550 #ifdef DEBUG_POLL
1552 static double prevtime;
1553 double currtime;
1554 struct timeval tv;
1556 if (gettimeofday (&tv, NULL)) {
1557 perror ("audio_run: gettimeofday");
1558 return;
1561 currtime = tv.tv_sec + tv.tv_usec * 1e-6;
1562 dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime);
1563 prevtime = currtime;
1565 #endif
1568 static struct audio_option audio_options[] = {
1569 /* DAC */
1571 .name = "DAC_FIXED_SETTINGS",
1572 .tag = AUD_OPT_BOOL,
1573 .valp = &conf.fixed_out.enabled,
1574 .descr = "Use fixed settings for host DAC"
1577 .name = "DAC_FIXED_FREQ",
1578 .tag = AUD_OPT_INT,
1579 .valp = &conf.fixed_out.settings.freq,
1580 .descr = "Frequency for fixed host DAC"
1583 .name = "DAC_FIXED_FMT",
1584 .tag = AUD_OPT_FMT,
1585 .valp = &conf.fixed_out.settings.fmt,
1586 .descr = "Format for fixed host DAC"
1589 .name = "DAC_FIXED_CHANNELS",
1590 .tag = AUD_OPT_INT,
1591 .valp = &conf.fixed_out.settings.nchannels,
1592 .descr = "Number of channels for fixed DAC (1 - mono, 2 - stereo)"
1595 .name = "DAC_VOICES",
1596 .tag = AUD_OPT_INT,
1597 .valp = &conf.fixed_out.nb_voices,
1598 .descr = "Number of voices for DAC"
1601 .name = "DAC_TRY_POLL",
1602 .tag = AUD_OPT_BOOL,
1603 .valp = &conf.try_poll_out,
1604 .descr = "Attempt using poll mode for DAC"
1606 /* ADC */
1608 .name = "ADC_FIXED_SETTINGS",
1609 .tag = AUD_OPT_BOOL,
1610 .valp = &conf.fixed_in.enabled,
1611 .descr = "Use fixed settings for host ADC"
1614 .name = "ADC_FIXED_FREQ",
1615 .tag = AUD_OPT_INT,
1616 .valp = &conf.fixed_in.settings.freq,
1617 .descr = "Frequency for fixed host ADC"
1620 .name = "ADC_FIXED_FMT",
1621 .tag = AUD_OPT_FMT,
1622 .valp = &conf.fixed_in.settings.fmt,
1623 .descr = "Format for fixed host ADC"
1626 .name = "ADC_FIXED_CHANNELS",
1627 .tag = AUD_OPT_INT,
1628 .valp = &conf.fixed_in.settings.nchannels,
1629 .descr = "Number of channels for fixed ADC (1 - mono, 2 - stereo)"
1632 .name = "ADC_VOICES",
1633 .tag = AUD_OPT_INT,
1634 .valp = &conf.fixed_in.nb_voices,
1635 .descr = "Number of voices for ADC"
1638 .name = "ADC_TRY_POLL",
1639 .tag = AUD_OPT_BOOL,
1640 .valp = &conf.try_poll_in,
1641 .descr = "Attempt using poll mode for ADC"
1643 /* Misc */
1645 .name = "TIMER_PERIOD",
1646 .tag = AUD_OPT_INT,
1647 .valp = &conf.period.hertz,
1648 .descr = "Timer period in HZ (0 - use lowest possible)"
1651 .name = "PLIVE",
1652 .tag = AUD_OPT_BOOL,
1653 .valp = &conf.plive,
1654 .descr = "(undocumented)"
1657 .name = "LOG_TO_MONITOR",
1658 .tag = AUD_OPT_BOOL,
1659 .valp = &conf.log_to_monitor,
1660 .descr = "Print logging messages to monitor instead of stderr"
1662 { /* End of list */ }
1665 static void audio_pp_nb_voices (const char *typ, int nb)
1667 switch (nb) {
1668 case 0:
1669 printf ("Does not support %s\n", typ);
1670 break;
1671 case 1:
1672 printf ("One %s voice\n", typ);
1673 break;
1674 case INT_MAX:
1675 printf ("Theoretically supports many %s voices\n", typ);
1676 break;
1677 default:
1678 printf ("Theoretically supports up to %d %s voices\n", nb, typ);
1679 break;
1684 void AUD_help (void)
1686 size_t i;
1688 audio_process_options ("AUDIO", audio_options);
1689 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1690 struct audio_driver *d = drvtab[i];
1691 if (d->options) {
1692 audio_process_options (d->name, d->options);
1696 printf ("Audio options:\n");
1697 audio_print_options ("AUDIO", audio_options);
1698 printf ("\n");
1700 printf ("Available drivers:\n");
1702 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1703 struct audio_driver *d = drvtab[i];
1705 printf ("Name: %s\n", d->name);
1706 printf ("Description: %s\n", d->descr);
1708 audio_pp_nb_voices ("playback", d->max_voices_out);
1709 audio_pp_nb_voices ("capture", d->max_voices_in);
1711 if (d->options) {
1712 printf ("Options:\n");
1713 audio_print_options (d->name, d->options);
1715 else {
1716 printf ("No options\n");
1718 printf ("\n");
1721 printf (
1722 "Options are settable through environment variables.\n"
1723 "Example:\n"
1724 #ifdef _WIN32
1725 " set QEMU_AUDIO_DRV=wav\n"
1726 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1727 #else
1728 " export QEMU_AUDIO_DRV=wav\n"
1729 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1730 "(for csh replace export with setenv in the above)\n"
1731 #endif
1732 " qemu ...\n\n"
1736 static int audio_driver_init (AudioState *s, struct audio_driver *drv)
1738 if (drv->options) {
1739 audio_process_options (drv->name, drv->options);
1741 s->drv_opaque = drv->init ();
1743 if (s->drv_opaque) {
1744 audio_init_nb_voices_out (drv);
1745 audio_init_nb_voices_in (drv);
1746 s->drv = drv;
1747 return 0;
1749 else {
1750 dolog ("Could not init `%s' audio driver\n", drv->name);
1751 return -1;
1755 static void audio_vm_change_state_handler (void *opaque, int running,
1756 RunState state)
1758 AudioState *s = opaque;
1759 HWVoiceOut *hwo = NULL;
1760 HWVoiceIn *hwi = NULL;
1761 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1763 s->vm_running = running;
1764 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1765 hwo->pcm_ops->ctl_out (hwo, op, conf.try_poll_out);
1768 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1769 hwi->pcm_ops->ctl_in (hwi, op, conf.try_poll_in);
1771 audio_reset_timer (s);
1774 static void audio_atexit (void)
1776 AudioState *s = &glob_audio_state;
1777 HWVoiceOut *hwo = NULL;
1778 HWVoiceIn *hwi = NULL;
1780 while ((hwo = audio_pcm_hw_find_any_out (hwo))) {
1781 SWVoiceCap *sc;
1783 if (hwo->enabled) {
1784 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1786 hwo->pcm_ops->fini_out (hwo);
1788 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1789 CaptureVoiceOut *cap = sc->cap;
1790 struct capture_callback *cb;
1792 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1793 cb->ops.destroy (cb->opaque);
1798 while ((hwi = audio_pcm_hw_find_any_in (hwi))) {
1799 if (hwi->enabled) {
1800 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1802 hwi->pcm_ops->fini_in (hwi);
1805 if (s->drv) {
1806 s->drv->fini (s->drv_opaque);
1810 static const VMStateDescription vmstate_audio = {
1811 .name = "audio",
1812 .version_id = 1,
1813 .minimum_version_id = 1,
1814 .minimum_version_id_old = 1,
1815 .fields = (VMStateField []) {
1816 VMSTATE_END_OF_LIST()
1820 static void audio_init (void)
1822 size_t i;
1823 int done = 0;
1824 const char *drvname;
1825 VMChangeStateEntry *e;
1826 AudioState *s = &glob_audio_state;
1828 if (s->drv) {
1829 return;
1832 QLIST_INIT (&s->hw_head_out);
1833 QLIST_INIT (&s->hw_head_in);
1834 QLIST_INIT (&s->cap_head);
1835 atexit (audio_atexit);
1837 s->ts = qemu_new_timer_ns (vm_clock, audio_timer, s);
1838 if (!s->ts) {
1839 hw_error("Could not create audio timer\n");
1842 audio_process_options ("AUDIO", audio_options);
1844 s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1845 s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1847 if (s->nb_hw_voices_out <= 0) {
1848 dolog ("Bogus number of playback voices %d, setting to 1\n",
1849 s->nb_hw_voices_out);
1850 s->nb_hw_voices_out = 1;
1853 if (s->nb_hw_voices_in <= 0) {
1854 dolog ("Bogus number of capture voices %d, setting to 0\n",
1855 s->nb_hw_voices_in);
1856 s->nb_hw_voices_in = 0;
1860 int def;
1861 drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1864 if (drvname) {
1865 int found = 0;
1867 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1868 if (!strcmp (drvname, drvtab[i]->name)) {
1869 done = !audio_driver_init (s, drvtab[i]);
1870 found = 1;
1871 break;
1875 if (!found) {
1876 dolog ("Unknown audio driver `%s'\n", drvname);
1877 dolog ("Run with -audio-help to list available drivers\n");
1881 if (!done) {
1882 for (i = 0; !done && i < ARRAY_SIZE (drvtab); i++) {
1883 if (drvtab[i]->can_be_default) {
1884 done = !audio_driver_init (s, drvtab[i]);
1889 if (!done) {
1890 done = !audio_driver_init (s, &no_audio_driver);
1891 if (!done) {
1892 hw_error("Could not initialize audio subsystem\n");
1894 else {
1895 dolog ("warning: Using timer based audio emulation\n");
1899 if (conf.period.hertz <= 0) {
1900 if (conf.period.hertz < 0) {
1901 dolog ("warning: Timer period is negative - %d "
1902 "treating as zero\n",
1903 conf.period.hertz);
1905 conf.period.ticks = 1;
1906 } else {
1907 conf.period.ticks =
1908 muldiv64 (1, get_ticks_per_sec (), conf.period.hertz);
1911 e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1912 if (!e) {
1913 dolog ("warning: Could not register change state handler\n"
1914 "(Audio can continue looping even after stopping the VM)\n");
1917 QLIST_INIT (&s->card_head);
1918 vmstate_register (NULL, 0, &vmstate_audio, s);
1921 void AUD_register_card (const char *name, QEMUSoundCard *card)
1923 audio_init ();
1924 card->name = g_strdup (name);
1925 memset (&card->entries, 0, sizeof (card->entries));
1926 QLIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries);
1929 void AUD_remove_card (QEMUSoundCard *card)
1931 QLIST_REMOVE (card, entries);
1932 g_free (card->name);
1936 CaptureVoiceOut *AUD_add_capture (
1937 struct audsettings *as,
1938 struct audio_capture_ops *ops,
1939 void *cb_opaque
1942 AudioState *s = &glob_audio_state;
1943 CaptureVoiceOut *cap;
1944 struct capture_callback *cb;
1946 if (audio_validate_settings (as)) {
1947 dolog ("Invalid settings were passed when trying to add capture\n");
1948 audio_print_settings (as);
1949 goto err0;
1952 cb = audio_calloc (AUDIO_FUNC, 1, sizeof (*cb));
1953 if (!cb) {
1954 dolog ("Could not allocate capture callback information, size %zu\n",
1955 sizeof (*cb));
1956 goto err0;
1958 cb->ops = *ops;
1959 cb->opaque = cb_opaque;
1961 cap = audio_pcm_capture_find_specific (as);
1962 if (cap) {
1963 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1964 return cap;
1966 else {
1967 HWVoiceOut *hw;
1968 CaptureVoiceOut *cap;
1970 cap = audio_calloc (AUDIO_FUNC, 1, sizeof (*cap));
1971 if (!cap) {
1972 dolog ("Could not allocate capture voice, size %zu\n",
1973 sizeof (*cap));
1974 goto err1;
1977 hw = &cap->hw;
1978 QLIST_INIT (&hw->sw_head);
1979 QLIST_INIT (&cap->cb_head);
1981 /* XXX find a more elegant way */
1982 hw->samples = 4096 * 4;
1983 hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples,
1984 sizeof (struct st_sample));
1985 if (!hw->mix_buf) {
1986 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1987 hw->samples);
1988 goto err2;
1991 audio_pcm_init_info (&hw->info, as);
1993 cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
1994 if (!cap->buf) {
1995 dolog ("Could not allocate capture buffer "
1996 "(%d samples, each %d bytes)\n",
1997 hw->samples, 1 << hw->info.shift);
1998 goto err3;
2001 hw->clip = mixeng_clip
2002 [hw->info.nchannels == 2]
2003 [hw->info.sign]
2004 [hw->info.swap_endianness]
2005 [audio_bits_to_index (hw->info.bits)];
2007 QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
2008 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
2010 hw = NULL;
2011 while ((hw = audio_pcm_hw_find_any_out (hw))) {
2012 audio_attach_capture (hw);
2014 return cap;
2016 err3:
2017 g_free (cap->hw.mix_buf);
2018 err2:
2019 g_free (cap);
2020 err1:
2021 g_free (cb);
2022 err0:
2023 return NULL;
2027 void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
2029 struct capture_callback *cb;
2031 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
2032 if (cb->opaque == cb_opaque) {
2033 cb->ops.destroy (cb_opaque);
2034 QLIST_REMOVE (cb, entries);
2035 g_free (cb);
2037 if (!cap->cb_head.lh_first) {
2038 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
2040 while (sw) {
2041 SWVoiceCap *sc = (SWVoiceCap *) sw;
2042 #ifdef DEBUG_CAPTURE
2043 dolog ("freeing %s\n", sw->name);
2044 #endif
2046 sw1 = sw->entries.le_next;
2047 if (sw->rate) {
2048 st_rate_stop (sw->rate);
2049 sw->rate = NULL;
2051 QLIST_REMOVE (sw, entries);
2052 QLIST_REMOVE (sc, entries);
2053 g_free (sc);
2054 sw = sw1;
2056 QLIST_REMOVE (cap, entries);
2057 g_free (cap);
2059 return;
2064 void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
2066 if (sw) {
2067 HWVoiceOut *hw = sw->hw;
2069 sw->vol.mute = mute;
2070 sw->vol.l = nominal_volume.l * lvol / 255;
2071 sw->vol.r = nominal_volume.r * rvol / 255;
2073 if (hw->pcm_ops->ctl_out) {
2074 hw->pcm_ops->ctl_out (hw, VOICE_VOLUME, sw);
2079 void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
2081 if (sw) {
2082 HWVoiceIn *hw = sw->hw;
2084 sw->vol.mute = mute;
2085 sw->vol.l = nominal_volume.l * lvol / 255;
2086 sw->vol.r = nominal_volume.r * rvol / 255;
2088 if (hw->pcm_ops->ctl_in) {
2089 hw->pcm_ops->ctl_in (hw, VOICE_VOLUME, sw);