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"
32 #define AUDIO_CAP "audio"
33 #include "audio_int.h"
35 /* #define DEBUG_LIVE */
36 /* #define DEBUG_OUT */
37 /* #define DEBUG_CAPTURE */
38 /* #define DEBUG_POLL */
40 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
43 /* Order of CONFIG_AUDIO_DRIVERS is import.
44 The 1st one is the one used by default, that is the reason
45 that we generate the list.
47 static struct audio_driver *drvtab[] = {
56 struct fixed_settings {
60 struct audsettings settings;
64 struct fixed_settings fixed_out;
65 struct fixed_settings fixed_in;
73 .fixed_out = { /* DAC fixed settings */
81 .endianness = AUDIO_HOST_ENDIANNESS,
85 .fixed_in = { /* ADC fixed settings */
93 .endianness = AUDIO_HOST_ENDIANNESS,
97 .period = { .hertz = 100 },
102 static AudioState glob_audio_state;
104 const struct mixeng_volume nominal_volume = {
115 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
118 static void audio_print_options (const char *prefix,
119 struct audio_option *opt);
121 int audio_bug (const char *funcname, int cond)
126 AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
128 struct audio_driver *d;
131 AUD_log (NULL, "Save all your work and restart without audio\n");
133 AUD_log (NULL, "I am sorry\n");
134 d = glob_audio_state.drv;
136 audio_print_options (d->name, d->options);
139 AUD_log (NULL, "Context:\n");
141 #if defined AUDIO_BREAKPOINT_ON_BUG
142 # if defined HOST_I386
143 # if defined __GNUC__
145 # elif defined _MSC_VER
160 static inline int audio_bits_to_index (int bits)
173 audio_bug ("bits_to_index", 1);
174 AUD_log (NULL, "invalid bits %d\n", bits);
179 void *audio_calloc (const char *funcname, int nmemb, size_t size)
185 cond = !nmemb || !size;
189 if (audio_bug ("audio_calloc", cond)) {
190 AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
192 AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
196 return g_malloc0 (len);
199 static char *audio_alloc_prefix (const char *s)
201 const char qemu_prefix[] = "QEMU_";
210 r = g_malloc (len + sizeof (qemu_prefix));
212 u = r + sizeof (qemu_prefix) - 1;
214 pstrcpy (r, len + sizeof (qemu_prefix), qemu_prefix);
215 pstrcat (r, len + sizeof (qemu_prefix), s);
217 for (i = 0; i < len; ++i) {
218 u[i] = qemu_toupper(u[i]);
224 static const char *audio_audfmt_to_string (audfmt_e fmt)
246 dolog ("Bogus audfmt %d returning S16\n", fmt);
250 static audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval,
253 if (!strcasecmp (s, "u8")) {
257 else if (!strcasecmp (s, "u16")) {
261 else if (!strcasecmp (s, "u32")) {
265 else if (!strcasecmp (s, "s8")) {
269 else if (!strcasecmp (s, "s16")) {
273 else if (!strcasecmp (s, "s32")) {
278 dolog ("Bogus audio format `%s' using %s\n",
279 s, audio_audfmt_to_string (defval));
285 static audfmt_e audio_get_conf_fmt (const char *envname,
289 const char *var = getenv (envname);
294 return audio_string_to_audfmt (var, defval, defaultp);
297 static int audio_get_conf_int (const char *key, int defval, int *defaultp)
302 strval = getenv (key);
314 static const char *audio_get_conf_str (const char *key,
318 const char *val = getenv (key);
329 void AUD_vlog (const char *cap, const char *fmt, va_list ap)
332 fprintf(stderr, "%s: ", cap);
335 vfprintf(stderr, fmt, ap);
338 void AUD_log (const char *cap, const char *fmt, ...)
343 AUD_vlog (cap, fmt, ap);
347 static void audio_print_options (const char *prefix,
348 struct audio_option *opt)
353 dolog ("No prefix specified\n");
358 dolog ("No options\n");
362 uprefix = audio_alloc_prefix (prefix);
364 for (; opt->name; opt++) {
365 const char *state = "default";
366 printf (" %s_%s: ", uprefix, opt->name);
368 if (opt->overriddenp && *opt->overriddenp) {
375 int *intp = opt->valp;
376 printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
382 int *intp = opt->valp;
383 printf ("integer, %s = %d\n", state, *intp);
389 audfmt_e *fmtp = opt->valp;
391 "format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
393 audio_audfmt_to_string (*fmtp)
400 const char **strp = opt->valp;
401 printf ("string, %s = %s\n",
403 *strp ? *strp : "(not set)");
409 dolog ("Bad value tag for option %s_%s %d\n",
410 uprefix, opt->name, opt->tag);
413 printf (" %s\n", opt->descr);
419 static void audio_process_options (const char *prefix,
420 struct audio_option *opt)
423 const char qemu_prefix[] = "QEMU_";
424 size_t preflen, optlen;
426 if (audio_bug (AUDIO_FUNC, !prefix)) {
427 dolog ("prefix = NULL\n");
431 if (audio_bug (AUDIO_FUNC, !opt)) {
432 dolog ("opt = NULL\n");
436 preflen = strlen (prefix);
438 for (; opt->name; opt++) {
443 dolog ("Option value pointer for `%s' is not set\n",
448 len = strlen (opt->name);
449 /* len of opt->name + len of prefix + size of qemu_prefix
450 * (includes trailing zero) + zero + underscore (on behalf of
452 optlen = len + preflen + sizeof (qemu_prefix) + 1;
453 optname = g_malloc (optlen);
455 pstrcpy (optname, optlen, qemu_prefix);
457 /* copy while upper-casing, including trailing zero */
458 for (i = 0; i <= preflen; ++i) {
459 optname[i + sizeof (qemu_prefix) - 1] = qemu_toupper(prefix[i]);
461 pstrcat (optname, optlen, "_");
462 pstrcat (optname, optlen, opt->name);
469 int *intp = opt->valp;
470 *intp = audio_get_conf_int (optname, *intp, &def);
476 audfmt_e *fmtp = opt->valp;
477 *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
483 const char **strp = opt->valp;
484 *strp = audio_get_conf_str (optname, *strp, &def);
489 dolog ("Bad value tag for option `%s' - %d\n",
494 if (!opt->overriddenp) {
495 opt->overriddenp = &opt->overridden;
497 *opt->overriddenp = !def;
502 static void audio_print_settings (struct audsettings *as)
504 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
508 AUD_log (NULL, "S8");
511 AUD_log (NULL, "U8");
514 AUD_log (NULL, "S16");
517 AUD_log (NULL, "U16");
520 AUD_log (NULL, "S32");
523 AUD_log (NULL, "U32");
526 AUD_log (NULL, "invalid(%d)", as->fmt);
530 AUD_log (NULL, " endianness=");
531 switch (as->endianness) {
533 AUD_log (NULL, "little");
536 AUD_log (NULL, "big");
539 AUD_log (NULL, "invalid");
542 AUD_log (NULL, "\n");
545 static int audio_validate_settings (struct audsettings *as)
549 invalid = as->nchannels != 1 && as->nchannels != 2;
550 invalid |= as->endianness != 0 && as->endianness != 1;
565 invalid |= as->freq <= 0;
566 return invalid ? -1 : 0;
569 static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
571 int bits = 8, sign = 0;
594 return info->freq == as->freq
595 && info->nchannels == as->nchannels
596 && info->sign == sign
597 && info->bits == bits
598 && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
601 void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
603 int bits = 8, sign = 0, shift = 0;
626 info->freq = as->freq;
629 info->nchannels = as->nchannels;
630 info->shift = (as->nchannels == 2) + shift;
631 info->align = (1 << info->shift) - 1;
632 info->bytes_per_second = info->freq << info->shift;
633 info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
636 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
643 memset (buf, 0x00, len << info->shift);
646 switch (info->bits) {
648 memset (buf, 0x80, len << info->shift);
655 int shift = info->nchannels - 1;
658 if (info->swap_endianness) {
662 for (i = 0; i < len << shift; i++) {
672 int shift = info->nchannels - 1;
673 int32_t s = INT32_MAX;
675 if (info->swap_endianness) {
679 for (i = 0; i < len << shift; i++) {
686 AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
696 static void noop_conv (struct st_sample *dst, const void *src, int samples)
703 static CaptureVoiceOut *audio_pcm_capture_find_specific (
704 struct audsettings *as
707 CaptureVoiceOut *cap;
708 AudioState *s = &glob_audio_state;
710 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
711 if (audio_pcm_info_eq (&cap->hw.info, as)) {
718 static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
720 struct capture_callback *cb;
723 dolog ("notification %d sent\n", cmd);
725 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
726 cb->ops.notify (cb->opaque, cmd);
730 static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
732 if (cap->hw.enabled != enabled) {
733 audcnotification_e cmd;
734 cap->hw.enabled = enabled;
735 cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
736 audio_notify_capture (cap, cmd);
740 static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
742 HWVoiceOut *hw = &cap->hw;
746 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
752 audio_capture_maybe_changed (cap, enabled);
755 static void audio_detach_capture (HWVoiceOut *hw)
757 SWVoiceCap *sc = hw->cap_head.lh_first;
760 SWVoiceCap *sc1 = sc->entries.le_next;
761 SWVoiceOut *sw = &sc->sw;
762 CaptureVoiceOut *cap = sc->cap;
763 int was_active = sw->active;
766 st_rate_stop (sw->rate);
770 QLIST_REMOVE (sw, entries);
771 QLIST_REMOVE (sc, entries);
774 /* We have removed soft voice from the capture:
775 this might have changed the overall status of the capture
776 since this might have been the only active voice */
777 audio_recalc_and_notify_capture (cap);
783 static int audio_attach_capture (HWVoiceOut *hw)
785 AudioState *s = &glob_audio_state;
786 CaptureVoiceOut *cap;
788 audio_detach_capture (hw);
789 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
792 HWVoiceOut *hw_cap = &cap->hw;
794 sc = audio_calloc (AUDIO_FUNC, 1, sizeof (*sc));
796 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
806 sw->active = hw->enabled;
807 sw->conv = noop_conv;
808 sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
809 sw->vol = nominal_volume;
810 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
812 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
816 QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
817 QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
819 sw->name = g_strdup_printf ("for %p %d,%d,%d",
820 hw, sw->info.freq, sw->info.bits,
822 dolog ("Added %s active = %d\n", sw->name, sw->active);
825 audio_capture_maybe_changed (cap, 1);
832 * Hard voice (capture)
834 static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
837 int m = hw->total_samples_captured;
839 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
841 m = audio_MIN (m, sw->total_hw_samples_acquired);
847 int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
849 int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
850 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
851 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
857 int audio_pcm_hw_clip_out (HWVoiceOut *hw, void *pcm_buf,
858 int live, int pending)
860 int left = hw->samples - pending;
861 int len = audio_MIN (left, live);
865 struct st_sample *src = hw->mix_buf + hw->rpos;
866 uint8_t *dst = advance (pcm_buf, hw->rpos << hw->info.shift);
867 int samples_till_end_of_buf = hw->samples - hw->rpos;
868 int samples_to_clip = audio_MIN (len, samples_till_end_of_buf);
870 hw->clip (dst, src, samples_to_clip);
872 hw->rpos = (hw->rpos + samples_to_clip) % hw->samples;
873 len -= samples_to_clip;
874 clipped += samples_to_clip;
880 * Soft voice (capture)
882 static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
884 HWVoiceIn *hw = sw->hw;
885 int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
888 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
889 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
893 rpos = hw->wpos - live;
898 return hw->samples + rpos;
902 int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
904 HWVoiceIn *hw = sw->hw;
905 int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
906 struct st_sample *src, *dst = sw->buf;
908 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
910 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
911 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
912 dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
916 samples = size >> sw->info.shift;
921 swlim = (live * sw->ratio) >> 32;
922 swlim = audio_MIN (swlim, samples);
925 src = hw->conv_buf + rpos;
926 isamp = hw->wpos - rpos;
929 isamp = hw->samples - rpos;
937 if (audio_bug (AUDIO_FUNC, osamp < 0)) {
938 dolog ("osamp=%d\n", osamp);
942 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
944 rpos = (rpos + isamp) % hw->samples;
950 if (!(hw->ctl_caps & VOICE_VOLUME_CAP)) {
951 mixeng_volume (sw->buf, ret, &sw->vol);
954 sw->clip (buf, sw->buf, ret);
955 sw->total_hw_samples_acquired += total;
956 return ret << sw->info.shift;
960 * Hard voice (playback)
962 static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
968 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
969 if (sw->active || !sw->empty) {
970 m = audio_MIN (m, sw->total_hw_samples_mixed);
979 static int audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
984 smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
992 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
993 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);
1037 if (!(sw->hw->ctl_caps & VOICE_VOLUME_CAP)) {
1038 mixeng_volume (sw->buf, swlim, &sw->vol);
1043 dead = hwsamples - live;
1044 left = hwsamples - wpos;
1045 blck = audio_MIN (dead, left);
1054 sw->hw->mix_buf + wpos,
1062 wpos = (wpos + osamp) % hwsamples;
1066 sw->total_hw_samples_mixed += total;
1067 sw->empty = sw->total_hw_samples_mixed == 0;
1071 "%s: write size %d ret %d total sw %d\n",
1073 size >> sw->info.shift,
1075 sw->total_hw_samples_mixed
1079 return ret << sw->info.shift;
1083 static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
1085 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1086 cap, info->bits, info->sign, info->freq, info->nchannels);
1091 #include "audio_template.h"
1093 #include "audio_template.h"
1098 static int audio_is_timer_needed (void)
1100 HWVoiceIn *hwi = NULL;
1101 HWVoiceOut *hwo = NULL;
1103 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1104 if (!hwo->poll_mode) return 1;
1106 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1107 if (!hwi->poll_mode) return 1;
1112 static void audio_reset_timer (AudioState *s)
1114 if (audio_is_timer_needed ()) {
1116 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + conf.period.ticks);
1123 static void audio_timer (void *opaque)
1125 audio_run ("timer");
1126 audio_reset_timer (opaque);
1132 int AUD_write (SWVoiceOut *sw, void *buf, int size)
1135 /* XXX: Consider options */
1139 if (!sw->hw->enabled) {
1140 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
1144 return sw->hw->pcm_ops->write(sw, buf, size);
1147 int AUD_read (SWVoiceIn *sw, void *buf, int size)
1150 /* XXX: Consider options */
1154 if (!sw->hw->enabled) {
1155 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1159 return sw->hw->pcm_ops->read(sw, buf, size);
1162 int AUD_get_buffer_size_out (SWVoiceOut *sw)
1164 return sw->hw->samples << sw->hw->info.shift;
1167 void AUD_set_active_out (SWVoiceOut *sw, int on)
1176 if (sw->active != on) {
1177 AudioState *s = &glob_audio_state;
1178 SWVoiceOut *temp_sw;
1182 hw->pending_disable = 0;
1185 if (s->vm_running) {
1186 hw->pcm_ops->ctl_out (hw, VOICE_ENABLE, conf.try_poll_out);
1187 audio_reset_timer (s);
1195 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1196 temp_sw = temp_sw->entries.le_next) {
1197 nb_active += temp_sw->active != 0;
1200 hw->pending_disable = nb_active == 1;
1204 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1205 sc->sw.active = hw->enabled;
1207 audio_capture_maybe_changed (sc->cap, 1);
1214 void AUD_set_active_in (SWVoiceIn *sw, int on)
1223 if (sw->active != on) {
1224 AudioState *s = &glob_audio_state;
1230 if (s->vm_running) {
1231 hw->pcm_ops->ctl_in (hw, VOICE_ENABLE, conf.try_poll_in);
1232 audio_reset_timer (s);
1235 sw->total_hw_samples_acquired = hw->total_samples_captured;
1241 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1242 temp_sw = temp_sw->entries.le_next) {
1243 nb_active += temp_sw->active != 0;
1246 if (nb_active == 1) {
1248 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
1256 static int audio_get_avail (SWVoiceIn *sw)
1264 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1265 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1266 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1271 "%s: get_avail live %d ret %" PRId64 "\n",
1273 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1276 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1279 static int audio_get_free (SWVoiceOut *sw)
1287 live = sw->total_hw_samples_mixed;
1289 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1290 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1294 dead = sw->hw->samples - live;
1297 dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
1299 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1302 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1305 static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1312 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1313 SWVoiceOut *sw = &sc->sw;
1318 int till_end_of_hw = hw->samples - rpos2;
1319 int to_write = audio_MIN (till_end_of_hw, n);
1320 int bytes = to_write << hw->info.shift;
1323 sw->buf = hw->mix_buf + rpos2;
1324 written = audio_pcm_sw_write (sw, NULL, bytes);
1325 if (written - bytes) {
1326 dolog ("Could not mix %d bytes into a capture "
1327 "buffer, mixed %d\n",
1332 rpos2 = (rpos2 + to_write) % hw->samples;
1337 n = audio_MIN (samples, hw->samples - rpos);
1338 mixeng_clear (hw->mix_buf + rpos, n);
1339 mixeng_clear (hw->mix_buf, samples - n);
1342 static void audio_run_out (AudioState *s)
1344 HWVoiceOut *hw = NULL;
1347 while ((hw = audio_pcm_hw_find_any_enabled_out (hw))) {
1349 int live, free, nb_live, cleanup_required, prev_rpos;
1351 live = audio_pcm_hw_get_live_out (hw, &nb_live);
1356 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1357 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1361 if (hw->pending_disable && !nb_live) {
1364 dolog ("Disabling voice\n");
1367 hw->pending_disable = 0;
1368 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
1369 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1371 audio_recalc_and_notify_capture (sc->cap);
1377 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1379 free = audio_get_free (sw);
1381 sw->callback.fn (sw->callback.opaque, free);
1388 prev_rpos = hw->rpos;
1389 played = hw->pcm_ops->run_out (hw, live);
1390 if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1391 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1392 hw->rpos, hw->samples, played);
1397 dolog ("played=%d\n", played);
1401 hw->ts_helper += played;
1402 audio_capture_mix_and_clear (hw, prev_rpos, played);
1405 cleanup_required = 0;
1406 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1407 if (!sw->active && sw->empty) {
1411 if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1412 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1413 played, sw->total_hw_samples_mixed);
1414 played = sw->total_hw_samples_mixed;
1417 sw->total_hw_samples_mixed -= played;
1419 if (!sw->total_hw_samples_mixed) {
1421 cleanup_required |= !sw->active && !sw->callback.fn;
1425 free = audio_get_free (sw);
1427 sw->callback.fn (sw->callback.opaque, free);
1432 if (cleanup_required) {
1435 sw = hw->sw_head.lh_first;
1437 sw1 = sw->entries.le_next;
1438 if (!sw->active && !sw->callback.fn) {
1439 audio_close_out (sw);
1447 static void audio_run_in (AudioState *s)
1449 HWVoiceIn *hw = NULL;
1451 while ((hw = audio_pcm_hw_find_any_enabled_in (hw))) {
1455 captured = hw->pcm_ops->run_in (hw);
1457 min = audio_pcm_hw_find_min_in (hw);
1458 hw->total_samples_captured += captured - min;
1459 hw->ts_helper += captured;
1461 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1462 sw->total_hw_samples_acquired -= min;
1467 avail = audio_get_avail (sw);
1469 sw->callback.fn (sw->callback.opaque, avail);
1476 static void audio_run_capture (AudioState *s)
1478 CaptureVoiceOut *cap;
1480 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1481 int live, rpos, captured;
1482 HWVoiceOut *hw = &cap->hw;
1485 captured = live = audio_pcm_hw_get_live_out (hw, NULL);
1488 int left = hw->samples - rpos;
1489 int to_capture = audio_MIN (live, left);
1490 struct st_sample *src;
1491 struct capture_callback *cb;
1493 src = hw->mix_buf + rpos;
1494 hw->clip (cap->buf, src, to_capture);
1495 mixeng_clear (src, to_capture);
1497 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1498 cb->ops.capture (cb->opaque, cap->buf,
1499 to_capture << hw->info.shift);
1501 rpos = (rpos + to_capture) % hw->samples;
1506 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1507 if (!sw->active && sw->empty) {
1511 if (audio_bug (AUDIO_FUNC, captured > sw->total_hw_samples_mixed)) {
1512 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1513 captured, sw->total_hw_samples_mixed);
1514 captured = sw->total_hw_samples_mixed;
1517 sw->total_hw_samples_mixed -= captured;
1518 sw->empty = sw->total_hw_samples_mixed == 0;
1523 void audio_run (const char *msg)
1525 AudioState *s = &glob_audio_state;
1529 audio_run_capture (s);
1532 static double prevtime;
1536 if (gettimeofday (&tv, NULL)) {
1537 perror ("audio_run: gettimeofday");
1541 currtime = tv.tv_sec + tv.tv_usec * 1e-6;
1542 dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime);
1543 prevtime = currtime;
1548 static struct audio_option audio_options[] = {
1551 .name = "DAC_FIXED_SETTINGS",
1552 .tag = AUD_OPT_BOOL,
1553 .valp = &conf.fixed_out.enabled,
1554 .descr = "Use fixed settings for host DAC"
1557 .name = "DAC_FIXED_FREQ",
1559 .valp = &conf.fixed_out.settings.freq,
1560 .descr = "Frequency for fixed host DAC"
1563 .name = "DAC_FIXED_FMT",
1565 .valp = &conf.fixed_out.settings.fmt,
1566 .descr = "Format for fixed host DAC"
1569 .name = "DAC_FIXED_CHANNELS",
1571 .valp = &conf.fixed_out.settings.nchannels,
1572 .descr = "Number of channels for fixed DAC (1 - mono, 2 - stereo)"
1575 .name = "DAC_VOICES",
1577 .valp = &conf.fixed_out.nb_voices,
1578 .descr = "Number of voices for DAC"
1581 .name = "DAC_TRY_POLL",
1582 .tag = AUD_OPT_BOOL,
1583 .valp = &conf.try_poll_out,
1584 .descr = "Attempt using poll mode for DAC"
1588 .name = "ADC_FIXED_SETTINGS",
1589 .tag = AUD_OPT_BOOL,
1590 .valp = &conf.fixed_in.enabled,
1591 .descr = "Use fixed settings for host ADC"
1594 .name = "ADC_FIXED_FREQ",
1596 .valp = &conf.fixed_in.settings.freq,
1597 .descr = "Frequency for fixed host ADC"
1600 .name = "ADC_FIXED_FMT",
1602 .valp = &conf.fixed_in.settings.fmt,
1603 .descr = "Format for fixed host ADC"
1606 .name = "ADC_FIXED_CHANNELS",
1608 .valp = &conf.fixed_in.settings.nchannels,
1609 .descr = "Number of channels for fixed ADC (1 - mono, 2 - stereo)"
1612 .name = "ADC_VOICES",
1614 .valp = &conf.fixed_in.nb_voices,
1615 .descr = "Number of voices for ADC"
1618 .name = "ADC_TRY_POLL",
1619 .tag = AUD_OPT_BOOL,
1620 .valp = &conf.try_poll_in,
1621 .descr = "Attempt using poll mode for ADC"
1625 .name = "TIMER_PERIOD",
1627 .valp = &conf.period.hertz,
1628 .descr = "Timer period in HZ (0 - use lowest possible)"
1630 { /* End of list */ }
1633 static void audio_pp_nb_voices (const char *typ, int nb)
1637 printf ("Does not support %s\n", typ);
1640 printf ("One %s voice\n", typ);
1643 printf ("Theoretically supports many %s voices\n", typ);
1646 printf ("Theoretically supports up to %d %s voices\n", nb, typ);
1652 void AUD_help (void)
1656 audio_process_options ("AUDIO", audio_options);
1657 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1658 struct audio_driver *d = drvtab[i];
1660 audio_process_options (d->name, d->options);
1664 printf ("Audio options:\n");
1665 audio_print_options ("AUDIO", audio_options);
1668 printf ("Available drivers:\n");
1670 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1671 struct audio_driver *d = drvtab[i];
1673 printf ("Name: %s\n", d->name);
1674 printf ("Description: %s\n", d->descr);
1676 audio_pp_nb_voices ("playback", d->max_voices_out);
1677 audio_pp_nb_voices ("capture", d->max_voices_in);
1680 printf ("Options:\n");
1681 audio_print_options (d->name, d->options);
1684 printf ("No options\n");
1690 "Options are settable through environment variables.\n"
1693 " set QEMU_AUDIO_DRV=wav\n"
1694 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1696 " export QEMU_AUDIO_DRV=wav\n"
1697 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1698 "(for csh replace export with setenv in the above)\n"
1704 static int audio_driver_init (AudioState *s, struct audio_driver *drv)
1707 audio_process_options (drv->name, drv->options);
1709 s->drv_opaque = drv->init ();
1711 if (s->drv_opaque) {
1712 audio_init_nb_voices_out (drv);
1713 audio_init_nb_voices_in (drv);
1718 dolog ("Could not init `%s' audio driver\n", drv->name);
1723 static void audio_vm_change_state_handler (void *opaque, int running,
1726 AudioState *s = opaque;
1727 HWVoiceOut *hwo = NULL;
1728 HWVoiceIn *hwi = NULL;
1729 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1731 s->vm_running = running;
1732 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1733 hwo->pcm_ops->ctl_out (hwo, op, conf.try_poll_out);
1736 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1737 hwi->pcm_ops->ctl_in (hwi, op, conf.try_poll_in);
1739 audio_reset_timer (s);
1742 static void audio_atexit (void)
1744 AudioState *s = &glob_audio_state;
1745 HWVoiceOut *hwo = NULL;
1746 HWVoiceIn *hwi = NULL;
1748 while ((hwo = audio_pcm_hw_find_any_out (hwo))) {
1752 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1754 hwo->pcm_ops->fini_out (hwo);
1756 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1757 CaptureVoiceOut *cap = sc->cap;
1758 struct capture_callback *cb;
1760 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1761 cb->ops.destroy (cb->opaque);
1766 while ((hwi = audio_pcm_hw_find_any_in (hwi))) {
1768 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1770 hwi->pcm_ops->fini_in (hwi);
1774 s->drv->fini (s->drv_opaque);
1778 static const VMStateDescription vmstate_audio = {
1781 .minimum_version_id = 1,
1782 .fields = (VMStateField[]) {
1783 VMSTATE_END_OF_LIST()
1787 static void audio_init (void)
1791 const char *drvname;
1792 VMChangeStateEntry *e;
1793 AudioState *s = &glob_audio_state;
1799 QLIST_INIT (&s->hw_head_out);
1800 QLIST_INIT (&s->hw_head_in);
1801 QLIST_INIT (&s->cap_head);
1802 atexit (audio_atexit);
1804 s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
1806 audio_process_options ("AUDIO", audio_options);
1808 s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1809 s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1811 if (s->nb_hw_voices_out <= 0) {
1812 dolog ("Bogus number of playback voices %d, setting to 1\n",
1813 s->nb_hw_voices_out);
1814 s->nb_hw_voices_out = 1;
1817 if (s->nb_hw_voices_in <= 0) {
1818 dolog ("Bogus number of capture voices %d, setting to 0\n",
1819 s->nb_hw_voices_in);
1820 s->nb_hw_voices_in = 0;
1825 drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1831 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1832 if (!strcmp (drvname, drvtab[i]->name)) {
1833 done = !audio_driver_init (s, drvtab[i]);
1840 dolog ("Unknown audio driver `%s'\n", drvname);
1841 dolog ("Run with -audio-help to list available drivers\n");
1846 for (i = 0; !done && i < ARRAY_SIZE (drvtab); i++) {
1847 if (drvtab[i]->can_be_default) {
1848 done = !audio_driver_init (s, drvtab[i]);
1854 done = !audio_driver_init (s, &no_audio_driver);
1856 dolog("warning: Using timer based audio emulation\n");
1859 if (conf.period.hertz <= 0) {
1860 if (conf.period.hertz < 0) {
1861 dolog ("warning: Timer period is negative - %d "
1862 "treating as zero\n",
1865 conf.period.ticks = 1;
1867 conf.period.ticks = NANOSECONDS_PER_SECOND / conf.period.hertz;
1870 e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1872 dolog ("warning: Could not register change state handler\n"
1873 "(Audio can continue looping even after stopping the VM)\n");
1876 QLIST_INIT (&s->card_head);
1877 vmstate_register (NULL, 0, &vmstate_audio, s);
1880 void AUD_register_card (const char *name, QEMUSoundCard *card)
1883 card->name = g_strdup (name);
1884 memset (&card->entries, 0, sizeof (card->entries));
1885 QLIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries);
1888 void AUD_remove_card (QEMUSoundCard *card)
1890 QLIST_REMOVE (card, entries);
1891 g_free (card->name);
1895 CaptureVoiceOut *AUD_add_capture (
1896 struct audsettings *as,
1897 struct audio_capture_ops *ops,
1901 AudioState *s = &glob_audio_state;
1902 CaptureVoiceOut *cap;
1903 struct capture_callback *cb;
1905 if (audio_validate_settings (as)) {
1906 dolog ("Invalid settings were passed when trying to add capture\n");
1907 audio_print_settings (as);
1911 cb = audio_calloc (AUDIO_FUNC, 1, sizeof (*cb));
1913 dolog ("Could not allocate capture callback information, size %zu\n",
1918 cb->opaque = cb_opaque;
1920 cap = audio_pcm_capture_find_specific (as);
1922 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1927 CaptureVoiceOut *cap;
1929 cap = audio_calloc (AUDIO_FUNC, 1, sizeof (*cap));
1931 dolog ("Could not allocate capture voice, size %zu\n",
1937 QLIST_INIT (&hw->sw_head);
1938 QLIST_INIT (&cap->cb_head);
1940 /* XXX find a more elegant way */
1941 hw->samples = 4096 * 4;
1942 hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples,
1943 sizeof (struct st_sample));
1945 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1950 audio_pcm_init_info (&hw->info, as);
1952 cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
1954 dolog ("Could not allocate capture buffer "
1955 "(%d samples, each %d bytes)\n",
1956 hw->samples, 1 << hw->info.shift);
1960 hw->clip = mixeng_clip
1961 [hw->info.nchannels == 2]
1963 [hw->info.swap_endianness]
1964 [audio_bits_to_index (hw->info.bits)];
1966 QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
1967 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1970 while ((hw = audio_pcm_hw_find_any_out (hw))) {
1971 audio_attach_capture (hw);
1976 g_free (cap->hw.mix_buf);
1986 void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
1988 struct capture_callback *cb;
1990 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1991 if (cb->opaque == cb_opaque) {
1992 cb->ops.destroy (cb_opaque);
1993 QLIST_REMOVE (cb, entries);
1996 if (!cap->cb_head.lh_first) {
1997 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
2000 SWVoiceCap *sc = (SWVoiceCap *) sw;
2001 #ifdef DEBUG_CAPTURE
2002 dolog ("freeing %s\n", sw->name);
2005 sw1 = sw->entries.le_next;
2007 st_rate_stop (sw->rate);
2010 QLIST_REMOVE (sw, entries);
2011 QLIST_REMOVE (sc, entries);
2015 QLIST_REMOVE (cap, entries);
2023 void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
2026 HWVoiceOut *hw = sw->hw;
2028 sw->vol.mute = mute;
2029 sw->vol.l = nominal_volume.l * lvol / 255;
2030 sw->vol.r = nominal_volume.r * rvol / 255;
2032 if (hw->pcm_ops->ctl_out) {
2033 hw->pcm_ops->ctl_out (hw, VOICE_VOLUME, sw);
2038 void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
2041 HWVoiceIn *hw = sw->hw;
2043 sw->vol.mute = mute;
2044 sw->vol.l = nominal_volume.l * lvol / 255;
2045 sw->vol.r = nominal_volume.r * rvol / 255;
2047 if (hw->pcm_ops->ctl_in) {
2048 hw->pcm_ops->ctl_in (hw, VOICE_VOLUME, sw);