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_LIVE */
34 /* #define DEBUG_OUT */
35 /* #define DEBUG_CAPTURE */
36 /* #define DEBUG_POLL */
38 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
42 #include "../tizen/src/debug_ch.h"
43 MULTI_DEBUG_CHANNEL(tizen, qemu_audio);
46 /* Order of CONFIG_AUDIO_DRIVERS is import.
47 The 1st one is the one used by default, that is the reason
48 that we generate the list.
50 static struct audio_driver *drvtab[] = {
59 struct fixed_settings {
63 struct audsettings settings;
67 struct fixed_settings fixed_out;
68 struct fixed_settings fixed_in;
76 .fixed_out = { /* DAC fixed settings */
84 .endianness = AUDIO_HOST_ENDIANNESS,
88 .fixed_in = { /* ADC fixed settings */
96 .endianness = AUDIO_HOST_ENDIANNESS,
100 .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");
135 AUD_log (NULL, "Please send bug report to av1474@comtv.ru\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)
335 fprintf(stderr, "%s: ", cap);
338 vfprintf(stderr, fmt, ap);
341 void AUD_log (const char *cap, const char *fmt, ...)
346 AUD_vlog (cap, fmt, ap);
350 static void audio_print_options (const char *prefix,
351 struct audio_option *opt)
356 dolog ("No prefix specified\n");
361 dolog ("No options\n");
365 uprefix = audio_alloc_prefix (prefix);
367 for (; opt->name; opt++) {
368 const char *state = "default";
369 printf (" %s_%s: ", uprefix, opt->name);
371 if (opt->overriddenp && *opt->overriddenp) {
378 int *intp = opt->valp;
379 printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
385 int *intp = opt->valp;
386 printf ("integer, %s = %d\n", state, *intp);
392 audfmt_e *fmtp = opt->valp;
394 "format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
396 audio_audfmt_to_string (*fmtp)
403 const char **strp = opt->valp;
404 printf ("string, %s = %s\n",
406 *strp ? *strp : "(not set)");
412 dolog ("Bad value tag for option %s_%s %d\n",
413 uprefix, opt->name, opt->tag);
416 printf (" %s\n", opt->descr);
422 static void audio_process_options (const char *prefix,
423 struct audio_option *opt)
426 const char qemu_prefix[] = "QEMU_";
427 size_t preflen, optlen;
429 if (audio_bug (AUDIO_FUNC, !prefix)) {
430 dolog ("prefix = NULL\n");
434 if (audio_bug (AUDIO_FUNC, !opt)) {
435 dolog ("opt = NULL\n");
439 preflen = strlen (prefix);
441 for (; opt->name; opt++) {
446 dolog ("Option value pointer for `%s' is not set\n",
451 len = strlen (opt->name);
452 /* len of opt->name + len of prefix + size of qemu_prefix
453 * (includes trailing zero) + zero + underscore (on behalf of
455 optlen = len + preflen + sizeof (qemu_prefix) + 1;
456 optname = g_malloc (optlen);
458 pstrcpy (optname, optlen, qemu_prefix);
460 /* copy while upper-casing, including trailing zero */
461 for (i = 0; i <= preflen; ++i) {
462 optname[i + sizeof (qemu_prefix) - 1] = qemu_toupper(prefix[i]);
464 pstrcat (optname, optlen, "_");
465 pstrcat (optname, optlen, opt->name);
472 int *intp = opt->valp;
473 *intp = audio_get_conf_int (optname, *intp, &def);
479 audfmt_e *fmtp = opt->valp;
480 *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
486 const char **strp = opt->valp;
487 *strp = audio_get_conf_str (optname, *strp, &def);
492 dolog ("Bad value tag for option `%s' - %d\n",
497 if (!opt->overriddenp) {
498 opt->overriddenp = &opt->overridden;
500 *opt->overriddenp = !def;
505 static void audio_print_settings (struct audsettings *as)
507 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
511 AUD_log (NULL, "S8");
514 AUD_log (NULL, "U8");
517 AUD_log (NULL, "S16");
520 AUD_log (NULL, "U16");
523 AUD_log (NULL, "S32");
526 AUD_log (NULL, "U32");
529 AUD_log (NULL, "invalid(%d)", as->fmt);
533 AUD_log (NULL, " endianness=");
534 switch (as->endianness) {
536 AUD_log (NULL, "little");
539 AUD_log (NULL, "big");
542 AUD_log (NULL, "invalid");
545 AUD_log (NULL, "\n");
548 static int audio_validate_settings (struct audsettings *as)
552 invalid = as->nchannels != 1 && as->nchannels != 2;
553 invalid |= as->endianness != 0 && as->endianness != 1;
568 invalid |= as->freq <= 0;
569 return invalid ? -1 : 0;
572 static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
574 int bits = 8, sign = 0;
597 return info->freq == as->freq
598 && info->nchannels == as->nchannels
599 && info->sign == sign
600 && info->bits == bits
601 && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
604 void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
606 int bits = 8, sign = 0, shift = 0;
629 info->freq = as->freq;
632 info->nchannels = as->nchannels;
633 info->shift = (as->nchannels == 2) + shift;
634 info->align = (1 << info->shift) - 1;
635 info->bytes_per_second = info->freq << info->shift;
636 info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
639 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
646 memset (buf, 0x00, len << info->shift);
649 switch (info->bits) {
651 memset (buf, 0x80, len << info->shift);
658 int shift = info->nchannels - 1;
661 if (info->swap_endianness) {
665 for (i = 0; i < len << shift; i++) {
675 int shift = info->nchannels - 1;
676 int32_t s = INT32_MAX;
678 if (info->swap_endianness) {
682 for (i = 0; i < len << shift; i++) {
689 AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
699 static void noop_conv (struct st_sample *dst, const void *src, int samples)
706 static CaptureVoiceOut *audio_pcm_capture_find_specific (
707 struct audsettings *as
710 CaptureVoiceOut *cap;
711 AudioState *s = &glob_audio_state;
713 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
714 if (audio_pcm_info_eq (&cap->hw.info, as)) {
721 static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
723 struct capture_callback *cb;
726 dolog ("notification %d sent\n", cmd);
728 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
729 cb->ops.notify (cb->opaque, cmd);
733 static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
735 if (cap->hw.enabled != enabled) {
736 audcnotification_e cmd;
737 cap->hw.enabled = enabled;
738 cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
739 audio_notify_capture (cap, cmd);
743 static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
745 HWVoiceOut *hw = &cap->hw;
749 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
755 audio_capture_maybe_changed (cap, enabled);
758 static void audio_detach_capture (HWVoiceOut *hw)
760 SWVoiceCap *sc = hw->cap_head.lh_first;
763 SWVoiceCap *sc1 = sc->entries.le_next;
764 SWVoiceOut *sw = &sc->sw;
765 CaptureVoiceOut *cap = sc->cap;
766 int was_active = sw->active;
769 st_rate_stop (sw->rate);
773 QLIST_REMOVE (sw, entries);
774 QLIST_REMOVE (sc, entries);
777 /* We have removed soft voice from the capture:
778 this might have changed the overall status of the capture
779 since this might have been the only active voice */
780 audio_recalc_and_notify_capture (cap);
786 static int audio_attach_capture (HWVoiceOut *hw)
788 AudioState *s = &glob_audio_state;
789 CaptureVoiceOut *cap;
791 audio_detach_capture (hw);
792 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
795 HWVoiceOut *hw_cap = &cap->hw;
797 sc = audio_calloc (AUDIO_FUNC, 1, sizeof (*sc));
799 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
809 sw->active = hw->enabled;
810 sw->conv = noop_conv;
811 sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
812 sw->vol = nominal_volume;
813 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
815 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
819 QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
820 QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
822 sw->name = g_strdup_printf ("for %p %d,%d,%d",
823 hw, sw->info.freq, sw->info.bits,
825 dolog ("Added %s active = %d\n", sw->name, sw->active);
828 audio_capture_maybe_changed (cap, 1);
835 * Hard voice (capture)
837 static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
840 int m = hw->total_samples_captured;
842 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
844 m = audio_MIN (m, sw->total_hw_samples_acquired);
850 int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
852 int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
853 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
854 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
860 int audio_pcm_hw_clip_out (HWVoiceOut *hw, void *pcm_buf,
861 int live, int pending)
863 int left = hw->samples - pending;
864 int len = audio_MIN (left, live);
868 struct st_sample *src = hw->mix_buf + hw->rpos;
869 uint8_t *dst = advance (pcm_buf, hw->rpos << hw->info.shift);
870 int samples_till_end_of_buf = hw->samples - hw->rpos;
871 int samples_to_clip = audio_MIN (len, samples_till_end_of_buf);
873 hw->clip (dst, src, samples_to_clip);
875 hw->rpos = (hw->rpos + samples_to_clip) % hw->samples;
876 len -= samples_to_clip;
877 clipped += samples_to_clip;
883 * Soft voice (capture)
885 static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
887 HWVoiceIn *hw = sw->hw;
888 int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
891 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
892 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
896 rpos = hw->wpos - live;
901 return hw->samples + rpos;
905 int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
907 HWVoiceIn *hw = sw->hw;
908 int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
909 struct st_sample *src, *dst = sw->buf;
911 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
913 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
914 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
915 dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
919 samples = size >> sw->info.shift;
924 swlim = (live * sw->ratio) >> 32;
925 swlim = audio_MIN (swlim, samples);
928 src = hw->conv_buf + rpos;
929 isamp = hw->wpos - rpos;
932 isamp = hw->samples - rpos;
940 if (audio_bug (AUDIO_FUNC, osamp < 0)) {
941 dolog ("osamp=%d\n", osamp);
945 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
947 rpos = (rpos + isamp) % hw->samples;
953 if (!(hw->ctl_caps & VOICE_VOLUME_CAP)) {
954 mixeng_volume (sw->buf, ret, &sw->vol);
957 sw->clip (buf, sw->buf, ret);
958 sw->total_hw_samples_acquired += total;
959 return ret << sw->info.shift;
963 * Hard voice (playback)
965 static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
971 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
972 if (sw->active || !sw->empty) {
973 m = audio_MIN (m, sw->total_hw_samples_mixed);
982 static int audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
987 smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
995 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
996 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1005 * Soft voice (playback)
1007 int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
1009 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
1010 int ret = 0, pos = 0, total = 0;
1016 hwsamples = sw->hw->samples;
1018 live = sw->total_hw_samples_mixed;
1019 if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
1020 dolog ("live=%d hw->samples=%d\n", live, hwsamples);
1024 if (live == hwsamples) {
1026 dolog ("%s is full %d\n", sw->name, live);
1031 wpos = (sw->hw->rpos + live) % hwsamples;
1032 samples = size >> sw->info.shift;
1034 dead = hwsamples - live;
1035 swlim = ((int64_t) dead << 32) / sw->ratio;
1036 swlim = audio_MIN (swlim, samples);
1038 sw->conv (sw->buf, buf, swlim);
1040 if (!(sw->hw->ctl_caps & VOICE_VOLUME_CAP)) {
1041 mixeng_volume (sw->buf, swlim, &sw->vol);
1046 dead = hwsamples - live;
1047 left = hwsamples - wpos;
1048 blck = audio_MIN (dead, left);
1057 sw->hw->mix_buf + wpos,
1065 wpos = (wpos + osamp) % hwsamples;
1069 sw->total_hw_samples_mixed += total;
1070 sw->empty = sw->total_hw_samples_mixed == 0;
1074 "%s: write size %d ret %d total sw %d\n",
1076 size >> sw->info.shift,
1078 sw->total_hw_samples_mixed
1082 return ret << sw->info.shift;
1086 static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
1088 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1089 cap, info->bits, info->sign, info->freq, info->nchannels);
1094 #include "audio_template.h"
1096 #include "audio_template.h"
1101 static int audio_is_timer_needed (void)
1103 HWVoiceIn *hwi = NULL;
1104 HWVoiceOut *hwo = NULL;
1106 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1107 if (!hwo->poll_mode) return 1;
1109 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1110 if (!hwi->poll_mode) return 1;
1115 static void audio_reset_timer (AudioState *s)
1117 if (audio_is_timer_needed ()) {
1119 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + conf.period.ticks);
1126 static void audio_timer (void *opaque)
1128 audio_run ("timer");
1129 audio_reset_timer (opaque);
1135 int AUD_write (SWVoiceOut *sw, void *buf, int size)
1140 /* XXX: Consider options */
1144 if (!sw->hw->enabled) {
1145 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
1149 bytes = sw->hw->pcm_ops->write (sw, buf, size);
1153 int AUD_read (SWVoiceIn *sw, void *buf, int size)
1158 /* XXX: Consider options */
1162 if (!sw->hw->enabled) {
1163 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1167 bytes = sw->hw->pcm_ops->read (sw, buf, size);
1171 int AUD_get_buffer_size_out (SWVoiceOut *sw)
1173 return sw->hw->samples << sw->hw->info.shift;
1176 void AUD_set_active_out (SWVoiceOut *sw, int on)
1185 if (sw->active != on) {
1186 AudioState *s = &glob_audio_state;
1187 SWVoiceOut *temp_sw;
1191 hw->pending_disable = 0;
1194 if (s->vm_running) {
1195 hw->pcm_ops->ctl_out (hw, VOICE_ENABLE, conf.try_poll_out);
1196 audio_reset_timer (s);
1204 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1205 temp_sw = temp_sw->entries.le_next) {
1206 nb_active += temp_sw->active != 0;
1209 hw->pending_disable = nb_active == 1;
1213 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1214 sc->sw.active = hw->enabled;
1216 audio_capture_maybe_changed (sc->cap, 1);
1223 void AUD_set_active_in (SWVoiceIn *sw, int on)
1232 if (sw->active != on) {
1233 AudioState *s = &glob_audio_state;
1239 if (s->vm_running) {
1240 hw->pcm_ops->ctl_in (hw, VOICE_ENABLE, conf.try_poll_in);
1241 audio_reset_timer (s);
1244 sw->total_hw_samples_acquired = hw->total_samples_captured;
1250 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1251 temp_sw = temp_sw->entries.le_next) {
1252 nb_active += temp_sw->active != 0;
1255 if (nb_active == 1) {
1257 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
1265 static int audio_get_avail (SWVoiceIn *sw)
1273 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1274 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1275 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1280 "%s: get_avail live %d ret %" PRId64 "\n",
1282 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1285 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1288 static int audio_get_free (SWVoiceOut *sw)
1296 live = sw->total_hw_samples_mixed;
1298 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1299 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1303 dead = sw->hw->samples - live;
1306 dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
1308 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1311 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1314 static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1321 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1322 SWVoiceOut *sw = &sc->sw;
1327 int till_end_of_hw = hw->samples - rpos2;
1328 int to_write = audio_MIN (till_end_of_hw, n);
1329 int bytes = to_write << hw->info.shift;
1332 sw->buf = hw->mix_buf + rpos2;
1333 written = audio_pcm_sw_write (sw, NULL, bytes);
1334 if (written - bytes) {
1335 dolog ("Could not mix %d bytes into a capture "
1336 "buffer, mixed %d\n",
1341 rpos2 = (rpos2 + to_write) % hw->samples;
1346 n = audio_MIN (samples, hw->samples - rpos);
1347 mixeng_clear (hw->mix_buf + rpos, n);
1348 mixeng_clear (hw->mix_buf, samples - n);
1351 static void audio_run_out (AudioState *s)
1353 HWVoiceOut *hw = NULL;
1356 while ((hw = audio_pcm_hw_find_any_enabled_out (hw))) {
1358 int live, free, nb_live, cleanup_required, prev_rpos;
1360 live = audio_pcm_hw_get_live_out (hw, &nb_live);
1365 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1366 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1370 if (hw->pending_disable && !nb_live) {
1373 dolog ("Disabling voice\n");
1376 hw->pending_disable = 0;
1377 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
1378 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1380 audio_recalc_and_notify_capture (sc->cap);
1386 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1388 free = audio_get_free (sw);
1390 sw->callback.fn (sw->callback.opaque, free);
1397 prev_rpos = hw->rpos;
1398 played = hw->pcm_ops->run_out (hw, live);
1399 if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1400 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1401 hw->rpos, hw->samples, played);
1406 dolog ("played=%d\n", played);
1410 hw->ts_helper += played;
1411 audio_capture_mix_and_clear (hw, prev_rpos, played);
1414 cleanup_required = 0;
1415 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1416 if (!sw->active && sw->empty) {
1420 if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1421 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1422 played, sw->total_hw_samples_mixed);
1423 played = sw->total_hw_samples_mixed;
1426 sw->total_hw_samples_mixed -= played;
1428 if (!sw->total_hw_samples_mixed) {
1430 cleanup_required |= !sw->active && !sw->callback.fn;
1434 free = audio_get_free (sw);
1436 sw->callback.fn (sw->callback.opaque, free);
1441 if (cleanup_required) {
1444 sw = hw->sw_head.lh_first;
1446 sw1 = sw->entries.le_next;
1447 if (!sw->active && !sw->callback.fn) {
1448 audio_close_out (sw);
1456 static void audio_run_in (AudioState *s)
1458 HWVoiceIn *hw = NULL;
1460 while ((hw = audio_pcm_hw_find_any_enabled_in (hw))) {
1464 captured = hw->pcm_ops->run_in (hw);
1466 min = audio_pcm_hw_find_min_in (hw);
1467 hw->total_samples_captured += captured - min;
1468 hw->ts_helper += captured;
1470 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1471 sw->total_hw_samples_acquired -= min;
1476 avail = audio_get_avail (sw);
1478 sw->callback.fn (sw->callback.opaque, avail);
1485 static void audio_run_capture (AudioState *s)
1487 CaptureVoiceOut *cap;
1489 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1490 int live, rpos, captured;
1491 HWVoiceOut *hw = &cap->hw;
1494 captured = live = audio_pcm_hw_get_live_out (hw, NULL);
1497 int left = hw->samples - rpos;
1498 int to_capture = audio_MIN (live, left);
1499 struct st_sample *src;
1500 struct capture_callback *cb;
1502 src = hw->mix_buf + rpos;
1503 hw->clip (cap->buf, src, to_capture);
1504 mixeng_clear (src, to_capture);
1506 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1507 cb->ops.capture (cb->opaque, cap->buf,
1508 to_capture << hw->info.shift);
1510 rpos = (rpos + to_capture) % hw->samples;
1515 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1516 if (!sw->active && sw->empty) {
1520 if (audio_bug (AUDIO_FUNC, captured > sw->total_hw_samples_mixed)) {
1521 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1522 captured, sw->total_hw_samples_mixed);
1523 captured = sw->total_hw_samples_mixed;
1526 sw->total_hw_samples_mixed -= captured;
1527 sw->empty = sw->total_hw_samples_mixed == 0;
1532 void audio_run (const char *msg)
1534 AudioState *s = &glob_audio_state;
1538 audio_run_capture (s);
1541 static double prevtime;
1545 if (gettimeofday (&tv, NULL)) {
1546 perror ("audio_run: gettimeofday");
1550 currtime = tv.tv_sec + tv.tv_usec * 1e-6;
1551 dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime);
1552 prevtime = currtime;
1557 static struct audio_option audio_options[] = {
1560 .name = "DAC_FIXED_SETTINGS",
1561 .tag = AUD_OPT_BOOL,
1562 .valp = &conf.fixed_out.enabled,
1563 .descr = "Use fixed settings for host DAC"
1566 .name = "DAC_FIXED_FREQ",
1568 .valp = &conf.fixed_out.settings.freq,
1569 .descr = "Frequency for fixed host DAC"
1572 .name = "DAC_FIXED_FMT",
1574 .valp = &conf.fixed_out.settings.fmt,
1575 .descr = "Format for fixed host DAC"
1578 .name = "DAC_FIXED_CHANNELS",
1580 .valp = &conf.fixed_out.settings.nchannels,
1581 .descr = "Number of channels for fixed DAC (1 - mono, 2 - stereo)"
1584 .name = "DAC_VOICES",
1586 .valp = &conf.fixed_out.nb_voices,
1587 .descr = "Number of voices for DAC"
1590 .name = "DAC_TRY_POLL",
1591 .tag = AUD_OPT_BOOL,
1592 .valp = &conf.try_poll_out,
1593 .descr = "Attempt using poll mode for DAC"
1597 .name = "ADC_FIXED_SETTINGS",
1598 .tag = AUD_OPT_BOOL,
1599 .valp = &conf.fixed_in.enabled,
1600 .descr = "Use fixed settings for host ADC"
1603 .name = "ADC_FIXED_FREQ",
1605 .valp = &conf.fixed_in.settings.freq,
1606 .descr = "Frequency for fixed host ADC"
1609 .name = "ADC_FIXED_FMT",
1611 .valp = &conf.fixed_in.settings.fmt,
1612 .descr = "Format for fixed host ADC"
1615 .name = "ADC_FIXED_CHANNELS",
1617 .valp = &conf.fixed_in.settings.nchannels,
1618 .descr = "Number of channels for fixed ADC (1 - mono, 2 - stereo)"
1621 .name = "ADC_VOICES",
1623 .valp = &conf.fixed_in.nb_voices,
1624 .descr = "Number of voices for ADC"
1627 .name = "ADC_TRY_POLL",
1628 .tag = AUD_OPT_BOOL,
1629 .valp = &conf.try_poll_in,
1630 .descr = "Attempt using poll mode for ADC"
1634 .name = "TIMER_PERIOD",
1636 .valp = &conf.period.hertz,
1637 .descr = "Timer period in HZ (0 - use lowest possible)"
1639 { /* End of list */ }
1642 static void audio_pp_nb_voices (const char *typ, int nb)
1646 printf ("Does not support %s\n", typ);
1649 printf ("One %s voice\n", typ);
1652 printf ("Theoretically supports many %s voices\n", typ);
1655 printf ("Theoretically supports up to %d %s voices\n", nb, typ);
1661 void AUD_help (void)
1665 audio_process_options ("AUDIO", audio_options);
1666 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1667 struct audio_driver *d = drvtab[i];
1669 audio_process_options (d->name, d->options);
1673 printf ("Audio options:\n");
1674 audio_print_options ("AUDIO", audio_options);
1677 printf ("Available drivers:\n");
1679 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1680 struct audio_driver *d = drvtab[i];
1682 printf ("Name: %s\n", d->name);
1683 printf ("Description: %s\n", d->descr);
1685 audio_pp_nb_voices ("playback", d->max_voices_out);
1686 audio_pp_nb_voices ("capture", d->max_voices_in);
1689 printf ("Options:\n");
1690 audio_print_options (d->name, d->options);
1693 printf ("No options\n");
1699 "Options are settable through environment variables.\n"
1702 " set QEMU_AUDIO_DRV=wav\n"
1703 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1705 " export QEMU_AUDIO_DRV=wav\n"
1706 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1707 "(for csh replace export with setenv in the above)\n"
1713 static int audio_driver_init (AudioState *s, struct audio_driver *drv)
1716 audio_process_options (drv->name, drv->options);
1718 s->drv_opaque = drv->init ();
1720 if (s->drv_opaque) {
1721 audio_init_nb_voices_out (drv);
1722 audio_init_nb_voices_in (drv);
1727 dolog ("Could not init `%s' audio driver\n", drv->name);
1732 static void audio_vm_change_state_handler (void *opaque, int running,
1735 AudioState *s = opaque;
1736 HWVoiceOut *hwo = NULL;
1737 HWVoiceIn *hwi = NULL;
1738 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1740 s->vm_running = running;
1741 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1742 hwo->pcm_ops->ctl_out (hwo, op, conf.try_poll_out);
1745 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1746 hwi->pcm_ops->ctl_in (hwi, op, conf.try_poll_in);
1748 audio_reset_timer (s);
1751 static void audio_atexit (void)
1753 AudioState *s = &glob_audio_state;
1754 HWVoiceOut *hwo = NULL;
1755 HWVoiceIn *hwi = NULL;
1757 while ((hwo = audio_pcm_hw_find_any_out (hwo))) {
1761 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1763 hwo->pcm_ops->fini_out (hwo);
1765 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1766 CaptureVoiceOut *cap = sc->cap;
1767 struct capture_callback *cb;
1769 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1770 cb->ops.destroy (cb->opaque);
1775 while ((hwi = audio_pcm_hw_find_any_in (hwi))) {
1777 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1779 hwi->pcm_ops->fini_in (hwi);
1783 s->drv->fini (s->drv_opaque);
1787 static const VMStateDescription vmstate_audio = {
1790 .minimum_version_id = 1,
1791 .fields = (VMStateField[]) {
1792 VMSTATE_END_OF_LIST()
1796 static void audio_init (void)
1800 const char *drvname;
1801 VMChangeStateEntry *e;
1802 AudioState *s = &glob_audio_state;
1808 QLIST_INIT (&s->hw_head_out);
1809 QLIST_INIT (&s->hw_head_in);
1810 QLIST_INIT (&s->cap_head);
1811 atexit (audio_atexit);
1813 s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
1815 hw_error("Could not create audio timer\n");
1818 audio_process_options ("AUDIO", audio_options);
1820 s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1821 s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1823 if (s->nb_hw_voices_out <= 0) {
1824 dolog ("Bogus number of playback voices %d, setting to 1\n",
1825 s->nb_hw_voices_out);
1826 s->nb_hw_voices_out = 1;
1829 if (s->nb_hw_voices_in <= 0) {
1830 dolog ("Bogus number of capture voices %d, setting to 0\n",
1831 s->nb_hw_voices_in);
1832 s->nb_hw_voices_in = 0;
1837 drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1843 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1844 if (!strcmp (drvname, drvtab[i]->name)) {
1845 done = !audio_driver_init (s, drvtab[i]);
1852 dolog ("Unknown audio driver `%s'\n", drvname);
1853 dolog ("Run with -audio-help to list available drivers\n");
1858 for (i = 0; !done && i < ARRAY_SIZE (drvtab); i++) {
1859 if (drvtab[i]->can_be_default) {
1860 done = !audio_driver_init (s, drvtab[i]);
1866 // Try to avoid certain wave out locking action in recent Windows...
1867 // If wave out is locked (because nothing is wired to output jack, ...),
1868 // QEMU can find voice out device, but open will failed. And it will cause guest audio lock-up.
1869 // So, we test whether opening is success or not.
1870 // It can not prevent lock-up caused by runtime voice out lock.
1871 // To prevent it, QEMU audio backend structure must be changed.
1872 // We should do it, but now we used simple fix temporarily.
1873 HWVoiceOut* hw = audio_calloc(AUDIO_FUNC, 1, s->drv->voice_size_out);
1875 dolog ("Can not allocate voice `%s' size %d\n",
1876 s->drv->name, s->drv->voice_size_out);
1878 if(s->drv->pcm_ops->init_out(hw, &conf.fixed_out.settings)) {
1879 INFO("Host audio out [%s] is malfunction. Change to noaudio driver\n",
1884 INFO("Host audio out [%s] is normal.\n", s->drv->name);
1885 s->drv->pcm_ops->fini_out(hw);
1891 done = !audio_driver_init (s, &no_audio_driver);
1893 hw_error("Could not initialize audio subsystem\n");
1896 dolog ("warning: Using timer based audio emulation\n");
1900 if (conf.period.hertz <= 0) {
1901 if (conf.period.hertz < 0) {
1902 dolog ("warning: Timer period is negative - %d "
1903 "treating as zero\n",
1906 conf.period.ticks = 1;
1909 muldiv64 (1, get_ticks_per_sec (), conf.period.hertz);
1912 e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1914 dolog ("warning: Could not register change state handler\n"
1915 "(Audio can continue looping even after stopping the VM)\n");
1918 QLIST_INIT (&s->card_head);
1919 vmstate_register (NULL, 0, &vmstate_audio, s);
1922 void AUD_register_card (const char *name, QEMUSoundCard *card)
1925 card->name = g_strdup (name);
1926 memset (&card->entries, 0, sizeof (card->entries));
1927 QLIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries);
1930 void AUD_remove_card (QEMUSoundCard *card)
1932 QLIST_REMOVE (card, entries);
1933 g_free (card->name);
1937 CaptureVoiceOut *AUD_add_capture (
1938 struct audsettings *as,
1939 struct audio_capture_ops *ops,
1943 AudioState *s = &glob_audio_state;
1944 CaptureVoiceOut *cap;
1945 struct capture_callback *cb;
1947 if (audio_validate_settings (as)) {
1948 dolog ("Invalid settings were passed when trying to add capture\n");
1949 audio_print_settings (as);
1953 cb = audio_calloc (AUDIO_FUNC, 1, sizeof (*cb));
1955 dolog ("Could not allocate capture callback information, size %zu\n",
1960 cb->opaque = cb_opaque;
1962 cap = audio_pcm_capture_find_specific (as);
1964 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1969 CaptureVoiceOut *cap;
1971 cap = audio_calloc (AUDIO_FUNC, 1, sizeof (*cap));
1973 dolog ("Could not allocate capture voice, size %zu\n",
1979 QLIST_INIT (&hw->sw_head);
1980 QLIST_INIT (&cap->cb_head);
1982 /* XXX find a more elegant way */
1983 hw->samples = 4096 * 4;
1984 hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples,
1985 sizeof (struct st_sample));
1987 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1992 audio_pcm_init_info (&hw->info, as);
1994 cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
1996 dolog ("Could not allocate capture buffer "
1997 "(%d samples, each %d bytes)\n",
1998 hw->samples, 1 << hw->info.shift);
2002 hw->clip = mixeng_clip
2003 [hw->info.nchannels == 2]
2005 [hw->info.swap_endianness]
2006 [audio_bits_to_index (hw->info.bits)];
2008 QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
2009 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
2012 while ((hw = audio_pcm_hw_find_any_out (hw))) {
2013 audio_attach_capture (hw);
2018 g_free (cap->hw.mix_buf);
2028 void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
2030 struct capture_callback *cb;
2032 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
2033 if (cb->opaque == cb_opaque) {
2034 cb->ops.destroy (cb_opaque);
2035 QLIST_REMOVE (cb, entries);
2038 if (!cap->cb_head.lh_first) {
2039 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
2042 SWVoiceCap *sc = (SWVoiceCap *) sw;
2043 #ifdef DEBUG_CAPTURE
2044 dolog ("freeing %s\n", sw->name);
2047 sw1 = sw->entries.le_next;
2049 st_rate_stop (sw->rate);
2052 QLIST_REMOVE (sw, entries);
2053 QLIST_REMOVE (sc, entries);
2057 QLIST_REMOVE (cap, entries);
2065 void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
2068 HWVoiceOut *hw = sw->hw;
2070 sw->vol.mute = mute;
2071 sw->vol.l = nominal_volume.l * lvol / 255;
2072 sw->vol.r = nominal_volume.r * rvol / 255;
2074 if (hw->pcm_ops->ctl_out) {
2075 hw->pcm_ops->ctl_out (hw, VOICE_VOLUME, sw);
2080 void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
2083 HWVoiceIn *hw = sw->hw;
2085 sw->vol.mute = mute;
2086 sw->vol.l = nominal_volume.l * lvol / 255;
2087 sw->vol.r = nominal_volume.r * rvol / 255;
2089 if (hw->pcm_ops->ctl_in) {
2090 hw->pcm_ops->ctl_in (hw, VOICE_VOLUME, sw);