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 #include "monitor/monitor.h"
27 #include "qemu/timer.h"
28 #include "sysemu/sysemu.h"
30 #define AUDIO_CAP "audio"
31 #include "audio_int.h"
33 /* #define DEBUG_PLIVE */
34 /* #define DEBUG_LIVE */
35 /* #define DEBUG_OUT */
36 /* #define DEBUG_CAPTURE */
37 /* #define DEBUG_POLL */
39 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
42 /* Order of CONFIG_AUDIO_DRIVERS is import.
43 The 1st one is the one used by default, that is the reason
44 that we generate the list.
46 static struct audio_driver *drvtab[] = {
55 struct fixed_settings {
59 struct audsettings settings;
63 struct fixed_settings fixed_out;
64 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 },
105 static AudioState glob_audio_state;
107 const struct mixeng_volume nominal_volume = {
118 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
121 static void audio_print_options (const char *prefix,
122 struct audio_option *opt);
124 int audio_bug (const char *funcname, int cond)
129 AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
131 struct audio_driver *d;
134 AUD_log (NULL, "Save all your work and restart without audio\n");
136 AUD_log (NULL, "I am sorry\n");
137 d = glob_audio_state.drv;
139 audio_print_options (d->name, d->options);
142 AUD_log (NULL, "Context:\n");
144 #if defined AUDIO_BREAKPOINT_ON_BUG
145 # if defined HOST_I386
146 # if defined __GNUC__
148 # elif defined _MSC_VER
163 static inline int audio_bits_to_index (int bits)
176 audio_bug ("bits_to_index", 1);
177 AUD_log (NULL, "invalid bits %d\n", bits);
182 void *audio_calloc (const char *funcname, int nmemb, size_t size)
188 cond = !nmemb || !size;
192 if (audio_bug ("audio_calloc", cond)) {
193 AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
195 AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
199 return g_malloc0 (len);
202 static char *audio_alloc_prefix (const char *s)
204 const char qemu_prefix[] = "QEMU_";
213 r = g_malloc (len + sizeof (qemu_prefix));
215 u = r + sizeof (qemu_prefix) - 1;
217 pstrcpy (r, len + sizeof (qemu_prefix), qemu_prefix);
218 pstrcat (r, len + sizeof (qemu_prefix), s);
220 for (i = 0; i < len; ++i) {
221 u[i] = qemu_toupper(u[i]);
227 static const char *audio_audfmt_to_string (audfmt_e fmt)
249 dolog ("Bogus audfmt %d returning S16\n", fmt);
253 static audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval,
256 if (!strcasecmp (s, "u8")) {
260 else if (!strcasecmp (s, "u16")) {
264 else if (!strcasecmp (s, "u32")) {
268 else if (!strcasecmp (s, "s8")) {
272 else if (!strcasecmp (s, "s16")) {
276 else if (!strcasecmp (s, "s32")) {
281 dolog ("Bogus audio format `%s' using %s\n",
282 s, audio_audfmt_to_string (defval));
288 static audfmt_e audio_get_conf_fmt (const char *envname,
292 const char *var = getenv (envname);
297 return audio_string_to_audfmt (var, defval, defaultp);
300 static int audio_get_conf_int (const char *key, int defval, int *defaultp)
305 strval = getenv (key);
317 static const char *audio_get_conf_str (const char *key,
321 const char *val = getenv (key);
332 void AUD_vlog (const char *cap, const char *fmt, va_list ap)
334 if (conf.log_to_monitor) {
336 monitor_printf(default_mon, "%s: ", cap);
339 monitor_vprintf(default_mon, fmt, ap);
343 fprintf (stderr, "%s: ", cap);
346 vfprintf (stderr, fmt, ap);
350 void AUD_log (const char *cap, const char *fmt, ...)
355 AUD_vlog (cap, fmt, ap);
359 static void audio_print_options (const char *prefix,
360 struct audio_option *opt)
365 dolog ("No prefix specified\n");
370 dolog ("No options\n");
374 uprefix = audio_alloc_prefix (prefix);
376 for (; opt->name; opt++) {
377 const char *state = "default";
378 printf (" %s_%s: ", uprefix, opt->name);
380 if (opt->overriddenp && *opt->overriddenp) {
387 int *intp = opt->valp;
388 printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
394 int *intp = opt->valp;
395 printf ("integer, %s = %d\n", state, *intp);
401 audfmt_e *fmtp = opt->valp;
403 "format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
405 audio_audfmt_to_string (*fmtp)
412 const char **strp = opt->valp;
413 printf ("string, %s = %s\n",
415 *strp ? *strp : "(not set)");
421 dolog ("Bad value tag for option %s_%s %d\n",
422 uprefix, opt->name, opt->tag);
425 printf (" %s\n", opt->descr);
431 static void audio_process_options (const char *prefix,
432 struct audio_option *opt)
435 const char qemu_prefix[] = "QEMU_";
436 size_t preflen, optlen;
438 if (audio_bug (AUDIO_FUNC, !prefix)) {
439 dolog ("prefix = NULL\n");
443 if (audio_bug (AUDIO_FUNC, !opt)) {
444 dolog ("opt = NULL\n");
448 preflen = strlen (prefix);
450 for (; opt->name; opt++) {
455 dolog ("Option value pointer for `%s' is not set\n",
460 len = strlen (opt->name);
461 /* len of opt->name + len of prefix + size of qemu_prefix
462 * (includes trailing zero) + zero + underscore (on behalf of
464 optlen = len + preflen + sizeof (qemu_prefix) + 1;
465 optname = g_malloc (optlen);
467 pstrcpy (optname, optlen, qemu_prefix);
469 /* copy while upper-casing, including trailing zero */
470 for (i = 0; i <= preflen; ++i) {
471 optname[i + sizeof (qemu_prefix) - 1] = qemu_toupper(prefix[i]);
473 pstrcat (optname, optlen, "_");
474 pstrcat (optname, optlen, opt->name);
481 int *intp = opt->valp;
482 *intp = audio_get_conf_int (optname, *intp, &def);
488 audfmt_e *fmtp = opt->valp;
489 *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
495 const char **strp = opt->valp;
496 *strp = audio_get_conf_str (optname, *strp, &def);
501 dolog ("Bad value tag for option `%s' - %d\n",
506 if (!opt->overriddenp) {
507 opt->overriddenp = &opt->overridden;
509 *opt->overriddenp = !def;
514 static void audio_print_settings (struct audsettings *as)
516 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
520 AUD_log (NULL, "S8");
523 AUD_log (NULL, "U8");
526 AUD_log (NULL, "S16");
529 AUD_log (NULL, "U16");
532 AUD_log (NULL, "S32");
535 AUD_log (NULL, "U32");
538 AUD_log (NULL, "invalid(%d)", as->fmt);
542 AUD_log (NULL, " endianness=");
543 switch (as->endianness) {
545 AUD_log (NULL, "little");
548 AUD_log (NULL, "big");
551 AUD_log (NULL, "invalid");
554 AUD_log (NULL, "\n");
557 static int audio_validate_settings (struct audsettings *as)
561 invalid = as->nchannels != 1 && as->nchannels != 2;
562 invalid |= as->endianness != 0 && as->endianness != 1;
577 invalid |= as->freq <= 0;
578 return invalid ? -1 : 0;
581 static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
583 int bits = 8, sign = 0;
606 return info->freq == as->freq
607 && info->nchannels == as->nchannels
608 && info->sign == sign
609 && info->bits == bits
610 && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
613 void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
615 int bits = 8, sign = 0, shift = 0;
638 info->freq = as->freq;
641 info->nchannels = as->nchannels;
642 info->shift = (as->nchannels == 2) + shift;
643 info->align = (1 << info->shift) - 1;
644 info->bytes_per_second = info->freq << info->shift;
645 info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
648 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
655 memset (buf, 0x00, len << info->shift);
658 switch (info->bits) {
660 memset (buf, 0x80, len << info->shift);
667 int shift = info->nchannels - 1;
670 if (info->swap_endianness) {
674 for (i = 0; i < len << shift; i++) {
684 int shift = info->nchannels - 1;
685 int32_t s = INT32_MAX;
687 if (info->swap_endianness) {
691 for (i = 0; i < len << shift; i++) {
698 AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
708 static void noop_conv (struct st_sample *dst, const void *src, int samples)
715 static CaptureVoiceOut *audio_pcm_capture_find_specific (
716 struct audsettings *as
719 CaptureVoiceOut *cap;
720 AudioState *s = &glob_audio_state;
722 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
723 if (audio_pcm_info_eq (&cap->hw.info, as)) {
730 static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
732 struct capture_callback *cb;
735 dolog ("notification %d sent\n", cmd);
737 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
738 cb->ops.notify (cb->opaque, cmd);
742 static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
744 if (cap->hw.enabled != enabled) {
745 audcnotification_e cmd;
746 cap->hw.enabled = enabled;
747 cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
748 audio_notify_capture (cap, cmd);
752 static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
754 HWVoiceOut *hw = &cap->hw;
758 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
764 audio_capture_maybe_changed (cap, enabled);
767 static void audio_detach_capture (HWVoiceOut *hw)
769 SWVoiceCap *sc = hw->cap_head.lh_first;
772 SWVoiceCap *sc1 = sc->entries.le_next;
773 SWVoiceOut *sw = &sc->sw;
774 CaptureVoiceOut *cap = sc->cap;
775 int was_active = sw->active;
778 st_rate_stop (sw->rate);
782 QLIST_REMOVE (sw, entries);
783 QLIST_REMOVE (sc, entries);
786 /* We have removed soft voice from the capture:
787 this might have changed the overall status of the capture
788 since this might have been the only active voice */
789 audio_recalc_and_notify_capture (cap);
795 static int audio_attach_capture (HWVoiceOut *hw)
797 AudioState *s = &glob_audio_state;
798 CaptureVoiceOut *cap;
800 audio_detach_capture (hw);
801 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
804 HWVoiceOut *hw_cap = &cap->hw;
806 sc = audio_calloc (AUDIO_FUNC, 1, sizeof (*sc));
808 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
818 sw->active = hw->enabled;
819 sw->conv = noop_conv;
820 sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
821 sw->vol = nominal_volume;
822 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
824 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
828 QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
829 QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
831 sw->name = g_strdup_printf ("for %p %d,%d,%d",
832 hw, sw->info.freq, sw->info.bits,
834 dolog ("Added %s active = %d\n", sw->name, sw->active);
837 audio_capture_maybe_changed (cap, 1);
844 * Hard voice (capture)
846 static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
849 int m = hw->total_samples_captured;
851 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
853 m = audio_MIN (m, sw->total_hw_samples_acquired);
859 int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
861 int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
862 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
863 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
869 int audio_pcm_hw_clip_out (HWVoiceOut *hw, void *pcm_buf,
870 int live, int pending)
872 int left = hw->samples - pending;
873 int len = audio_MIN (left, live);
877 struct st_sample *src = hw->mix_buf + hw->rpos;
878 uint8_t *dst = advance (pcm_buf, hw->rpos << hw->info.shift);
879 int samples_till_end_of_buf = hw->samples - hw->rpos;
880 int samples_to_clip = audio_MIN (len, samples_till_end_of_buf);
882 hw->clip (dst, src, samples_to_clip);
884 hw->rpos = (hw->rpos + samples_to_clip) % hw->samples;
885 len -= samples_to_clip;
886 clipped += samples_to_clip;
892 * Soft voice (capture)
894 static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
896 HWVoiceIn *hw = sw->hw;
897 int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
900 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
901 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
905 rpos = hw->wpos - live;
910 return hw->samples + rpos;
914 int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
916 HWVoiceIn *hw = sw->hw;
917 int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
918 struct st_sample *src, *dst = sw->buf;
920 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
922 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
923 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
924 dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
928 samples = size >> sw->info.shift;
933 swlim = (live * sw->ratio) >> 32;
934 swlim = audio_MIN (swlim, samples);
937 src = hw->conv_buf + rpos;
938 isamp = hw->wpos - rpos;
941 isamp = hw->samples - rpos;
949 if (audio_bug (AUDIO_FUNC, osamp < 0)) {
950 dolog ("osamp=%d\n", osamp);
954 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
956 rpos = (rpos + isamp) % hw->samples;
962 if (!(hw->ctl_caps & VOICE_VOLUME_CAP)) {
963 mixeng_volume (sw->buf, ret, &sw->vol);
966 sw->clip (buf, sw->buf, ret);
967 sw->total_hw_samples_acquired += total;
968 return ret << sw->info.shift;
972 * Hard voice (playback)
974 static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
980 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
981 if (sw->active || !sw->empty) {
982 m = audio_MIN (m, sw->total_hw_samples_mixed);
991 static int audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
996 smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
1004 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1005 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1014 * Soft voice (playback)
1016 int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
1018 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
1019 int ret = 0, pos = 0, total = 0;
1025 hwsamples = sw->hw->samples;
1027 live = sw->total_hw_samples_mixed;
1028 if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
1029 dolog ("live=%d hw->samples=%d\n", live, hwsamples);
1033 if (live == hwsamples) {
1035 dolog ("%s is full %d\n", sw->name, live);
1040 wpos = (sw->hw->rpos + live) % hwsamples;
1041 samples = size >> sw->info.shift;
1043 dead = hwsamples - live;
1044 swlim = ((int64_t) dead << 32) / sw->ratio;
1045 swlim = audio_MIN (swlim, samples);
1047 sw->conv (sw->buf, buf, swlim);
1049 if (!(sw->hw->ctl_caps & VOICE_VOLUME_CAP)) {
1050 mixeng_volume (sw->buf, swlim, &sw->vol);
1055 dead = hwsamples - live;
1056 left = hwsamples - wpos;
1057 blck = audio_MIN (dead, left);
1066 sw->hw->mix_buf + wpos,
1074 wpos = (wpos + osamp) % hwsamples;
1078 sw->total_hw_samples_mixed += total;
1079 sw->empty = sw->total_hw_samples_mixed == 0;
1083 "%s: write size %d ret %d total sw %d\n",
1085 size >> sw->info.shift,
1087 sw->total_hw_samples_mixed
1091 return ret << sw->info.shift;
1095 static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
1097 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1098 cap, info->bits, info->sign, info->freq, info->nchannels);
1103 #include "audio_template.h"
1105 #include "audio_template.h"
1110 static int audio_is_timer_needed (void)
1112 HWVoiceIn *hwi = NULL;
1113 HWVoiceOut *hwo = NULL;
1115 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1116 if (!hwo->poll_mode) return 1;
1118 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1119 if (!hwi->poll_mode) return 1;
1124 static void audio_reset_timer (AudioState *s)
1126 if (audio_is_timer_needed ()) {
1128 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + conf.period.ticks);
1135 static void audio_timer (void *opaque)
1137 audio_run ("timer");
1138 audio_reset_timer (opaque);
1144 int AUD_write (SWVoiceOut *sw, void *buf, int size)
1149 /* XXX: Consider options */
1153 if (!sw->hw->enabled) {
1154 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
1158 bytes = sw->hw->pcm_ops->write (sw, buf, size);
1162 int AUD_read (SWVoiceIn *sw, void *buf, int size)
1167 /* XXX: Consider options */
1171 if (!sw->hw->enabled) {
1172 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1176 bytes = sw->hw->pcm_ops->read (sw, buf, size);
1180 int AUD_get_buffer_size_out (SWVoiceOut *sw)
1182 return sw->hw->samples << sw->hw->info.shift;
1185 void AUD_set_active_out (SWVoiceOut *sw, int on)
1194 if (sw->active != on) {
1195 AudioState *s = &glob_audio_state;
1196 SWVoiceOut *temp_sw;
1200 hw->pending_disable = 0;
1203 if (s->vm_running) {
1204 hw->pcm_ops->ctl_out (hw, VOICE_ENABLE, conf.try_poll_out);
1205 audio_reset_timer (s);
1213 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1214 temp_sw = temp_sw->entries.le_next) {
1215 nb_active += temp_sw->active != 0;
1218 hw->pending_disable = nb_active == 1;
1222 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1223 sc->sw.active = hw->enabled;
1225 audio_capture_maybe_changed (sc->cap, 1);
1232 void AUD_set_active_in (SWVoiceIn *sw, int on)
1241 if (sw->active != on) {
1242 AudioState *s = &glob_audio_state;
1248 if (s->vm_running) {
1249 hw->pcm_ops->ctl_in (hw, VOICE_ENABLE, conf.try_poll_in);
1250 audio_reset_timer (s);
1253 sw->total_hw_samples_acquired = hw->total_samples_captured;
1259 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1260 temp_sw = temp_sw->entries.le_next) {
1261 nb_active += temp_sw->active != 0;
1264 if (nb_active == 1) {
1266 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
1274 static int audio_get_avail (SWVoiceIn *sw)
1282 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1283 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1284 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1289 "%s: get_avail live %d ret %" PRId64 "\n",
1291 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1294 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1297 static int audio_get_free (SWVoiceOut *sw)
1305 live = sw->total_hw_samples_mixed;
1307 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1308 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1312 dead = sw->hw->samples - live;
1315 dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
1317 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1320 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1323 static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1330 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1331 SWVoiceOut *sw = &sc->sw;
1336 int till_end_of_hw = hw->samples - rpos2;
1337 int to_write = audio_MIN (till_end_of_hw, n);
1338 int bytes = to_write << hw->info.shift;
1341 sw->buf = hw->mix_buf + rpos2;
1342 written = audio_pcm_sw_write (sw, NULL, bytes);
1343 if (written - bytes) {
1344 dolog ("Could not mix %d bytes into a capture "
1345 "buffer, mixed %d\n",
1350 rpos2 = (rpos2 + to_write) % hw->samples;
1355 n = audio_MIN (samples, hw->samples - rpos);
1356 mixeng_clear (hw->mix_buf + rpos, n);
1357 mixeng_clear (hw->mix_buf, samples - n);
1360 static void audio_run_out (AudioState *s)
1362 HWVoiceOut *hw = NULL;
1365 while ((hw = audio_pcm_hw_find_any_enabled_out (hw))) {
1367 int live, free, nb_live, cleanup_required, prev_rpos;
1369 live = audio_pcm_hw_get_live_out (hw, &nb_live);
1374 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1375 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1379 if (hw->pending_disable && !nb_live) {
1382 dolog ("Disabling voice\n");
1385 hw->pending_disable = 0;
1386 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
1387 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1389 audio_recalc_and_notify_capture (sc->cap);
1395 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1397 free = audio_get_free (sw);
1399 sw->callback.fn (sw->callback.opaque, free);
1406 prev_rpos = hw->rpos;
1407 played = hw->pcm_ops->run_out (hw, live);
1408 if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1409 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1410 hw->rpos, hw->samples, played);
1415 dolog ("played=%d\n", played);
1419 hw->ts_helper += played;
1420 audio_capture_mix_and_clear (hw, prev_rpos, played);
1423 cleanup_required = 0;
1424 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1425 if (!sw->active && sw->empty) {
1429 if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1430 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1431 played, sw->total_hw_samples_mixed);
1432 played = sw->total_hw_samples_mixed;
1435 sw->total_hw_samples_mixed -= played;
1437 if (!sw->total_hw_samples_mixed) {
1439 cleanup_required |= !sw->active && !sw->callback.fn;
1443 free = audio_get_free (sw);
1445 sw->callback.fn (sw->callback.opaque, free);
1450 if (cleanup_required) {
1453 sw = hw->sw_head.lh_first;
1455 sw1 = sw->entries.le_next;
1456 if (!sw->active && !sw->callback.fn) {
1458 dolog ("Finishing with old voice\n");
1460 audio_close_out (sw);
1468 static void audio_run_in (AudioState *s)
1470 HWVoiceIn *hw = NULL;
1472 while ((hw = audio_pcm_hw_find_any_enabled_in (hw))) {
1476 captured = hw->pcm_ops->run_in (hw);
1478 min = audio_pcm_hw_find_min_in (hw);
1479 hw->total_samples_captured += captured - min;
1480 hw->ts_helper += captured;
1482 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1483 sw->total_hw_samples_acquired -= min;
1488 avail = audio_get_avail (sw);
1490 sw->callback.fn (sw->callback.opaque, avail);
1497 static void audio_run_capture (AudioState *s)
1499 CaptureVoiceOut *cap;
1501 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1502 int live, rpos, captured;
1503 HWVoiceOut *hw = &cap->hw;
1506 captured = live = audio_pcm_hw_get_live_out (hw, NULL);
1509 int left = hw->samples - rpos;
1510 int to_capture = audio_MIN (live, left);
1511 struct st_sample *src;
1512 struct capture_callback *cb;
1514 src = hw->mix_buf + rpos;
1515 hw->clip (cap->buf, src, to_capture);
1516 mixeng_clear (src, to_capture);
1518 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1519 cb->ops.capture (cb->opaque, cap->buf,
1520 to_capture << hw->info.shift);
1522 rpos = (rpos + to_capture) % hw->samples;
1527 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1528 if (!sw->active && sw->empty) {
1532 if (audio_bug (AUDIO_FUNC, captured > sw->total_hw_samples_mixed)) {
1533 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1534 captured, sw->total_hw_samples_mixed);
1535 captured = sw->total_hw_samples_mixed;
1538 sw->total_hw_samples_mixed -= captured;
1539 sw->empty = sw->total_hw_samples_mixed == 0;
1544 void audio_run (const char *msg)
1546 AudioState *s = &glob_audio_state;
1550 audio_run_capture (s);
1553 static double prevtime;
1557 if (gettimeofday (&tv, NULL)) {
1558 perror ("audio_run: gettimeofday");
1562 currtime = tv.tv_sec + tv.tv_usec * 1e-6;
1563 dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime);
1564 prevtime = currtime;
1569 static struct audio_option audio_options[] = {
1572 .name = "DAC_FIXED_SETTINGS",
1573 .tag = AUD_OPT_BOOL,
1574 .valp = &conf.fixed_out.enabled,
1575 .descr = "Use fixed settings for host DAC"
1578 .name = "DAC_FIXED_FREQ",
1580 .valp = &conf.fixed_out.settings.freq,
1581 .descr = "Frequency for fixed host DAC"
1584 .name = "DAC_FIXED_FMT",
1586 .valp = &conf.fixed_out.settings.fmt,
1587 .descr = "Format for fixed host DAC"
1590 .name = "DAC_FIXED_CHANNELS",
1592 .valp = &conf.fixed_out.settings.nchannels,
1593 .descr = "Number of channels for fixed DAC (1 - mono, 2 - stereo)"
1596 .name = "DAC_VOICES",
1598 .valp = &conf.fixed_out.nb_voices,
1599 .descr = "Number of voices for DAC"
1602 .name = "DAC_TRY_POLL",
1603 .tag = AUD_OPT_BOOL,
1604 .valp = &conf.try_poll_out,
1605 .descr = "Attempt using poll mode for DAC"
1609 .name = "ADC_FIXED_SETTINGS",
1610 .tag = AUD_OPT_BOOL,
1611 .valp = &conf.fixed_in.enabled,
1612 .descr = "Use fixed settings for host ADC"
1615 .name = "ADC_FIXED_FREQ",
1617 .valp = &conf.fixed_in.settings.freq,
1618 .descr = "Frequency for fixed host ADC"
1621 .name = "ADC_FIXED_FMT",
1623 .valp = &conf.fixed_in.settings.fmt,
1624 .descr = "Format for fixed host ADC"
1627 .name = "ADC_FIXED_CHANNELS",
1629 .valp = &conf.fixed_in.settings.nchannels,
1630 .descr = "Number of channels for fixed ADC (1 - mono, 2 - stereo)"
1633 .name = "ADC_VOICES",
1635 .valp = &conf.fixed_in.nb_voices,
1636 .descr = "Number of voices for ADC"
1639 .name = "ADC_TRY_POLL",
1640 .tag = AUD_OPT_BOOL,
1641 .valp = &conf.try_poll_in,
1642 .descr = "Attempt using poll mode for ADC"
1646 .name = "TIMER_PERIOD",
1648 .valp = &conf.period.hertz,
1649 .descr = "Timer period in HZ (0 - use lowest possible)"
1653 .tag = AUD_OPT_BOOL,
1654 .valp = &conf.plive,
1655 .descr = "(undocumented)"
1658 .name = "LOG_TO_MONITOR",
1659 .tag = AUD_OPT_BOOL,
1660 .valp = &conf.log_to_monitor,
1661 .descr = "Print logging messages to monitor instead of stderr"
1663 { /* End of list */ }
1666 static void audio_pp_nb_voices (const char *typ, int nb)
1670 printf ("Does not support %s\n", typ);
1673 printf ("One %s voice\n", typ);
1676 printf ("Theoretically supports many %s voices\n", typ);
1679 printf ("Theoretically supports up to %d %s voices\n", nb, typ);
1685 void AUD_help (void)
1689 audio_process_options ("AUDIO", audio_options);
1690 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1691 struct audio_driver *d = drvtab[i];
1693 audio_process_options (d->name, d->options);
1697 printf ("Audio options:\n");
1698 audio_print_options ("AUDIO", audio_options);
1701 printf ("Available drivers:\n");
1703 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1704 struct audio_driver *d = drvtab[i];
1706 printf ("Name: %s\n", d->name);
1707 printf ("Description: %s\n", d->descr);
1709 audio_pp_nb_voices ("playback", d->max_voices_out);
1710 audio_pp_nb_voices ("capture", d->max_voices_in);
1713 printf ("Options:\n");
1714 audio_print_options (d->name, d->options);
1717 printf ("No options\n");
1723 "Options are settable through environment variables.\n"
1726 " set QEMU_AUDIO_DRV=wav\n"
1727 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1729 " export QEMU_AUDIO_DRV=wav\n"
1730 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1731 "(for csh replace export with setenv in the above)\n"
1737 static int audio_driver_init (AudioState *s, struct audio_driver *drv)
1740 audio_process_options (drv->name, drv->options);
1742 s->drv_opaque = drv->init ();
1744 if (s->drv_opaque) {
1745 audio_init_nb_voices_out (drv);
1746 audio_init_nb_voices_in (drv);
1751 dolog ("Could not init `%s' audio driver\n", drv->name);
1756 static void audio_vm_change_state_handler (void *opaque, int running,
1759 AudioState *s = opaque;
1760 HWVoiceOut *hwo = NULL;
1761 HWVoiceIn *hwi = NULL;
1762 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1764 s->vm_running = running;
1765 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1766 hwo->pcm_ops->ctl_out (hwo, op, conf.try_poll_out);
1769 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1770 hwi->pcm_ops->ctl_in (hwi, op, conf.try_poll_in);
1772 audio_reset_timer (s);
1775 static void audio_atexit (void)
1777 AudioState *s = &glob_audio_state;
1778 HWVoiceOut *hwo = NULL;
1779 HWVoiceIn *hwi = NULL;
1781 while ((hwo = audio_pcm_hw_find_any_out (hwo))) {
1785 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1787 hwo->pcm_ops->fini_out (hwo);
1789 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1790 CaptureVoiceOut *cap = sc->cap;
1791 struct capture_callback *cb;
1793 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1794 cb->ops.destroy (cb->opaque);
1799 while ((hwi = audio_pcm_hw_find_any_in (hwi))) {
1801 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1803 hwi->pcm_ops->fini_in (hwi);
1807 s->drv->fini (s->drv_opaque);
1811 static const VMStateDescription vmstate_audio = {
1814 .minimum_version_id = 1,
1815 .fields = (VMStateField[]) {
1816 VMSTATE_END_OF_LIST()
1820 static void audio_init (void)
1824 const char *drvname;
1825 VMChangeStateEntry *e;
1826 AudioState *s = &glob_audio_state;
1832 QLIST_INIT (&s->hw_head_out);
1833 QLIST_INIT (&s->hw_head_in);
1834 QLIST_INIT (&s->cap_head);
1835 atexit (audio_atexit);
1837 s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
1839 hw_error("Could not create audio timer\n");
1842 audio_process_options ("AUDIO", audio_options);
1844 s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1845 s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1847 if (s->nb_hw_voices_out <= 0) {
1848 dolog ("Bogus number of playback voices %d, setting to 1\n",
1849 s->nb_hw_voices_out);
1850 s->nb_hw_voices_out = 1;
1853 if (s->nb_hw_voices_in <= 0) {
1854 dolog ("Bogus number of capture voices %d, setting to 0\n",
1855 s->nb_hw_voices_in);
1856 s->nb_hw_voices_in = 0;
1861 drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1867 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1868 if (!strcmp (drvname, drvtab[i]->name)) {
1869 done = !audio_driver_init (s, drvtab[i]);
1876 dolog ("Unknown audio driver `%s'\n", drvname);
1877 dolog ("Run with -audio-help to list available drivers\n");
1882 for (i = 0; !done && i < ARRAY_SIZE (drvtab); i++) {
1883 if (drvtab[i]->can_be_default) {
1884 done = !audio_driver_init (s, drvtab[i]);
1890 done = !audio_driver_init (s, &no_audio_driver);
1892 hw_error("Could not initialize audio subsystem\n");
1895 dolog ("warning: Using timer based audio emulation\n");
1899 if (conf.period.hertz <= 0) {
1900 if (conf.period.hertz < 0) {
1901 dolog ("warning: Timer period is negative - %d "
1902 "treating as zero\n",
1905 conf.period.ticks = 1;
1908 muldiv64 (1, get_ticks_per_sec (), conf.period.hertz);
1911 e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1913 dolog ("warning: Could not register change state handler\n"
1914 "(Audio can continue looping even after stopping the VM)\n");
1917 QLIST_INIT (&s->card_head);
1918 vmstate_register (NULL, 0, &vmstate_audio, s);
1921 void AUD_register_card (const char *name, QEMUSoundCard *card)
1924 card->name = g_strdup (name);
1925 memset (&card->entries, 0, sizeof (card->entries));
1926 QLIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries);
1929 void AUD_remove_card (QEMUSoundCard *card)
1931 QLIST_REMOVE (card, entries);
1932 g_free (card->name);
1936 CaptureVoiceOut *AUD_add_capture (
1937 struct audsettings *as,
1938 struct audio_capture_ops *ops,
1942 AudioState *s = &glob_audio_state;
1943 CaptureVoiceOut *cap;
1944 struct capture_callback *cb;
1946 if (audio_validate_settings (as)) {
1947 dolog ("Invalid settings were passed when trying to add capture\n");
1948 audio_print_settings (as);
1952 cb = audio_calloc (AUDIO_FUNC, 1, sizeof (*cb));
1954 dolog ("Could not allocate capture callback information, size %zu\n",
1959 cb->opaque = cb_opaque;
1961 cap = audio_pcm_capture_find_specific (as);
1963 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1968 CaptureVoiceOut *cap;
1970 cap = audio_calloc (AUDIO_FUNC, 1, sizeof (*cap));
1972 dolog ("Could not allocate capture voice, size %zu\n",
1978 QLIST_INIT (&hw->sw_head);
1979 QLIST_INIT (&cap->cb_head);
1981 /* XXX find a more elegant way */
1982 hw->samples = 4096 * 4;
1983 hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples,
1984 sizeof (struct st_sample));
1986 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1991 audio_pcm_init_info (&hw->info, as);
1993 cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
1995 dolog ("Could not allocate capture buffer "
1996 "(%d samples, each %d bytes)\n",
1997 hw->samples, 1 << hw->info.shift);
2001 hw->clip = mixeng_clip
2002 [hw->info.nchannels == 2]
2004 [hw->info.swap_endianness]
2005 [audio_bits_to_index (hw->info.bits)];
2007 QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
2008 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
2011 while ((hw = audio_pcm_hw_find_any_out (hw))) {
2012 audio_attach_capture (hw);
2017 g_free (cap->hw.mix_buf);
2027 void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
2029 struct capture_callback *cb;
2031 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
2032 if (cb->opaque == cb_opaque) {
2033 cb->ops.destroy (cb_opaque);
2034 QLIST_REMOVE (cb, entries);
2037 if (!cap->cb_head.lh_first) {
2038 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
2041 SWVoiceCap *sc = (SWVoiceCap *) sw;
2042 #ifdef DEBUG_CAPTURE
2043 dolog ("freeing %s\n", sw->name);
2046 sw1 = sw->entries.le_next;
2048 st_rate_stop (sw->rate);
2051 QLIST_REMOVE (sw, entries);
2052 QLIST_REMOVE (sc, entries);
2056 QLIST_REMOVE (cap, entries);
2064 void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
2067 HWVoiceOut *hw = sw->hw;
2069 sw->vol.mute = mute;
2070 sw->vol.l = nominal_volume.l * lvol / 255;
2071 sw->vol.r = nominal_volume.r * rvol / 255;
2073 if (hw->pcm_ops->ctl_out) {
2074 hw->pcm_ops->ctl_out (hw, VOICE_VOLUME, sw);
2079 void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
2082 HWVoiceIn *hw = sw->hw;
2084 sw->vol.mute = mute;
2085 sw->vol.l = nominal_volume.l * lvol / 255;
2086 sw->vol.r = nominal_volume.r * rvol / 255;
2088 if (hw->pcm_ops->ctl_in) {
2089 hw->pcm_ops->ctl_in (hw, VOICE_VOLUME, sw);