Don't include audio.h when building tools.
[qemu/mini2440.git] / audio / audio.c
blobdd86c4d7a74851dc2865aad54499373768237316
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 "vl.h"
26 #define AUDIO_CAP "audio"
27 #include "audio_int.h"
29 /* #define DEBUG_PLIVE */
30 /* #define DEBUG_LIVE */
31 /* #define DEBUG_OUT */
32 /* #define DEBUG_CAPTURE */
34 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
36 static struct audio_driver *drvtab[] = {
37 #ifdef CONFIG_OSS
38 &oss_audio_driver,
39 #endif
40 #ifdef CONFIG_ALSA
41 &alsa_audio_driver,
42 #endif
43 #ifdef CONFIG_COREAUDIO
44 &coreaudio_audio_driver,
45 #endif
46 #ifdef CONFIG_DSOUND
47 &dsound_audio_driver,
48 #endif
49 #ifdef CONFIG_FMOD
50 &fmod_audio_driver,
51 #endif
52 #ifdef CONFIG_SDL
53 &sdl_audio_driver,
54 #endif
55 &no_audio_driver,
56 &wav_audio_driver
59 struct fixed_settings {
60 int enabled;
61 int nb_voices;
62 int greedy;
63 audsettings_t settings;
66 static struct {
67 struct fixed_settings fixed_out;
68 struct fixed_settings fixed_in;
69 union {
70 int hz;
71 int64_t ticks;
72 } period;
73 int plive;
74 int log_to_monitor;
75 } conf = {
76 { /* DAC fixed settings */
77 1, /* enabled */
78 1, /* nb_voices */
79 1, /* greedy */
81 44100, /* freq */
82 2, /* nchannels */
83 AUD_FMT_S16 /* fmt */
87 { /* ADC fixed settings */
88 1, /* enabled */
89 1, /* nb_voices */
90 1, /* greedy */
92 44100, /* freq */
93 2, /* nchannels */
94 AUD_FMT_S16 /* fmt */
98 { 0 }, /* period */
99 0, /* plive */
100 0 /* log_to_monitor */
103 static AudioState glob_audio_state;
105 volume_t nominal_volume = {
107 #ifdef FLOAT_MIXENG
108 1.0,
110 #else
111 UINT_MAX,
112 UINT_MAX
113 #endif
116 /* http://www.df.lth.se/~john_e/gems/gem002d.html */
117 /* http://www.multi-platforms.com/Tips/PopCount.htm */
118 uint32_t popcount (uint32_t u)
120 u = ((u&0x55555555) + ((u>>1)&0x55555555));
121 u = ((u&0x33333333) + ((u>>2)&0x33333333));
122 u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
123 u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
124 u = ( u&0x0000ffff) + (u>>16);
125 return u;
128 inline uint32_t lsbindex (uint32_t u)
130 return popcount ((u&-u)-1);
133 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
134 #error No its not
135 #else
136 int audio_bug (const char *funcname, int cond)
138 if (cond) {
139 static int shown;
141 AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
142 if (!shown) {
143 shown = 1;
144 AUD_log (NULL, "Save all your work and restart without audio\n");
145 AUD_log (NULL, "Please send bug report to malc@pulsesoft.com\n");
146 AUD_log (NULL, "I am sorry\n");
148 AUD_log (NULL, "Context:\n");
150 #if defined AUDIO_BREAKPOINT_ON_BUG
151 # if defined HOST_I386
152 # if defined __GNUC__
153 __asm__ ("int3");
154 # elif defined _MSC_VER
155 _asm _emit 0xcc;
156 # else
157 abort ();
158 # endif
159 # else
160 abort ();
161 # endif
162 #endif
165 return cond;
167 #endif
169 void *audio_calloc (const char *funcname, int nmemb, size_t size)
171 int cond;
172 size_t len;
174 len = nmemb * size;
175 cond = !nmemb || !size;
176 cond |= nmemb < 0;
177 cond |= len < size;
179 if (audio_bug ("audio_calloc", cond)) {
180 AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
181 funcname);
182 AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
183 return NULL;
186 return qemu_mallocz (len);
189 static char *audio_alloc_prefix (const char *s)
191 const char qemu_prefix[] = "QEMU_";
192 size_t len;
193 char *r;
195 if (!s) {
196 return NULL;
199 len = strlen (s);
200 r = qemu_malloc (len + sizeof (qemu_prefix));
202 if (r) {
203 size_t i;
204 char *u = r + sizeof (qemu_prefix) - 1;
206 strcpy (r, qemu_prefix);
207 strcat (r, s);
209 for (i = 0; i < len; ++i) {
210 u[i] = toupper (u[i]);
213 return r;
216 const char *audio_audfmt_to_string (audfmt_e fmt)
218 switch (fmt) {
219 case AUD_FMT_U8:
220 return "U8";
222 case AUD_FMT_U16:
223 return "U16";
225 case AUD_FMT_S8:
226 return "S8";
228 case AUD_FMT_S16:
229 return "S16";
232 dolog ("Bogus audfmt %d returning S16\n", fmt);
233 return "S16";
236 audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval, int *defaultp)
238 if (!strcasecmp (s, "u8")) {
239 *defaultp = 0;
240 return AUD_FMT_U8;
242 else if (!strcasecmp (s, "u16")) {
243 *defaultp = 0;
244 return AUD_FMT_U16;
246 else if (!strcasecmp (s, "s8")) {
247 *defaultp = 0;
248 return AUD_FMT_S8;
250 else if (!strcasecmp (s, "s16")) {
251 *defaultp = 0;
252 return AUD_FMT_S16;
254 else {
255 dolog ("Bogus audio format `%s' using %s\n",
256 s, audio_audfmt_to_string (defval));
257 *defaultp = 1;
258 return defval;
262 static audfmt_e audio_get_conf_fmt (const char *envname,
263 audfmt_e defval,
264 int *defaultp)
266 const char *var = getenv (envname);
267 if (!var) {
268 *defaultp = 1;
269 return defval;
271 return audio_string_to_audfmt (var, defval, defaultp);
274 static int audio_get_conf_int (const char *key, int defval, int *defaultp)
276 int val;
277 char *strval;
279 strval = getenv (key);
280 if (strval) {
281 *defaultp = 0;
282 val = atoi (strval);
283 return val;
285 else {
286 *defaultp = 1;
287 return defval;
291 static const char *audio_get_conf_str (const char *key,
292 const char *defval,
293 int *defaultp)
295 const char *val = getenv (key);
296 if (!val) {
297 *defaultp = 1;
298 return defval;
300 else {
301 *defaultp = 0;
302 return val;
306 void AUD_vlog (const char *cap, const char *fmt, va_list ap)
308 if (conf.log_to_monitor) {
309 if (cap) {
310 term_printf ("%s: ", cap);
313 term_vprintf (fmt, ap);
315 else {
316 if (cap) {
317 fprintf (stderr, "%s: ", cap);
320 vfprintf (stderr, fmt, ap);
324 void AUD_log (const char *cap, const char *fmt, ...)
326 va_list ap;
328 va_start (ap, fmt);
329 AUD_vlog (cap, fmt, ap);
330 va_end (ap);
333 static void audio_print_options (const char *prefix,
334 struct audio_option *opt)
336 char *uprefix;
338 if (!prefix) {
339 dolog ("No prefix specified\n");
340 return;
343 if (!opt) {
344 dolog ("No options\n");
345 return;
348 uprefix = audio_alloc_prefix (prefix);
350 for (; opt->name; opt++) {
351 const char *state = "default";
352 printf (" %s_%s: ", uprefix, opt->name);
354 if (opt->overridenp && *opt->overridenp) {
355 state = "current";
358 switch (opt->tag) {
359 case AUD_OPT_BOOL:
361 int *intp = opt->valp;
362 printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
364 break;
366 case AUD_OPT_INT:
368 int *intp = opt->valp;
369 printf ("integer, %s = %d\n", state, *intp);
371 break;
373 case AUD_OPT_FMT:
375 audfmt_e *fmtp = opt->valp;
376 printf (
377 "format, %s = %s, (one of: U8 S8 U16 S16)\n",
378 state,
379 audio_audfmt_to_string (*fmtp)
382 break;
384 case AUD_OPT_STR:
386 const char **strp = opt->valp;
387 printf ("string, %s = %s\n",
388 state,
389 *strp ? *strp : "(not set)");
391 break;
393 default:
394 printf ("???\n");
395 dolog ("Bad value tag for option %s_%s %d\n",
396 uprefix, opt->name, opt->tag);
397 break;
399 printf (" %s\n", opt->descr);
402 qemu_free (uprefix);
405 static void audio_process_options (const char *prefix,
406 struct audio_option *opt)
408 char *optname;
409 const char qemu_prefix[] = "QEMU_";
410 size_t preflen;
412 if (audio_bug (AUDIO_FUNC, !prefix)) {
413 dolog ("prefix = NULL\n");
414 return;
417 if (audio_bug (AUDIO_FUNC, !opt)) {
418 dolog ("opt = NULL\n");
419 return;
422 preflen = strlen (prefix);
424 for (; opt->name; opt++) {
425 size_t len, i;
426 int def;
428 if (!opt->valp) {
429 dolog ("Option value pointer for `%s' is not set\n",
430 opt->name);
431 continue;
434 len = strlen (opt->name);
435 /* len of opt->name + len of prefix + size of qemu_prefix
436 * (includes trailing zero) + zero + underscore (on behalf of
437 * sizeof) */
438 optname = qemu_malloc (len + preflen + sizeof (qemu_prefix) + 1);
439 if (!optname) {
440 dolog ("Could not allocate memory for option name `%s'\n",
441 opt->name);
442 continue;
445 strcpy (optname, qemu_prefix);
447 /* copy while upper-casing, including trailing zero */
448 for (i = 0; i <= preflen; ++i) {
449 optname[i + sizeof (qemu_prefix) - 1] = toupper (prefix[i]);
451 strcat (optname, "_");
452 strcat (optname, opt->name);
454 def = 1;
455 switch (opt->tag) {
456 case AUD_OPT_BOOL:
457 case AUD_OPT_INT:
459 int *intp = opt->valp;
460 *intp = audio_get_conf_int (optname, *intp, &def);
462 break;
464 case AUD_OPT_FMT:
466 audfmt_e *fmtp = opt->valp;
467 *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
469 break;
471 case AUD_OPT_STR:
473 const char **strp = opt->valp;
474 *strp = audio_get_conf_str (optname, *strp, &def);
476 break;
478 default:
479 dolog ("Bad value tag for option `%s' - %d\n",
480 optname, opt->tag);
481 break;
484 if (!opt->overridenp) {
485 opt->overridenp = &opt->overriden;
487 *opt->overridenp = !def;
488 qemu_free (optname);
492 static void audio_print_settings (audsettings_t *as)
494 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
496 switch (as->fmt) {
497 case AUD_FMT_S8:
498 AUD_log (NULL, "S8");
499 break;
500 case AUD_FMT_U8:
501 AUD_log (NULL, "U8");
502 break;
503 case AUD_FMT_S16:
504 AUD_log (NULL, "S16");
505 break;
506 case AUD_FMT_U16:
507 AUD_log (NULL, "U16");
508 break;
509 default:
510 AUD_log (NULL, "invalid(%d)", as->fmt);
511 break;
513 AUD_log (NULL, "endianness=");
514 switch (as->endianness) {
515 case 0:
516 AUD_log (NULL, "little");
517 break;
518 case 1:
519 AUD_log (NULL, "big");
520 break;
521 default:
522 AUD_log (NULL, "invalid");
523 break;
525 AUD_log (NULL, "\n");
528 static int audio_validate_settigs (audsettings_t *as)
530 int invalid;
532 invalid = as->nchannels != 1 && as->nchannels != 2;
533 invalid |= as->endianness != 0 && as->endianness != 1;
535 switch (as->fmt) {
536 case AUD_FMT_S8:
537 case AUD_FMT_U8:
538 case AUD_FMT_S16:
539 case AUD_FMT_U16:
540 break;
541 default:
542 invalid = 1;
543 break;
546 invalid |= as->freq <= 0;
547 return invalid ? -1 : 0;
550 static int audio_pcm_info_eq (struct audio_pcm_info *info, audsettings_t *as)
552 int bits = 8, sign = 0;
554 switch (as->fmt) {
555 case AUD_FMT_S8:
556 sign = 1;
557 case AUD_FMT_U8:
558 break;
560 case AUD_FMT_S16:
561 sign = 1;
562 case AUD_FMT_U16:
563 bits = 16;
564 break;
566 return info->freq == as->freq
567 && info->nchannels == as->nchannels
568 && info->sign == sign
569 && info->bits == bits
570 && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
573 void audio_pcm_init_info (struct audio_pcm_info *info, audsettings_t *as)
575 int bits = 8, sign = 0;
577 switch (as->fmt) {
578 case AUD_FMT_S8:
579 sign = 1;
580 case AUD_FMT_U8:
581 break;
583 case AUD_FMT_S16:
584 sign = 1;
585 case AUD_FMT_U16:
586 bits = 16;
587 break;
590 info->freq = as->freq;
591 info->bits = bits;
592 info->sign = sign;
593 info->nchannels = as->nchannels;
594 info->shift = (as->nchannels == 2) + (bits == 16);
595 info->align = (1 << info->shift) - 1;
596 info->bytes_per_second = info->freq << info->shift;
597 info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
600 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
602 if (!len) {
603 return;
606 if (info->sign) {
607 memset (buf, len << info->shift, 0x00);
609 else {
610 if (info->bits == 8) {
611 memset (buf, len << info->shift, 0x80);
613 else {
614 int i;
615 uint16_t *p = buf;
616 int shift = info->nchannels - 1;
617 short s = INT16_MAX;
619 if (info->swap_endianness) {
620 s = bswap16 (s);
623 for (i = 0; i < len << shift; i++) {
624 p[i] = s;
631 * Capture
633 static void noop_conv (st_sample_t *dst, const void *src,
634 int samples, volume_t *vol)
636 (void) src;
637 (void) dst;
638 (void) samples;
639 (void) vol;
642 static CaptureVoiceOut *audio_pcm_capture_find_specific (
643 AudioState *s,
644 audsettings_t *as
647 CaptureVoiceOut *cap;
649 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
650 if (audio_pcm_info_eq (&cap->hw.info, as)) {
651 return cap;
654 return NULL;
657 static void audio_notify_capture (CaptureVoiceOut *cap, int enabled)
659 if (cap->hw.enabled != enabled) {
660 struct capture_callback *cb;
662 cap->hw.enabled = enabled;
663 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
664 cb->ops.state (cb->opaque, enabled);
669 static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
671 HWVoiceOut *hw = &cap->hw;
672 SWVoiceOut *sw;
673 int enabled = 0;
675 for (sw = hw->sw_cap_head.lh_first; sw; sw = sw->cap_entries.le_next) {
676 if (sw->active) {
677 enabled = 1;
678 break;
681 audio_notify_capture (cap, enabled);
684 static void audio_detach_capture (HWVoiceOut *hw)
686 SWVoiceOut *sw;
688 for (sw = hw->sw_cap_head.lh_first; sw; sw = sw->cap_entries.le_next) {
689 if (sw->rate) {
690 st_rate_stop (sw->rate);
691 sw->rate = NULL;
694 LIST_REMOVE (sw, entries);
695 LIST_REMOVE (sw, cap_entries);
696 qemu_free (sw);
697 audio_recalc_and_notify_capture ((CaptureVoiceOut *) sw->hw);
701 static int audio_attach_capture (AudioState *s, HWVoiceOut *hw)
703 CaptureVoiceOut *cap;
705 audio_detach_capture (hw);
706 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
707 SWVoiceOut *sw;
708 HWVoiceOut *hw_cap;
710 hw_cap = &cap->hw;
711 sw = audio_calloc (AUDIO_FUNC, 1, sizeof (*sw));
712 if (!sw) {
713 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
714 sizeof (*sw));
715 return -1;
718 sw->info = hw->info;
719 sw->hw = hw_cap;
720 sw->empty = 1;
721 sw->active = hw->enabled;
722 sw->conv = noop_conv;
723 sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
724 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
725 if (!sw->rate) {
726 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
727 qemu_free (sw);
728 return -1;
730 LIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
731 LIST_INSERT_HEAD (&hw->sw_cap_head, sw, cap_entries);
732 if (sw->active) {
733 audio_notify_capture (cap, 1);
735 else {
736 audio_recalc_and_notify_capture (cap);
739 return 0;
743 * Hard voice (capture)
745 static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
747 SWVoiceIn *sw;
748 int m = hw->total_samples_captured;
750 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
751 if (sw->active) {
752 m = audio_MIN (m, sw->total_hw_samples_acquired);
755 return m;
758 int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
760 int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
761 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
762 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
763 return 0;
765 return live;
769 * Soft voice (capture)
771 static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
773 HWVoiceIn *hw = sw->hw;
774 int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
775 int rpos;
777 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
778 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
779 return 0;
782 rpos = hw->wpos - live;
783 if (rpos >= 0) {
784 return rpos;
786 else {
787 return hw->samples + rpos;
791 int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
793 HWVoiceIn *hw = sw->hw;
794 int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
795 st_sample_t *src, *dst = sw->buf;
797 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
799 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
800 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
801 dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
802 return 0;
805 samples = size >> sw->info.shift;
806 if (!live) {
807 return 0;
810 swlim = (live * sw->ratio) >> 32;
811 swlim = audio_MIN (swlim, samples);
813 while (swlim) {
814 src = hw->conv_buf + rpos;
815 isamp = hw->wpos - rpos;
816 /* XXX: <= ? */
817 if (isamp <= 0) {
818 isamp = hw->samples - rpos;
821 if (!isamp) {
822 break;
824 osamp = swlim;
826 if (audio_bug (AUDIO_FUNC, osamp < 0)) {
827 dolog ("osamp=%d\n", osamp);
828 return 0;
831 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
832 swlim -= osamp;
833 rpos = (rpos + isamp) % hw->samples;
834 dst += osamp;
835 ret += osamp;
836 total += isamp;
839 sw->clip (buf, sw->buf, ret);
840 sw->total_hw_samples_acquired += total;
841 return ret << sw->info.shift;
845 * Hard voice (playback)
847 static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
849 SWVoiceOut *sw;
850 int m = INT_MAX;
851 int nb_live = 0;
853 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
854 if (sw->active || !sw->empty) {
855 m = audio_MIN (m, sw->total_hw_samples_mixed);
856 nb_live += 1;
860 *nb_livep = nb_live;
861 return m;
864 int audio_pcm_hw_get_live_out2 (HWVoiceOut *hw, int *nb_live)
866 int smin;
868 smin = audio_pcm_hw_find_min_out (hw, nb_live);
870 if (!*nb_live) {
871 return 0;
873 else {
874 int live = smin;
876 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
877 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
878 return 0;
880 return live;
884 int audio_pcm_hw_get_live_out (HWVoiceOut *hw)
886 int nb_live;
887 int live;
889 live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
890 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
891 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
892 return 0;
894 return live;
898 * Soft voice (playback)
900 int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
902 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
903 int ret = 0, pos = 0, total = 0;
905 if (!sw) {
906 return size;
909 hwsamples = sw->hw->samples;
911 live = sw->total_hw_samples_mixed;
912 if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
913 dolog ("live=%d hw->samples=%d\n", live, hwsamples);
914 return 0;
917 if (live == hwsamples) {
918 return 0;
921 wpos = (sw->hw->rpos + live) % hwsamples;
922 samples = size >> sw->info.shift;
924 dead = hwsamples - live;
925 swlim = ((int64_t) dead << 32) / sw->ratio;
926 swlim = audio_MIN (swlim, samples);
927 if (swlim) {
928 sw->conv (sw->buf, buf, swlim, &sw->vol);
931 while (swlim) {
932 dead = hwsamples - live;
933 left = hwsamples - wpos;
934 blck = audio_MIN (dead, left);
935 if (!blck) {
936 break;
938 isamp = swlim;
939 osamp = blck;
940 st_rate_flow_mix (
941 sw->rate,
942 sw->buf + pos,
943 sw->hw->mix_buf + wpos,
944 &isamp,
945 &osamp
947 ret += isamp;
948 swlim -= isamp;
949 pos += isamp;
950 live += osamp;
951 wpos = (wpos + osamp) % hwsamples;
952 total += osamp;
955 sw->total_hw_samples_mixed += total;
956 sw->empty = sw->total_hw_samples_mixed == 0;
958 #ifdef DEBUG_OUT
959 dolog (
960 "%s: write size %d ret %d total sw %d\n",
961 SW_NAME (sw),
962 size >> sw->info.shift,
963 ret,
964 sw->total_hw_samples_mixed
966 #endif
968 return ret << sw->info.shift;
971 #ifdef DEBUG_AUDIO
972 static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
974 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
975 cap, info->bits, info->sign, info->freq, info->nchannels);
977 #endif
979 #define DAC
980 #include "audio_template.h"
981 #undef DAC
982 #include "audio_template.h"
984 int AUD_write (SWVoiceOut *sw, void *buf, int size)
986 int bytes;
988 if (!sw) {
989 /* XXX: Consider options */
990 return size;
993 if (!sw->hw->enabled) {
994 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
995 return 0;
998 bytes = sw->hw->pcm_ops->write (sw, buf, size);
999 return bytes;
1002 int AUD_read (SWVoiceIn *sw, void *buf, int size)
1004 int bytes;
1006 if (!sw) {
1007 /* XXX: Consider options */
1008 return size;
1011 if (!sw->hw->enabled) {
1012 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1013 return 0;
1016 bytes = sw->hw->pcm_ops->read (sw, buf, size);
1017 return bytes;
1020 int AUD_get_buffer_size_out (SWVoiceOut *sw)
1022 return sw->hw->samples << sw->hw->info.shift;
1025 void AUD_set_active_out (SWVoiceOut *sw, int on)
1027 HWVoiceOut *hw;
1029 if (!sw) {
1030 return;
1033 hw = sw->hw;
1034 if (sw->active != on) {
1035 SWVoiceOut *temp_sw;
1037 if (on) {
1038 hw->pending_disable = 0;
1039 if (!hw->enabled) {
1040 hw->enabled = 1;
1041 hw->pcm_ops->ctl_out (hw, VOICE_ENABLE);
1044 else {
1045 if (hw->enabled) {
1046 int nb_active = 0;
1048 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1049 temp_sw = temp_sw->entries.le_next) {
1050 nb_active += temp_sw->active != 0;
1053 hw->pending_disable = nb_active == 1;
1056 for (temp_sw = hw->sw_cap_head.lh_first; temp_sw;
1057 temp_sw = temp_sw->entries.le_next) {
1058 temp_sw->active = hw->enabled;
1059 if (hw->enabled) {
1060 audio_notify_capture ((CaptureVoiceOut *) temp_sw->hw, 1);
1063 sw->active = on;
1067 void AUD_set_active_in (SWVoiceIn *sw, int on)
1069 HWVoiceIn *hw;
1071 if (!sw) {
1072 return;
1075 hw = sw->hw;
1076 if (sw->active != on) {
1077 SWVoiceIn *temp_sw;
1079 if (on) {
1080 if (!hw->enabled) {
1081 hw->enabled = 1;
1082 hw->pcm_ops->ctl_in (hw, VOICE_ENABLE);
1084 sw->total_hw_samples_acquired = hw->total_samples_captured;
1086 else {
1087 if (hw->enabled) {
1088 int nb_active = 0;
1090 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1091 temp_sw = temp_sw->entries.le_next) {
1092 nb_active += temp_sw->active != 0;
1095 if (nb_active == 1) {
1096 hw->enabled = 0;
1097 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
1101 sw->active = on;
1105 static int audio_get_avail (SWVoiceIn *sw)
1107 int live;
1109 if (!sw) {
1110 return 0;
1113 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1114 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1115 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1116 return 0;
1119 ldebug (
1120 "%s: get_avail live %d ret %" PRId64 "\n",
1121 SW_NAME (sw),
1122 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1125 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1128 static int audio_get_free (SWVoiceOut *sw)
1130 int live, dead;
1132 if (!sw) {
1133 return 0;
1136 live = sw->total_hw_samples_mixed;
1138 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1139 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1140 return 0;
1143 dead = sw->hw->samples - live;
1145 #ifdef DEBUG_OUT
1146 dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
1147 SW_NAME (sw),
1148 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1149 #endif
1151 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1154 static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1156 int n;
1158 if (hw->enabled) {
1159 SWVoiceOut *sw;
1161 for (sw = hw->sw_cap_head.lh_first; sw; sw = sw->cap_entries.le_next) {
1162 int rpos2 = rpos;
1164 n = samples;
1165 while (n) {
1166 int till_end_of_hw = hw->samples - rpos2;
1167 int to_write = audio_MIN (till_end_of_hw, n);
1168 int bytes = to_write << hw->info.shift;
1169 int written;
1171 sw->buf = hw->mix_buf + rpos2;
1172 written = audio_pcm_sw_write (sw, NULL, bytes);
1173 if (written - bytes) {
1174 dolog ("Could not mix %d bytes into a capture buffer",
1175 bytes);
1176 break;
1178 n -= to_write;
1179 rpos2 = (rpos2 + to_write) % hw->samples;
1184 n = audio_MIN (samples, hw->samples - rpos);
1185 mixeng_clear (hw->mix_buf + rpos, n);
1186 mixeng_clear (hw->mix_buf, samples - n);
1189 static void audio_run_out (AudioState *s)
1191 HWVoiceOut *hw = NULL;
1192 SWVoiceOut *sw;
1194 while ((hw = audio_pcm_hw_find_any_enabled_out (s, hw))) {
1195 int played;
1196 int live, free, nb_live, cleanup_required, prev_rpos;
1198 live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
1199 if (!nb_live) {
1200 live = 0;
1203 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1204 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1205 continue;
1208 if (hw->pending_disable && !nb_live) {
1209 #ifdef DEBUG_OUT
1210 dolog ("Disabling voice\n");
1211 #endif
1212 hw->enabled = 0;
1213 hw->pending_disable = 0;
1214 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
1215 for (sw = hw->sw_cap_head.lh_first; sw;
1216 sw = sw->cap_entries.le_next) {
1217 sw->active = 0;
1218 audio_recalc_and_notify_capture ((CaptureVoiceOut *) sw->hw);
1220 continue;
1223 if (!live) {
1224 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1225 if (sw->active) {
1226 free = audio_get_free (sw);
1227 if (free > 0) {
1228 sw->callback.fn (sw->callback.opaque, free);
1232 continue;
1235 prev_rpos = hw->rpos;
1236 played = hw->pcm_ops->run_out (hw);
1237 if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1238 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1239 hw->rpos, hw->samples, played);
1240 hw->rpos = 0;
1243 #ifdef DEBUG_OUT
1244 dolog ("played=%d\n", played);
1245 #endif
1247 if (played) {
1248 hw->ts_helper += played;
1249 audio_capture_mix_and_clear (hw, prev_rpos, played);
1252 cleanup_required = 0;
1253 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1254 if (!sw->active && sw->empty) {
1255 continue;
1258 if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1259 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1260 played, sw->total_hw_samples_mixed);
1261 played = sw->total_hw_samples_mixed;
1264 sw->total_hw_samples_mixed -= played;
1266 if (!sw->total_hw_samples_mixed) {
1267 sw->empty = 1;
1268 cleanup_required |= !sw->active && !sw->callback.fn;
1271 if (sw->active) {
1272 free = audio_get_free (sw);
1273 if (free > 0) {
1274 sw->callback.fn (sw->callback.opaque, free);
1279 if (cleanup_required) {
1280 restart:
1281 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1282 if (!sw->active && !sw->callback.fn) {
1283 #ifdef DEBUG_PLIVE
1284 dolog ("Finishing with old voice\n");
1285 #endif
1286 audio_close_out (s, sw);
1287 goto restart; /* play it safe */
1294 static void audio_run_in (AudioState *s)
1296 HWVoiceIn *hw = NULL;
1298 while ((hw = audio_pcm_hw_find_any_enabled_in (s, hw))) {
1299 SWVoiceIn *sw;
1300 int captured, min;
1302 captured = hw->pcm_ops->run_in (hw);
1304 min = audio_pcm_hw_find_min_in (hw);
1305 hw->total_samples_captured += captured - min;
1306 hw->ts_helper += captured;
1308 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1309 sw->total_hw_samples_acquired -= min;
1311 if (sw->active) {
1312 int avail;
1314 avail = audio_get_avail (sw);
1315 if (avail > 0) {
1316 sw->callback.fn (sw->callback.opaque, avail);
1323 static void audio_run_capture (AudioState *s)
1325 CaptureVoiceOut *cap;
1327 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1328 int live, rpos, captured;
1329 HWVoiceOut *hw = &cap->hw;
1330 SWVoiceOut *sw;
1332 captured = live = audio_pcm_hw_get_live_out (hw);
1333 rpos = hw->rpos;
1334 while (live) {
1335 int left = hw->samples - rpos;
1336 int to_capture = audio_MIN (live, left);
1337 st_sample_t *src;
1338 struct capture_callback *cb;
1340 src = hw->mix_buf + rpos;
1341 hw->clip (cap->buf, src, to_capture);
1342 mixeng_clear (src, to_capture);
1344 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1345 cb->ops.capture (cb->opaque, cap->buf,
1346 to_capture << hw->info.shift);
1348 rpos = (rpos + to_capture) % hw->samples;
1349 live -= to_capture;
1351 hw->rpos = rpos;
1353 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1354 if (!sw->active && sw->empty) {
1355 continue;
1358 if (audio_bug (AUDIO_FUNC, captured > sw->total_hw_samples_mixed)) {
1359 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1360 captured, sw->total_hw_samples_mixed);
1361 captured = sw->total_hw_samples_mixed;
1364 sw->total_hw_samples_mixed -= captured;
1365 sw->empty = sw->total_hw_samples_mixed == 0;
1370 static void audio_timer (void *opaque)
1372 AudioState *s = opaque;
1374 audio_run_out (s);
1375 audio_run_in (s);
1376 audio_run_capture (s);
1378 qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);
1381 static struct audio_option audio_options[] = {
1382 /* DAC */
1383 {"DAC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_out.enabled,
1384 "Use fixed settings for host DAC", NULL, 0},
1386 {"DAC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_out.settings.freq,
1387 "Frequency for fixed host DAC", NULL, 0},
1389 {"DAC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_out.settings.fmt,
1390 "Format for fixed host DAC", NULL, 0},
1392 {"DAC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_out.settings.nchannels,
1393 "Number of channels for fixed DAC (1 - mono, 2 - stereo)", NULL, 0},
1395 {"DAC_VOICES", AUD_OPT_INT, &conf.fixed_out.nb_voices,
1396 "Number of voices for DAC", NULL, 0},
1398 /* ADC */
1399 {"ADC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_in.enabled,
1400 "Use fixed settings for host ADC", NULL, 0},
1402 {"ADC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_in.settings.freq,
1403 "Frequency for fixed host ADC", NULL, 0},
1405 {"ADC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_in.settings.fmt,
1406 "Format for fixed host ADC", NULL, 0},
1408 {"ADC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_in.settings.nchannels,
1409 "Number of channels for fixed ADC (1 - mono, 2 - stereo)", NULL, 0},
1411 {"ADC_VOICES", AUD_OPT_INT, &conf.fixed_in.nb_voices,
1412 "Number of voices for ADC", NULL, 0},
1414 /* Misc */
1415 {"TIMER_PERIOD", AUD_OPT_INT, &conf.period.hz,
1416 "Timer period in HZ (0 - use lowest possible)", NULL, 0},
1418 {"PLIVE", AUD_OPT_BOOL, &conf.plive,
1419 "(undocumented)", NULL, 0},
1421 {"LOG_TO_MONITOR", AUD_OPT_BOOL, &conf.log_to_monitor,
1422 "print logging messages to montior instead of stderr", NULL, 0},
1424 {NULL, 0, NULL, NULL, NULL, 0}
1427 static void audio_pp_nb_voices (const char *typ, int nb)
1429 switch (nb) {
1430 case 0:
1431 printf ("Does not support %s\n", typ);
1432 break;
1433 case 1:
1434 printf ("One %s voice\n", typ);
1435 break;
1436 case INT_MAX:
1437 printf ("Theoretically supports many %s voices\n", typ);
1438 break;
1439 default:
1440 printf ("Theoretically supports upto %d %s voices\n", nb, typ);
1441 break;
1446 void AUD_help (void)
1448 size_t i;
1450 audio_process_options ("AUDIO", audio_options);
1451 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1452 struct audio_driver *d = drvtab[i];
1453 if (d->options) {
1454 audio_process_options (d->name, d->options);
1458 printf ("Audio options:\n");
1459 audio_print_options ("AUDIO", audio_options);
1460 printf ("\n");
1462 printf ("Available drivers:\n");
1464 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1465 struct audio_driver *d = drvtab[i];
1467 printf ("Name: %s\n", d->name);
1468 printf ("Description: %s\n", d->descr);
1470 audio_pp_nb_voices ("playback", d->max_voices_out);
1471 audio_pp_nb_voices ("capture", d->max_voices_in);
1473 if (d->options) {
1474 printf ("Options:\n");
1475 audio_print_options (d->name, d->options);
1477 else {
1478 printf ("No options\n");
1480 printf ("\n");
1483 printf (
1484 "Options are settable through environment variables.\n"
1485 "Example:\n"
1486 #ifdef _WIN32
1487 " set QEMU_AUDIO_DRV=wav\n"
1488 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1489 #else
1490 " export QEMU_AUDIO_DRV=wav\n"
1491 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1492 "(for csh replace export with setenv in the above)\n"
1493 #endif
1494 " qemu ...\n\n"
1498 static int audio_driver_init (AudioState *s, struct audio_driver *drv)
1500 if (drv->options) {
1501 audio_process_options (drv->name, drv->options);
1503 s->drv_opaque = drv->init ();
1505 if (s->drv_opaque) {
1506 audio_init_nb_voices_out (s, drv);
1507 audio_init_nb_voices_in (s, drv);
1508 s->drv = drv;
1509 return 0;
1511 else {
1512 dolog ("Could not init `%s' audio driver\n", drv->name);
1513 return -1;
1517 static void audio_vm_change_state_handler (void *opaque, int running)
1519 AudioState *s = opaque;
1520 HWVoiceOut *hwo = NULL;
1521 HWVoiceIn *hwi = NULL;
1522 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1524 while ((hwo = audio_pcm_hw_find_any_enabled_out (s, hwo))) {
1525 hwo->pcm_ops->ctl_out (hwo, op);
1528 while ((hwi = audio_pcm_hw_find_any_enabled_in (s, hwi))) {
1529 hwi->pcm_ops->ctl_in (hwi, op);
1533 static void audio_atexit (void)
1535 AudioState *s = &glob_audio_state;
1536 HWVoiceOut *hwo = NULL;
1537 HWVoiceIn *hwi = NULL;
1539 while ((hwo = audio_pcm_hw_find_any_enabled_out (s, hwo))) {
1540 SWVoiceOut *sw;
1542 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1543 hwo->pcm_ops->fini_out (hwo);
1545 for (sw = hwo->sw_cap_head.lh_first; sw; sw = sw->entries.le_next) {
1546 audio_notify_capture ((CaptureVoiceOut *) sw->hw, 0);
1550 while ((hwi = audio_pcm_hw_find_any_enabled_in (s, hwi))) {
1551 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1552 hwi->pcm_ops->fini_in (hwi);
1555 if (s->drv) {
1556 s->drv->fini (s->drv_opaque);
1560 static void audio_save (QEMUFile *f, void *opaque)
1562 (void) f;
1563 (void) opaque;
1566 static int audio_load (QEMUFile *f, void *opaque, int version_id)
1568 (void) f;
1569 (void) opaque;
1571 if (version_id != 1) {
1572 return -EINVAL;
1575 return 0;
1578 void AUD_register_card (AudioState *s, const char *name, QEMUSoundCard *card)
1580 card->audio = s;
1581 card->name = qemu_strdup (name);
1582 memset (&card->entries, 0, sizeof (card->entries));
1583 LIST_INSERT_HEAD (&s->card_head, card, entries);
1586 void AUD_remove_card (QEMUSoundCard *card)
1588 LIST_REMOVE (card, entries);
1589 card->audio = NULL;
1590 qemu_free (card->name);
1593 AudioState *AUD_init (void)
1595 size_t i;
1596 int done = 0;
1597 const char *drvname;
1598 AudioState *s = &glob_audio_state;
1600 LIST_INIT (&s->hw_head_out);
1601 LIST_INIT (&s->hw_head_in);
1602 LIST_INIT (&s->cap_head);
1603 atexit (audio_atexit);
1605 s->ts = qemu_new_timer (vm_clock, audio_timer, s);
1606 if (!s->ts) {
1607 dolog ("Could not create audio timer\n");
1608 return NULL;
1611 audio_process_options ("AUDIO", audio_options);
1613 s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1614 s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1616 if (s->nb_hw_voices_out <= 0) {
1617 dolog ("Bogus number of playback voices %d, setting to 1\n",
1618 s->nb_hw_voices_out);
1619 s->nb_hw_voices_out = 1;
1622 if (s->nb_hw_voices_in <= 0) {
1623 dolog ("Bogus number of capture voices %d, setting to 0\n",
1624 s->nb_hw_voices_in);
1625 s->nb_hw_voices_in = 0;
1629 int def;
1630 drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1633 if (drvname) {
1634 int found = 0;
1636 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1637 if (!strcmp (drvname, drvtab[i]->name)) {
1638 done = !audio_driver_init (s, drvtab[i]);
1639 found = 1;
1640 break;
1644 if (!found) {
1645 dolog ("Unknown audio driver `%s'\n", drvname);
1646 dolog ("Run with -audio-help to list available drivers\n");
1650 if (!done) {
1651 for (i = 0; !done && i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1652 if (drvtab[i]->can_be_default) {
1653 done = !audio_driver_init (s, drvtab[i]);
1658 if (!done) {
1659 done = !audio_driver_init (s, &no_audio_driver);
1660 if (!done) {
1661 dolog ("Could not initialize audio subsystem\n");
1663 else {
1664 dolog ("warning: Using timer based audio emulation\n");
1668 if (done) {
1669 VMChangeStateEntry *e;
1671 if (conf.period.hz <= 0) {
1672 if (conf.period.hz < 0) {
1673 dolog ("warning: Timer period is negative - %d "
1674 "treating as zero\n",
1675 conf.period.hz);
1677 conf.period.ticks = 1;
1679 else {
1680 conf.period.ticks = ticks_per_sec / conf.period.hz;
1683 e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1684 if (!e) {
1685 dolog ("warning: Could not register change state handler\n"
1686 "(Audio can continue looping even after stopping the VM)\n");
1689 else {
1690 qemu_del_timer (s->ts);
1691 return NULL;
1694 LIST_INIT (&s->card_head);
1695 register_savevm ("audio", 0, 1, audio_save, audio_load, s);
1696 qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);
1697 return s;
1700 int AUD_add_capture (
1701 AudioState *s,
1702 audsettings_t *as,
1703 struct audio_capture_ops *ops,
1704 void *cb_opaque
1707 CaptureVoiceOut *cap;
1708 struct capture_callback *cb;
1710 if (!s) {
1711 /* XXX suppress */
1712 s = &glob_audio_state;
1715 if (audio_validate_settigs (as)) {
1716 dolog ("Invalid settings were passed when trying to add capture\n");
1717 audio_print_settings (as);
1718 return -1;
1721 cb = audio_calloc (AUDIO_FUNC, 1, sizeof (*cb));
1722 if (!cb) {
1723 dolog ("Could not allocate capture callback information, size %zu\n",
1724 sizeof (*cb));
1725 goto err0;
1727 cb->ops = *ops;
1728 cb->opaque = cb_opaque;
1730 cap = audio_pcm_capture_find_specific (s, as);
1731 if (cap) {
1732 LIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1733 return 0;
1735 else {
1736 HWVoiceOut *hw;
1737 CaptureVoiceOut *cap;
1739 cap = audio_calloc (AUDIO_FUNC, 1, sizeof (*cap));
1740 if (!cap) {
1741 dolog ("Could not allocate capture voice, size %zu\n",
1742 sizeof (*cap));
1743 goto err1;
1746 hw = &cap->hw;
1747 LIST_INIT (&hw->sw_head);
1748 LIST_INIT (&cap->cb_head);
1750 /* XXX find a more elegant way */
1751 hw->samples = 4096 * 4;
1752 hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples,
1753 sizeof (st_sample_t));
1754 if (!hw->mix_buf) {
1755 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1756 hw->samples);
1757 goto err2;
1760 audio_pcm_init_info (&hw->info, as);
1762 cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
1763 if (!cap->buf) {
1764 dolog ("Could not allocate capture buffer "
1765 "(%d samples, each %d bytes)\n",
1766 hw->samples, 1 << hw->info.shift);
1767 goto err3;
1770 hw->clip = mixeng_clip
1771 [hw->info.nchannels == 2]
1772 [hw->info.sign]
1773 [hw->info.swap_endianness]
1774 [hw->info.bits == 16];
1776 LIST_INSERT_HEAD (&s->cap_head, cap, entries);
1777 LIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1779 hw = NULL;
1780 while ((hw = audio_pcm_hw_find_any_out (s, hw))) {
1781 audio_attach_capture (s, hw);
1783 return 0;
1785 err3:
1786 qemu_free (cap->hw.mix_buf);
1787 err2:
1788 qemu_free (cap);
1789 err1:
1790 qemu_free (cb);
1791 err0:
1792 return -1;