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
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[] = {
43 #ifdef CONFIG_COREAUDIO
44 &coreaudio_audio_driver,
59 struct fixed_settings {
63 audsettings_t settings;
67 struct fixed_settings fixed_out;
68 struct fixed_settings fixed_in;
76 { /* DAC fixed settings */
83 AUD_FMT_S16, /* fmt */
88 { /* ADC fixed settings */
95 AUD_FMT_S16, /* fmt */
102 0 /* log_to_monitor */
105 static AudioState glob_audio_state;
107 volume_t nominal_volume = {
118 /* http://www.df.lth.se/~john_e/gems/gem002d.html */
119 /* http://www.multi-platforms.com/Tips/PopCount.htm */
120 uint32_t popcount (uint32_t u)
122 u = ((u&0x55555555) + ((u>>1)&0x55555555));
123 u = ((u&0x33333333) + ((u>>2)&0x33333333));
124 u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
125 u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
126 u = ( u&0x0000ffff) + (u>>16);
130 inline uint32_t lsbindex (uint32_t u)
132 return popcount ((u&-u)-1);
135 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
138 int audio_bug (const char *funcname, int cond)
143 AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
146 AUD_log (NULL, "Save all your work and restart without audio\n");
148 AUD_log (NULL, "I am sorry\n");
150 AUD_log (NULL, "Context:\n");
152 #if defined AUDIO_BREAKPOINT_ON_BUG
153 # if defined HOST_I386
154 # if defined __GNUC__
156 # elif defined _MSC_VER
171 static inline int audio_bits_to_index (int bits)
184 audio_bug ("bits_to_index", 1);
185 AUD_log (NULL, "invalid bits %d\n", bits);
190 void *audio_calloc (const char *funcname, int nmemb, size_t size)
196 cond = !nmemb || !size;
200 if (audio_bug ("audio_calloc", cond)) {
201 AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
203 AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
207 return qemu_mallocz (len);
210 static char *audio_alloc_prefix (const char *s)
212 const char qemu_prefix[] = "QEMU_";
221 r = qemu_malloc (len + sizeof (qemu_prefix));
225 char *u = r + sizeof (qemu_prefix) - 1;
227 strcpy (r, qemu_prefix);
230 for (i = 0; i < len; ++i) {
231 u[i] = toupper (u[i]);
237 const char *audio_audfmt_to_string (audfmt_e fmt)
259 dolog ("Bogus audfmt %d returning S16\n", fmt);
263 audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval, int *defaultp)
265 if (!strcasecmp (s, "u8")) {
269 else if (!strcasecmp (s, "u16")) {
273 else if (!strcasecmp (s, "u32")) {
277 else if (!strcasecmp (s, "s8")) {
281 else if (!strcasecmp (s, "s16")) {
285 else if (!strcasecmp (s, "s32")) {
290 dolog ("Bogus audio format `%s' using %s\n",
291 s, audio_audfmt_to_string (defval));
297 static audfmt_e audio_get_conf_fmt (const char *envname,
301 const char *var = getenv (envname);
306 return audio_string_to_audfmt (var, defval, defaultp);
309 static int audio_get_conf_int (const char *key, int defval, int *defaultp)
314 strval = getenv (key);
326 static const char *audio_get_conf_str (const char *key,
330 const char *val = getenv (key);
341 void AUD_vlog (const char *cap, const char *fmt, va_list ap)
343 if (conf.log_to_monitor) {
345 term_printf ("%s: ", cap);
348 term_vprintf (fmt, ap);
352 fprintf (stderr, "%s: ", cap);
355 vfprintf (stderr, fmt, ap);
359 void AUD_log (const char *cap, const char *fmt, ...)
364 AUD_vlog (cap, fmt, ap);
368 static void audio_print_options (const char *prefix,
369 struct audio_option *opt)
374 dolog ("No prefix specified\n");
379 dolog ("No options\n");
383 uprefix = audio_alloc_prefix (prefix);
385 for (; opt->name; opt++) {
386 const char *state = "default";
387 printf (" %s_%s: ", uprefix, opt->name);
389 if (opt->overridenp && *opt->overridenp) {
396 int *intp = opt->valp;
397 printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
403 int *intp = opt->valp;
404 printf ("integer, %s = %d\n", state, *intp);
410 audfmt_e *fmtp = opt->valp;
412 "format, %s = %s, (one of: U8 S8 U16 S16)\n",
414 audio_audfmt_to_string (*fmtp)
421 const char **strp = opt->valp;
422 printf ("string, %s = %s\n",
424 *strp ? *strp : "(not set)");
430 dolog ("Bad value tag for option %s_%s %d\n",
431 uprefix, opt->name, opt->tag);
434 printf (" %s\n", opt->descr);
440 static void audio_process_options (const char *prefix,
441 struct audio_option *opt)
444 const char qemu_prefix[] = "QEMU_";
447 if (audio_bug (AUDIO_FUNC, !prefix)) {
448 dolog ("prefix = NULL\n");
452 if (audio_bug (AUDIO_FUNC, !opt)) {
453 dolog ("opt = NULL\n");
457 preflen = strlen (prefix);
459 for (; opt->name; opt++) {
464 dolog ("Option value pointer for `%s' is not set\n",
469 len = strlen (opt->name);
470 /* len of opt->name + len of prefix + size of qemu_prefix
471 * (includes trailing zero) + zero + underscore (on behalf of
473 optname = qemu_malloc (len + preflen + sizeof (qemu_prefix) + 1);
475 dolog ("Could not allocate memory for option name `%s'\n",
480 strcpy (optname, qemu_prefix);
482 /* copy while upper-casing, including trailing zero */
483 for (i = 0; i <= preflen; ++i) {
484 optname[i + sizeof (qemu_prefix) - 1] = toupper (prefix[i]);
486 strcat (optname, "_");
487 strcat (optname, opt->name);
494 int *intp = opt->valp;
495 *intp = audio_get_conf_int (optname, *intp, &def);
501 audfmt_e *fmtp = opt->valp;
502 *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
508 const char **strp = opt->valp;
509 *strp = audio_get_conf_str (optname, *strp, &def);
514 dolog ("Bad value tag for option `%s' - %d\n",
519 if (!opt->overridenp) {
520 opt->overridenp = &opt->overriden;
522 *opt->overridenp = !def;
527 static void audio_print_settings (audsettings_t *as)
529 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
533 AUD_log (NULL, "S8");
536 AUD_log (NULL, "U8");
539 AUD_log (NULL, "S16");
542 AUD_log (NULL, "U16");
545 AUD_log (NULL, "invalid(%d)", as->fmt);
549 AUD_log (NULL, " endianness=");
550 switch (as->endianness) {
552 AUD_log (NULL, "little");
555 AUD_log (NULL, "big");
558 AUD_log (NULL, "invalid");
561 AUD_log (NULL, "\n");
564 static int audio_validate_settings (audsettings_t *as)
568 invalid = as->nchannels != 1 && as->nchannels != 2;
569 invalid |= as->endianness != 0 && as->endianness != 1;
584 invalid |= as->freq <= 0;
585 return invalid ? -1 : 0;
588 static int audio_pcm_info_eq (struct audio_pcm_info *info, audsettings_t *as)
590 int bits = 8, sign = 0;
610 return info->freq == as->freq
611 && info->nchannels == as->nchannels
612 && info->sign == sign
613 && info->bits == bits
614 && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
617 void audio_pcm_init_info (struct audio_pcm_info *info, audsettings_t *as)
619 int bits = 8, sign = 0, shift = 0;
642 info->freq = as->freq;
645 info->nchannels = as->nchannels;
646 info->shift = (as->nchannels == 2) + shift;
647 info->align = (1 << info->shift) - 1;
648 info->bytes_per_second = info->freq << info->shift;
649 info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
652 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
659 memset (buf, 0x00, len << info->shift);
662 switch (info->bits) {
664 memset (buf, 0x80, len << info->shift);
671 int shift = info->nchannels - 1;
674 if (info->swap_endianness) {
678 for (i = 0; i < len << shift; i++) {
688 int shift = info->nchannels - 1;
689 int32_t s = INT32_MAX;
691 if (info->swap_endianness) {
695 for (i = 0; i < len << shift; i++) {
702 AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
712 static void noop_conv (st_sample_t *dst, const void *src,
713 int samples, volume_t *vol)
721 static CaptureVoiceOut *audio_pcm_capture_find_specific (
726 CaptureVoiceOut *cap;
728 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
729 if (audio_pcm_info_eq (&cap->hw.info, as)) {
736 static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
738 struct capture_callback *cb;
741 dolog ("notification %d sent\n", cmd);
743 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
744 cb->ops.notify (cb->opaque, cmd);
748 static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
750 if (cap->hw.enabled != enabled) {
751 audcnotification_e cmd;
752 cap->hw.enabled = enabled;
753 cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
754 audio_notify_capture (cap, cmd);
758 static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
760 HWVoiceOut *hw = &cap->hw;
764 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
770 audio_capture_maybe_changed (cap, enabled);
773 static void audio_detach_capture (HWVoiceOut *hw)
775 SWVoiceCap *sc = hw->cap_head.lh_first;
778 SWVoiceCap *sc1 = sc->entries.le_next;
779 SWVoiceOut *sw = &sc->sw;
780 CaptureVoiceOut *cap = sc->cap;
781 int was_active = sw->active;
784 st_rate_stop (sw->rate);
788 LIST_REMOVE (sw, entries);
789 LIST_REMOVE (sc, entries);
792 /* We have removed soft voice from the capture:
793 this might have changed the overall status of the capture
794 since this might have been the only active voice */
795 audio_recalc_and_notify_capture (cap);
801 static int audio_attach_capture (AudioState *s, HWVoiceOut *hw)
803 CaptureVoiceOut *cap;
805 audio_detach_capture (hw);
806 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
809 HWVoiceOut *hw_cap = &cap->hw;
811 sc = audio_calloc (AUDIO_FUNC, 1, sizeof (*sc));
813 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
823 sw->active = hw->enabled;
824 sw->conv = noop_conv;
825 sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
826 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
828 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
832 LIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
833 LIST_INSERT_HEAD (&hw->cap_head, sc, entries);
835 asprintf (&sw->name, "for %p %d,%d,%d",
836 hw, sw->info.freq, sw->info.bits, sw->info.nchannels);
837 dolog ("Added %s active = %d\n", sw->name, sw->active);
840 audio_capture_maybe_changed (cap, 1);
847 * Hard voice (capture)
849 static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
852 int m = hw->total_samples_captured;
854 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
856 m = audio_MIN (m, sw->total_hw_samples_acquired);
862 int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
864 int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
865 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
866 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
873 * Soft voice (capture)
875 static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
877 HWVoiceIn *hw = sw->hw;
878 int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
881 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
882 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
886 rpos = hw->wpos - live;
891 return hw->samples + rpos;
895 int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
897 HWVoiceIn *hw = sw->hw;
898 int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
899 st_sample_t *src, *dst = sw->buf;
901 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
903 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
904 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
905 dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
909 samples = size >> sw->info.shift;
914 swlim = (live * sw->ratio) >> 32;
915 swlim = audio_MIN (swlim, samples);
918 src = hw->conv_buf + rpos;
919 isamp = hw->wpos - rpos;
922 isamp = hw->samples - rpos;
930 if (audio_bug (AUDIO_FUNC, osamp < 0)) {
931 dolog ("osamp=%d\n", osamp);
935 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
937 rpos = (rpos + isamp) % hw->samples;
943 sw->clip (buf, sw->buf, ret);
944 sw->total_hw_samples_acquired += total;
945 return ret << sw->info.shift;
949 * Hard voice (playback)
951 static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
957 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
958 if (sw->active || !sw->empty) {
959 m = audio_MIN (m, sw->total_hw_samples_mixed);
968 int audio_pcm_hw_get_live_out2 (HWVoiceOut *hw, int *nb_live)
972 smin = audio_pcm_hw_find_min_out (hw, nb_live);
980 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
981 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
988 int audio_pcm_hw_get_live_out (HWVoiceOut *hw)
993 live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
994 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
995 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1002 * Soft voice (playback)
1004 int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
1006 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
1007 int ret = 0, pos = 0, total = 0;
1013 hwsamples = sw->hw->samples;
1015 live = sw->total_hw_samples_mixed;
1016 if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
1017 dolog ("live=%d hw->samples=%d\n", live, hwsamples);
1021 if (live == hwsamples) {
1023 dolog ("%s is full %d\n", sw->name, live);
1028 wpos = (sw->hw->rpos + live) % hwsamples;
1029 samples = size >> sw->info.shift;
1031 dead = hwsamples - live;
1032 swlim = ((int64_t) dead << 32) / sw->ratio;
1033 swlim = audio_MIN (swlim, samples);
1035 sw->conv (sw->buf, buf, swlim, &sw->vol);
1039 dead = hwsamples - live;
1040 left = hwsamples - wpos;
1041 blck = audio_MIN (dead, left);
1050 sw->hw->mix_buf + wpos,
1058 wpos = (wpos + osamp) % hwsamples;
1062 sw->total_hw_samples_mixed += total;
1063 sw->empty = sw->total_hw_samples_mixed == 0;
1067 "%s: write size %d ret %d total sw %d\n",
1069 size >> sw->info.shift,
1071 sw->total_hw_samples_mixed
1075 return ret << sw->info.shift;
1079 static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
1081 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1082 cap, info->bits, info->sign, info->freq, info->nchannels);
1087 #include "audio_template.h"
1089 #include "audio_template.h"
1091 int AUD_write (SWVoiceOut *sw, void *buf, int size)
1096 /* XXX: Consider options */
1100 if (!sw->hw->enabled) {
1101 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
1105 bytes = sw->hw->pcm_ops->write (sw, buf, size);
1109 int AUD_read (SWVoiceIn *sw, void *buf, int size)
1114 /* XXX: Consider options */
1118 if (!sw->hw->enabled) {
1119 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1123 bytes = sw->hw->pcm_ops->read (sw, buf, size);
1127 int AUD_get_buffer_size_out (SWVoiceOut *sw)
1129 return sw->hw->samples << sw->hw->info.shift;
1132 void AUD_set_active_out (SWVoiceOut *sw, int on)
1141 if (sw->active != on) {
1142 SWVoiceOut *temp_sw;
1146 hw->pending_disable = 0;
1149 hw->pcm_ops->ctl_out (hw, VOICE_ENABLE);
1156 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1157 temp_sw = temp_sw->entries.le_next) {
1158 nb_active += temp_sw->active != 0;
1161 hw->pending_disable = nb_active == 1;
1165 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1166 sc->sw.active = hw->enabled;
1168 audio_capture_maybe_changed (sc->cap, 1);
1175 void AUD_set_active_in (SWVoiceIn *sw, int on)
1184 if (sw->active != on) {
1190 hw->pcm_ops->ctl_in (hw, VOICE_ENABLE);
1192 sw->total_hw_samples_acquired = hw->total_samples_captured;
1198 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1199 temp_sw = temp_sw->entries.le_next) {
1200 nb_active += temp_sw->active != 0;
1203 if (nb_active == 1) {
1205 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
1213 static int audio_get_avail (SWVoiceIn *sw)
1221 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1222 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1223 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1228 "%s: get_avail live %d ret %" PRId64 "\n",
1230 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1233 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1236 static int audio_get_free (SWVoiceOut *sw)
1244 live = sw->total_hw_samples_mixed;
1246 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1247 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1251 dead = sw->hw->samples - live;
1254 dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
1256 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1259 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1262 static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1269 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1270 SWVoiceOut *sw = &sc->sw;
1275 int till_end_of_hw = hw->samples - rpos2;
1276 int to_write = audio_MIN (till_end_of_hw, n);
1277 int bytes = to_write << hw->info.shift;
1280 sw->buf = hw->mix_buf + rpos2;
1281 written = audio_pcm_sw_write (sw, NULL, bytes);
1282 if (written - bytes) {
1283 dolog ("Could not mix %d bytes into a capture "
1284 "buffer, mixed %d\n",
1289 rpos2 = (rpos2 + to_write) % hw->samples;
1294 n = audio_MIN (samples, hw->samples - rpos);
1295 mixeng_clear (hw->mix_buf + rpos, n);
1296 mixeng_clear (hw->mix_buf, samples - n);
1299 static void audio_run_out (AudioState *s)
1301 HWVoiceOut *hw = NULL;
1304 while ((hw = audio_pcm_hw_find_any_enabled_out (s, hw))) {
1306 int live, free, nb_live, cleanup_required, prev_rpos;
1308 live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
1313 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1314 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1318 if (hw->pending_disable && !nb_live) {
1321 dolog ("Disabling voice\n");
1324 hw->pending_disable = 0;
1325 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
1326 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1328 audio_recalc_and_notify_capture (sc->cap);
1334 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1336 free = audio_get_free (sw);
1338 sw->callback.fn (sw->callback.opaque, free);
1345 prev_rpos = hw->rpos;
1346 played = hw->pcm_ops->run_out (hw);
1347 if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1348 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1349 hw->rpos, hw->samples, played);
1354 dolog ("played=%d\n", played);
1358 hw->ts_helper += played;
1359 audio_capture_mix_and_clear (hw, prev_rpos, played);
1362 cleanup_required = 0;
1363 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1364 if (!sw->active && sw->empty) {
1368 if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1369 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1370 played, sw->total_hw_samples_mixed);
1371 played = sw->total_hw_samples_mixed;
1374 sw->total_hw_samples_mixed -= played;
1376 if (!sw->total_hw_samples_mixed) {
1378 cleanup_required |= !sw->active && !sw->callback.fn;
1382 free = audio_get_free (sw);
1384 sw->callback.fn (sw->callback.opaque, free);
1389 if (cleanup_required) {
1392 sw = hw->sw_head.lh_first;
1394 sw1 = sw->entries.le_next;
1395 if (!sw->active && !sw->callback.fn) {
1397 dolog ("Finishing with old voice\n");
1399 audio_close_out (s, sw);
1407 static void audio_run_in (AudioState *s)
1409 HWVoiceIn *hw = NULL;
1411 while ((hw = audio_pcm_hw_find_any_enabled_in (s, hw))) {
1415 captured = hw->pcm_ops->run_in (hw);
1417 min = audio_pcm_hw_find_min_in (hw);
1418 hw->total_samples_captured += captured - min;
1419 hw->ts_helper += captured;
1421 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1422 sw->total_hw_samples_acquired -= min;
1427 avail = audio_get_avail (sw);
1429 sw->callback.fn (sw->callback.opaque, avail);
1436 static void audio_run_capture (AudioState *s)
1438 CaptureVoiceOut *cap;
1440 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1441 int live, rpos, captured;
1442 HWVoiceOut *hw = &cap->hw;
1445 captured = live = audio_pcm_hw_get_live_out (hw);
1448 int left = hw->samples - rpos;
1449 int to_capture = audio_MIN (live, left);
1451 struct capture_callback *cb;
1453 src = hw->mix_buf + rpos;
1454 hw->clip (cap->buf, src, to_capture);
1455 mixeng_clear (src, to_capture);
1457 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1458 cb->ops.capture (cb->opaque, cap->buf,
1459 to_capture << hw->info.shift);
1461 rpos = (rpos + to_capture) % hw->samples;
1466 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1467 if (!sw->active && sw->empty) {
1471 if (audio_bug (AUDIO_FUNC, captured > sw->total_hw_samples_mixed)) {
1472 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1473 captured, sw->total_hw_samples_mixed);
1474 captured = sw->total_hw_samples_mixed;
1477 sw->total_hw_samples_mixed -= captured;
1478 sw->empty = sw->total_hw_samples_mixed == 0;
1483 static void audio_timer (void *opaque)
1485 AudioState *s = opaque;
1489 audio_run_capture (s);
1491 qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);
1494 static struct audio_option audio_options[] = {
1496 {"DAC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_out.enabled,
1497 "Use fixed settings for host DAC", NULL, 0},
1499 {"DAC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_out.settings.freq,
1500 "Frequency for fixed host DAC", NULL, 0},
1502 {"DAC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_out.settings.fmt,
1503 "Format for fixed host DAC", NULL, 0},
1505 {"DAC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_out.settings.nchannels,
1506 "Number of channels for fixed DAC (1 - mono, 2 - stereo)", NULL, 0},
1508 {"DAC_VOICES", AUD_OPT_INT, &conf.fixed_out.nb_voices,
1509 "Number of voices for DAC", NULL, 0},
1512 {"ADC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_in.enabled,
1513 "Use fixed settings for host ADC", NULL, 0},
1515 {"ADC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_in.settings.freq,
1516 "Frequency for fixed host ADC", NULL, 0},
1518 {"ADC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_in.settings.fmt,
1519 "Format for fixed host ADC", NULL, 0},
1521 {"ADC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_in.settings.nchannels,
1522 "Number of channels for fixed ADC (1 - mono, 2 - stereo)", NULL, 0},
1524 {"ADC_VOICES", AUD_OPT_INT, &conf.fixed_in.nb_voices,
1525 "Number of voices for ADC", NULL, 0},
1528 {"TIMER_PERIOD", AUD_OPT_INT, &conf.period.hz,
1529 "Timer period in HZ (0 - use lowest possible)", NULL, 0},
1531 {"PLIVE", AUD_OPT_BOOL, &conf.plive,
1532 "(undocumented)", NULL, 0},
1534 {"LOG_TO_MONITOR", AUD_OPT_BOOL, &conf.log_to_monitor,
1535 "print logging messages to montior instead of stderr", NULL, 0},
1537 {NULL, 0, NULL, NULL, NULL, 0}
1540 static void audio_pp_nb_voices (const char *typ, int nb)
1544 printf ("Does not support %s\n", typ);
1547 printf ("One %s voice\n", typ);
1550 printf ("Theoretically supports many %s voices\n", typ);
1553 printf ("Theoretically supports upto %d %s voices\n", nb, typ);
1559 void AUD_help (void)
1563 audio_process_options ("AUDIO", audio_options);
1564 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1565 struct audio_driver *d = drvtab[i];
1567 audio_process_options (d->name, d->options);
1571 printf ("Audio options:\n");
1572 audio_print_options ("AUDIO", audio_options);
1575 printf ("Available drivers:\n");
1577 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1578 struct audio_driver *d = drvtab[i];
1580 printf ("Name: %s\n", d->name);
1581 printf ("Description: %s\n", d->descr);
1583 audio_pp_nb_voices ("playback", d->max_voices_out);
1584 audio_pp_nb_voices ("capture", d->max_voices_in);
1587 printf ("Options:\n");
1588 audio_print_options (d->name, d->options);
1591 printf ("No options\n");
1597 "Options are settable through environment variables.\n"
1600 " set QEMU_AUDIO_DRV=wav\n"
1601 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1603 " export QEMU_AUDIO_DRV=wav\n"
1604 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1605 "(for csh replace export with setenv in the above)\n"
1611 static int audio_driver_init (AudioState *s, struct audio_driver *drv)
1614 audio_process_options (drv->name, drv->options);
1616 s->drv_opaque = drv->init ();
1618 if (s->drv_opaque) {
1619 audio_init_nb_voices_out (s, drv);
1620 audio_init_nb_voices_in (s, drv);
1625 dolog ("Could not init `%s' audio driver\n", drv->name);
1630 static void audio_vm_change_state_handler (void *opaque, int running)
1632 AudioState *s = opaque;
1633 HWVoiceOut *hwo = NULL;
1634 HWVoiceIn *hwi = NULL;
1635 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1637 while ((hwo = audio_pcm_hw_find_any_enabled_out (s, hwo))) {
1638 hwo->pcm_ops->ctl_out (hwo, op);
1641 while ((hwi = audio_pcm_hw_find_any_enabled_in (s, hwi))) {
1642 hwi->pcm_ops->ctl_in (hwi, op);
1646 static void audio_atexit (void)
1648 AudioState *s = &glob_audio_state;
1649 HWVoiceOut *hwo = NULL;
1650 HWVoiceIn *hwi = NULL;
1652 while ((hwo = audio_pcm_hw_find_any_enabled_out (s, hwo))) {
1655 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1656 hwo->pcm_ops->fini_out (hwo);
1658 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1659 CaptureVoiceOut *cap = sc->cap;
1660 struct capture_callback *cb;
1662 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1663 cb->ops.destroy (cb->opaque);
1668 while ((hwi = audio_pcm_hw_find_any_enabled_in (s, hwi))) {
1669 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1670 hwi->pcm_ops->fini_in (hwi);
1674 s->drv->fini (s->drv_opaque);
1678 static void audio_save (QEMUFile *f, void *opaque)
1684 static int audio_load (QEMUFile *f, void *opaque, int version_id)
1689 if (version_id != 1) {
1696 void AUD_register_card (AudioState *s, const char *name, QEMUSoundCard *card)
1699 card->name = qemu_strdup (name);
1700 memset (&card->entries, 0, sizeof (card->entries));
1701 LIST_INSERT_HEAD (&s->card_head, card, entries);
1704 void AUD_remove_card (QEMUSoundCard *card)
1706 LIST_REMOVE (card, entries);
1708 qemu_free (card->name);
1711 AudioState *AUD_init (void)
1715 const char *drvname;
1716 AudioState *s = &glob_audio_state;
1718 LIST_INIT (&s->hw_head_out);
1719 LIST_INIT (&s->hw_head_in);
1720 LIST_INIT (&s->cap_head);
1721 atexit (audio_atexit);
1723 s->ts = qemu_new_timer (vm_clock, audio_timer, s);
1725 dolog ("Could not create audio timer\n");
1729 audio_process_options ("AUDIO", audio_options);
1731 s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1732 s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1734 if (s->nb_hw_voices_out <= 0) {
1735 dolog ("Bogus number of playback voices %d, setting to 1\n",
1736 s->nb_hw_voices_out);
1737 s->nb_hw_voices_out = 1;
1740 if (s->nb_hw_voices_in <= 0) {
1741 dolog ("Bogus number of capture voices %d, setting to 0\n",
1742 s->nb_hw_voices_in);
1743 s->nb_hw_voices_in = 0;
1748 drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1754 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1755 if (!strcmp (drvname, drvtab[i]->name)) {
1756 done = !audio_driver_init (s, drvtab[i]);
1763 dolog ("Unknown audio driver `%s'\n", drvname);
1764 dolog ("Run with -audio-help to list available drivers\n");
1769 for (i = 0; !done && i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1770 if (drvtab[i]->can_be_default) {
1771 done = !audio_driver_init (s, drvtab[i]);
1777 done = !audio_driver_init (s, &no_audio_driver);
1779 dolog ("Could not initialize audio subsystem\n");
1782 dolog ("warning: Using timer based audio emulation\n");
1787 VMChangeStateEntry *e;
1789 if (conf.period.hz <= 0) {
1790 if (conf.period.hz < 0) {
1791 dolog ("warning: Timer period is negative - %d "
1792 "treating as zero\n",
1795 conf.period.ticks = 1;
1798 conf.period.ticks = ticks_per_sec / conf.period.hz;
1801 e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1803 dolog ("warning: Could not register change state handler\n"
1804 "(Audio can continue looping even after stopping the VM)\n");
1808 qemu_del_timer (s->ts);
1812 LIST_INIT (&s->card_head);
1813 register_savevm ("audio", 0, 1, audio_save, audio_load, s);
1814 qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);
1818 CaptureVoiceOut *AUD_add_capture (
1821 struct audio_capture_ops *ops,
1825 CaptureVoiceOut *cap;
1826 struct capture_callback *cb;
1830 s = &glob_audio_state;
1833 if (audio_validate_settings (as)) {
1834 dolog ("Invalid settings were passed when trying to add capture\n");
1835 audio_print_settings (as);
1839 cb = audio_calloc (AUDIO_FUNC, 1, sizeof (*cb));
1841 dolog ("Could not allocate capture callback information, size %zu\n",
1846 cb->opaque = cb_opaque;
1848 cap = audio_pcm_capture_find_specific (s, as);
1850 LIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1855 CaptureVoiceOut *cap;
1857 cap = audio_calloc (AUDIO_FUNC, 1, sizeof (*cap));
1859 dolog ("Could not allocate capture voice, size %zu\n",
1865 LIST_INIT (&hw->sw_head);
1866 LIST_INIT (&cap->cb_head);
1868 /* XXX find a more elegant way */
1869 hw->samples = 4096 * 4;
1870 hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples,
1871 sizeof (st_sample_t));
1873 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1878 audio_pcm_init_info (&hw->info, as);
1880 cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
1882 dolog ("Could not allocate capture buffer "
1883 "(%d samples, each %d bytes)\n",
1884 hw->samples, 1 << hw->info.shift);
1888 hw->clip = mixeng_clip
1889 [hw->info.nchannels == 2]
1891 [hw->info.swap_endianness]
1892 [audio_bits_to_index (hw->info.bits)];
1894 LIST_INSERT_HEAD (&s->cap_head, cap, entries);
1895 LIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1898 while ((hw = audio_pcm_hw_find_any_out (s, hw))) {
1899 audio_attach_capture (s, hw);
1904 qemu_free (cap->hw.mix_buf);
1914 void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
1916 struct capture_callback *cb;
1918 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1919 if (cb->opaque == cb_opaque) {
1920 cb->ops.destroy (cb_opaque);
1921 LIST_REMOVE (cb, entries);
1924 if (!cap->cb_head.lh_first) {
1925 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
1928 SWVoiceCap *sc = (SWVoiceCap *) sw;
1929 #ifdef DEBUG_CAPTURE
1930 dolog ("freeing %s\n", sw->name);
1933 sw1 = sw->entries.le_next;
1935 st_rate_stop (sw->rate);
1938 LIST_REMOVE (sw, entries);
1939 LIST_REMOVE (sc, entries);
1943 LIST_REMOVE (cap, entries);