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"
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[] = {
52 struct fixed_settings {
56 struct audsettings settings;
60 struct fixed_settings fixed_out;
61 struct fixed_settings fixed_in;
71 .fixed_out = { /* DAC fixed settings */
79 .endianness = AUDIO_HOST_ENDIANNESS,
83 .fixed_in = { /* ADC fixed settings */
91 .endianness = AUDIO_HOST_ENDIANNESS,
95 .period = { .hertz = 250 },
102 static AudioState glob_audio_state;
104 struct mixeng_volume nominal_volume = {
115 /* http://www.df.lth.se/~john_e/gems/gem002d.html */
116 /* http://www.multi-platforms.com/Tips/PopCount.htm */
117 uint32_t popcount (uint32_t u)
119 u = ((u&0x55555555) + ((u>>1)&0x55555555));
120 u = ((u&0x33333333) + ((u>>2)&0x33333333));
121 u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
122 u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
123 u = ( u&0x0000ffff) + (u>>16);
127 inline uint32_t lsbindex (uint32_t u)
129 return popcount ((u&-u)-1);
132 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
135 int audio_bug (const char *funcname, int cond)
140 AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
143 AUD_log (NULL, "Save all your work and restart without audio\n");
144 AUD_log (NULL, "Please send bug report to malc@pulsesoft.com\n");
145 AUD_log (NULL, "I am sorry\n");
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 qemu_mallocz (len);
207 static char *audio_alloc_prefix (const char *s)
209 const char qemu_prefix[] = "QEMU_";
218 r = qemu_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(cur_mon, "%s: ", cap);
344 monitor_vprintf(cur_mon, fmt, ap);
348 fprintf (stderr, "%s: ", cap);
351 vfprintf (stderr, fmt, ap);
355 void AUD_log (const char *cap, const char *fmt, ...)
360 AUD_vlog (cap, fmt, ap);
364 static void audio_print_options (const char *prefix,
365 struct audio_option *opt)
370 dolog ("No prefix specified\n");
375 dolog ("No options\n");
379 uprefix = audio_alloc_prefix (prefix);
381 for (; opt->name; opt++) {
382 const char *state = "default";
383 printf (" %s_%s: ", uprefix, opt->name);
385 if (opt->overriddenp && *opt->overriddenp) {
392 int *intp = opt->valp;
393 printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
399 int *intp = opt->valp;
400 printf ("integer, %s = %d\n", state, *intp);
406 audfmt_e *fmtp = opt->valp;
408 "format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
410 audio_audfmt_to_string (*fmtp)
417 const char **strp = opt->valp;
418 printf ("string, %s = %s\n",
420 *strp ? *strp : "(not set)");
426 dolog ("Bad value tag for option %s_%s %d\n",
427 uprefix, opt->name, opt->tag);
430 printf (" %s\n", opt->descr);
436 static void audio_process_options (const char *prefix,
437 struct audio_option *opt)
440 const char qemu_prefix[] = "QEMU_";
441 size_t preflen, optlen;
443 if (audio_bug (AUDIO_FUNC, !prefix)) {
444 dolog ("prefix = NULL\n");
448 if (audio_bug (AUDIO_FUNC, !opt)) {
449 dolog ("opt = NULL\n");
453 preflen = strlen (prefix);
455 for (; opt->name; opt++) {
460 dolog ("Option value pointer for `%s' is not set\n",
465 len = strlen (opt->name);
466 /* len of opt->name + len of prefix + size of qemu_prefix
467 * (includes trailing zero) + zero + underscore (on behalf of
469 optlen = len + preflen + sizeof (qemu_prefix) + 1;
470 optname = qemu_malloc (optlen);
472 pstrcpy (optname, optlen, qemu_prefix);
474 /* copy while upper-casing, including trailing zero */
475 for (i = 0; i <= preflen; ++i) {
476 optname[i + sizeof (qemu_prefix) - 1] = qemu_toupper(prefix[i]);
478 pstrcat (optname, optlen, "_");
479 pstrcat (optname, optlen, opt->name);
486 int *intp = opt->valp;
487 *intp = audio_get_conf_int (optname, *intp, &def);
493 audfmt_e *fmtp = opt->valp;
494 *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
500 const char **strp = opt->valp;
501 *strp = audio_get_conf_str (optname, *strp, &def);
506 dolog ("Bad value tag for option `%s' - %d\n",
511 if (!opt->overriddenp) {
512 opt->overriddenp = &opt->overridden;
514 *opt->overriddenp = !def;
519 static void audio_print_settings (struct audsettings *as)
521 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
525 AUD_log (NULL, "S8");
528 AUD_log (NULL, "U8");
531 AUD_log (NULL, "S16");
534 AUD_log (NULL, "U16");
537 AUD_log (NULL, "S32");
540 AUD_log (NULL, "U32");
543 AUD_log (NULL, "invalid(%d)", as->fmt);
547 AUD_log (NULL, " endianness=");
548 switch (as->endianness) {
550 AUD_log (NULL, "little");
553 AUD_log (NULL, "big");
556 AUD_log (NULL, "invalid");
559 AUD_log (NULL, "\n");
562 static int audio_validate_settings (struct audsettings *as)
566 invalid = as->nchannels != 1 && as->nchannels != 2;
567 invalid |= as->endianness != 0 && as->endianness != 1;
582 invalid |= as->freq <= 0;
583 return invalid ? -1 : 0;
586 static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
588 int bits = 8, sign = 0;
608 return info->freq == as->freq
609 && info->nchannels == as->nchannels
610 && info->sign == sign
611 && info->bits == bits
612 && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
615 void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
617 int bits = 8, sign = 0, shift = 0;
640 info->freq = as->freq;
643 info->nchannels = as->nchannels;
644 info->shift = (as->nchannels == 2) + shift;
645 info->align = (1 << info->shift) - 1;
646 info->bytes_per_second = info->freq << info->shift;
647 info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
650 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
657 memset (buf, 0x00, len << info->shift);
660 switch (info->bits) {
662 memset (buf, 0x80, len << info->shift);
669 int shift = info->nchannels - 1;
672 if (info->swap_endianness) {
676 for (i = 0; i < len << shift; i++) {
686 int shift = info->nchannels - 1;
687 int32_t s = INT32_MAX;
689 if (info->swap_endianness) {
693 for (i = 0; i < len << shift; i++) {
700 AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
710 static void noop_conv (struct st_sample *dst, const void *src,
711 int samples, struct mixeng_volume *vol)
719 static CaptureVoiceOut *audio_pcm_capture_find_specific (
720 struct audsettings *as
723 CaptureVoiceOut *cap;
724 AudioState *s = &glob_audio_state;
726 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
727 if (audio_pcm_info_eq (&cap->hw.info, as)) {
734 static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
736 struct capture_callback *cb;
739 dolog ("notification %d sent\n", cmd);
741 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
742 cb->ops.notify (cb->opaque, cmd);
746 static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
748 if (cap->hw.enabled != enabled) {
749 audcnotification_e cmd;
750 cap->hw.enabled = enabled;
751 cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
752 audio_notify_capture (cap, cmd);
756 static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
758 HWVoiceOut *hw = &cap->hw;
762 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
768 audio_capture_maybe_changed (cap, enabled);
771 static void audio_detach_capture (HWVoiceOut *hw)
773 SWVoiceCap *sc = hw->cap_head.lh_first;
776 SWVoiceCap *sc1 = sc->entries.le_next;
777 SWVoiceOut *sw = &sc->sw;
778 CaptureVoiceOut *cap = sc->cap;
779 int was_active = sw->active;
782 st_rate_stop (sw->rate);
786 LIST_REMOVE (sw, entries);
787 LIST_REMOVE (sc, entries);
790 /* We have removed soft voice from the capture:
791 this might have changed the overall status of the capture
792 since this might have been the only active voice */
793 audio_recalc_and_notify_capture (cap);
799 static int audio_attach_capture (HWVoiceOut *hw)
801 AudioState *s = &glob_audio_state;
802 CaptureVoiceOut *cap;
804 audio_detach_capture (hw);
805 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
808 HWVoiceOut *hw_cap = &cap->hw;
810 sc = audio_calloc (AUDIO_FUNC, 1, sizeof (*sc));
812 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
822 sw->active = hw->enabled;
823 sw->conv = noop_conv;
824 sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
825 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
827 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
831 LIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
832 LIST_INSERT_HEAD (&hw->cap_head, sc, entries);
834 asprintf (&sw->name, "for %p %d,%d,%d",
835 hw, sw->info.freq, sw->info.bits, sw->info.nchannels);
836 dolog ("Added %s active = %d\n", sw->name, sw->active);
839 audio_capture_maybe_changed (cap, 1);
846 * Hard voice (capture)
848 static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
851 int m = hw->total_samples_captured;
853 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
855 m = audio_MIN (m, sw->total_hw_samples_acquired);
861 int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
863 int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
864 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
865 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
872 * Soft voice (capture)
874 static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
876 HWVoiceIn *hw = sw->hw;
877 int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
880 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
881 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
885 rpos = hw->wpos - live;
890 return hw->samples + rpos;
894 int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
896 HWVoiceIn *hw = sw->hw;
897 int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
898 struct st_sample *src, *dst = sw->buf;
900 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
902 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
903 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
904 dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
908 samples = size >> sw->info.shift;
913 swlim = (live * sw->ratio) >> 32;
914 swlim = audio_MIN (swlim, samples);
917 src = hw->conv_buf + rpos;
918 isamp = hw->wpos - rpos;
921 isamp = hw->samples - rpos;
929 if (audio_bug (AUDIO_FUNC, osamp < 0)) {
930 dolog ("osamp=%d\n", osamp);
934 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
936 rpos = (rpos + isamp) % hw->samples;
942 sw->clip (buf, sw->buf, ret);
943 sw->total_hw_samples_acquired += total;
944 return ret << sw->info.shift;
948 * Hard voice (playback)
950 static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
956 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
957 if (sw->active || !sw->empty) {
958 m = audio_MIN (m, sw->total_hw_samples_mixed);
967 int audio_pcm_hw_get_live_out2 (HWVoiceOut *hw, int *nb_live)
971 smin = audio_pcm_hw_find_min_out (hw, nb_live);
979 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
980 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
987 int audio_pcm_hw_get_live_out (HWVoiceOut *hw)
992 live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
993 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
994 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1001 * Soft voice (playback)
1003 int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
1005 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
1006 int ret = 0, pos = 0, total = 0;
1012 hwsamples = sw->hw->samples;
1014 live = sw->total_hw_samples_mixed;
1015 if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
1016 dolog ("live=%d hw->samples=%d\n", live, hwsamples);
1020 if (live == hwsamples) {
1022 dolog ("%s is full %d\n", sw->name, live);
1027 wpos = (sw->hw->rpos + live) % hwsamples;
1028 samples = size >> sw->info.shift;
1030 dead = hwsamples - live;
1031 swlim = ((int64_t) dead << 32) / sw->ratio;
1032 swlim = audio_MIN (swlim, samples);
1034 sw->conv (sw->buf, buf, swlim, &sw->vol);
1038 dead = hwsamples - live;
1039 left = hwsamples - wpos;
1040 blck = audio_MIN (dead, left);
1049 sw->hw->mix_buf + wpos,
1057 wpos = (wpos + osamp) % hwsamples;
1061 sw->total_hw_samples_mixed += total;
1062 sw->empty = sw->total_hw_samples_mixed == 0;
1066 "%s: write size %d ret %d total sw %d\n",
1068 size >> sw->info.shift,
1070 sw->total_hw_samples_mixed
1074 return ret << sw->info.shift;
1078 static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
1080 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1081 cap, info->bits, info->sign, info->freq, info->nchannels);
1086 #include "audio_template.h"
1088 #include "audio_template.h"
1093 static void audio_timer (void *opaque)
1095 AudioState *s = opaque;
1097 audio_run ("timer");
1098 qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);
1102 static int audio_is_timer_needed (void)
1104 HWVoiceIn *hwi = NULL;
1105 HWVoiceOut *hwo = NULL;
1107 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1108 if (!hwo->poll_mode) return 1;
1110 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1111 if (!hwi->poll_mode) return 1;
1116 static void audio_reset_timer (void)
1118 AudioState *s = &glob_audio_state;
1120 if (audio_is_timer_needed ()) {
1121 qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + 1);
1124 qemu_del_timer (s->ts);
1131 int AUD_write (SWVoiceOut *sw, void *buf, int size)
1136 /* XXX: Consider options */
1140 if (!sw->hw->enabled) {
1141 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
1145 bytes = sw->hw->pcm_ops->write (sw, buf, size);
1149 int AUD_read (SWVoiceIn *sw, void *buf, int size)
1154 /* XXX: Consider options */
1158 if (!sw->hw->enabled) {
1159 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1163 bytes = sw->hw->pcm_ops->read (sw, buf, size);
1167 int AUD_get_buffer_size_out (SWVoiceOut *sw)
1169 return sw->hw->samples << sw->hw->info.shift;
1172 void AUD_set_active_out (SWVoiceOut *sw, int on)
1181 if (sw->active != on) {
1182 AudioState *s = &glob_audio_state;
1183 SWVoiceOut *temp_sw;
1187 hw->pending_disable = 0;
1190 if (s->vm_running) {
1191 hw->pcm_ops->ctl_out (hw, VOICE_ENABLE, conf.try_poll_out);
1192 audio_reset_timer ();
1200 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1201 temp_sw = temp_sw->entries.le_next) {
1202 nb_active += temp_sw->active != 0;
1205 hw->pending_disable = nb_active == 1;
1209 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1210 sc->sw.active = hw->enabled;
1212 audio_capture_maybe_changed (sc->cap, 1);
1219 void AUD_set_active_in (SWVoiceIn *sw, int on)
1228 if (sw->active != on) {
1229 AudioState *s = &glob_audio_state;
1235 if (s->vm_running) {
1236 hw->pcm_ops->ctl_in (hw, VOICE_ENABLE, conf.try_poll_in);
1239 sw->total_hw_samples_acquired = hw->total_samples_captured;
1245 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1246 temp_sw = temp_sw->entries.le_next) {
1247 nb_active += temp_sw->active != 0;
1250 if (nb_active == 1) {
1252 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
1260 static int audio_get_avail (SWVoiceIn *sw)
1268 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1269 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1270 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1275 "%s: get_avail live %d ret %" PRId64 "\n",
1277 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1280 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1283 static int audio_get_free (SWVoiceOut *sw)
1291 live = sw->total_hw_samples_mixed;
1293 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1294 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1298 dead = sw->hw->samples - live;
1301 dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
1303 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1306 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1309 static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1316 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1317 SWVoiceOut *sw = &sc->sw;
1322 int till_end_of_hw = hw->samples - rpos2;
1323 int to_write = audio_MIN (till_end_of_hw, n);
1324 int bytes = to_write << hw->info.shift;
1327 sw->buf = hw->mix_buf + rpos2;
1328 written = audio_pcm_sw_write (sw, NULL, bytes);
1329 if (written - bytes) {
1330 dolog ("Could not mix %d bytes into a capture "
1331 "buffer, mixed %d\n",
1336 rpos2 = (rpos2 + to_write) % hw->samples;
1341 n = audio_MIN (samples, hw->samples - rpos);
1342 mixeng_clear (hw->mix_buf + rpos, n);
1343 mixeng_clear (hw->mix_buf, samples - n);
1346 static void audio_run_out (AudioState *s)
1348 HWVoiceOut *hw = NULL;
1351 while ((hw = audio_pcm_hw_find_any_enabled_out (hw))) {
1353 int live, free, nb_live, cleanup_required, prev_rpos;
1355 live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
1360 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1361 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1365 if (hw->pending_disable && !nb_live) {
1368 dolog ("Disabling voice\n");
1371 hw->pending_disable = 0;
1372 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
1373 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1375 audio_recalc_and_notify_capture (sc->cap);
1381 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1383 free = audio_get_free (sw);
1385 sw->callback.fn (sw->callback.opaque, free);
1392 prev_rpos = hw->rpos;
1393 played = hw->pcm_ops->run_out (hw);
1394 if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1395 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1396 hw->rpos, hw->samples, played);
1401 dolog ("played=%d\n", played);
1405 hw->ts_helper += played;
1406 audio_capture_mix_and_clear (hw, prev_rpos, played);
1409 cleanup_required = 0;
1410 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1411 if (!sw->active && sw->empty) {
1415 if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1416 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1417 played, sw->total_hw_samples_mixed);
1418 played = sw->total_hw_samples_mixed;
1421 sw->total_hw_samples_mixed -= played;
1423 if (!sw->total_hw_samples_mixed) {
1425 cleanup_required |= !sw->active && !sw->callback.fn;
1429 free = audio_get_free (sw);
1431 sw->callback.fn (sw->callback.opaque, free);
1436 if (cleanup_required) {
1439 sw = hw->sw_head.lh_first;
1441 sw1 = sw->entries.le_next;
1442 if (!sw->active && !sw->callback.fn) {
1444 dolog ("Finishing with old voice\n");
1446 audio_close_out (sw);
1454 static void audio_run_in (AudioState *s)
1456 HWVoiceIn *hw = NULL;
1458 while ((hw = audio_pcm_hw_find_any_enabled_in (hw))) {
1462 captured = hw->pcm_ops->run_in (hw);
1464 min = audio_pcm_hw_find_min_in (hw);
1465 hw->total_samples_captured += captured - min;
1466 hw->ts_helper += captured;
1468 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1469 sw->total_hw_samples_acquired -= min;
1474 avail = audio_get_avail (sw);
1476 sw->callback.fn (sw->callback.opaque, avail);
1483 static void audio_run_capture (AudioState *s)
1485 CaptureVoiceOut *cap;
1487 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1488 int live, rpos, captured;
1489 HWVoiceOut *hw = &cap->hw;
1492 captured = live = audio_pcm_hw_get_live_out (hw);
1495 int left = hw->samples - rpos;
1496 int to_capture = audio_MIN (live, left);
1497 struct st_sample *src;
1498 struct capture_callback *cb;
1500 src = hw->mix_buf + rpos;
1501 hw->clip (cap->buf, src, to_capture);
1502 mixeng_clear (src, to_capture);
1504 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1505 cb->ops.capture (cb->opaque, cap->buf,
1506 to_capture << hw->info.shift);
1508 rpos = (rpos + to_capture) % hw->samples;
1513 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1514 if (!sw->active && sw->empty) {
1518 if (audio_bug (AUDIO_FUNC, captured > sw->total_hw_samples_mixed)) {
1519 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1520 captured, sw->total_hw_samples_mixed);
1521 captured = sw->total_hw_samples_mixed;
1524 sw->total_hw_samples_mixed -= captured;
1525 sw->empty = sw->total_hw_samples_mixed == 0;
1530 void audio_run (const char *msg)
1532 AudioState *s = &glob_audio_state;
1536 audio_run_capture (s);
1539 static double prevtime;
1543 if (gettimeofday (&tv, NULL)) {
1544 perror ("audio_run: gettimeofday");
1548 currtime = tv.tv_sec + tv.tv_usec * 1e-6;
1549 dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime);
1550 prevtime = currtime;
1555 static struct audio_option audio_options[] = {
1558 .name = "DAC_FIXED_SETTINGS",
1559 .tag = AUD_OPT_BOOL,
1560 .valp = &conf.fixed_out.enabled,
1561 .descr = "Use fixed settings for host DAC"
1564 .name = "DAC_FIXED_FREQ",
1566 .valp = &conf.fixed_out.settings.freq,
1567 .descr = "Frequency for fixed host DAC"
1570 .name = "DAC_FIXED_FMT",
1572 .valp = &conf.fixed_out.settings.fmt,
1573 .descr = "Format for fixed host DAC"
1576 .name = "DAC_FIXED_CHANNELS",
1578 .valp = &conf.fixed_out.settings.nchannels,
1579 .descr = "Number of channels for fixed DAC (1 - mono, 2 - stereo)"
1582 .name = "DAC_VOICES",
1584 .valp = &conf.fixed_out.nb_voices,
1585 .descr = "Number of voices for DAC"
1588 .name = "DAC_TRY_POLL",
1589 .tag = AUD_OPT_BOOL,
1590 .valp = &conf.try_poll_out,
1591 .descr = "Attempt using poll mode for DAC"
1595 .name = "ADC_FIXED_SETTINGS",
1596 .tag = AUD_OPT_BOOL,
1597 .valp = &conf.fixed_in.enabled,
1598 .descr = "Use fixed settings for host ADC"
1601 .name = "ADC_FIXED_FREQ",
1603 .valp = &conf.fixed_in.settings.freq,
1604 .descr = "Frequency for fixed host ADC"
1607 .name = "ADC_FIXED_FMT",
1609 .valp = &conf.fixed_in.settings.fmt,
1610 .descr = "Format for fixed host ADC"
1613 .name = "ADC_FIXED_CHANNELS",
1615 .valp = &conf.fixed_in.settings.nchannels,
1616 .descr = "Number of channels for fixed ADC (1 - mono, 2 - stereo)"
1619 .name = "ADC_VOICES",
1621 .valp = &conf.fixed_in.nb_voices,
1622 .descr = "Number of voices for ADC"
1625 .name = "ADC_TRY_POLL",
1626 .tag = AUD_OPT_BOOL,
1627 .valp = &conf.try_poll_out,
1628 .descr = "Attempt using poll mode for ADC"
1632 .name = "TIMER_PERIOD",
1634 .valp = &conf.period.hertz,
1635 .descr = "Timer period in HZ (0 - use lowest possible)"
1639 .tag = AUD_OPT_BOOL,
1640 .valp = &conf.plive,
1641 .descr = "(undocumented)"
1644 .name = "LOG_TO_MONITOR",
1645 .tag = AUD_OPT_BOOL,
1646 .valp = &conf.log_to_monitor,
1647 .descr = "Print logging messages to monitor instead of stderr"
1649 { /* End of list */ }
1652 static void audio_pp_nb_voices (const char *typ, int nb)
1656 printf ("Does not support %s\n", typ);
1659 printf ("One %s voice\n", typ);
1662 printf ("Theoretically supports many %s voices\n", typ);
1665 printf ("Theoretically supports upto %d %s voices\n", nb, typ);
1671 void AUD_help (void)
1675 audio_process_options ("AUDIO", audio_options);
1676 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1677 struct audio_driver *d = drvtab[i];
1679 audio_process_options (d->name, d->options);
1683 printf ("Audio options:\n");
1684 audio_print_options ("AUDIO", audio_options);
1687 printf ("Available drivers:\n");
1689 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1690 struct audio_driver *d = drvtab[i];
1692 printf ("Name: %s\n", d->name);
1693 printf ("Description: %s\n", d->descr);
1695 audio_pp_nb_voices ("playback", d->max_voices_out);
1696 audio_pp_nb_voices ("capture", d->max_voices_in);
1699 printf ("Options:\n");
1700 audio_print_options (d->name, d->options);
1703 printf ("No options\n");
1709 "Options are settable through environment variables.\n"
1712 " set QEMU_AUDIO_DRV=wav\n"
1713 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1715 " export QEMU_AUDIO_DRV=wav\n"
1716 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1717 "(for csh replace export with setenv in the above)\n"
1723 static int audio_driver_init (AudioState *s, struct audio_driver *drv)
1726 audio_process_options (drv->name, drv->options);
1728 s->drv_opaque = drv->init ();
1730 if (s->drv_opaque) {
1731 audio_init_nb_voices_out (drv);
1732 audio_init_nb_voices_in (drv);
1737 dolog ("Could not init `%s' audio driver\n", drv->name);
1742 static void audio_vm_change_state_handler (void *opaque, int running,
1745 AudioState *s = opaque;
1746 HWVoiceOut *hwo = NULL;
1747 HWVoiceIn *hwi = NULL;
1748 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1750 s->vm_running = running;
1751 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1752 hwo->pcm_ops->ctl_out (hwo, op, conf.try_poll_out);
1755 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1756 hwi->pcm_ops->ctl_in (hwi, op, conf.try_poll_in);
1758 audio_reset_timer ();
1761 static void audio_atexit (void)
1763 AudioState *s = &glob_audio_state;
1764 HWVoiceOut *hwo = NULL;
1765 HWVoiceIn *hwi = NULL;
1767 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1770 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1771 hwo->pcm_ops->fini_out (hwo);
1773 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1774 CaptureVoiceOut *cap = sc->cap;
1775 struct capture_callback *cb;
1777 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1778 cb->ops.destroy (cb->opaque);
1783 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1784 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1785 hwi->pcm_ops->fini_in (hwi);
1789 s->drv->fini (s->drv_opaque);
1793 static void audio_save (QEMUFile *f, void *opaque)
1799 static int audio_load (QEMUFile *f, void *opaque, int version_id)
1804 if (version_id != 1) {
1811 static void audio_init (void)
1815 const char *drvname;
1816 VMChangeStateEntry *e;
1817 AudioState *s = &glob_audio_state;
1823 LIST_INIT (&s->hw_head_out);
1824 LIST_INIT (&s->hw_head_in);
1825 LIST_INIT (&s->cap_head);
1826 atexit (audio_atexit);
1828 s->ts = qemu_new_timer (vm_clock, audio_timer, s);
1830 hw_error("Could not create audio timer\n");
1833 audio_process_options ("AUDIO", audio_options);
1835 s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1836 s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1838 if (s->nb_hw_voices_out <= 0) {
1839 dolog ("Bogus number of playback voices %d, setting to 1\n",
1840 s->nb_hw_voices_out);
1841 s->nb_hw_voices_out = 1;
1844 if (s->nb_hw_voices_in <= 0) {
1845 dolog ("Bogus number of capture voices %d, setting to 0\n",
1846 s->nb_hw_voices_in);
1847 s->nb_hw_voices_in = 0;
1852 drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1858 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1859 if (!strcmp (drvname, drvtab[i]->name)) {
1860 done = !audio_driver_init (s, drvtab[i]);
1867 dolog ("Unknown audio driver `%s'\n", drvname);
1868 dolog ("Run with -audio-help to list available drivers\n");
1873 for (i = 0; !done && i < ARRAY_SIZE (drvtab); i++) {
1874 if (drvtab[i]->can_be_default) {
1875 done = !audio_driver_init (s, drvtab[i]);
1881 done = !audio_driver_init (s, &no_audio_driver);
1883 hw_error("Could not initialize audio subsystem\n");
1886 dolog ("warning: Using timer based audio emulation\n");
1890 if (conf.period.hertz <= 0) {
1891 if (conf.period.hertz < 0) {
1892 dolog ("warning: Timer period is negative - %d "
1893 "treating as zero\n",
1896 conf.period.ticks = 1;
1898 conf.period.ticks = get_ticks_per_sec() / conf.period.hertz;
1901 e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1903 dolog ("warning: Could not register change state handler\n"
1904 "(Audio can continue looping even after stopping the VM)\n");
1907 LIST_INIT (&s->card_head);
1908 register_savevm ("audio", 0, 1, audio_save, audio_load, s);
1911 void AUD_register_card (const char *name, QEMUSoundCard *card)
1914 card->name = qemu_strdup (name);
1915 memset (&card->entries, 0, sizeof (card->entries));
1916 LIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries);
1919 void AUD_remove_card (QEMUSoundCard *card)
1921 LIST_REMOVE (card, entries);
1922 qemu_free (card->name);
1926 CaptureVoiceOut *AUD_add_capture (
1927 struct audsettings *as,
1928 struct audio_capture_ops *ops,
1932 AudioState *s = &glob_audio_state;
1933 CaptureVoiceOut *cap;
1934 struct capture_callback *cb;
1936 if (audio_validate_settings (as)) {
1937 dolog ("Invalid settings were passed when trying to add capture\n");
1938 audio_print_settings (as);
1942 cb = audio_calloc (AUDIO_FUNC, 1, sizeof (*cb));
1944 dolog ("Could not allocate capture callback information, size %zu\n",
1949 cb->opaque = cb_opaque;
1951 cap = audio_pcm_capture_find_specific (as);
1953 LIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1958 CaptureVoiceOut *cap;
1960 cap = audio_calloc (AUDIO_FUNC, 1, sizeof (*cap));
1962 dolog ("Could not allocate capture voice, size %zu\n",
1968 LIST_INIT (&hw->sw_head);
1969 LIST_INIT (&cap->cb_head);
1971 /* XXX find a more elegant way */
1972 hw->samples = 4096 * 4;
1973 hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples,
1974 sizeof (struct st_sample));
1976 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1981 audio_pcm_init_info (&hw->info, as);
1983 cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
1985 dolog ("Could not allocate capture buffer "
1986 "(%d samples, each %d bytes)\n",
1987 hw->samples, 1 << hw->info.shift);
1991 hw->clip = mixeng_clip
1992 [hw->info.nchannels == 2]
1994 [hw->info.swap_endianness]
1995 [audio_bits_to_index (hw->info.bits)];
1997 LIST_INSERT_HEAD (&s->cap_head, cap, entries);
1998 LIST_INSERT_HEAD (&cap->cb_head, cb, entries);
2001 while ((hw = audio_pcm_hw_find_any_out (hw))) {
2002 audio_attach_capture (hw);
2007 qemu_free (cap->hw.mix_buf);
2017 void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
2019 struct capture_callback *cb;
2021 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
2022 if (cb->opaque == cb_opaque) {
2023 cb->ops.destroy (cb_opaque);
2024 LIST_REMOVE (cb, entries);
2027 if (!cap->cb_head.lh_first) {
2028 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
2031 SWVoiceCap *sc = (SWVoiceCap *) sw;
2032 #ifdef DEBUG_CAPTURE
2033 dolog ("freeing %s\n", sw->name);
2036 sw1 = sw->entries.le_next;
2038 st_rate_stop (sw->rate);
2041 LIST_REMOVE (sw, entries);
2042 LIST_REMOVE (sc, entries);
2046 LIST_REMOVE (cap, entries);
2054 void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
2057 sw->vol.mute = mute;
2058 sw->vol.l = nominal_volume.l * lvol / 255;
2059 sw->vol.r = nominal_volume.r * rvol / 255;
2063 void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
2066 sw->vol.mute = mute;
2067 sw->vol.l = nominal_volume.l * lvol / 255;
2068 sw->vol.r = nominal_volume.r * rvol / 255;