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
27 #include "qemu-timer.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"
43 #include "../tizen/src/debug_ch.h"
44 MULTI_DEBUG_CHANNEL(tizen, qemu_audio);
47 /* Order of CONFIG_AUDIO_DRIVERS is import.
48 The 1st one is the one used by default, that is the reason
49 that we generate the list.
51 static struct audio_driver *drvtab[] = {
60 struct fixed_settings {
64 struct audsettings settings;
68 struct fixed_settings fixed_out;
69 struct fixed_settings fixed_in;
79 .fixed_out = { /* DAC fixed settings */
87 .endianness = AUDIO_HOST_ENDIANNESS,
91 .fixed_in = { /* ADC fixed settings */
99 .endianness = AUDIO_HOST_ENDIANNESS,
103 .period = { .hertz = 250 },
110 static AudioState glob_audio_state;
112 const struct mixeng_volume nominal_volume = {
123 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
126 static void audio_print_options (const char *prefix,
127 struct audio_option *opt);
129 int audio_bug (const char *funcname, int cond)
134 AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
136 struct audio_driver *d;
139 AUD_log (NULL, "Save all your work and restart without audio\n");
140 AUD_log (NULL, "Please send bug report to av1474@comtv.ru\n");
141 AUD_log (NULL, "I am sorry\n");
142 d = glob_audio_state.drv;
144 audio_print_options (d->name, d->options);
147 AUD_log (NULL, "Context:\n");
149 #if defined AUDIO_BREAKPOINT_ON_BUG
150 # if defined HOST_I386
151 # if defined __GNUC__
153 # elif defined _MSC_VER
168 static inline int audio_bits_to_index (int bits)
181 audio_bug ("bits_to_index", 1);
182 AUD_log (NULL, "invalid bits %d\n", bits);
187 void *audio_calloc (const char *funcname, int nmemb, size_t size)
193 cond = !nmemb || !size;
197 if (audio_bug ("audio_calloc", cond)) {
198 AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
200 AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
204 return g_malloc0 (len);
207 static char *audio_alloc_prefix (const char *s)
209 const char qemu_prefix[] = "QEMU_";
218 r = g_malloc (len + sizeof (qemu_prefix));
220 u = r + sizeof (qemu_prefix) - 1;
222 pstrcpy (r, len + sizeof (qemu_prefix), qemu_prefix);
223 pstrcat (r, len + sizeof (qemu_prefix), s);
225 for (i = 0; i < len; ++i) {
226 u[i] = qemu_toupper(u[i]);
232 static const char *audio_audfmt_to_string (audfmt_e fmt)
254 dolog ("Bogus audfmt %d returning S16\n", fmt);
258 static audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval,
261 if (!strcasecmp (s, "u8")) {
265 else if (!strcasecmp (s, "u16")) {
269 else if (!strcasecmp (s, "u32")) {
273 else if (!strcasecmp (s, "s8")) {
277 else if (!strcasecmp (s, "s16")) {
281 else if (!strcasecmp (s, "s32")) {
286 dolog ("Bogus audio format `%s' using %s\n",
287 s, audio_audfmt_to_string (defval));
293 static audfmt_e audio_get_conf_fmt (const char *envname,
297 const char *var = getenv (envname);
302 return audio_string_to_audfmt (var, defval, defaultp);
305 static int audio_get_conf_int (const char *key, int defval, int *defaultp)
310 strval = getenv (key);
322 static const char *audio_get_conf_str (const char *key,
326 const char *val = getenv (key);
337 void AUD_vlog (const char *cap, const char *fmt, va_list ap)
339 if (conf.log_to_monitor) {
341 monitor_printf(default_mon, "%s: ", cap);
344 monitor_vprintf(default_mon, fmt, ap);
351 fprintf (stderr, "%s: ", cap);
354 vfprintf (stderr, fmt, ap);
359 void AUD_log (const char *cap, const char *fmt, ...)
364 AUD_vlog (cap, fmt, ap);
368 static void audio_print_options (const char *prefix,
369 struct audio_option *opt)
374 dolog ("No prefix specified\n");
379 dolog ("No options\n");
383 uprefix = audio_alloc_prefix (prefix);
385 for (; opt->name; opt++) {
386 const char *state = "default";
387 printf (" %s_%s: ", uprefix, opt->name);
389 if (opt->overriddenp && *opt->overriddenp) {
396 int *intp = opt->valp;
397 printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
403 int *intp = opt->valp;
404 printf ("integer, %s = %d\n", state, *intp);
410 audfmt_e *fmtp = opt->valp;
412 "format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
414 audio_audfmt_to_string (*fmtp)
421 const char **strp = opt->valp;
422 printf ("string, %s = %s\n",
424 *strp ? *strp : "(not set)");
430 dolog ("Bad value tag for option %s_%s %d\n",
431 uprefix, opt->name, opt->tag);
434 printf (" %s\n", opt->descr);
440 static void audio_process_options (const char *prefix,
441 struct audio_option *opt)
444 const char qemu_prefix[] = "QEMU_";
445 size_t preflen, optlen;
447 if (audio_bug (AUDIO_FUNC, !prefix)) {
448 dolog ("prefix = NULL\n");
452 if (audio_bug (AUDIO_FUNC, !opt)) {
453 dolog ("opt = NULL\n");
457 preflen = strlen (prefix);
459 for (; opt->name; opt++) {
464 dolog ("Option value pointer for `%s' is not set\n",
469 len = strlen (opt->name);
470 /* len of opt->name + len of prefix + size of qemu_prefix
471 * (includes trailing zero) + zero + underscore (on behalf of
473 optlen = len + preflen + sizeof (qemu_prefix) + 1;
474 optname = g_malloc (optlen);
476 pstrcpy (optname, optlen, qemu_prefix);
478 /* copy while upper-casing, including trailing zero */
479 for (i = 0; i <= preflen; ++i) {
480 optname[i + sizeof (qemu_prefix) - 1] = qemu_toupper(prefix[i]);
482 pstrcat (optname, optlen, "_");
483 pstrcat (optname, optlen, opt->name);
490 int *intp = opt->valp;
491 *intp = audio_get_conf_int (optname, *intp, &def);
497 audfmt_e *fmtp = opt->valp;
498 *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
504 const char **strp = opt->valp;
505 *strp = audio_get_conf_str (optname, *strp, &def);
510 dolog ("Bad value tag for option `%s' - %d\n",
515 if (!opt->overriddenp) {
516 opt->overriddenp = &opt->overridden;
518 *opt->overriddenp = !def;
523 static void audio_print_settings (struct audsettings *as)
525 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
529 AUD_log (NULL, "S8");
532 AUD_log (NULL, "U8");
535 AUD_log (NULL, "S16");
538 AUD_log (NULL, "U16");
541 AUD_log (NULL, "S32");
544 AUD_log (NULL, "U32");
547 AUD_log (NULL, "invalid(%d)", as->fmt);
551 AUD_log (NULL, " endianness=");
552 switch (as->endianness) {
554 AUD_log (NULL, "little");
557 AUD_log (NULL, "big");
560 AUD_log (NULL, "invalid");
563 AUD_log (NULL, "\n");
566 static int audio_validate_settings (struct audsettings *as)
570 invalid = as->nchannels != 1 && as->nchannels != 2;
571 invalid |= as->endianness != 0 && as->endianness != 1;
586 invalid |= as->freq <= 0;
587 return invalid ? -1 : 0;
590 static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
592 int bits = 8, sign = 0;
615 return info->freq == as->freq
616 && info->nchannels == as->nchannels
617 && info->sign == sign
618 && info->bits == bits
619 && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
622 void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
624 int bits = 8, sign = 0, shift = 0;
647 info->freq = as->freq;
650 info->nchannels = as->nchannels;
651 info->shift = (as->nchannels == 2) + shift;
652 info->align = (1 << info->shift) - 1;
653 info->bytes_per_second = info->freq << info->shift;
654 info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
657 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
664 memset (buf, 0x00, len << info->shift);
667 switch (info->bits) {
669 memset (buf, 0x80, len << info->shift);
676 int shift = info->nchannels - 1;
679 if (info->swap_endianness) {
683 for (i = 0; i < len << shift; i++) {
693 int shift = info->nchannels - 1;
694 int32_t s = INT32_MAX;
696 if (info->swap_endianness) {
700 for (i = 0; i < len << shift; i++) {
707 AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
717 static void noop_conv (struct st_sample *dst, const void *src, int samples)
724 static CaptureVoiceOut *audio_pcm_capture_find_specific (
725 struct audsettings *as
728 CaptureVoiceOut *cap;
729 AudioState *s = &glob_audio_state;
731 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
732 if (audio_pcm_info_eq (&cap->hw.info, as)) {
739 static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
741 struct capture_callback *cb;
744 dolog ("notification %d sent\n", cmd);
746 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
747 cb->ops.notify (cb->opaque, cmd);
751 static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
753 if (cap->hw.enabled != enabled) {
754 audcnotification_e cmd;
755 cap->hw.enabled = enabled;
756 cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
757 audio_notify_capture (cap, cmd);
761 static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
763 HWVoiceOut *hw = &cap->hw;
767 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
773 audio_capture_maybe_changed (cap, enabled);
776 static void audio_detach_capture (HWVoiceOut *hw)
778 SWVoiceCap *sc = hw->cap_head.lh_first;
781 SWVoiceCap *sc1 = sc->entries.le_next;
782 SWVoiceOut *sw = &sc->sw;
783 CaptureVoiceOut *cap = sc->cap;
784 int was_active = sw->active;
787 st_rate_stop (sw->rate);
791 QLIST_REMOVE (sw, entries);
792 QLIST_REMOVE (sc, entries);
795 /* We have removed soft voice from the capture:
796 this might have changed the overall status of the capture
797 since this might have been the only active voice */
798 audio_recalc_and_notify_capture (cap);
804 static int audio_attach_capture (HWVoiceOut *hw)
806 AudioState *s = &glob_audio_state;
807 CaptureVoiceOut *cap;
809 audio_detach_capture (hw);
810 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
813 HWVoiceOut *hw_cap = &cap->hw;
815 sc = audio_calloc (AUDIO_FUNC, 1, sizeof (*sc));
817 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
827 sw->active = hw->enabled;
828 sw->conv = noop_conv;
829 sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
830 sw->vol = nominal_volume;
831 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
833 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
837 QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
838 QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
840 asprintf (&sw->name, "for %p %d,%d,%d",
841 hw, sw->info.freq, sw->info.bits, sw->info.nchannels);
842 dolog ("Added %s active = %d\n", sw->name, sw->active);
845 audio_capture_maybe_changed (cap, 1);
852 * Hard voice (capture)
854 static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
857 int m = hw->total_samples_captured;
859 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
861 m = audio_MIN (m, sw->total_hw_samples_acquired);
867 int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
869 int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
870 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
871 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
877 int audio_pcm_hw_clip_out (HWVoiceOut *hw, void *pcm_buf,
878 int live, int pending)
880 int left = hw->samples - pending;
881 int len = audio_MIN (left, live);
885 struct st_sample *src = hw->mix_buf + hw->rpos;
886 uint8_t *dst = advance (pcm_buf, hw->rpos << hw->info.shift);
887 int samples_till_end_of_buf = hw->samples - hw->rpos;
888 int samples_to_clip = audio_MIN (len, samples_till_end_of_buf);
890 hw->clip (dst, src, samples_to_clip);
892 hw->rpos = (hw->rpos + samples_to_clip) % hw->samples;
893 len -= samples_to_clip;
894 clipped += samples_to_clip;
900 * Soft voice (capture)
902 static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
904 HWVoiceIn *hw = sw->hw;
905 int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
908 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
909 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
913 rpos = hw->wpos - live;
918 return hw->samples + rpos;
922 int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
924 HWVoiceIn *hw = sw->hw;
925 int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
926 struct st_sample *src, *dst = sw->buf;
928 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
930 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
931 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
932 dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
936 samples = size >> sw->info.shift;
941 swlim = (live * sw->ratio) >> 32;
942 swlim = audio_MIN (swlim, samples);
945 src = hw->conv_buf + rpos;
946 isamp = hw->wpos - rpos;
949 isamp = hw->samples - rpos;
957 if (audio_bug (AUDIO_FUNC, osamp < 0)) {
958 dolog ("osamp=%d\n", osamp);
962 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
964 rpos = (rpos + isamp) % hw->samples;
970 if (!(hw->ctl_caps & VOICE_VOLUME_CAP)) {
971 mixeng_volume (sw->buf, ret, &sw->vol);
974 sw->clip (buf, sw->buf, ret);
975 sw->total_hw_samples_acquired += total;
976 return ret << sw->info.shift;
980 * Hard voice (playback)
982 static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
988 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
989 if (sw->active || !sw->empty) {
990 m = audio_MIN (m, sw->total_hw_samples_mixed);
999 static int audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
1004 smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
1006 *nb_live = nb_live1;
1012 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1013 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1022 * Soft voice (playback)
1024 int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
1026 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
1027 int ret = 0, pos = 0, total = 0;
1033 hwsamples = sw->hw->samples;
1035 live = sw->total_hw_samples_mixed;
1036 if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
1037 dolog ("live=%d hw->samples=%d\n", live, hwsamples);
1041 if (live == hwsamples) {
1043 dolog ("%s is full %d\n", sw->name, live);
1048 wpos = (sw->hw->rpos + live) % hwsamples;
1049 samples = size >> sw->info.shift;
1051 dead = hwsamples - live;
1052 swlim = ((int64_t) dead << 32) / sw->ratio;
1053 swlim = audio_MIN (swlim, samples);
1055 sw->conv (sw->buf, buf, swlim);
1057 if (!(sw->hw->ctl_caps & VOICE_VOLUME_CAP)) {
1058 mixeng_volume (sw->buf, swlim, &sw->vol);
1063 dead = hwsamples - live;
1064 left = hwsamples - wpos;
1065 blck = audio_MIN (dead, left);
1074 sw->hw->mix_buf + wpos,
1082 wpos = (wpos + osamp) % hwsamples;
1086 sw->total_hw_samples_mixed += total;
1087 sw->empty = sw->total_hw_samples_mixed == 0;
1091 "%s: write size %d ret %d total sw %d\n",
1093 size >> sw->info.shift,
1095 sw->total_hw_samples_mixed
1099 return ret << sw->info.shift;
1103 static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
1105 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1106 cap, info->bits, info->sign, info->freq, info->nchannels);
1111 #include "audio_template.h"
1113 #include "audio_template.h"
1118 static int audio_is_timer_needed (void)
1120 HWVoiceIn *hwi = NULL;
1121 HWVoiceOut *hwo = NULL;
1123 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1124 if (!hwo->poll_mode) return 1;
1126 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1127 if (!hwi->poll_mode) return 1;
1132 static void audio_reset_timer (AudioState *s)
1134 if (audio_is_timer_needed ()) {
1135 qemu_mod_timer (s->ts, qemu_get_clock_ns (vm_clock) + 1);
1138 qemu_del_timer (s->ts);
1142 static void audio_timer (void *opaque)
1144 audio_run ("timer");
1145 audio_reset_timer (opaque);
1151 int AUD_write (SWVoiceOut *sw, void *buf, int size)
1156 /* XXX: Consider options */
1160 if (!sw->hw->enabled) {
1161 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
1165 bytes = sw->hw->pcm_ops->write (sw, buf, size);
1169 int AUD_read (SWVoiceIn *sw, void *buf, int size)
1174 /* XXX: Consider options */
1178 if (!sw->hw->enabled) {
1179 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1183 bytes = sw->hw->pcm_ops->read (sw, buf, size);
1187 int AUD_get_buffer_size_out (SWVoiceOut *sw)
1189 return sw->hw->samples << sw->hw->info.shift;
1192 void AUD_set_active_out (SWVoiceOut *sw, int on)
1201 if (sw->active != on) {
1202 AudioState *s = &glob_audio_state;
1203 SWVoiceOut *temp_sw;
1207 hw->pending_disable = 0;
1210 if (s->vm_running) {
1211 hw->pcm_ops->ctl_out (hw, VOICE_ENABLE, conf.try_poll_out);
1212 audio_reset_timer (s);
1220 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1221 temp_sw = temp_sw->entries.le_next) {
1222 nb_active += temp_sw->active != 0;
1225 hw->pending_disable = nb_active == 1;
1229 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1230 sc->sw.active = hw->enabled;
1232 audio_capture_maybe_changed (sc->cap, 1);
1239 void AUD_set_active_in (SWVoiceIn *sw, int on)
1248 if (sw->active != on) {
1249 AudioState *s = &glob_audio_state;
1255 if (s->vm_running) {
1256 hw->pcm_ops->ctl_in (hw, VOICE_ENABLE, conf.try_poll_in);
1257 audio_reset_timer (s);
1260 sw->total_hw_samples_acquired = hw->total_samples_captured;
1266 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1267 temp_sw = temp_sw->entries.le_next) {
1268 nb_active += temp_sw->active != 0;
1271 if (nb_active == 1) {
1273 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
1281 static int audio_get_avail (SWVoiceIn *sw)
1289 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1290 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1291 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1296 "%s: get_avail live %d ret %" PRId64 "\n",
1298 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1301 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1304 static int audio_get_free (SWVoiceOut *sw)
1312 live = sw->total_hw_samples_mixed;
1314 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1315 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1319 dead = sw->hw->samples - live;
1322 dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
1324 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1327 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1330 static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1337 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1338 SWVoiceOut *sw = &sc->sw;
1343 int till_end_of_hw = hw->samples - rpos2;
1344 int to_write = audio_MIN (till_end_of_hw, n);
1345 int bytes = to_write << hw->info.shift;
1348 sw->buf = hw->mix_buf + rpos2;
1349 written = audio_pcm_sw_write (sw, NULL, bytes);
1350 if (written - bytes) {
1351 dolog ("Could not mix %d bytes into a capture "
1352 "buffer, mixed %d\n",
1357 rpos2 = (rpos2 + to_write) % hw->samples;
1362 n = audio_MIN (samples, hw->samples - rpos);
1363 mixeng_clear (hw->mix_buf + rpos, n);
1364 mixeng_clear (hw->mix_buf, samples - n);
1367 static void audio_run_out (AudioState *s)
1369 HWVoiceOut *hw = NULL;
1372 while ((hw = audio_pcm_hw_find_any_enabled_out (hw))) {
1374 int live, free, nb_live, cleanup_required, prev_rpos;
1376 live = audio_pcm_hw_get_live_out (hw, &nb_live);
1381 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1382 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1386 if (hw->pending_disable && !nb_live) {
1389 dolog ("Disabling voice\n");
1392 hw->pending_disable = 0;
1393 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
1394 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1396 audio_recalc_and_notify_capture (sc->cap);
1402 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1404 free = audio_get_free (sw);
1406 sw->callback.fn (sw->callback.opaque, free);
1413 prev_rpos = hw->rpos;
1414 played = hw->pcm_ops->run_out (hw, live);
1415 if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1416 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1417 hw->rpos, hw->samples, played);
1422 dolog ("played=%d\n", played);
1426 hw->ts_helper += played;
1427 audio_capture_mix_and_clear (hw, prev_rpos, played);
1430 cleanup_required = 0;
1431 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1432 if (!sw->active && sw->empty) {
1436 if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1437 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1438 played, sw->total_hw_samples_mixed);
1439 played = sw->total_hw_samples_mixed;
1442 sw->total_hw_samples_mixed -= played;
1444 if (!sw->total_hw_samples_mixed) {
1446 cleanup_required |= !sw->active && !sw->callback.fn;
1450 free = audio_get_free (sw);
1452 sw->callback.fn (sw->callback.opaque, free);
1457 if (cleanup_required) {
1460 sw = hw->sw_head.lh_first;
1462 sw1 = sw->entries.le_next;
1463 if (!sw->active && !sw->callback.fn) {
1465 dolog ("Finishing with old voice\n");
1467 audio_close_out (sw);
1475 static void audio_run_in (AudioState *s)
1477 HWVoiceIn *hw = NULL;
1479 while ((hw = audio_pcm_hw_find_any_enabled_in (hw))) {
1483 captured = hw->pcm_ops->run_in (hw);
1485 min = audio_pcm_hw_find_min_in (hw);
1486 hw->total_samples_captured += captured - min;
1487 hw->ts_helper += captured;
1489 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1490 sw->total_hw_samples_acquired -= min;
1495 avail = audio_get_avail (sw);
1497 sw->callback.fn (sw->callback.opaque, avail);
1504 static void audio_run_capture (AudioState *s)
1506 CaptureVoiceOut *cap;
1508 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1509 int live, rpos, captured;
1510 HWVoiceOut *hw = &cap->hw;
1513 captured = live = audio_pcm_hw_get_live_out (hw, NULL);
1516 int left = hw->samples - rpos;
1517 int to_capture = audio_MIN (live, left);
1518 struct st_sample *src;
1519 struct capture_callback *cb;
1521 src = hw->mix_buf + rpos;
1522 hw->clip (cap->buf, src, to_capture);
1523 mixeng_clear (src, to_capture);
1525 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1526 cb->ops.capture (cb->opaque, cap->buf,
1527 to_capture << hw->info.shift);
1529 rpos = (rpos + to_capture) % hw->samples;
1534 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1535 if (!sw->active && sw->empty) {
1539 if (audio_bug (AUDIO_FUNC, captured > sw->total_hw_samples_mixed)) {
1540 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1541 captured, sw->total_hw_samples_mixed);
1542 captured = sw->total_hw_samples_mixed;
1545 sw->total_hw_samples_mixed -= captured;
1546 sw->empty = sw->total_hw_samples_mixed == 0;
1551 void audio_run (const char *msg)
1553 AudioState *s = &glob_audio_state;
1557 audio_run_capture (s);
1560 static double prevtime;
1564 if (gettimeofday (&tv, NULL)) {
1565 perror ("audio_run: gettimeofday");
1569 currtime = tv.tv_sec + tv.tv_usec * 1e-6;
1570 dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime);
1571 prevtime = currtime;
1576 static struct audio_option audio_options[] = {
1579 .name = "DAC_FIXED_SETTINGS",
1580 .tag = AUD_OPT_BOOL,
1581 .valp = &conf.fixed_out.enabled,
1582 .descr = "Use fixed settings for host DAC"
1585 .name = "DAC_FIXED_FREQ",
1587 .valp = &conf.fixed_out.settings.freq,
1588 .descr = "Frequency for fixed host DAC"
1591 .name = "DAC_FIXED_FMT",
1593 .valp = &conf.fixed_out.settings.fmt,
1594 .descr = "Format for fixed host DAC"
1597 .name = "DAC_FIXED_CHANNELS",
1599 .valp = &conf.fixed_out.settings.nchannels,
1600 .descr = "Number of channels for fixed DAC (1 - mono, 2 - stereo)"
1603 .name = "DAC_VOICES",
1605 .valp = &conf.fixed_out.nb_voices,
1606 .descr = "Number of voices for DAC"
1609 .name = "DAC_TRY_POLL",
1610 .tag = AUD_OPT_BOOL,
1611 .valp = &conf.try_poll_out,
1612 .descr = "Attempt using poll mode for DAC"
1616 .name = "ADC_FIXED_SETTINGS",
1617 .tag = AUD_OPT_BOOL,
1618 .valp = &conf.fixed_in.enabled,
1619 .descr = "Use fixed settings for host ADC"
1622 .name = "ADC_FIXED_FREQ",
1624 .valp = &conf.fixed_in.settings.freq,
1625 .descr = "Frequency for fixed host ADC"
1628 .name = "ADC_FIXED_FMT",
1630 .valp = &conf.fixed_in.settings.fmt,
1631 .descr = "Format for fixed host ADC"
1634 .name = "ADC_FIXED_CHANNELS",
1636 .valp = &conf.fixed_in.settings.nchannels,
1637 .descr = "Number of channels for fixed ADC (1 - mono, 2 - stereo)"
1640 .name = "ADC_VOICES",
1642 .valp = &conf.fixed_in.nb_voices,
1643 .descr = "Number of voices for ADC"
1646 .name = "ADC_TRY_POLL",
1647 .tag = AUD_OPT_BOOL,
1648 .valp = &conf.try_poll_in,
1649 .descr = "Attempt using poll mode for ADC"
1653 .name = "TIMER_PERIOD",
1655 .valp = &conf.period.hertz,
1656 .descr = "Timer period in HZ (0 - use lowest possible)"
1660 .tag = AUD_OPT_BOOL,
1661 .valp = &conf.plive,
1662 .descr = "(undocumented)"
1665 .name = "LOG_TO_MONITOR",
1666 .tag = AUD_OPT_BOOL,
1667 .valp = &conf.log_to_monitor,
1668 .descr = "Print logging messages to monitor instead of stderr"
1670 { /* End of list */ }
1673 static void audio_pp_nb_voices (const char *typ, int nb)
1677 printf ("Does not support %s\n", typ);
1680 printf ("One %s voice\n", typ);
1683 printf ("Theoretically supports many %s voices\n", typ);
1686 printf ("Theoretically supports up to %d %s voices\n", nb, typ);
1692 void AUD_help (void)
1696 audio_process_options ("AUDIO", audio_options);
1697 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1698 struct audio_driver *d = drvtab[i];
1700 audio_process_options (d->name, d->options);
1704 printf ("Audio options:\n");
1705 audio_print_options ("AUDIO", audio_options);
1708 printf ("Available drivers:\n");
1710 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1711 struct audio_driver *d = drvtab[i];
1713 printf ("Name: %s\n", d->name);
1714 printf ("Description: %s\n", d->descr);
1716 audio_pp_nb_voices ("playback", d->max_voices_out);
1717 audio_pp_nb_voices ("capture", d->max_voices_in);
1720 printf ("Options:\n");
1721 audio_print_options (d->name, d->options);
1724 printf ("No options\n");
1730 "Options are settable through environment variables.\n"
1733 " set QEMU_AUDIO_DRV=wav\n"
1734 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1736 " export QEMU_AUDIO_DRV=wav\n"
1737 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1738 "(for csh replace export with setenv in the above)\n"
1744 static int audio_driver_init (AudioState *s, struct audio_driver *drv)
1747 audio_process_options (drv->name, drv->options);
1749 s->drv_opaque = drv->init ();
1751 if (s->drv_opaque) {
1752 audio_init_nb_voices_out (drv);
1753 audio_init_nb_voices_in (drv);
1758 dolog ("Could not init `%s' audio driver\n", drv->name);
1763 static void audio_vm_change_state_handler (void *opaque, int running,
1766 AudioState *s = opaque;
1767 HWVoiceOut *hwo = NULL;
1768 HWVoiceIn *hwi = NULL;
1769 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1771 s->vm_running = running;
1772 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1773 hwo->pcm_ops->ctl_out (hwo, op, conf.try_poll_out);
1776 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1777 hwi->pcm_ops->ctl_in (hwi, op, conf.try_poll_in);
1779 audio_reset_timer (s);
1782 static void audio_atexit (void)
1784 AudioState *s = &glob_audio_state;
1785 HWVoiceOut *hwo = NULL;
1786 HWVoiceIn *hwi = NULL;
1788 while ((hwo = audio_pcm_hw_find_any_out (hwo))) {
1792 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1794 hwo->pcm_ops->fini_out (hwo);
1796 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1797 CaptureVoiceOut *cap = sc->cap;
1798 struct capture_callback *cb;
1800 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1801 cb->ops.destroy (cb->opaque);
1806 while ((hwi = audio_pcm_hw_find_any_in (hwi))) {
1808 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1810 hwi->pcm_ops->fini_in (hwi);
1814 s->drv->fini (s->drv_opaque);
1818 static const VMStateDescription vmstate_audio = {
1821 .minimum_version_id = 1,
1822 .minimum_version_id_old = 1,
1823 .fields = (VMStateField []) {
1824 VMSTATE_END_OF_LIST()
1828 static void audio_init (void)
1832 const char *drvname;
1833 VMChangeStateEntry *e;
1834 AudioState *s = &glob_audio_state;
1840 QLIST_INIT (&s->hw_head_out);
1841 QLIST_INIT (&s->hw_head_in);
1842 QLIST_INIT (&s->cap_head);
1843 atexit (audio_atexit);
1845 s->ts = qemu_new_timer_ns (vm_clock, audio_timer, s);
1847 hw_error("Could not create audio timer\n");
1850 audio_process_options ("AUDIO", audio_options);
1852 s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1853 s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1855 if (s->nb_hw_voices_out <= 0) {
1856 dolog ("Bogus number of playback voices %d, setting to 1\n",
1857 s->nb_hw_voices_out);
1858 s->nb_hw_voices_out = 1;
1861 if (s->nb_hw_voices_in <= 0) {
1862 dolog ("Bogus number of capture voices %d, setting to 0\n",
1863 s->nb_hw_voices_in);
1864 s->nb_hw_voices_in = 0;
1869 drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1875 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1876 if (!strcmp (drvname, drvtab[i]->name)) {
1877 done = !audio_driver_init (s, drvtab[i]);
1884 dolog ("Unknown audio driver `%s'\n", drvname);
1885 dolog ("Run with -audio-help to list available drivers\n");
1890 for (i = 0; !done && i < ARRAY_SIZE (drvtab); i++) {
1891 if (drvtab[i]->can_be_default) {
1892 done = !audio_driver_init (s, drvtab[i]);
1898 // Try to avoid certain wave out locking action in recent Windows...
1899 // If wave out is locked (because nothing is wired to output jack, ...),
1900 // QEMU can find voice out device, but open will failed. And it will cause guest audio lock-up.
1901 // So, we test whether opening is success or not.
1902 // It can not prevent lock-up caused by runtime voice out lock.
1903 // To prevent it, QEMU audio backend structure must be changed.
1904 // We should do it, but now we used simple fix temporarily.
1905 HWVoiceOut* hw = audio_calloc(AUDIO_FUNC, 1, s->drv->voice_size_out);
1907 dolog ("Can not allocate voice `%s' size %d\n",
1908 s->drv->name, s->drv->voice_size_out);
1910 if(s->drv->pcm_ops->init_out(hw, &conf.fixed_out.settings)) {
1911 INFO("Host audio out [%s] is malfunction. Change to noaudio driver\n",
1916 INFO("Host audio out [%s] is normal.\n", s->drv->name);
1917 s->drv->pcm_ops->fini_out(hw);
1923 done = !audio_driver_init (s, &no_audio_driver);
1925 hw_error("Could not initialize audio subsystem\n");
1928 dolog ("warning: Using timer based audio emulation\n");
1932 if (conf.period.hertz <= 0) {
1933 if (conf.period.hertz < 0) {
1934 dolog ("warning: Timer period is negative - %d "
1935 "treating as zero\n",
1938 conf.period.ticks = 1;
1941 muldiv64 (1, get_ticks_per_sec (), conf.period.hertz);
1944 e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1946 dolog ("warning: Could not register change state handler\n"
1947 "(Audio can continue looping even after stopping the VM)\n");
1950 QLIST_INIT (&s->card_head);
1951 vmstate_register (NULL, 0, &vmstate_audio, s);
1954 void AUD_register_card (const char *name, QEMUSoundCard *card)
1957 card->name = g_strdup (name);
1958 memset (&card->entries, 0, sizeof (card->entries));
1959 QLIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries);
1962 void AUD_remove_card (QEMUSoundCard *card)
1964 QLIST_REMOVE (card, entries);
1965 g_free (card->name);
1969 CaptureVoiceOut *AUD_add_capture (
1970 struct audsettings *as,
1971 struct audio_capture_ops *ops,
1975 AudioState *s = &glob_audio_state;
1976 CaptureVoiceOut *cap;
1977 struct capture_callback *cb;
1979 if (audio_validate_settings (as)) {
1980 dolog ("Invalid settings were passed when trying to add capture\n");
1981 audio_print_settings (as);
1985 cb = audio_calloc (AUDIO_FUNC, 1, sizeof (*cb));
1987 dolog ("Could not allocate capture callback information, size %zu\n",
1992 cb->opaque = cb_opaque;
1994 cap = audio_pcm_capture_find_specific (as);
1996 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
2001 CaptureVoiceOut *cap;
2003 cap = audio_calloc (AUDIO_FUNC, 1, sizeof (*cap));
2005 dolog ("Could not allocate capture voice, size %zu\n",
2011 QLIST_INIT (&hw->sw_head);
2012 QLIST_INIT (&cap->cb_head);
2014 /* XXX find a more elegant way */
2015 hw->samples = 4096 * 4;
2016 hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples,
2017 sizeof (struct st_sample));
2019 dolog ("Could not allocate capture mix buffer (%d samples)\n",
2024 audio_pcm_init_info (&hw->info, as);
2026 cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
2028 dolog ("Could not allocate capture buffer "
2029 "(%d samples, each %d bytes)\n",
2030 hw->samples, 1 << hw->info.shift);
2034 hw->clip = mixeng_clip
2035 [hw->info.nchannels == 2]
2037 [hw->info.swap_endianness]
2038 [audio_bits_to_index (hw->info.bits)];
2040 QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
2041 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
2044 while ((hw = audio_pcm_hw_find_any_out (hw))) {
2045 audio_attach_capture (hw);
2050 g_free (cap->hw.mix_buf);
2060 void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
2062 struct capture_callback *cb;
2064 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
2065 if (cb->opaque == cb_opaque) {
2066 cb->ops.destroy (cb_opaque);
2067 QLIST_REMOVE (cb, entries);
2070 if (!cap->cb_head.lh_first) {
2071 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
2074 SWVoiceCap *sc = (SWVoiceCap *) sw;
2075 #ifdef DEBUG_CAPTURE
2076 dolog ("freeing %s\n", sw->name);
2079 sw1 = sw->entries.le_next;
2081 st_rate_stop (sw->rate);
2084 QLIST_REMOVE (sw, entries);
2085 QLIST_REMOVE (sc, entries);
2089 QLIST_REMOVE (cap, entries);
2097 void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
2100 HWVoiceOut *hw = sw->hw;
2102 sw->vol.mute = mute;
2103 sw->vol.l = nominal_volume.l * lvol / 255;
2104 sw->vol.r = nominal_volume.r * rvol / 255;
2106 if (hw->pcm_ops->ctl_out) {
2107 hw->pcm_ops->ctl_out (hw, VOICE_VOLUME, sw);
2112 void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
2115 HWVoiceIn *hw = sw->hw;
2117 sw->vol.mute = mute;
2118 sw->vol.l = nominal_volume.l * lvol / 255;
2119 sw->vol.r = nominal_volume.r * rvol / 255;
2121 if (hw->pcm_ops->ctl_in) {
2122 hw->pcm_ops->ctl_in (hw, VOICE_VOLUME, sw);