target-i386: Rename XMM_[BWLSDQ] helpers to ZMM_*
[qemu/cris-port.git] / audio / audio.c
bloba0fc8b31ae7d2388c2139fc2104d762e3b1debee
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_LIVE */
34 /* #define DEBUG_OUT */
35 /* #define DEBUG_CAPTURE */
36 /* #define DEBUG_POLL */
38 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
41 /* Order of CONFIG_AUDIO_DRIVERS is import.
42 The 1st one is the one used by default, that is the reason
43 that we generate the list.
45 static struct audio_driver *drvtab[] = {
46 #ifdef CONFIG_SPICE
47 &spice_audio_driver,
48 #endif
49 CONFIG_AUDIO_DRIVERS
50 &no_audio_driver,
51 &wav_audio_driver
54 struct fixed_settings {
55 int enabled;
56 int nb_voices;
57 int greedy;
58 struct audsettings settings;
61 static struct {
62 struct fixed_settings fixed_out;
63 struct fixed_settings fixed_in;
64 union {
65 int hertz;
66 int64_t ticks;
67 } period;
68 int try_poll_in;
69 int try_poll_out;
70 } conf = {
71 .fixed_out = { /* DAC fixed settings */
72 .enabled = 1,
73 .nb_voices = 1,
74 .greedy = 1,
75 .settings = {
76 .freq = 44100,
77 .nchannels = 2,
78 .fmt = AUD_FMT_S16,
79 .endianness = AUDIO_HOST_ENDIANNESS,
83 .fixed_in = { /* ADC fixed settings */
84 .enabled = 1,
85 .nb_voices = 1,
86 .greedy = 1,
87 .settings = {
88 .freq = 44100,
89 .nchannels = 2,
90 .fmt = AUD_FMT_S16,
91 .endianness = AUDIO_HOST_ENDIANNESS,
95 .period = { .hertz = 100 },
96 .try_poll_in = 1,
97 .try_poll_out = 1,
100 static AudioState glob_audio_state;
102 const struct mixeng_volume nominal_volume = {
103 .mute = 0,
104 #ifdef FLOAT_MIXENG
105 .r = 1.0,
106 .l = 1.0,
107 #else
108 .r = 1ULL << 32,
109 .l = 1ULL << 32,
110 #endif
113 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
114 #error No its not
115 #else
116 static void audio_print_options (const char *prefix,
117 struct audio_option *opt);
119 int audio_bug (const char *funcname, int cond)
121 if (cond) {
122 static int shown;
124 AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
125 if (!shown) {
126 struct audio_driver *d;
128 shown = 1;
129 AUD_log (NULL, "Save all your work and restart without audio\n");
130 AUD_log (NULL, "Please send bug report to av1474@comtv.ru\n");
131 AUD_log (NULL, "I am sorry\n");
132 d = glob_audio_state.drv;
133 if (d) {
134 audio_print_options (d->name, d->options);
137 AUD_log (NULL, "Context:\n");
139 #if defined AUDIO_BREAKPOINT_ON_BUG
140 # if defined HOST_I386
141 # if defined __GNUC__
142 __asm__ ("int3");
143 # elif defined _MSC_VER
144 _asm _emit 0xcc;
145 # else
146 abort ();
147 # endif
148 # else
149 abort ();
150 # endif
151 #endif
154 return cond;
156 #endif
158 static inline int audio_bits_to_index (int bits)
160 switch (bits) {
161 case 8:
162 return 0;
164 case 16:
165 return 1;
167 case 32:
168 return 2;
170 default:
171 audio_bug ("bits_to_index", 1);
172 AUD_log (NULL, "invalid bits %d\n", bits);
173 return 0;
177 void *audio_calloc (const char *funcname, int nmemb, size_t size)
179 int cond;
180 size_t len;
182 len = nmemb * size;
183 cond = !nmemb || !size;
184 cond |= nmemb < 0;
185 cond |= len < size;
187 if (audio_bug ("audio_calloc", cond)) {
188 AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
189 funcname);
190 AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
191 return NULL;
194 return g_malloc0 (len);
197 static char *audio_alloc_prefix (const char *s)
199 const char qemu_prefix[] = "QEMU_";
200 size_t len, i;
201 char *r, *u;
203 if (!s) {
204 return NULL;
207 len = strlen (s);
208 r = g_malloc (len + sizeof (qemu_prefix));
210 u = r + sizeof (qemu_prefix) - 1;
212 pstrcpy (r, len + sizeof (qemu_prefix), qemu_prefix);
213 pstrcat (r, len + sizeof (qemu_prefix), s);
215 for (i = 0; i < len; ++i) {
216 u[i] = qemu_toupper(u[i]);
219 return r;
222 static const char *audio_audfmt_to_string (audfmt_e fmt)
224 switch (fmt) {
225 case AUD_FMT_U8:
226 return "U8";
228 case AUD_FMT_U16:
229 return "U16";
231 case AUD_FMT_S8:
232 return "S8";
234 case AUD_FMT_S16:
235 return "S16";
237 case AUD_FMT_U32:
238 return "U32";
240 case AUD_FMT_S32:
241 return "S32";
244 dolog ("Bogus audfmt %d returning S16\n", fmt);
245 return "S16";
248 static audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval,
249 int *defaultp)
251 if (!strcasecmp (s, "u8")) {
252 *defaultp = 0;
253 return AUD_FMT_U8;
255 else if (!strcasecmp (s, "u16")) {
256 *defaultp = 0;
257 return AUD_FMT_U16;
259 else if (!strcasecmp (s, "u32")) {
260 *defaultp = 0;
261 return AUD_FMT_U32;
263 else if (!strcasecmp (s, "s8")) {
264 *defaultp = 0;
265 return AUD_FMT_S8;
267 else if (!strcasecmp (s, "s16")) {
268 *defaultp = 0;
269 return AUD_FMT_S16;
271 else if (!strcasecmp (s, "s32")) {
272 *defaultp = 0;
273 return AUD_FMT_S32;
275 else {
276 dolog ("Bogus audio format `%s' using %s\n",
277 s, audio_audfmt_to_string (defval));
278 *defaultp = 1;
279 return defval;
283 static audfmt_e audio_get_conf_fmt (const char *envname,
284 audfmt_e defval,
285 int *defaultp)
287 const char *var = getenv (envname);
288 if (!var) {
289 *defaultp = 1;
290 return defval;
292 return audio_string_to_audfmt (var, defval, defaultp);
295 static int audio_get_conf_int (const char *key, int defval, int *defaultp)
297 int val;
298 char *strval;
300 strval = getenv (key);
301 if (strval) {
302 *defaultp = 0;
303 val = atoi (strval);
304 return val;
306 else {
307 *defaultp = 1;
308 return defval;
312 static const char *audio_get_conf_str (const char *key,
313 const char *defval,
314 int *defaultp)
316 const char *val = getenv (key);
317 if (!val) {
318 *defaultp = 1;
319 return defval;
321 else {
322 *defaultp = 0;
323 return val;
327 void AUD_vlog (const char *cap, const char *fmt, va_list ap)
329 if (cap) {
330 fprintf(stderr, "%s: ", cap);
333 vfprintf(stderr, fmt, ap);
336 void AUD_log (const char *cap, const char *fmt, ...)
338 va_list ap;
340 va_start (ap, fmt);
341 AUD_vlog (cap, fmt, ap);
342 va_end (ap);
345 static void audio_print_options (const char *prefix,
346 struct audio_option *opt)
348 char *uprefix;
350 if (!prefix) {
351 dolog ("No prefix specified\n");
352 return;
355 if (!opt) {
356 dolog ("No options\n");
357 return;
360 uprefix = audio_alloc_prefix (prefix);
362 for (; opt->name; opt++) {
363 const char *state = "default";
364 printf (" %s_%s: ", uprefix, opt->name);
366 if (opt->overriddenp && *opt->overriddenp) {
367 state = "current";
370 switch (opt->tag) {
371 case AUD_OPT_BOOL:
373 int *intp = opt->valp;
374 printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
376 break;
378 case AUD_OPT_INT:
380 int *intp = opt->valp;
381 printf ("integer, %s = %d\n", state, *intp);
383 break;
385 case AUD_OPT_FMT:
387 audfmt_e *fmtp = opt->valp;
388 printf (
389 "format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
390 state,
391 audio_audfmt_to_string (*fmtp)
394 break;
396 case AUD_OPT_STR:
398 const char **strp = opt->valp;
399 printf ("string, %s = %s\n",
400 state,
401 *strp ? *strp : "(not set)");
403 break;
405 default:
406 printf ("???\n");
407 dolog ("Bad value tag for option %s_%s %d\n",
408 uprefix, opt->name, opt->tag);
409 break;
411 printf (" %s\n", opt->descr);
414 g_free (uprefix);
417 static void audio_process_options (const char *prefix,
418 struct audio_option *opt)
420 char *optname;
421 const char qemu_prefix[] = "QEMU_";
422 size_t preflen, optlen;
424 if (audio_bug (AUDIO_FUNC, !prefix)) {
425 dolog ("prefix = NULL\n");
426 return;
429 if (audio_bug (AUDIO_FUNC, !opt)) {
430 dolog ("opt = NULL\n");
431 return;
434 preflen = strlen (prefix);
436 for (; opt->name; opt++) {
437 size_t len, i;
438 int def;
440 if (!opt->valp) {
441 dolog ("Option value pointer for `%s' is not set\n",
442 opt->name);
443 continue;
446 len = strlen (opt->name);
447 /* len of opt->name + len of prefix + size of qemu_prefix
448 * (includes trailing zero) + zero + underscore (on behalf of
449 * sizeof) */
450 optlen = len + preflen + sizeof (qemu_prefix) + 1;
451 optname = g_malloc (optlen);
453 pstrcpy (optname, optlen, qemu_prefix);
455 /* copy while upper-casing, including trailing zero */
456 for (i = 0; i <= preflen; ++i) {
457 optname[i + sizeof (qemu_prefix) - 1] = qemu_toupper(prefix[i]);
459 pstrcat (optname, optlen, "_");
460 pstrcat (optname, optlen, opt->name);
462 def = 1;
463 switch (opt->tag) {
464 case AUD_OPT_BOOL:
465 case AUD_OPT_INT:
467 int *intp = opt->valp;
468 *intp = audio_get_conf_int (optname, *intp, &def);
470 break;
472 case AUD_OPT_FMT:
474 audfmt_e *fmtp = opt->valp;
475 *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
477 break;
479 case AUD_OPT_STR:
481 const char **strp = opt->valp;
482 *strp = audio_get_conf_str (optname, *strp, &def);
484 break;
486 default:
487 dolog ("Bad value tag for option `%s' - %d\n",
488 optname, opt->tag);
489 break;
492 if (!opt->overriddenp) {
493 opt->overriddenp = &opt->overridden;
495 *opt->overriddenp = !def;
496 g_free (optname);
500 static void audio_print_settings (struct audsettings *as)
502 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
504 switch (as->fmt) {
505 case AUD_FMT_S8:
506 AUD_log (NULL, "S8");
507 break;
508 case AUD_FMT_U8:
509 AUD_log (NULL, "U8");
510 break;
511 case AUD_FMT_S16:
512 AUD_log (NULL, "S16");
513 break;
514 case AUD_FMT_U16:
515 AUD_log (NULL, "U16");
516 break;
517 case AUD_FMT_S32:
518 AUD_log (NULL, "S32");
519 break;
520 case AUD_FMT_U32:
521 AUD_log (NULL, "U32");
522 break;
523 default:
524 AUD_log (NULL, "invalid(%d)", as->fmt);
525 break;
528 AUD_log (NULL, " endianness=");
529 switch (as->endianness) {
530 case 0:
531 AUD_log (NULL, "little");
532 break;
533 case 1:
534 AUD_log (NULL, "big");
535 break;
536 default:
537 AUD_log (NULL, "invalid");
538 break;
540 AUD_log (NULL, "\n");
543 static int audio_validate_settings (struct audsettings *as)
545 int invalid;
547 invalid = as->nchannels != 1 && as->nchannels != 2;
548 invalid |= as->endianness != 0 && as->endianness != 1;
550 switch (as->fmt) {
551 case AUD_FMT_S8:
552 case AUD_FMT_U8:
553 case AUD_FMT_S16:
554 case AUD_FMT_U16:
555 case AUD_FMT_S32:
556 case AUD_FMT_U32:
557 break;
558 default:
559 invalid = 1;
560 break;
563 invalid |= as->freq <= 0;
564 return invalid ? -1 : 0;
567 static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
569 int bits = 8, sign = 0;
571 switch (as->fmt) {
572 case AUD_FMT_S8:
573 sign = 1;
574 /* fall through */
575 case AUD_FMT_U8:
576 break;
578 case AUD_FMT_S16:
579 sign = 1;
580 /* fall through */
581 case AUD_FMT_U16:
582 bits = 16;
583 break;
585 case AUD_FMT_S32:
586 sign = 1;
587 /* fall through */
588 case AUD_FMT_U32:
589 bits = 32;
590 break;
592 return info->freq == as->freq
593 && info->nchannels == as->nchannels
594 && info->sign == sign
595 && info->bits == bits
596 && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
599 void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
601 int bits = 8, sign = 0, shift = 0;
603 switch (as->fmt) {
604 case AUD_FMT_S8:
605 sign = 1;
606 case AUD_FMT_U8:
607 break;
609 case AUD_FMT_S16:
610 sign = 1;
611 case AUD_FMT_U16:
612 bits = 16;
613 shift = 1;
614 break;
616 case AUD_FMT_S32:
617 sign = 1;
618 case AUD_FMT_U32:
619 bits = 32;
620 shift = 2;
621 break;
624 info->freq = as->freq;
625 info->bits = bits;
626 info->sign = sign;
627 info->nchannels = as->nchannels;
628 info->shift = (as->nchannels == 2) + shift;
629 info->align = (1 << info->shift) - 1;
630 info->bytes_per_second = info->freq << info->shift;
631 info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
634 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
636 if (!len) {
637 return;
640 if (info->sign) {
641 memset (buf, 0x00, len << info->shift);
643 else {
644 switch (info->bits) {
645 case 8:
646 memset (buf, 0x80, len << info->shift);
647 break;
649 case 16:
651 int i;
652 uint16_t *p = buf;
653 int shift = info->nchannels - 1;
654 short s = INT16_MAX;
656 if (info->swap_endianness) {
657 s = bswap16 (s);
660 for (i = 0; i < len << shift; i++) {
661 p[i] = s;
664 break;
666 case 32:
668 int i;
669 uint32_t *p = buf;
670 int shift = info->nchannels - 1;
671 int32_t s = INT32_MAX;
673 if (info->swap_endianness) {
674 s = bswap32 (s);
677 for (i = 0; i < len << shift; i++) {
678 p[i] = s;
681 break;
683 default:
684 AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
685 info->bits);
686 break;
692 * Capture
694 static void noop_conv (struct st_sample *dst, const void *src, int samples)
696 (void) src;
697 (void) dst;
698 (void) samples;
701 static CaptureVoiceOut *audio_pcm_capture_find_specific (
702 struct audsettings *as
705 CaptureVoiceOut *cap;
706 AudioState *s = &glob_audio_state;
708 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
709 if (audio_pcm_info_eq (&cap->hw.info, as)) {
710 return cap;
713 return NULL;
716 static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
718 struct capture_callback *cb;
720 #ifdef DEBUG_CAPTURE
721 dolog ("notification %d sent\n", cmd);
722 #endif
723 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
724 cb->ops.notify (cb->opaque, cmd);
728 static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
730 if (cap->hw.enabled != enabled) {
731 audcnotification_e cmd;
732 cap->hw.enabled = enabled;
733 cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
734 audio_notify_capture (cap, cmd);
738 static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
740 HWVoiceOut *hw = &cap->hw;
741 SWVoiceOut *sw;
742 int enabled = 0;
744 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
745 if (sw->active) {
746 enabled = 1;
747 break;
750 audio_capture_maybe_changed (cap, enabled);
753 static void audio_detach_capture (HWVoiceOut *hw)
755 SWVoiceCap *sc = hw->cap_head.lh_first;
757 while (sc) {
758 SWVoiceCap *sc1 = sc->entries.le_next;
759 SWVoiceOut *sw = &sc->sw;
760 CaptureVoiceOut *cap = sc->cap;
761 int was_active = sw->active;
763 if (sw->rate) {
764 st_rate_stop (sw->rate);
765 sw->rate = NULL;
768 QLIST_REMOVE (sw, entries);
769 QLIST_REMOVE (sc, entries);
770 g_free (sc);
771 if (was_active) {
772 /* We have removed soft voice from the capture:
773 this might have changed the overall status of the capture
774 since this might have been the only active voice */
775 audio_recalc_and_notify_capture (cap);
777 sc = sc1;
781 static int audio_attach_capture (HWVoiceOut *hw)
783 AudioState *s = &glob_audio_state;
784 CaptureVoiceOut *cap;
786 audio_detach_capture (hw);
787 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
788 SWVoiceCap *sc;
789 SWVoiceOut *sw;
790 HWVoiceOut *hw_cap = &cap->hw;
792 sc = audio_calloc (AUDIO_FUNC, 1, sizeof (*sc));
793 if (!sc) {
794 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
795 sizeof (*sc));
796 return -1;
799 sc->cap = cap;
800 sw = &sc->sw;
801 sw->hw = hw_cap;
802 sw->info = hw->info;
803 sw->empty = 1;
804 sw->active = hw->enabled;
805 sw->conv = noop_conv;
806 sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
807 sw->vol = nominal_volume;
808 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
809 if (!sw->rate) {
810 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
811 g_free (sw);
812 return -1;
814 QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
815 QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
816 #ifdef DEBUG_CAPTURE
817 sw->name = g_strdup_printf ("for %p %d,%d,%d",
818 hw, sw->info.freq, sw->info.bits,
819 sw->info.nchannels);
820 dolog ("Added %s active = %d\n", sw->name, sw->active);
821 #endif
822 if (sw->active) {
823 audio_capture_maybe_changed (cap, 1);
826 return 0;
830 * Hard voice (capture)
832 static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
834 SWVoiceIn *sw;
835 int m = hw->total_samples_captured;
837 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
838 if (sw->active) {
839 m = audio_MIN (m, sw->total_hw_samples_acquired);
842 return m;
845 int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
847 int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
848 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
849 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
850 return 0;
852 return live;
855 int audio_pcm_hw_clip_out (HWVoiceOut *hw, void *pcm_buf,
856 int live, int pending)
858 int left = hw->samples - pending;
859 int len = audio_MIN (left, live);
860 int clipped = 0;
862 while (len) {
863 struct st_sample *src = hw->mix_buf + hw->rpos;
864 uint8_t *dst = advance (pcm_buf, hw->rpos << hw->info.shift);
865 int samples_till_end_of_buf = hw->samples - hw->rpos;
866 int samples_to_clip = audio_MIN (len, samples_till_end_of_buf);
868 hw->clip (dst, src, samples_to_clip);
870 hw->rpos = (hw->rpos + samples_to_clip) % hw->samples;
871 len -= samples_to_clip;
872 clipped += samples_to_clip;
874 return clipped;
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 struct st_sample *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 if (!(hw->ctl_caps & VOICE_VOLUME_CAP)) {
949 mixeng_volume (sw->buf, ret, &sw->vol);
952 sw->clip (buf, sw->buf, ret);
953 sw->total_hw_samples_acquired += total;
954 return ret << sw->info.shift;
958 * Hard voice (playback)
960 static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
962 SWVoiceOut *sw;
963 int m = INT_MAX;
964 int nb_live = 0;
966 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
967 if (sw->active || !sw->empty) {
968 m = audio_MIN (m, sw->total_hw_samples_mixed);
969 nb_live += 1;
973 *nb_livep = nb_live;
974 return m;
977 static int audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
979 int smin;
980 int nb_live1;
982 smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
983 if (nb_live) {
984 *nb_live = nb_live1;
987 if (nb_live1) {
988 int live = smin;
990 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
991 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
992 return 0;
994 return live;
996 return 0;
1000 * Soft voice (playback)
1002 int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
1004 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
1005 int ret = 0, pos = 0, total = 0;
1007 if (!sw) {
1008 return size;
1011 hwsamples = sw->hw->samples;
1013 live = sw->total_hw_samples_mixed;
1014 if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
1015 dolog ("live=%d hw->samples=%d\n", live, hwsamples);
1016 return 0;
1019 if (live == hwsamples) {
1020 #ifdef DEBUG_OUT
1021 dolog ("%s is full %d\n", sw->name, live);
1022 #endif
1023 return 0;
1026 wpos = (sw->hw->rpos + live) % hwsamples;
1027 samples = size >> sw->info.shift;
1029 dead = hwsamples - live;
1030 swlim = ((int64_t) dead << 32) / sw->ratio;
1031 swlim = audio_MIN (swlim, samples);
1032 if (swlim) {
1033 sw->conv (sw->buf, buf, swlim);
1035 if (!(sw->hw->ctl_caps & VOICE_VOLUME_CAP)) {
1036 mixeng_volume (sw->buf, swlim, &sw->vol);
1040 while (swlim) {
1041 dead = hwsamples - live;
1042 left = hwsamples - wpos;
1043 blck = audio_MIN (dead, left);
1044 if (!blck) {
1045 break;
1047 isamp = swlim;
1048 osamp = blck;
1049 st_rate_flow_mix (
1050 sw->rate,
1051 sw->buf + pos,
1052 sw->hw->mix_buf + wpos,
1053 &isamp,
1054 &osamp
1056 ret += isamp;
1057 swlim -= isamp;
1058 pos += isamp;
1059 live += osamp;
1060 wpos = (wpos + osamp) % hwsamples;
1061 total += osamp;
1064 sw->total_hw_samples_mixed += total;
1065 sw->empty = sw->total_hw_samples_mixed == 0;
1067 #ifdef DEBUG_OUT
1068 dolog (
1069 "%s: write size %d ret %d total sw %d\n",
1070 SW_NAME (sw),
1071 size >> sw->info.shift,
1072 ret,
1073 sw->total_hw_samples_mixed
1075 #endif
1077 return ret << sw->info.shift;
1080 #ifdef DEBUG_AUDIO
1081 static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
1083 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1084 cap, info->bits, info->sign, info->freq, info->nchannels);
1086 #endif
1088 #define DAC
1089 #include "audio_template.h"
1090 #undef DAC
1091 #include "audio_template.h"
1094 * Timer
1096 static int audio_is_timer_needed (void)
1098 HWVoiceIn *hwi = NULL;
1099 HWVoiceOut *hwo = NULL;
1101 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1102 if (!hwo->poll_mode) return 1;
1104 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1105 if (!hwi->poll_mode) return 1;
1107 return 0;
1110 static void audio_reset_timer (AudioState *s)
1112 if (audio_is_timer_needed ()) {
1113 timer_mod (s->ts,
1114 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + conf.period.ticks);
1116 else {
1117 timer_del (s->ts);
1121 static void audio_timer (void *opaque)
1123 audio_run ("timer");
1124 audio_reset_timer (opaque);
1128 * Public API
1130 int AUD_write (SWVoiceOut *sw, void *buf, int size)
1132 int bytes;
1134 if (!sw) {
1135 /* XXX: Consider options */
1136 return size;
1139 if (!sw->hw->enabled) {
1140 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
1141 return 0;
1144 bytes = sw->hw->pcm_ops->write (sw, buf, size);
1145 return bytes;
1148 int AUD_read (SWVoiceIn *sw, void *buf, int size)
1150 int bytes;
1152 if (!sw) {
1153 /* XXX: Consider options */
1154 return size;
1157 if (!sw->hw->enabled) {
1158 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1159 return 0;
1162 bytes = sw->hw->pcm_ops->read (sw, buf, size);
1163 return bytes;
1166 int AUD_get_buffer_size_out (SWVoiceOut *sw)
1168 return sw->hw->samples << sw->hw->info.shift;
1171 void AUD_set_active_out (SWVoiceOut *sw, int on)
1173 HWVoiceOut *hw;
1175 if (!sw) {
1176 return;
1179 hw = sw->hw;
1180 if (sw->active != on) {
1181 AudioState *s = &glob_audio_state;
1182 SWVoiceOut *temp_sw;
1183 SWVoiceCap *sc;
1185 if (on) {
1186 hw->pending_disable = 0;
1187 if (!hw->enabled) {
1188 hw->enabled = 1;
1189 if (s->vm_running) {
1190 hw->pcm_ops->ctl_out (hw, VOICE_ENABLE, conf.try_poll_out);
1191 audio_reset_timer (s);
1195 else {
1196 if (hw->enabled) {
1197 int nb_active = 0;
1199 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1200 temp_sw = temp_sw->entries.le_next) {
1201 nb_active += temp_sw->active != 0;
1204 hw->pending_disable = nb_active == 1;
1208 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1209 sc->sw.active = hw->enabled;
1210 if (hw->enabled) {
1211 audio_capture_maybe_changed (sc->cap, 1);
1214 sw->active = on;
1218 void AUD_set_active_in (SWVoiceIn *sw, int on)
1220 HWVoiceIn *hw;
1222 if (!sw) {
1223 return;
1226 hw = sw->hw;
1227 if (sw->active != on) {
1228 AudioState *s = &glob_audio_state;
1229 SWVoiceIn *temp_sw;
1231 if (on) {
1232 if (!hw->enabled) {
1233 hw->enabled = 1;
1234 if (s->vm_running) {
1235 hw->pcm_ops->ctl_in (hw, VOICE_ENABLE, conf.try_poll_in);
1236 audio_reset_timer (s);
1239 sw->total_hw_samples_acquired = hw->total_samples_captured;
1241 else {
1242 if (hw->enabled) {
1243 int nb_active = 0;
1245 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1246 temp_sw = temp_sw->entries.le_next) {
1247 nb_active += temp_sw->active != 0;
1250 if (nb_active == 1) {
1251 hw->enabled = 0;
1252 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
1256 sw->active = on;
1260 static int audio_get_avail (SWVoiceIn *sw)
1262 int live;
1264 if (!sw) {
1265 return 0;
1268 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1269 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1270 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1271 return 0;
1274 ldebug (
1275 "%s: get_avail live %d ret %" PRId64 "\n",
1276 SW_NAME (sw),
1277 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1280 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1283 static int audio_get_free (SWVoiceOut *sw)
1285 int live, dead;
1287 if (!sw) {
1288 return 0;
1291 live = sw->total_hw_samples_mixed;
1293 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1294 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1295 return 0;
1298 dead = sw->hw->samples - live;
1300 #ifdef DEBUG_OUT
1301 dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
1302 SW_NAME (sw),
1303 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1304 #endif
1306 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1309 static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1311 int n;
1313 if (hw->enabled) {
1314 SWVoiceCap *sc;
1316 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1317 SWVoiceOut *sw = &sc->sw;
1318 int rpos2 = rpos;
1320 n = samples;
1321 while (n) {
1322 int till_end_of_hw = hw->samples - rpos2;
1323 int to_write = audio_MIN (till_end_of_hw, n);
1324 int bytes = to_write << hw->info.shift;
1325 int written;
1327 sw->buf = hw->mix_buf + rpos2;
1328 written = audio_pcm_sw_write (sw, NULL, bytes);
1329 if (written - bytes) {
1330 dolog ("Could not mix %d bytes into a capture "
1331 "buffer, mixed %d\n",
1332 bytes, written);
1333 break;
1335 n -= to_write;
1336 rpos2 = (rpos2 + to_write) % hw->samples;
1341 n = audio_MIN (samples, hw->samples - rpos);
1342 mixeng_clear (hw->mix_buf + rpos, n);
1343 mixeng_clear (hw->mix_buf, samples - n);
1346 static void audio_run_out (AudioState *s)
1348 HWVoiceOut *hw = NULL;
1349 SWVoiceOut *sw;
1351 while ((hw = audio_pcm_hw_find_any_enabled_out (hw))) {
1352 int played;
1353 int live, free, nb_live, cleanup_required, prev_rpos;
1355 live = audio_pcm_hw_get_live_out (hw, &nb_live);
1356 if (!nb_live) {
1357 live = 0;
1360 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1361 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1362 continue;
1365 if (hw->pending_disable && !nb_live) {
1366 SWVoiceCap *sc;
1367 #ifdef DEBUG_OUT
1368 dolog ("Disabling voice\n");
1369 #endif
1370 hw->enabled = 0;
1371 hw->pending_disable = 0;
1372 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
1373 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1374 sc->sw.active = 0;
1375 audio_recalc_and_notify_capture (sc->cap);
1377 continue;
1380 if (!live) {
1381 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1382 if (sw->active) {
1383 free = audio_get_free (sw);
1384 if (free > 0) {
1385 sw->callback.fn (sw->callback.opaque, free);
1389 continue;
1392 prev_rpos = hw->rpos;
1393 played = hw->pcm_ops->run_out (hw, live);
1394 if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1395 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1396 hw->rpos, hw->samples, played);
1397 hw->rpos = 0;
1400 #ifdef DEBUG_OUT
1401 dolog ("played=%d\n", played);
1402 #endif
1404 if (played) {
1405 hw->ts_helper += played;
1406 audio_capture_mix_and_clear (hw, prev_rpos, played);
1409 cleanup_required = 0;
1410 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1411 if (!sw->active && sw->empty) {
1412 continue;
1415 if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1416 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1417 played, sw->total_hw_samples_mixed);
1418 played = sw->total_hw_samples_mixed;
1421 sw->total_hw_samples_mixed -= played;
1423 if (!sw->total_hw_samples_mixed) {
1424 sw->empty = 1;
1425 cleanup_required |= !sw->active && !sw->callback.fn;
1428 if (sw->active) {
1429 free = audio_get_free (sw);
1430 if (free > 0) {
1431 sw->callback.fn (sw->callback.opaque, free);
1436 if (cleanup_required) {
1437 SWVoiceOut *sw1;
1439 sw = hw->sw_head.lh_first;
1440 while (sw) {
1441 sw1 = sw->entries.le_next;
1442 if (!sw->active && !sw->callback.fn) {
1443 audio_close_out (sw);
1445 sw = sw1;
1451 static void audio_run_in (AudioState *s)
1453 HWVoiceIn *hw = NULL;
1455 while ((hw = audio_pcm_hw_find_any_enabled_in (hw))) {
1456 SWVoiceIn *sw;
1457 int captured, min;
1459 captured = hw->pcm_ops->run_in (hw);
1461 min = audio_pcm_hw_find_min_in (hw);
1462 hw->total_samples_captured += captured - min;
1463 hw->ts_helper += captured;
1465 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1466 sw->total_hw_samples_acquired -= min;
1468 if (sw->active) {
1469 int avail;
1471 avail = audio_get_avail (sw);
1472 if (avail > 0) {
1473 sw->callback.fn (sw->callback.opaque, avail);
1480 static void audio_run_capture (AudioState *s)
1482 CaptureVoiceOut *cap;
1484 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1485 int live, rpos, captured;
1486 HWVoiceOut *hw = &cap->hw;
1487 SWVoiceOut *sw;
1489 captured = live = audio_pcm_hw_get_live_out (hw, NULL);
1490 rpos = hw->rpos;
1491 while (live) {
1492 int left = hw->samples - rpos;
1493 int to_capture = audio_MIN (live, left);
1494 struct st_sample *src;
1495 struct capture_callback *cb;
1497 src = hw->mix_buf + rpos;
1498 hw->clip (cap->buf, src, to_capture);
1499 mixeng_clear (src, to_capture);
1501 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1502 cb->ops.capture (cb->opaque, cap->buf,
1503 to_capture << hw->info.shift);
1505 rpos = (rpos + to_capture) % hw->samples;
1506 live -= to_capture;
1508 hw->rpos = rpos;
1510 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1511 if (!sw->active && sw->empty) {
1512 continue;
1515 if (audio_bug (AUDIO_FUNC, captured > sw->total_hw_samples_mixed)) {
1516 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1517 captured, sw->total_hw_samples_mixed);
1518 captured = sw->total_hw_samples_mixed;
1521 sw->total_hw_samples_mixed -= captured;
1522 sw->empty = sw->total_hw_samples_mixed == 0;
1527 void audio_run (const char *msg)
1529 AudioState *s = &glob_audio_state;
1531 audio_run_out (s);
1532 audio_run_in (s);
1533 audio_run_capture (s);
1534 #ifdef DEBUG_POLL
1536 static double prevtime;
1537 double currtime;
1538 struct timeval tv;
1540 if (gettimeofday (&tv, NULL)) {
1541 perror ("audio_run: gettimeofday");
1542 return;
1545 currtime = tv.tv_sec + tv.tv_usec * 1e-6;
1546 dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime);
1547 prevtime = currtime;
1549 #endif
1552 static struct audio_option audio_options[] = {
1553 /* DAC */
1555 .name = "DAC_FIXED_SETTINGS",
1556 .tag = AUD_OPT_BOOL,
1557 .valp = &conf.fixed_out.enabled,
1558 .descr = "Use fixed settings for host DAC"
1561 .name = "DAC_FIXED_FREQ",
1562 .tag = AUD_OPT_INT,
1563 .valp = &conf.fixed_out.settings.freq,
1564 .descr = "Frequency for fixed host DAC"
1567 .name = "DAC_FIXED_FMT",
1568 .tag = AUD_OPT_FMT,
1569 .valp = &conf.fixed_out.settings.fmt,
1570 .descr = "Format for fixed host DAC"
1573 .name = "DAC_FIXED_CHANNELS",
1574 .tag = AUD_OPT_INT,
1575 .valp = &conf.fixed_out.settings.nchannels,
1576 .descr = "Number of channels for fixed DAC (1 - mono, 2 - stereo)"
1579 .name = "DAC_VOICES",
1580 .tag = AUD_OPT_INT,
1581 .valp = &conf.fixed_out.nb_voices,
1582 .descr = "Number of voices for DAC"
1585 .name = "DAC_TRY_POLL",
1586 .tag = AUD_OPT_BOOL,
1587 .valp = &conf.try_poll_out,
1588 .descr = "Attempt using poll mode for DAC"
1590 /* ADC */
1592 .name = "ADC_FIXED_SETTINGS",
1593 .tag = AUD_OPT_BOOL,
1594 .valp = &conf.fixed_in.enabled,
1595 .descr = "Use fixed settings for host ADC"
1598 .name = "ADC_FIXED_FREQ",
1599 .tag = AUD_OPT_INT,
1600 .valp = &conf.fixed_in.settings.freq,
1601 .descr = "Frequency for fixed host ADC"
1604 .name = "ADC_FIXED_FMT",
1605 .tag = AUD_OPT_FMT,
1606 .valp = &conf.fixed_in.settings.fmt,
1607 .descr = "Format for fixed host ADC"
1610 .name = "ADC_FIXED_CHANNELS",
1611 .tag = AUD_OPT_INT,
1612 .valp = &conf.fixed_in.settings.nchannels,
1613 .descr = "Number of channels for fixed ADC (1 - mono, 2 - stereo)"
1616 .name = "ADC_VOICES",
1617 .tag = AUD_OPT_INT,
1618 .valp = &conf.fixed_in.nb_voices,
1619 .descr = "Number of voices for ADC"
1622 .name = "ADC_TRY_POLL",
1623 .tag = AUD_OPT_BOOL,
1624 .valp = &conf.try_poll_in,
1625 .descr = "Attempt using poll mode for ADC"
1627 /* Misc */
1629 .name = "TIMER_PERIOD",
1630 .tag = AUD_OPT_INT,
1631 .valp = &conf.period.hertz,
1632 .descr = "Timer period in HZ (0 - use lowest possible)"
1634 { /* End of list */ }
1637 static void audio_pp_nb_voices (const char *typ, int nb)
1639 switch (nb) {
1640 case 0:
1641 printf ("Does not support %s\n", typ);
1642 break;
1643 case 1:
1644 printf ("One %s voice\n", typ);
1645 break;
1646 case INT_MAX:
1647 printf ("Theoretically supports many %s voices\n", typ);
1648 break;
1649 default:
1650 printf ("Theoretically supports up to %d %s voices\n", nb, typ);
1651 break;
1656 void AUD_help (void)
1658 size_t i;
1660 audio_process_options ("AUDIO", audio_options);
1661 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1662 struct audio_driver *d = drvtab[i];
1663 if (d->options) {
1664 audio_process_options (d->name, d->options);
1668 printf ("Audio options:\n");
1669 audio_print_options ("AUDIO", audio_options);
1670 printf ("\n");
1672 printf ("Available drivers:\n");
1674 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1675 struct audio_driver *d = drvtab[i];
1677 printf ("Name: %s\n", d->name);
1678 printf ("Description: %s\n", d->descr);
1680 audio_pp_nb_voices ("playback", d->max_voices_out);
1681 audio_pp_nb_voices ("capture", d->max_voices_in);
1683 if (d->options) {
1684 printf ("Options:\n");
1685 audio_print_options (d->name, d->options);
1687 else {
1688 printf ("No options\n");
1690 printf ("\n");
1693 printf (
1694 "Options are settable through environment variables.\n"
1695 "Example:\n"
1696 #ifdef _WIN32
1697 " set QEMU_AUDIO_DRV=wav\n"
1698 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1699 #else
1700 " export QEMU_AUDIO_DRV=wav\n"
1701 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1702 "(for csh replace export with setenv in the above)\n"
1703 #endif
1704 " qemu ...\n\n"
1708 static int audio_driver_init (AudioState *s, struct audio_driver *drv)
1710 if (drv->options) {
1711 audio_process_options (drv->name, drv->options);
1713 s->drv_opaque = drv->init ();
1715 if (s->drv_opaque) {
1716 audio_init_nb_voices_out (drv);
1717 audio_init_nb_voices_in (drv);
1718 s->drv = drv;
1719 return 0;
1721 else {
1722 dolog ("Could not init `%s' audio driver\n", drv->name);
1723 return -1;
1727 static void audio_vm_change_state_handler (void *opaque, int running,
1728 RunState state)
1730 AudioState *s = opaque;
1731 HWVoiceOut *hwo = NULL;
1732 HWVoiceIn *hwi = NULL;
1733 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1735 s->vm_running = running;
1736 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1737 hwo->pcm_ops->ctl_out (hwo, op, conf.try_poll_out);
1740 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1741 hwi->pcm_ops->ctl_in (hwi, op, conf.try_poll_in);
1743 audio_reset_timer (s);
1746 static void audio_atexit (void)
1748 AudioState *s = &glob_audio_state;
1749 HWVoiceOut *hwo = NULL;
1750 HWVoiceIn *hwi = NULL;
1752 while ((hwo = audio_pcm_hw_find_any_out (hwo))) {
1753 SWVoiceCap *sc;
1755 if (hwo->enabled) {
1756 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1758 hwo->pcm_ops->fini_out (hwo);
1760 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1761 CaptureVoiceOut *cap = sc->cap;
1762 struct capture_callback *cb;
1764 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1765 cb->ops.destroy (cb->opaque);
1770 while ((hwi = audio_pcm_hw_find_any_in (hwi))) {
1771 if (hwi->enabled) {
1772 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1774 hwi->pcm_ops->fini_in (hwi);
1777 if (s->drv) {
1778 s->drv->fini (s->drv_opaque);
1782 static const VMStateDescription vmstate_audio = {
1783 .name = "audio",
1784 .version_id = 1,
1785 .minimum_version_id = 1,
1786 .fields = (VMStateField[]) {
1787 VMSTATE_END_OF_LIST()
1791 static void audio_init (void)
1793 size_t i;
1794 int done = 0;
1795 const char *drvname;
1796 VMChangeStateEntry *e;
1797 AudioState *s = &glob_audio_state;
1799 if (s->drv) {
1800 return;
1803 QLIST_INIT (&s->hw_head_out);
1804 QLIST_INIT (&s->hw_head_in);
1805 QLIST_INIT (&s->cap_head);
1806 atexit (audio_atexit);
1808 s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
1810 audio_process_options ("AUDIO", audio_options);
1812 s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1813 s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1815 if (s->nb_hw_voices_out <= 0) {
1816 dolog ("Bogus number of playback voices %d, setting to 1\n",
1817 s->nb_hw_voices_out);
1818 s->nb_hw_voices_out = 1;
1821 if (s->nb_hw_voices_in <= 0) {
1822 dolog ("Bogus number of capture voices %d, setting to 0\n",
1823 s->nb_hw_voices_in);
1824 s->nb_hw_voices_in = 0;
1828 int def;
1829 drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1832 if (drvname) {
1833 int found = 0;
1835 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1836 if (!strcmp (drvname, drvtab[i]->name)) {
1837 done = !audio_driver_init (s, drvtab[i]);
1838 found = 1;
1839 break;
1843 if (!found) {
1844 dolog ("Unknown audio driver `%s'\n", drvname);
1845 dolog ("Run with -audio-help to list available drivers\n");
1849 if (!done) {
1850 for (i = 0; !done && i < ARRAY_SIZE (drvtab); i++) {
1851 if (drvtab[i]->can_be_default) {
1852 done = !audio_driver_init (s, drvtab[i]);
1857 if (!done) {
1858 done = !audio_driver_init (s, &no_audio_driver);
1859 assert(done);
1860 dolog("warning: Using timer based audio emulation\n");
1863 if (conf.period.hertz <= 0) {
1864 if (conf.period.hertz < 0) {
1865 dolog ("warning: Timer period is negative - %d "
1866 "treating as zero\n",
1867 conf.period.hertz);
1869 conf.period.ticks = 1;
1870 } else {
1871 conf.period.ticks =
1872 muldiv64 (1, get_ticks_per_sec (), conf.period.hertz);
1875 e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1876 if (!e) {
1877 dolog ("warning: Could not register change state handler\n"
1878 "(Audio can continue looping even after stopping the VM)\n");
1881 QLIST_INIT (&s->card_head);
1882 vmstate_register (NULL, 0, &vmstate_audio, s);
1885 void AUD_register_card (const char *name, QEMUSoundCard *card)
1887 audio_init ();
1888 card->name = g_strdup (name);
1889 memset (&card->entries, 0, sizeof (card->entries));
1890 QLIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries);
1893 void AUD_remove_card (QEMUSoundCard *card)
1895 QLIST_REMOVE (card, entries);
1896 g_free (card->name);
1900 CaptureVoiceOut *AUD_add_capture (
1901 struct audsettings *as,
1902 struct audio_capture_ops *ops,
1903 void *cb_opaque
1906 AudioState *s = &glob_audio_state;
1907 CaptureVoiceOut *cap;
1908 struct capture_callback *cb;
1910 if (audio_validate_settings (as)) {
1911 dolog ("Invalid settings were passed when trying to add capture\n");
1912 audio_print_settings (as);
1913 goto err0;
1916 cb = audio_calloc (AUDIO_FUNC, 1, sizeof (*cb));
1917 if (!cb) {
1918 dolog ("Could not allocate capture callback information, size %zu\n",
1919 sizeof (*cb));
1920 goto err0;
1922 cb->ops = *ops;
1923 cb->opaque = cb_opaque;
1925 cap = audio_pcm_capture_find_specific (as);
1926 if (cap) {
1927 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1928 return cap;
1930 else {
1931 HWVoiceOut *hw;
1932 CaptureVoiceOut *cap;
1934 cap = audio_calloc (AUDIO_FUNC, 1, sizeof (*cap));
1935 if (!cap) {
1936 dolog ("Could not allocate capture voice, size %zu\n",
1937 sizeof (*cap));
1938 goto err1;
1941 hw = &cap->hw;
1942 QLIST_INIT (&hw->sw_head);
1943 QLIST_INIT (&cap->cb_head);
1945 /* XXX find a more elegant way */
1946 hw->samples = 4096 * 4;
1947 hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples,
1948 sizeof (struct st_sample));
1949 if (!hw->mix_buf) {
1950 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1951 hw->samples);
1952 goto err2;
1955 audio_pcm_init_info (&hw->info, as);
1957 cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
1958 if (!cap->buf) {
1959 dolog ("Could not allocate capture buffer "
1960 "(%d samples, each %d bytes)\n",
1961 hw->samples, 1 << hw->info.shift);
1962 goto err3;
1965 hw->clip = mixeng_clip
1966 [hw->info.nchannels == 2]
1967 [hw->info.sign]
1968 [hw->info.swap_endianness]
1969 [audio_bits_to_index (hw->info.bits)];
1971 QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
1972 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1974 hw = NULL;
1975 while ((hw = audio_pcm_hw_find_any_out (hw))) {
1976 audio_attach_capture (hw);
1978 return cap;
1980 err3:
1981 g_free (cap->hw.mix_buf);
1982 err2:
1983 g_free (cap);
1984 err1:
1985 g_free (cb);
1986 err0:
1987 return NULL;
1991 void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
1993 struct capture_callback *cb;
1995 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1996 if (cb->opaque == cb_opaque) {
1997 cb->ops.destroy (cb_opaque);
1998 QLIST_REMOVE (cb, entries);
1999 g_free (cb);
2001 if (!cap->cb_head.lh_first) {
2002 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
2004 while (sw) {
2005 SWVoiceCap *sc = (SWVoiceCap *) sw;
2006 #ifdef DEBUG_CAPTURE
2007 dolog ("freeing %s\n", sw->name);
2008 #endif
2010 sw1 = sw->entries.le_next;
2011 if (sw->rate) {
2012 st_rate_stop (sw->rate);
2013 sw->rate = NULL;
2015 QLIST_REMOVE (sw, entries);
2016 QLIST_REMOVE (sc, entries);
2017 g_free (sc);
2018 sw = sw1;
2020 QLIST_REMOVE (cap, entries);
2021 g_free (cap);
2023 return;
2028 void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
2030 if (sw) {
2031 HWVoiceOut *hw = sw->hw;
2033 sw->vol.mute = mute;
2034 sw->vol.l = nominal_volume.l * lvol / 255;
2035 sw->vol.r = nominal_volume.r * rvol / 255;
2037 if (hw->pcm_ops->ctl_out) {
2038 hw->pcm_ops->ctl_out (hw, VOICE_VOLUME, sw);
2043 void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
2045 if (sw) {
2046 HWVoiceIn *hw = sw->hw;
2048 sw->vol.mute = mute;
2049 sw->vol.l = nominal_volume.l * lvol / 255;
2050 sw->vol.r = nominal_volume.r * rvol / 255;
2052 if (hw->pcm_ops->ctl_in) {
2053 hw->pcm_ops->ctl_in (hw, VOICE_VOLUME, sw);