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
24 #include "qemu/osdep.h"
27 #include "monitor/monitor.h"
28 #include "qemu/timer.h"
29 #include "sysemu/sysemu.h"
30 #include "qemu/cutils.h"
31 #include "sysemu/replay.h"
33 #define AUDIO_CAP "audio"
34 #include "audio_int.h"
36 /* #define DEBUG_LIVE */
37 /* #define DEBUG_OUT */
38 /* #define DEBUG_CAPTURE */
39 /* #define DEBUG_POLL */
41 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
44 /* Order of CONFIG_AUDIO_DRIVERS is import.
45 The 1st one is the one used by default, that is the reason
46 that we generate the list.
48 static struct audio_driver *drvtab[] = {
57 struct fixed_settings {
61 struct audsettings settings;
65 struct fixed_settings fixed_out;
66 struct fixed_settings fixed_in;
74 .fixed_out = { /* DAC fixed settings */
82 .endianness = AUDIO_HOST_ENDIANNESS,
86 .fixed_in = { /* ADC fixed settings */
94 .endianness = AUDIO_HOST_ENDIANNESS,
98 .period = { .hertz = 100 },
103 static AudioState glob_audio_state;
105 const struct mixeng_volume nominal_volume = {
116 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
119 static void audio_print_options (const char *prefix,
120 struct audio_option *opt);
122 int audio_bug (const char *funcname, int cond)
127 AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
129 struct audio_driver *d;
132 AUD_log (NULL, "Save all your work and restart without audio\n");
134 AUD_log (NULL, "I am sorry\n");
135 d = glob_audio_state.drv;
137 audio_print_options (d->name, d->options);
140 AUD_log (NULL, "Context:\n");
142 #if defined AUDIO_BREAKPOINT_ON_BUG
143 # if defined HOST_I386
144 # if defined __GNUC__
146 # elif defined _MSC_VER
161 static inline int audio_bits_to_index (int bits)
174 audio_bug ("bits_to_index", 1);
175 AUD_log (NULL, "invalid bits %d\n", bits);
180 void *audio_calloc (const char *funcname, int nmemb, size_t size)
186 cond = !nmemb || !size;
190 if (audio_bug ("audio_calloc", cond)) {
191 AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
193 AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
197 return g_malloc0 (len);
200 static char *audio_alloc_prefix (const char *s)
202 const char qemu_prefix[] = "QEMU_";
211 r = g_malloc (len + sizeof (qemu_prefix));
213 u = r + sizeof (qemu_prefix) - 1;
215 pstrcpy (r, len + sizeof (qemu_prefix), qemu_prefix);
216 pstrcat (r, len + sizeof (qemu_prefix), s);
218 for (i = 0; i < len; ++i) {
219 u[i] = qemu_toupper(u[i]);
225 static const char *audio_audfmt_to_string (audfmt_e fmt)
247 dolog ("Bogus audfmt %d returning S16\n", fmt);
251 static audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval,
254 if (!strcasecmp (s, "u8")) {
258 else if (!strcasecmp (s, "u16")) {
262 else if (!strcasecmp (s, "u32")) {
266 else if (!strcasecmp (s, "s8")) {
270 else if (!strcasecmp (s, "s16")) {
274 else if (!strcasecmp (s, "s32")) {
279 dolog ("Bogus audio format `%s' using %s\n",
280 s, audio_audfmt_to_string (defval));
286 static audfmt_e audio_get_conf_fmt (const char *envname,
290 const char *var = getenv (envname);
295 return audio_string_to_audfmt (var, defval, defaultp);
298 static int audio_get_conf_int (const char *key, int defval, int *defaultp)
303 strval = getenv (key);
315 static const char *audio_get_conf_str (const char *key,
319 const char *val = getenv (key);
330 void AUD_vlog (const char *cap, const char *fmt, va_list ap)
333 fprintf(stderr, "%s: ", cap);
336 vfprintf(stderr, fmt, ap);
339 void AUD_log (const char *cap, const char *fmt, ...)
344 AUD_vlog (cap, fmt, ap);
348 static void audio_print_options (const char *prefix,
349 struct audio_option *opt)
354 dolog ("No prefix specified\n");
359 dolog ("No options\n");
363 uprefix = audio_alloc_prefix (prefix);
365 for (; opt->name; opt++) {
366 const char *state = "default";
367 printf (" %s_%s: ", uprefix, opt->name);
369 if (opt->overriddenp && *opt->overriddenp) {
376 int *intp = opt->valp;
377 printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
383 int *intp = opt->valp;
384 printf ("integer, %s = %d\n", state, *intp);
390 audfmt_e *fmtp = opt->valp;
392 "format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
394 audio_audfmt_to_string (*fmtp)
401 const char **strp = opt->valp;
402 printf ("string, %s = %s\n",
404 *strp ? *strp : "(not set)");
410 dolog ("Bad value tag for option %s_%s %d\n",
411 uprefix, opt->name, opt->tag);
414 printf (" %s\n", opt->descr);
420 static void audio_process_options (const char *prefix,
421 struct audio_option *opt)
424 const char qemu_prefix[] = "QEMU_";
425 size_t preflen, optlen;
427 if (audio_bug (AUDIO_FUNC, !prefix)) {
428 dolog ("prefix = NULL\n");
432 if (audio_bug (AUDIO_FUNC, !opt)) {
433 dolog ("opt = NULL\n");
437 preflen = strlen (prefix);
439 for (; opt->name; opt++) {
444 dolog ("Option value pointer for `%s' is not set\n",
449 len = strlen (opt->name);
450 /* len of opt->name + len of prefix + size of qemu_prefix
451 * (includes trailing zero) + zero + underscore (on behalf of
453 optlen = len + preflen + sizeof (qemu_prefix) + 1;
454 optname = g_malloc (optlen);
456 pstrcpy (optname, optlen, qemu_prefix);
458 /* copy while upper-casing, including trailing zero */
459 for (i = 0; i <= preflen; ++i) {
460 optname[i + sizeof (qemu_prefix) - 1] = qemu_toupper(prefix[i]);
462 pstrcat (optname, optlen, "_");
463 pstrcat (optname, optlen, opt->name);
470 int *intp = opt->valp;
471 *intp = audio_get_conf_int (optname, *intp, &def);
477 audfmt_e *fmtp = opt->valp;
478 *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
484 const char **strp = opt->valp;
485 *strp = audio_get_conf_str (optname, *strp, &def);
490 dolog ("Bad value tag for option `%s' - %d\n",
495 if (!opt->overriddenp) {
496 opt->overriddenp = &opt->overridden;
498 *opt->overriddenp = !def;
503 static void audio_print_settings (struct audsettings *as)
505 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
509 AUD_log (NULL, "S8");
512 AUD_log (NULL, "U8");
515 AUD_log (NULL, "S16");
518 AUD_log (NULL, "U16");
521 AUD_log (NULL, "S32");
524 AUD_log (NULL, "U32");
527 AUD_log (NULL, "invalid(%d)", as->fmt);
531 AUD_log (NULL, " endianness=");
532 switch (as->endianness) {
534 AUD_log (NULL, "little");
537 AUD_log (NULL, "big");
540 AUD_log (NULL, "invalid");
543 AUD_log (NULL, "\n");
546 static int audio_validate_settings (struct audsettings *as)
550 invalid = as->nchannels != 1 && as->nchannels != 2;
551 invalid |= as->endianness != 0 && as->endianness != 1;
566 invalid |= as->freq <= 0;
567 return invalid ? -1 : 0;
570 static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
572 int bits = 8, sign = 0;
595 return info->freq == as->freq
596 && info->nchannels == as->nchannels
597 && info->sign == sign
598 && info->bits == bits
599 && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
602 void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
604 int bits = 8, sign = 0, shift = 0;
627 info->freq = as->freq;
630 info->nchannels = as->nchannels;
631 info->shift = (as->nchannels == 2) + shift;
632 info->align = (1 << info->shift) - 1;
633 info->bytes_per_second = info->freq << info->shift;
634 info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
637 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
644 memset (buf, 0x00, len << info->shift);
647 switch (info->bits) {
649 memset (buf, 0x80, len << info->shift);
656 int shift = info->nchannels - 1;
659 if (info->swap_endianness) {
663 for (i = 0; i < len << shift; i++) {
673 int shift = info->nchannels - 1;
674 int32_t s = INT32_MAX;
676 if (info->swap_endianness) {
680 for (i = 0; i < len << shift; i++) {
687 AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
697 static void noop_conv (struct st_sample *dst, const void *src, int samples)
704 static CaptureVoiceOut *audio_pcm_capture_find_specific (
705 struct audsettings *as
708 CaptureVoiceOut *cap;
709 AudioState *s = &glob_audio_state;
711 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
712 if (audio_pcm_info_eq (&cap->hw.info, as)) {
719 static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
721 struct capture_callback *cb;
724 dolog ("notification %d sent\n", cmd);
726 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
727 cb->ops.notify (cb->opaque, cmd);
731 static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
733 if (cap->hw.enabled != enabled) {
734 audcnotification_e cmd;
735 cap->hw.enabled = enabled;
736 cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
737 audio_notify_capture (cap, cmd);
741 static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
743 HWVoiceOut *hw = &cap->hw;
747 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
753 audio_capture_maybe_changed (cap, enabled);
756 static void audio_detach_capture (HWVoiceOut *hw)
758 SWVoiceCap *sc = hw->cap_head.lh_first;
761 SWVoiceCap *sc1 = sc->entries.le_next;
762 SWVoiceOut *sw = &sc->sw;
763 CaptureVoiceOut *cap = sc->cap;
764 int was_active = sw->active;
767 st_rate_stop (sw->rate);
771 QLIST_REMOVE (sw, entries);
772 QLIST_REMOVE (sc, entries);
775 /* We have removed soft voice from the capture:
776 this might have changed the overall status of the capture
777 since this might have been the only active voice */
778 audio_recalc_and_notify_capture (cap);
784 static int audio_attach_capture (HWVoiceOut *hw)
786 AudioState *s = &glob_audio_state;
787 CaptureVoiceOut *cap;
789 audio_detach_capture (hw);
790 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
793 HWVoiceOut *hw_cap = &cap->hw;
795 sc = audio_calloc (AUDIO_FUNC, 1, sizeof (*sc));
797 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
807 sw->active = hw->enabled;
808 sw->conv = noop_conv;
809 sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
810 sw->vol = nominal_volume;
811 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
813 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
817 QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
818 QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
820 sw->name = g_strdup_printf ("for %p %d,%d,%d",
821 hw, sw->info.freq, sw->info.bits,
823 dolog ("Added %s active = %d\n", sw->name, sw->active);
826 audio_capture_maybe_changed (cap, 1);
833 * Hard voice (capture)
835 static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
838 int m = hw->total_samples_captured;
840 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
842 m = audio_MIN (m, sw->total_hw_samples_acquired);
848 int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
850 int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
851 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
852 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
858 int audio_pcm_hw_clip_out (HWVoiceOut *hw, void *pcm_buf,
859 int live, int pending)
861 int left = hw->samples - pending;
862 int len = audio_MIN (left, live);
866 struct st_sample *src = hw->mix_buf + hw->rpos;
867 uint8_t *dst = advance (pcm_buf, hw->rpos << hw->info.shift);
868 int samples_till_end_of_buf = hw->samples - hw->rpos;
869 int samples_to_clip = audio_MIN (len, samples_till_end_of_buf);
871 hw->clip (dst, src, samples_to_clip);
873 hw->rpos = (hw->rpos + samples_to_clip) % hw->samples;
874 len -= samples_to_clip;
875 clipped += samples_to_clip;
881 * Soft voice (capture)
883 static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
885 HWVoiceIn *hw = sw->hw;
886 int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
889 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
890 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
894 rpos = hw->wpos - live;
899 return hw->samples + rpos;
903 int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
905 HWVoiceIn *hw = sw->hw;
906 int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
907 struct st_sample *src, *dst = sw->buf;
909 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
911 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
912 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
913 dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
917 samples = size >> sw->info.shift;
922 swlim = (live * sw->ratio) >> 32;
923 swlim = audio_MIN (swlim, samples);
926 src = hw->conv_buf + rpos;
927 isamp = hw->wpos - rpos;
930 isamp = hw->samples - rpos;
938 if (audio_bug (AUDIO_FUNC, osamp < 0)) {
939 dolog ("osamp=%d\n", osamp);
943 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
945 rpos = (rpos + isamp) % hw->samples;
951 if (!(hw->ctl_caps & VOICE_VOLUME_CAP)) {
952 mixeng_volume (sw->buf, ret, &sw->vol);
955 sw->clip (buf, sw->buf, ret);
956 sw->total_hw_samples_acquired += total;
957 return ret << sw->info.shift;
961 * Hard voice (playback)
963 static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
969 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
970 if (sw->active || !sw->empty) {
971 m = audio_MIN (m, sw->total_hw_samples_mixed);
980 static int audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
985 smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
993 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
994 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1003 * Soft voice (playback)
1005 int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
1007 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
1008 int ret = 0, pos = 0, total = 0;
1014 hwsamples = sw->hw->samples;
1016 live = sw->total_hw_samples_mixed;
1017 if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
1018 dolog ("live=%d hw->samples=%d\n", live, hwsamples);
1022 if (live == hwsamples) {
1024 dolog ("%s is full %d\n", sw->name, live);
1029 wpos = (sw->hw->rpos + live) % hwsamples;
1030 samples = size >> sw->info.shift;
1032 dead = hwsamples - live;
1033 swlim = ((int64_t) dead << 32) / sw->ratio;
1034 swlim = audio_MIN (swlim, samples);
1036 sw->conv (sw->buf, buf, swlim);
1038 if (!(sw->hw->ctl_caps & VOICE_VOLUME_CAP)) {
1039 mixeng_volume (sw->buf, swlim, &sw->vol);
1044 dead = hwsamples - live;
1045 left = hwsamples - wpos;
1046 blck = audio_MIN (dead, left);
1055 sw->hw->mix_buf + wpos,
1063 wpos = (wpos + osamp) % hwsamples;
1067 sw->total_hw_samples_mixed += total;
1068 sw->empty = sw->total_hw_samples_mixed == 0;
1072 "%s: write size %d ret %d total sw %d\n",
1074 size >> sw->info.shift,
1076 sw->total_hw_samples_mixed
1080 return ret << sw->info.shift;
1084 static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
1086 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1087 cap, info->bits, info->sign, info->freq, info->nchannels);
1092 #include "audio_template.h"
1094 #include "audio_template.h"
1099 static int audio_is_timer_needed (void)
1101 HWVoiceIn *hwi = NULL;
1102 HWVoiceOut *hwo = NULL;
1104 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1105 if (!hwo->poll_mode) return 1;
1107 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1108 if (!hwi->poll_mode) return 1;
1113 static void audio_reset_timer (AudioState *s)
1115 if (audio_is_timer_needed ()) {
1116 timer_mod_anticipate_ns(s->ts,
1117 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + conf.period.ticks);
1124 static void audio_timer (void *opaque)
1126 audio_run ("timer");
1127 audio_reset_timer (opaque);
1133 int AUD_write (SWVoiceOut *sw, void *buf, int size)
1136 /* XXX: Consider options */
1140 if (!sw->hw->enabled) {
1141 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
1145 return sw->hw->pcm_ops->write(sw, buf, size);
1148 int AUD_read (SWVoiceIn *sw, void *buf, int size)
1151 /* XXX: Consider options */
1155 if (!sw->hw->enabled) {
1156 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1160 return sw->hw->pcm_ops->read(sw, buf, size);
1163 int AUD_get_buffer_size_out (SWVoiceOut *sw)
1165 return sw->hw->samples << sw->hw->info.shift;
1168 void AUD_set_active_out (SWVoiceOut *sw, int on)
1177 if (sw->active != on) {
1178 AudioState *s = &glob_audio_state;
1179 SWVoiceOut *temp_sw;
1183 hw->pending_disable = 0;
1186 if (s->vm_running) {
1187 hw->pcm_ops->ctl_out (hw, VOICE_ENABLE, conf.try_poll_out);
1188 audio_reset_timer (s);
1196 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1197 temp_sw = temp_sw->entries.le_next) {
1198 nb_active += temp_sw->active != 0;
1201 hw->pending_disable = nb_active == 1;
1205 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1206 sc->sw.active = hw->enabled;
1208 audio_capture_maybe_changed (sc->cap, 1);
1215 void AUD_set_active_in (SWVoiceIn *sw, int on)
1224 if (sw->active != on) {
1225 AudioState *s = &glob_audio_state;
1231 if (s->vm_running) {
1232 hw->pcm_ops->ctl_in (hw, VOICE_ENABLE, conf.try_poll_in);
1233 audio_reset_timer (s);
1236 sw->total_hw_samples_acquired = hw->total_samples_captured;
1242 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1243 temp_sw = temp_sw->entries.le_next) {
1244 nb_active += temp_sw->active != 0;
1247 if (nb_active == 1) {
1249 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
1257 static int audio_get_avail (SWVoiceIn *sw)
1265 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1266 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1267 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1272 "%s: get_avail live %d ret %" PRId64 "\n",
1274 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1277 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1280 static int audio_get_free (SWVoiceOut *sw)
1288 live = sw->total_hw_samples_mixed;
1290 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1291 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1295 dead = sw->hw->samples - live;
1298 dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
1300 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1303 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1306 static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1313 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1314 SWVoiceOut *sw = &sc->sw;
1319 int till_end_of_hw = hw->samples - rpos2;
1320 int to_write = audio_MIN (till_end_of_hw, n);
1321 int bytes = to_write << hw->info.shift;
1324 sw->buf = hw->mix_buf + rpos2;
1325 written = audio_pcm_sw_write (sw, NULL, bytes);
1326 if (written - bytes) {
1327 dolog ("Could not mix %d bytes into a capture "
1328 "buffer, mixed %d\n",
1333 rpos2 = (rpos2 + to_write) % hw->samples;
1338 n = audio_MIN (samples, hw->samples - rpos);
1339 mixeng_clear (hw->mix_buf + rpos, n);
1340 mixeng_clear (hw->mix_buf, samples - n);
1343 static void audio_run_out (AudioState *s)
1345 HWVoiceOut *hw = NULL;
1348 while ((hw = audio_pcm_hw_find_any_enabled_out (hw))) {
1350 int live, free, nb_live, cleanup_required, prev_rpos;
1352 live = audio_pcm_hw_get_live_out (hw, &nb_live);
1357 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1358 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1362 if (hw->pending_disable && !nb_live) {
1365 dolog ("Disabling voice\n");
1368 hw->pending_disable = 0;
1369 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
1370 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1372 audio_recalc_and_notify_capture (sc->cap);
1378 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1380 free = audio_get_free (sw);
1382 sw->callback.fn (sw->callback.opaque, free);
1389 prev_rpos = hw->rpos;
1390 played = hw->pcm_ops->run_out (hw, live);
1391 replay_audio_out(&played);
1392 if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1393 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1394 hw->rpos, hw->samples, played);
1399 dolog ("played=%d\n", played);
1403 hw->ts_helper += played;
1404 audio_capture_mix_and_clear (hw, prev_rpos, played);
1407 cleanup_required = 0;
1408 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1409 if (!sw->active && sw->empty) {
1413 if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1414 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1415 played, sw->total_hw_samples_mixed);
1416 played = sw->total_hw_samples_mixed;
1419 sw->total_hw_samples_mixed -= played;
1421 if (!sw->total_hw_samples_mixed) {
1423 cleanup_required |= !sw->active && !sw->callback.fn;
1427 free = audio_get_free (sw);
1429 sw->callback.fn (sw->callback.opaque, free);
1434 if (cleanup_required) {
1437 sw = hw->sw_head.lh_first;
1439 sw1 = sw->entries.le_next;
1440 if (!sw->active && !sw->callback.fn) {
1441 audio_close_out (sw);
1449 static void audio_run_in (AudioState *s)
1451 HWVoiceIn *hw = NULL;
1453 while ((hw = audio_pcm_hw_find_any_enabled_in (hw))) {
1455 int captured = 0, min;
1457 if (replay_mode != REPLAY_MODE_PLAY) {
1458 captured = hw->pcm_ops->run_in(hw);
1460 replay_audio_in(&captured, hw->conv_buf, &hw->wpos, hw->samples);
1462 min = audio_pcm_hw_find_min_in (hw);
1463 hw->total_samples_captured += captured - min;
1464 hw->ts_helper += captured;
1466 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1467 sw->total_hw_samples_acquired -= min;
1472 avail = audio_get_avail (sw);
1474 sw->callback.fn (sw->callback.opaque, avail);
1481 static void audio_run_capture (AudioState *s)
1483 CaptureVoiceOut *cap;
1485 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1486 int live, rpos, captured;
1487 HWVoiceOut *hw = &cap->hw;
1490 captured = live = audio_pcm_hw_get_live_out (hw, NULL);
1493 int left = hw->samples - rpos;
1494 int to_capture = audio_MIN (live, left);
1495 struct st_sample *src;
1496 struct capture_callback *cb;
1498 src = hw->mix_buf + rpos;
1499 hw->clip (cap->buf, src, to_capture);
1500 mixeng_clear (src, to_capture);
1502 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1503 cb->ops.capture (cb->opaque, cap->buf,
1504 to_capture << hw->info.shift);
1506 rpos = (rpos + to_capture) % hw->samples;
1511 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1512 if (!sw->active && sw->empty) {
1516 if (audio_bug (AUDIO_FUNC, captured > sw->total_hw_samples_mixed)) {
1517 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1518 captured, sw->total_hw_samples_mixed);
1519 captured = sw->total_hw_samples_mixed;
1522 sw->total_hw_samples_mixed -= captured;
1523 sw->empty = sw->total_hw_samples_mixed == 0;
1528 void audio_run (const char *msg)
1530 AudioState *s = &glob_audio_state;
1534 audio_run_capture (s);
1537 static double prevtime;
1541 if (gettimeofday (&tv, NULL)) {
1542 perror ("audio_run: gettimeofday");
1546 currtime = tv.tv_sec + tv.tv_usec * 1e-6;
1547 dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime);
1548 prevtime = currtime;
1553 static struct audio_option audio_options[] = {
1556 .name = "DAC_FIXED_SETTINGS",
1557 .tag = AUD_OPT_BOOL,
1558 .valp = &conf.fixed_out.enabled,
1559 .descr = "Use fixed settings for host DAC"
1562 .name = "DAC_FIXED_FREQ",
1564 .valp = &conf.fixed_out.settings.freq,
1565 .descr = "Frequency for fixed host DAC"
1568 .name = "DAC_FIXED_FMT",
1570 .valp = &conf.fixed_out.settings.fmt,
1571 .descr = "Format for fixed host DAC"
1574 .name = "DAC_FIXED_CHANNELS",
1576 .valp = &conf.fixed_out.settings.nchannels,
1577 .descr = "Number of channels for fixed DAC (1 - mono, 2 - stereo)"
1580 .name = "DAC_VOICES",
1582 .valp = &conf.fixed_out.nb_voices,
1583 .descr = "Number of voices for DAC"
1586 .name = "DAC_TRY_POLL",
1587 .tag = AUD_OPT_BOOL,
1588 .valp = &conf.try_poll_out,
1589 .descr = "Attempt using poll mode for DAC"
1593 .name = "ADC_FIXED_SETTINGS",
1594 .tag = AUD_OPT_BOOL,
1595 .valp = &conf.fixed_in.enabled,
1596 .descr = "Use fixed settings for host ADC"
1599 .name = "ADC_FIXED_FREQ",
1601 .valp = &conf.fixed_in.settings.freq,
1602 .descr = "Frequency for fixed host ADC"
1605 .name = "ADC_FIXED_FMT",
1607 .valp = &conf.fixed_in.settings.fmt,
1608 .descr = "Format for fixed host ADC"
1611 .name = "ADC_FIXED_CHANNELS",
1613 .valp = &conf.fixed_in.settings.nchannels,
1614 .descr = "Number of channels for fixed ADC (1 - mono, 2 - stereo)"
1617 .name = "ADC_VOICES",
1619 .valp = &conf.fixed_in.nb_voices,
1620 .descr = "Number of voices for ADC"
1623 .name = "ADC_TRY_POLL",
1624 .tag = AUD_OPT_BOOL,
1625 .valp = &conf.try_poll_in,
1626 .descr = "Attempt using poll mode for ADC"
1630 .name = "TIMER_PERIOD",
1632 .valp = &conf.period.hertz,
1633 .descr = "Timer period in HZ (0 - use lowest possible)"
1635 { /* End of list */ }
1638 static void audio_pp_nb_voices (const char *typ, int nb)
1642 printf ("Does not support %s\n", typ);
1645 printf ("One %s voice\n", typ);
1648 printf ("Theoretically supports many %s voices\n", typ);
1651 printf ("Theoretically supports up to %d %s voices\n", nb, typ);
1657 void AUD_help (void)
1661 audio_process_options ("AUDIO", audio_options);
1662 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1663 struct audio_driver *d = drvtab[i];
1665 audio_process_options (d->name, d->options);
1669 printf ("Audio options:\n");
1670 audio_print_options ("AUDIO", audio_options);
1673 printf ("Available drivers:\n");
1675 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1676 struct audio_driver *d = drvtab[i];
1678 printf ("Name: %s\n", d->name);
1679 printf ("Description: %s\n", d->descr);
1681 audio_pp_nb_voices ("playback", d->max_voices_out);
1682 audio_pp_nb_voices ("capture", d->max_voices_in);
1685 printf ("Options:\n");
1686 audio_print_options (d->name, d->options);
1689 printf ("No options\n");
1695 "Options are settable through environment variables.\n"
1698 " set QEMU_AUDIO_DRV=wav\n"
1699 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1701 " export QEMU_AUDIO_DRV=wav\n"
1702 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1703 "(for csh replace export with setenv in the above)\n"
1709 static int audio_driver_init (AudioState *s, struct audio_driver *drv)
1712 audio_process_options (drv->name, drv->options);
1714 s->drv_opaque = drv->init ();
1716 if (s->drv_opaque) {
1717 audio_init_nb_voices_out (drv);
1718 audio_init_nb_voices_in (drv);
1723 dolog ("Could not init `%s' audio driver\n", drv->name);
1728 static void audio_vm_change_state_handler (void *opaque, int running,
1731 AudioState *s = opaque;
1732 HWVoiceOut *hwo = NULL;
1733 HWVoiceIn *hwi = NULL;
1734 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1736 s->vm_running = running;
1737 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1738 hwo->pcm_ops->ctl_out (hwo, op, conf.try_poll_out);
1741 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1742 hwi->pcm_ops->ctl_in (hwi, op, conf.try_poll_in);
1744 audio_reset_timer (s);
1747 static bool is_cleaning_up;
1749 bool audio_is_cleaning_up(void)
1751 return is_cleaning_up;
1754 void audio_cleanup(void)
1756 AudioState *s = &glob_audio_state;
1757 HWVoiceOut *hwo, *hwon;
1758 HWVoiceIn *hwi, *hwin;
1760 is_cleaning_up = true;
1761 QLIST_FOREACH_SAFE(hwo, &glob_audio_state.hw_head_out, entries, hwon) {
1765 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1767 hwo->pcm_ops->fini_out (hwo);
1769 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1770 CaptureVoiceOut *cap = sc->cap;
1771 struct capture_callback *cb;
1773 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1774 cb->ops.destroy (cb->opaque);
1777 QLIST_REMOVE(hwo, entries);
1780 QLIST_FOREACH_SAFE(hwi, &glob_audio_state.hw_head_in, entries, hwin) {
1782 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1784 hwi->pcm_ops->fini_in (hwi);
1785 QLIST_REMOVE(hwi, entries);
1789 s->drv->fini (s->drv_opaque);
1794 static const VMStateDescription vmstate_audio = {
1797 .minimum_version_id = 1,
1798 .fields = (VMStateField[]) {
1799 VMSTATE_END_OF_LIST()
1803 static void audio_init (void)
1807 const char *drvname;
1808 VMChangeStateEntry *e;
1809 AudioState *s = &glob_audio_state;
1815 QLIST_INIT (&s->hw_head_out);
1816 QLIST_INIT (&s->hw_head_in);
1817 QLIST_INIT (&s->cap_head);
1818 atexit(audio_cleanup);
1820 s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
1822 audio_process_options ("AUDIO", audio_options);
1824 s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1825 s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1827 if (s->nb_hw_voices_out <= 0) {
1828 dolog ("Bogus number of playback voices %d, setting to 1\n",
1829 s->nb_hw_voices_out);
1830 s->nb_hw_voices_out = 1;
1833 if (s->nb_hw_voices_in <= 0) {
1834 dolog ("Bogus number of capture voices %d, setting to 0\n",
1835 s->nb_hw_voices_in);
1836 s->nb_hw_voices_in = 0;
1841 drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1847 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1848 if (!strcmp (drvname, drvtab[i]->name)) {
1849 done = !audio_driver_init (s, drvtab[i]);
1856 dolog ("Unknown audio driver `%s'\n", drvname);
1857 dolog ("Run with -audio-help to list available drivers\n");
1862 for (i = 0; !done && i < ARRAY_SIZE (drvtab); i++) {
1863 if (drvtab[i]->can_be_default) {
1864 done = !audio_driver_init (s, drvtab[i]);
1870 done = !audio_driver_init (s, &no_audio_driver);
1872 dolog("warning: Using timer based audio emulation\n");
1875 if (conf.period.hertz <= 0) {
1876 if (conf.period.hertz < 0) {
1877 dolog ("warning: Timer period is negative - %d "
1878 "treating as zero\n",
1881 conf.period.ticks = 1;
1883 conf.period.ticks = NANOSECONDS_PER_SECOND / conf.period.hertz;
1886 e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1888 dolog ("warning: Could not register change state handler\n"
1889 "(Audio can continue looping even after stopping the VM)\n");
1892 QLIST_INIT (&s->card_head);
1893 vmstate_register (NULL, 0, &vmstate_audio, s);
1896 void AUD_register_card (const char *name, QEMUSoundCard *card)
1899 card->name = g_strdup (name);
1900 memset (&card->entries, 0, sizeof (card->entries));
1901 QLIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries);
1904 void AUD_remove_card (QEMUSoundCard *card)
1906 QLIST_REMOVE (card, entries);
1907 g_free (card->name);
1911 CaptureVoiceOut *AUD_add_capture (
1912 struct audsettings *as,
1913 struct audio_capture_ops *ops,
1917 AudioState *s = &glob_audio_state;
1918 CaptureVoiceOut *cap;
1919 struct capture_callback *cb;
1921 if (audio_validate_settings (as)) {
1922 dolog ("Invalid settings were passed when trying to add capture\n");
1923 audio_print_settings (as);
1927 cb = audio_calloc (AUDIO_FUNC, 1, sizeof (*cb));
1929 dolog ("Could not allocate capture callback information, size %zu\n",
1934 cb->opaque = cb_opaque;
1936 cap = audio_pcm_capture_find_specific (as);
1938 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1943 CaptureVoiceOut *cap;
1945 cap = audio_calloc (AUDIO_FUNC, 1, sizeof (*cap));
1947 dolog ("Could not allocate capture voice, size %zu\n",
1953 QLIST_INIT (&hw->sw_head);
1954 QLIST_INIT (&cap->cb_head);
1956 /* XXX find a more elegant way */
1957 hw->samples = 4096 * 4;
1958 hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples,
1959 sizeof (struct st_sample));
1961 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1966 audio_pcm_init_info (&hw->info, as);
1968 cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
1970 dolog ("Could not allocate capture buffer "
1971 "(%d samples, each %d bytes)\n",
1972 hw->samples, 1 << hw->info.shift);
1976 hw->clip = mixeng_clip
1977 [hw->info.nchannels == 2]
1979 [hw->info.swap_endianness]
1980 [audio_bits_to_index (hw->info.bits)];
1982 QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
1983 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1985 QLIST_FOREACH(hw, &glob_audio_state.hw_head_out, entries) {
1986 audio_attach_capture (hw);
1991 g_free (cap->hw.mix_buf);
2001 void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
2003 struct capture_callback *cb;
2005 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
2006 if (cb->opaque == cb_opaque) {
2007 cb->ops.destroy (cb_opaque);
2008 QLIST_REMOVE (cb, entries);
2011 if (!cap->cb_head.lh_first) {
2012 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
2015 SWVoiceCap *sc = (SWVoiceCap *) sw;
2016 #ifdef DEBUG_CAPTURE
2017 dolog ("freeing %s\n", sw->name);
2020 sw1 = sw->entries.le_next;
2022 st_rate_stop (sw->rate);
2025 QLIST_REMOVE (sw, entries);
2026 QLIST_REMOVE (sc, entries);
2030 QLIST_REMOVE (cap, entries);
2031 g_free (cap->hw.mix_buf);
2040 void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
2043 HWVoiceOut *hw = sw->hw;
2045 sw->vol.mute = mute;
2046 sw->vol.l = nominal_volume.l * lvol / 255;
2047 sw->vol.r = nominal_volume.r * rvol / 255;
2049 if (hw->pcm_ops->ctl_out) {
2050 hw->pcm_ops->ctl_out (hw, VOICE_VOLUME, sw);
2055 void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
2058 HWVoiceIn *hw = sw->hw;
2060 sw->vol.mute = mute;
2061 sw->vol.l = nominal_volume.l * lvol / 255;
2062 sw->vol.r = nominal_volume.r * rvol / 255;
2064 if (hw->pcm_ops->ctl_in) {
2065 hw->pcm_ops->ctl_in (hw, VOICE_VOLUME, sw);