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 #define AUDIO_CAP "audio"
27 #include "audio_int.h"
29 /* #define DEBUG_PLIVE */
30 /* #define DEBUG_LIVE */
31 /* #define DEBUG_OUT */
33 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
35 static struct audio_driver *drvtab[] = {
42 #ifdef CONFIG_COREAUDIO
43 &coreaudio_audio_driver,
58 struct fixed_settings {
62 audsettings_t settings;
66 struct fixed_settings fixed_out;
67 struct fixed_settings fixed_in;
75 { /* DAC fixed settings */
86 { /* ADC fixed settings */
102 static AudioState glob_audio_state;
104 volume_t 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, "Error a bug that was just triggered in %s\n", funcname);
143 AUD_log (NULL, "Save all your work and restart without audio\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 void *audio_calloc (const char *funcname, int nmemb, size_t size)
174 cond = !nmemb || !size;
178 if (audio_bug ("audio_calloc", cond)) {
179 AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
181 AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
185 return qemu_mallocz (len);
188 static char *audio_alloc_prefix (const char *s)
190 const char qemu_prefix[] = "QEMU_";
199 r = qemu_malloc (len + sizeof (qemu_prefix));
203 char *u = r + sizeof (qemu_prefix) - 1;
205 strcpy (r, qemu_prefix);
208 for (i = 0; i < len; ++i) {
209 u[i] = toupper (u[i]);
215 const char *audio_audfmt_to_string (audfmt_e fmt)
231 dolog ("Bogus audfmt %d returning S16\n", fmt);
235 audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval, int *defaultp)
237 if (!strcasecmp (s, "u8")) {
241 else if (!strcasecmp (s, "u16")) {
245 else if (!strcasecmp (s, "s8")) {
249 else if (!strcasecmp (s, "s16")) {
254 dolog ("Bogus audio format `%s' using %s\n",
255 s, audio_audfmt_to_string (defval));
261 static audfmt_e audio_get_conf_fmt (const char *envname,
265 const char *var = getenv (envname);
270 return audio_string_to_audfmt (var, defval, defaultp);
273 static int audio_get_conf_int (const char *key, int defval, int *defaultp)
278 strval = getenv (key);
290 static const char *audio_get_conf_str (const char *key,
294 const char *val = getenv (key);
305 void AUD_vlog (const char *cap, const char *fmt, va_list ap)
307 if (conf.log_to_monitor) {
309 term_printf ("%s: ", cap);
312 term_vprintf (fmt, ap);
316 fprintf (stderr, "%s: ", cap);
319 vfprintf (stderr, fmt, ap);
323 void AUD_log (const char *cap, const char *fmt, ...)
328 AUD_vlog (cap, fmt, ap);
332 static void audio_print_options (const char *prefix,
333 struct audio_option *opt)
338 dolog ("No prefix specified\n");
343 dolog ("No options\n");
347 uprefix = audio_alloc_prefix (prefix);
349 for (; opt->name; opt++) {
350 const char *state = "default";
351 printf (" %s_%s: ", uprefix, opt->name);
353 if (opt->overridenp && *opt->overridenp) {
360 int *intp = opt->valp;
361 printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
367 int *intp = opt->valp;
368 printf ("integer, %s = %d\n", state, *intp);
374 audfmt_e *fmtp = opt->valp;
376 "format, %s = %s, (one of: U8 S8 U16 S16)\n",
378 audio_audfmt_to_string (*fmtp)
385 const char **strp = opt->valp;
386 printf ("string, %s = %s\n",
388 *strp ? *strp : "(not set)");
394 dolog ("Bad value tag for option %s_%s %d\n",
395 uprefix, opt->name, opt->tag);
398 printf (" %s\n", opt->descr);
404 static void audio_process_options (const char *prefix,
405 struct audio_option *opt)
408 const char qemu_prefix[] = "QEMU_";
411 if (audio_bug (AUDIO_FUNC, !prefix)) {
412 dolog ("prefix = NULL\n");
416 if (audio_bug (AUDIO_FUNC, !opt)) {
417 dolog ("opt = NULL\n");
421 preflen = strlen (prefix);
423 for (; opt->name; opt++) {
428 dolog ("Option value pointer for `%s' is not set\n",
433 len = strlen (opt->name);
434 /* len of opt->name + len of prefix + size of qemu_prefix
435 * (includes trailing zero) + zero + underscore (on behalf of
437 optname = qemu_malloc (len + preflen + sizeof (qemu_prefix) + 1);
439 dolog ("Could not allocate memory for option name `%s'\n",
444 strcpy (optname, qemu_prefix);
446 /* copy while upper-casing, including trailing zero */
447 for (i = 0; i <= preflen; ++i) {
448 optname[i + sizeof (qemu_prefix) - 1] = toupper (prefix[i]);
450 strcat (optname, "_");
451 strcat (optname, opt->name);
458 int *intp = opt->valp;
459 *intp = audio_get_conf_int (optname, *intp, &def);
465 audfmt_e *fmtp = opt->valp;
466 *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
472 const char **strp = opt->valp;
473 *strp = audio_get_conf_str (optname, *strp, &def);
478 dolog ("Bad value tag for option `%s' - %d\n",
483 if (!opt->overridenp) {
484 opt->overridenp = &opt->overriden;
486 *opt->overridenp = !def;
491 static void audio_print_settings (audsettings_t *as)
493 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
497 AUD_log (NULL, "S8");
500 AUD_log (NULL, "U8");
503 AUD_log (NULL, "S16");
506 AUD_log (NULL, "U16");
509 AUD_log (NULL, "invalid(%d)", as->fmt);
512 AUD_log (NULL, "\n");
515 static int audio_validate_settigs (audsettings_t *as)
519 invalid = as->nchannels != 1 && as->nchannels != 2;
532 invalid |= as->freq <= 0;
540 static int audio_pcm_info_eq (struct audio_pcm_info *info, audsettings_t *as)
542 int bits = 8, sign = 0;
556 return info->freq == as->freq
557 && info->nchannels == as->nchannels
558 && info->sign == sign
559 && info->bits == bits;
562 void audio_pcm_init_info (
563 struct audio_pcm_info *info,
568 int bits = 8, sign = 0;
583 info->freq = as->freq;
586 info->nchannels = as->nchannels;
587 info->shift = (as->nchannels == 2) + (bits == 16);
588 info->align = (1 << info->shift) - 1;
589 info->bytes_per_second = info->freq << info->shift;
590 info->swap_endian = swap_endian;
593 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
600 memset (buf, len << info->shift, 0x00);
603 if (info->bits == 8) {
604 memset (buf, len << info->shift, 0x80);
609 int shift = info->nchannels - 1;
612 if (info->swap_endian) {
616 for (i = 0; i < len << shift; i++) {
624 * Hard voice (capture)
626 static void audio_pcm_hw_free_resources_in (HWVoiceIn *hw)
629 qemu_free (hw->conv_buf);
634 static int audio_pcm_hw_alloc_resources_in (HWVoiceIn *hw)
636 hw->conv_buf = audio_calloc (AUDIO_FUNC, hw->samples, sizeof (st_sample_t));
638 dolog ("Could not allocate ADC conversion buffer (%d samples)\n",
645 static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
648 int m = hw->total_samples_captured;
650 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
652 m = audio_MIN (m, sw->total_hw_samples_acquired);
658 int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
660 int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
661 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
662 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
669 * Soft voice (capture)
671 static void audio_pcm_sw_free_resources_in (SWVoiceIn *sw)
674 qemu_free (sw->conv_buf);
678 st_rate_stop (sw->rate);
685 static int audio_pcm_sw_alloc_resources_in (SWVoiceIn *sw)
687 int samples = ((int64_t) sw->hw->samples << 32) / sw->ratio;
688 sw->conv_buf = audio_calloc (AUDIO_FUNC, samples, sizeof (st_sample_t));
690 dolog ("Could not allocate buffer for `%s' (%d samples)\n",
691 SW_NAME (sw), samples);
695 sw->rate = st_rate_start (sw->hw->info.freq, sw->info.freq);
697 qemu_free (sw->conv_buf);
704 static int audio_pcm_sw_init_in (
711 /* None of the cards emulated by QEMU are big-endian
712 hence following shortcut */
713 audio_pcm_init_info (&sw->info, as, audio_need_to_swap_endian (0));
715 sw->ratio = ((int64_t) sw->info.freq << 32) / sw->hw->info.freq;
719 [sw->info.nchannels == 2]
721 [sw->info.swap_endian]
722 [sw->info.bits == 16];
724 sw->name = qemu_strdup (name);
725 audio_pcm_sw_free_resources_in (sw);
726 return audio_pcm_sw_alloc_resources_in (sw);
729 static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
731 HWVoiceIn *hw = sw->hw;
732 int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
735 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
736 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
740 rpos = hw->wpos - live;
745 return hw->samples + rpos;
749 int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
751 HWVoiceIn *hw = sw->hw;
752 int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
753 st_sample_t *src, *dst = sw->conv_buf;
755 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
757 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
758 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
759 dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
763 samples = size >> sw->info.shift;
768 swlim = (live * sw->ratio) >> 32;
769 swlim = audio_MIN (swlim, samples);
772 src = hw->conv_buf + rpos;
773 isamp = hw->wpos - rpos;
776 isamp = hw->samples - rpos;
784 if (audio_bug (AUDIO_FUNC, osamp < 0)) {
785 dolog ("osamp=%d\n", osamp);
789 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
791 rpos = (rpos + isamp) % hw->samples;
797 sw->clip (buf, sw->conv_buf, ret);
798 sw->total_hw_samples_acquired += total;
799 return ret << sw->info.shift;
803 * Hard voice (playback)
805 static void audio_pcm_hw_free_resources_out (HWVoiceOut *hw)
808 qemu_free (hw->mix_buf);
814 static int audio_pcm_hw_alloc_resources_out (HWVoiceOut *hw)
816 hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples, sizeof (st_sample_t));
818 dolog ("Could not allocate DAC mixing buffer (%d samples)\n",
826 static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
832 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
833 if (sw->active || !sw->empty) {
834 m = audio_MIN (m, sw->total_hw_samples_mixed);
843 int audio_pcm_hw_get_live_out2 (HWVoiceOut *hw, int *nb_live)
847 smin = audio_pcm_hw_find_min_out (hw, nb_live);
855 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
856 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
863 int audio_pcm_hw_get_live_out (HWVoiceOut *hw)
868 live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
869 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
870 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
877 * Soft voice (playback)
879 static void audio_pcm_sw_free_resources_out (SWVoiceOut *sw)
886 st_rate_stop (sw->rate);
893 static int audio_pcm_sw_alloc_resources_out (SWVoiceOut *sw)
895 sw->buf = audio_calloc (AUDIO_FUNC, sw->hw->samples, sizeof (st_sample_t));
897 dolog ("Could not allocate buffer for `%s' (%d samples)\n",
898 SW_NAME (sw), sw->hw->samples);
902 sw->rate = st_rate_start (sw->info.freq, sw->hw->info.freq);
911 static int audio_pcm_sw_init_out (
918 /* None of the cards emulated by QEMU are big-endian
919 hence following shortcut */
920 audio_pcm_init_info (&sw->info, as, audio_need_to_swap_endian (0));
924 sw->ratio = ((int64_t) sw->hw->info.freq << 32) / sw->info.freq;
925 sw->total_hw_samples_mixed = 0;
929 [sw->info.nchannels == 2]
931 [sw->info.swap_endian]
932 [sw->info.bits == 16];
933 sw->name = qemu_strdup (name);
935 audio_pcm_sw_free_resources_out (sw);
936 return audio_pcm_sw_alloc_resources_out (sw);
939 int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
941 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
942 int ret = 0, pos = 0, total = 0;
948 hwsamples = sw->hw->samples;
950 live = sw->total_hw_samples_mixed;
951 if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
952 dolog ("live=%d hw->samples=%d\n", live, hwsamples);
956 if (live == hwsamples) {
960 wpos = (sw->hw->rpos + live) % hwsamples;
961 samples = size >> sw->info.shift;
963 dead = hwsamples - live;
964 swlim = ((int64_t) dead << 32) / sw->ratio;
965 swlim = audio_MIN (swlim, samples);
967 sw->conv (sw->buf, buf, swlim, &sw->vol);
971 dead = hwsamples - live;
972 left = hwsamples - wpos;
973 blck = audio_MIN (dead, left);
982 sw->hw->mix_buf + wpos,
990 wpos = (wpos + osamp) % hwsamples;
994 sw->total_hw_samples_mixed += total;
995 sw->empty = sw->total_hw_samples_mixed == 0;
999 "%s: write size %d ret %d total sw %d\n",
1001 size >> sw->info.shift,
1003 sw->total_hw_samples_mixed
1007 return ret << sw->info.shift;
1011 static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
1013 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1014 cap, info->bits, info->sign, info->freq, info->nchannels);
1019 #include "audio_template.h"
1021 #include "audio_template.h"
1023 int AUD_write (SWVoiceOut *sw, void *buf, int size)
1028 /* XXX: Consider options */
1032 if (!sw->hw->enabled) {
1033 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
1037 bytes = sw->hw->pcm_ops->write (sw, buf, size);
1041 int AUD_read (SWVoiceIn *sw, void *buf, int size)
1046 /* XXX: Consider options */
1050 if (!sw->hw->enabled) {
1051 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1055 bytes = sw->hw->pcm_ops->read (sw, buf, size);
1059 int AUD_get_buffer_size_out (SWVoiceOut *sw)
1061 return sw->hw->samples << sw->hw->info.shift;
1064 void AUD_set_active_out (SWVoiceOut *sw, int on)
1073 if (sw->active != on) {
1074 SWVoiceOut *temp_sw;
1079 hw->pending_disable = 0;
1082 hw->pcm_ops->ctl_out (hw, VOICE_ENABLE);
1093 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1094 temp_sw = temp_sw->entries.le_next) {
1095 nb_active += temp_sw->active != 0;
1098 hw->pending_disable = nb_active == 1;
1105 void AUD_set_active_in (SWVoiceIn *sw, int on)
1114 if (sw->active != on) {
1120 hw->pcm_ops->ctl_in (hw, VOICE_ENABLE);
1122 sw->total_hw_samples_acquired = hw->total_samples_captured;
1128 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1129 temp_sw = temp_sw->entries.le_next) {
1130 nb_active += temp_sw->active != 0;
1133 if (nb_active == 1) {
1135 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
1143 static int audio_get_avail (SWVoiceIn *sw)
1151 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1152 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1153 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1158 "%s: get_avail live %d ret %lld\n",
1160 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1163 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1166 static int audio_get_free (SWVoiceOut *sw)
1174 live = sw->total_hw_samples_mixed;
1176 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1177 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1181 dead = sw->hw->samples - live;
1184 dolog ("%s: get_free live %d dead %d ret %lld\n",
1186 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1189 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1192 static void audio_run_out (AudioState *s)
1194 HWVoiceOut *hw = NULL;
1197 while ((hw = audio_pcm_hw_find_any_enabled_out (s, hw))) {
1199 int live, free, nb_live, cleanup_required;
1201 live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
1206 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1207 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1211 if (hw->pending_disable && !nb_live) {
1213 dolog ("Disabling voice\n");
1216 hw->pending_disable = 0;
1217 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
1222 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1224 free = audio_get_free (sw);
1226 sw->callback.fn (sw->callback.opaque, free);
1233 played = hw->pcm_ops->run_out (hw);
1234 if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1235 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1236 hw->rpos, hw->samples, played);
1241 dolog ("played=%d\n", played);
1245 hw->ts_helper += played;
1248 cleanup_required = 0;
1249 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1250 if (!sw->active && sw->empty) {
1254 if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1255 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1256 played, sw->total_hw_samples_mixed);
1257 played = sw->total_hw_samples_mixed;
1260 sw->total_hw_samples_mixed -= played;
1262 if (!sw->total_hw_samples_mixed) {
1264 cleanup_required |= !sw->active && !sw->callback.fn;
1268 free = audio_get_free (sw);
1270 sw->callback.fn (sw->callback.opaque, free);
1275 if (cleanup_required) {
1277 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1278 if (!sw->active && !sw->callback.fn) {
1280 dolog ("Finishing with old voice\n");
1282 audio_close_out (s, sw);
1283 goto restart; /* play it safe */
1290 static void audio_run_in (AudioState *s)
1292 HWVoiceIn *hw = NULL;
1294 while ((hw = audio_pcm_hw_find_any_enabled_in (s, hw))) {
1298 captured = hw->pcm_ops->run_in (hw);
1300 min = audio_pcm_hw_find_min_in (hw);
1301 hw->total_samples_captured += captured - min;
1302 hw->ts_helper += captured;
1304 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1305 sw->total_hw_samples_acquired -= min;
1310 avail = audio_get_avail (sw);
1312 sw->callback.fn (sw->callback.opaque, avail);
1319 static struct audio_option audio_options[] = {
1321 {"DAC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_out.enabled,
1322 "Use fixed settings for host DAC", NULL, 0},
1324 {"DAC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_out.settings.freq,
1325 "Frequency for fixed host DAC", NULL, 0},
1327 {"DAC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_out.settings.fmt,
1328 "Format for fixed host DAC", NULL, 0},
1330 {"DAC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_out.settings.nchannels,
1331 "Number of channels for fixed DAC (1 - mono, 2 - stereo)", NULL, 0},
1333 {"DAC_VOICES", AUD_OPT_INT, &conf.fixed_out.nb_voices,
1334 "Number of voices for DAC", NULL, 0},
1337 {"ADC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_in.enabled,
1338 "Use fixed settings for host ADC", NULL, 0},
1340 {"ADC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_in.settings.freq,
1341 "Frequency for fixed host ADC", NULL, 0},
1343 {"ADC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_in.settings.fmt,
1344 "Format for fixed host ADC", NULL, 0},
1346 {"ADC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_in.settings.nchannels,
1347 "Number of channels for fixed ADC (1 - mono, 2 - stereo)", NULL, 0},
1349 {"ADC_VOICES", AUD_OPT_INT, &conf.fixed_in.nb_voices,
1350 "Number of voices for ADC", NULL, 0},
1353 {"TIMER_PERIOD", AUD_OPT_INT, &conf.period.hz,
1354 "Timer period in HZ (0 - use lowest possible)", NULL, 0},
1356 {"PLIVE", AUD_OPT_BOOL, &conf.plive,
1357 "(undocumented)", NULL, 0},
1360 {"LOG_TO_MONITOR", AUD_OPT_BOOL, &conf.log_to_monitor,
1361 "print logging messages to montior instead of stderr", NULL, 0},
1363 {NULL, 0, NULL, NULL, NULL, 0}
1366 void AUD_help (void)
1370 audio_process_options ("AUDIO", audio_options);
1371 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1372 struct audio_driver *d = drvtab[i];
1374 audio_process_options (d->name, d->options);
1378 printf ("Audio options:\n");
1379 audio_print_options ("AUDIO", audio_options);
1382 printf ("Available drivers:\n");
1384 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1385 struct audio_driver *d = drvtab[i];
1387 printf ("Name: %s\n", d->name);
1388 printf ("Description: %s\n", d->descr);
1390 switch (d->max_voices_out) {
1392 printf ("Does not support DAC\n");
1395 printf ("One DAC voice\n");
1398 printf ("Theoretically supports many DAC voices\n");
1401 printf ("Theoretically supports upto %d DAC voices\n",
1406 switch (d->max_voices_in) {
1408 printf ("Does not support ADC\n");
1411 printf ("One ADC voice\n");
1414 printf ("Theoretically supports many ADC voices\n");
1417 printf ("Theoretically supports upto %d ADC voices\n",
1423 printf ("Options:\n");
1424 audio_print_options (d->name, d->options);
1427 printf ("No options\n");
1433 "Options are settable through environment variables.\n"
1436 " set QEMU_AUDIO_DRV=wav\n"
1437 " set QEMU_WAV_PATH=c:/tune.wav\n"
1439 " export QEMU_AUDIO_DRV=wav\n"
1440 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1441 "(for csh replace export with setenv in the above)\n"
1447 void audio_timer (void *opaque)
1449 AudioState *s = opaque;
1454 qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);
1457 static int audio_driver_init (AudioState *s, struct audio_driver *drv)
1460 audio_process_options (drv->name, drv->options);
1462 s->drv_opaque = drv->init ();
1464 if (s->drv_opaque) {
1465 if (s->nb_hw_voices_out > drv->max_voices_out) {
1466 if (!drv->max_voices_out) {
1467 dolog ("`%s' does not support DAC\n", drv->name);
1471 "`%s' does not support %d multiple DAC voicess\n"
1472 "Resetting to %d\n",
1474 s->nb_hw_voices_out,
1478 s->nb_hw_voices_out = drv->max_voices_out;
1482 if (!drv->voice_size_in && drv->max_voices_in) {
1483 ldebug ("warning: No ADC voice size defined for `%s'\n",
1485 drv->max_voices_in = 0;
1488 if (!drv->voice_size_out && drv->max_voices_out) {
1489 ldebug ("warning: No DAC voice size defined for `%s'\n",
1493 if (drv->voice_size_in && !drv->max_voices_in) {
1494 ldebug ("warning: `%s' ADC voice size %d, zero voices \n",
1495 drv->name, drv->voice_size_out);
1498 if (drv->voice_size_out && !drv->max_voices_out) {
1499 ldebug ("warning: `%s' DAC voice size %d, zero voices \n",
1500 drv->name, drv->voice_size_in);
1503 if (s->nb_hw_voices_in > drv->max_voices_in) {
1504 if (!drv->max_voices_in) {
1505 ldebug ("`%s' does not support ADC\n", drv->name);
1509 "`%s' does not support %d multiple ADC voices\n"
1510 "Resetting to %d\n",
1516 s->nb_hw_voices_in = drv->max_voices_in;
1519 LIST_INIT (&s->hw_head_out);
1520 LIST_INIT (&s->hw_head_in);
1525 dolog ("Could not init `%s' audio driver\n", drv->name);
1530 static void audio_vm_change_state_handler (void *opaque, int running)
1532 AudioState *s = opaque;
1533 HWVoiceOut *hwo = NULL;
1534 HWVoiceIn *hwi = NULL;
1535 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1537 while ((hwo = audio_pcm_hw_find_any_enabled_out (s, hwo))) {
1538 hwo->pcm_ops->ctl_out (hwo, op);
1541 while ((hwi = audio_pcm_hw_find_any_enabled_in (s, hwi))) {
1542 hwi->pcm_ops->ctl_in (hwi, op);
1546 static void audio_atexit (void)
1548 AudioState *s = &glob_audio_state;
1549 HWVoiceOut *hwo = NULL;
1550 HWVoiceIn *hwi = NULL;
1552 while ((hwo = audio_pcm_hw_find_any_out (s, hwo))) {
1553 if (!hwo->pcm_ops) {
1558 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1560 hwo->pcm_ops->fini_out (hwo);
1563 while ((hwi = audio_pcm_hw_find_any_in (s, hwi))) {
1564 if (!hwi->pcm_ops) {
1569 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1571 hwi->pcm_ops->fini_in (hwi);
1575 s->drv->fini (s->drv_opaque);
1579 static void audio_save (QEMUFile *f, void *opaque)
1585 static int audio_load (QEMUFile *f, void *opaque, int version_id)
1590 if (version_id != 1) {
1597 void AUD_register_card (AudioState *s, const char *name, QEMUSoundCard *card)
1600 card->name = qemu_strdup (name);
1601 memset (&card->entries, 0, sizeof (card->entries));
1602 LIST_INSERT_HEAD (&s->card_head, card, entries);
1605 void AUD_remove_card (QEMUSoundCard *card)
1607 LIST_REMOVE (card, entries);
1609 qemu_free (card->name);
1612 AudioState *AUD_init (void)
1616 const char *drvname;
1617 AudioState *s = &glob_audio_state;
1619 audio_process_options ("AUDIO", audio_options);
1621 s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1622 s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1624 if (s->nb_hw_voices_out <= 0) {
1625 dolog ("Bogus number of DAC voices %d\n",
1626 s->nb_hw_voices_out);
1627 s->nb_hw_voices_out = 1;
1630 if (s->nb_hw_voices_in <= 0) {
1631 dolog ("Bogus number of ADC voices %d\n",
1632 s->nb_hw_voices_in);
1633 s->nb_hw_voices_in = 1;
1638 drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1641 s->ts = qemu_new_timer (vm_clock, audio_timer, s);
1643 dolog ("Could not create audio timer\n");
1650 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1651 if (!strcmp (drvname, drvtab[i]->name)) {
1652 done = !audio_driver_init (s, drvtab[i]);
1659 dolog ("Unknown audio driver `%s'\n", drvname);
1660 dolog ("Run with -audio-help to list available drivers\n");
1665 for (i = 0; !done && i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1666 if (drvtab[i]->can_be_default) {
1667 done = !audio_driver_init (s, drvtab[i]);
1673 done = !audio_driver_init (s, &no_audio_driver);
1675 dolog ("Could not initialize audio subsystem\n");
1678 dolog ("warning: Using timer based audio emulation\n");
1683 if (conf.period.hz <= 0) {
1684 if (conf.period.hz < 0) {
1685 dolog ("warning: Timer period is negative - %d "
1686 "treating as zero\n",
1689 conf.period.ticks = 1;
1692 conf.period.ticks = ticks_per_sec / conf.period.hz;
1695 qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1698 qemu_del_timer (s->ts);
1702 LIST_INIT (&s->card_head);
1703 register_savevm ("audio", 0, 1, audio_save, audio_load, s);
1704 atexit (audio_atexit);
1705 qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);