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 static void audio_pcm_hw_fini_in (HWVoiceIn *hw);
30 static void audio_pcm_hw_fini_out (HWVoiceOut *hw);
32 static LIST_HEAD (hw_in_listhead, HWVoiceIn) hw_head_in;
33 static LIST_HEAD (hw_out_listhead, HWVoiceOut) hw_head_out;
35 /* #define DEBUG_PLIVE */
36 /* #define DEBUG_LIVE */
37 /* #define DEBUG_OUT */
39 static struct audio_driver *drvtab[] = {
46 #ifdef CONFIG_COREAUDIO
47 &coreaudio_audio_driver,
62 AudioState audio_state = {
64 1, /* use fixed settings */
65 44100, /* fixed frequency */
66 2, /* fixed channels */
67 AUD_FMT_S16, /* fixed format */
68 1, /* number of hw voices */
72 1, /* use fixed settings */
73 44100, /* fixed frequency */
74 2, /* fixed channels */
75 AUD_FMT_S16, /* fixed format */
76 1, /* number of hw voices */
79 NULL, /* driver opaque */
82 NULL, /* timer handle */
87 volume_t nominal_volume = {
98 /* http://www.df.lth.se/~john_e/gems/gem002d.html */
99 /* http://www.multi-platforms.com/Tips/PopCount.htm */
100 uint32_t popcount (uint32_t u)
102 u = ((u&0x55555555) + ((u>>1)&0x55555555));
103 u = ((u&0x33333333) + ((u>>2)&0x33333333));
104 u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
105 u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
106 u = ( u&0x0000ffff) + (u>>16);
110 inline uint32_t lsbindex (uint32_t u)
112 return popcount ((u&-u)-1);
115 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
118 int audio_bug (const char *funcname, int cond)
123 AUD_log (NULL, "Error a bug that was just triggered in %s\n", funcname);
126 AUD_log (NULL, "Save all your work and restart without audio\n");
127 AUD_log (NULL, "Please send bug report to malc@pulsesoft.com\n");
128 AUD_log (NULL, "I am sorry\n");
130 AUD_log (NULL, "Context:\n");
132 #if defined AUDIO_BREAKPOINT_ON_BUG
133 # if defined HOST_I386
134 # if defined __GNUC__
136 # elif defined _MSC_VER
151 static char *audio_alloc_prefix (const char *s)
153 const char qemu_prefix[] = "QEMU_";
162 r = qemu_malloc (len + sizeof (qemu_prefix));
166 char *u = r + sizeof (qemu_prefix) - 1;
168 strcpy (r, qemu_prefix);
171 for (i = 0; i < len; ++i) {
172 u[i] = toupper (u[i]);
178 const char *audio_audfmt_to_string (audfmt_e fmt)
194 dolog ("Bogus audfmt %d returning S16\n", fmt);
198 audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval, int *defaultp)
200 if (!strcasecmp (s, "u8")) {
204 else if (!strcasecmp (s, "u16")) {
208 else if (!strcasecmp (s, "s8")) {
212 else if (!strcasecmp (s, "s16")) {
217 dolog ("Bogus audio format `%s' using %s\n",
218 s, audio_audfmt_to_string (defval));
224 static audfmt_e audio_get_conf_fmt (const char *envname,
228 const char *var = getenv (envname);
233 return audio_string_to_audfmt (var, defval, defaultp);
236 static int audio_get_conf_int (const char *key, int defval, int *defaultp)
241 strval = getenv (key);
253 static const char *audio_get_conf_str (const char *key,
257 const char *val = getenv (key);
268 void AUD_log (const char *cap, const char *fmt, ...)
272 fprintf (stderr, "%s: ", cap);
275 vfprintf (stderr, fmt, ap);
279 void AUD_vlog (const char *cap, const char *fmt, va_list ap)
282 fprintf (stderr, "%s: ", cap);
284 vfprintf (stderr, fmt, ap);
287 static void audio_print_options (const char *prefix,
288 struct audio_option *opt)
293 dolog ("No prefix specified\n");
298 dolog ("No options\n");
302 uprefix = audio_alloc_prefix (prefix);
304 for (; opt->name; opt++) {
305 const char *state = "default";
306 printf (" %s_%s: ", uprefix, opt->name);
308 if (opt->overridenp && *opt->overridenp) {
315 int *intp = opt->valp;
316 printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
322 int *intp = opt->valp;
323 printf ("integer, %s = %d\n", state, *intp);
329 audfmt_e *fmtp = opt->valp;
331 "format, %s = %s, (one of: U8 S8 U16 S16)\n",
333 audio_audfmt_to_string (*fmtp)
340 const char **strp = opt->valp;
341 printf ("string, %s = %s\n",
343 *strp ? *strp : "(not set)");
349 dolog ("Bad value tag for option %s_%s %d\n",
350 uprefix, opt->name, opt->tag);
353 printf (" %s\n", opt->descr);
359 static void audio_process_options (const char *prefix,
360 struct audio_option *opt)
363 const char qemu_prefix[] = "QEMU_";
366 if (audio_bug (AUDIO_FUNC, !prefix)) {
367 dolog ("prefix = NULL\n");
371 if (audio_bug (AUDIO_FUNC, !opt)) {
372 dolog ("opt = NULL\n");
376 preflen = strlen (prefix);
378 for (; opt->name; opt++) {
383 dolog ("Option value pointer for `%s' is not set\n",
388 len = strlen (opt->name);
389 optname = qemu_malloc (len + preflen + sizeof (qemu_prefix) + 1);
391 dolog ("Can not allocate memory for option name `%s'\n",
396 strcpy (optname, qemu_prefix);
397 for (i = 0; i <= preflen; ++i) {
398 optname[i + sizeof (qemu_prefix) - 1] = toupper (prefix[i]);
400 strcat (optname, "_");
401 strcat (optname, opt->name);
408 int *intp = opt->valp;
409 *intp = audio_get_conf_int (optname, *intp, &def);
415 audfmt_e *fmtp = opt->valp;
416 *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
422 const char **strp = opt->valp;
423 *strp = audio_get_conf_str (optname, *strp, &def);
428 dolog ("Bad value tag for option `%s' - %d\n",
433 if (!opt->overridenp) {
434 opt->overridenp = &opt->overriden;
436 *opt->overridenp = !def;
441 static int audio_pcm_info_eq (struct audio_pcm_info *info, int freq,
442 int nchannels, audfmt_e fmt)
444 int bits = 8, sign = 0;
458 return info->freq == freq
459 && info->nchannels == nchannels
460 && info->sign == sign
461 && info->bits == bits;
464 void audio_pcm_init_info (struct audio_pcm_info *info, int freq,
465 int nchannels, audfmt_e fmt, int swap_endian)
467 int bits = 8, sign = 0;
485 info->nchannels = nchannels;
486 info->shift = (nchannels == 2) + (bits == 16);
487 info->align = (1 << info->shift) - 1;
488 info->bytes_per_second = info->freq << info->shift;
489 info->swap_endian = swap_endian;
492 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
499 memset (buf, len << info->shift, 0x00);
502 if (info->bits == 8) {
503 memset (buf, len << info->shift, 0x80);
508 int shift = info->nchannels - 1;
511 if (info->swap_endian) {
515 for (i = 0; i < len << shift; i++) {
523 * Hard voice (capture)
525 static void audio_pcm_hw_free_resources_in (HWVoiceIn *hw)
528 qemu_free (hw->conv_buf);
533 static int audio_pcm_hw_alloc_resources_in (HWVoiceIn *hw)
535 hw->conv_buf = qemu_mallocz (hw->samples * sizeof (st_sample_t));
542 static int audio_pcm_hw_init_in (HWVoiceIn *hw, int freq, int nchannels, audfmt_e fmt)
544 audio_pcm_hw_fini_in (hw);
546 if (hw->pcm_ops->init_in (hw, freq, nchannels, fmt)) {
547 memset (hw, 0, audio_state.drv->voice_size_in);
550 LIST_INIT (&hw->sw_head);
552 hw->samples = hw->bufsize >> hw->info.shift;
557 [hw->info.swap_endian]
558 [hw->info.bits == 16];
559 if (audio_pcm_hw_alloc_resources_in (hw)) {
560 audio_pcm_hw_free_resources_in (hw);
566 static uint64_t audio_pcm_hw_find_min_in (HWVoiceIn *hw)
569 int m = hw->total_samples_captured;
571 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
573 m = audio_MIN (m, sw->total_hw_samples_acquired);
579 int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
581 int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
582 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
583 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
590 * Soft voice (capture)
592 static void audio_pcm_sw_free_resources_in (SWVoiceIn *sw)
595 qemu_free (sw->conv_buf);
599 st_rate_stop (sw->rate);
606 static int audio_pcm_sw_alloc_resources_in (SWVoiceIn *sw)
608 int samples = ((int64_t) sw->hw->samples << 32) / sw->ratio;
609 sw->conv_buf = qemu_mallocz (samples * sizeof (st_sample_t));
614 sw->rate = st_rate_start (sw->hw->info.freq, sw->info.freq);
616 qemu_free (sw->conv_buf);
623 static int audio_pcm_sw_init_in (SWVoiceIn *sw, HWVoiceIn *hw, const char *name,
624 int freq, int nchannels, audfmt_e fmt)
626 audio_pcm_init_info (&sw->info, freq, nchannels, fmt,
627 /* None of the cards emulated by QEMU are big-endian
628 hence following shortcut */
629 audio_need_to_swap_endian (0));
631 sw->ratio = ((int64_t) sw->info.freq << 32) / sw->hw->info.freq;
637 [sw->info.swap_endian]
638 [sw->info.bits == 16];
640 sw->name = qemu_strdup (name);
641 audio_pcm_sw_free_resources_in (sw);
642 return audio_pcm_sw_alloc_resources_in (sw);
645 static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
647 HWVoiceIn *hw = sw->hw;
648 int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
651 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
652 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
656 rpos = hw->wpos - live;
661 return hw->samples + rpos;
665 int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
667 HWVoiceIn *hw = sw->hw;
668 int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
669 st_sample_t *src, *dst = sw->conv_buf;
671 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
673 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
674 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
675 dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
679 samples = size >> sw->info.shift;
684 swlim = (live * sw->ratio) >> 32;
685 swlim = audio_MIN (swlim, samples);
688 src = hw->conv_buf + rpos;
689 isamp = hw->wpos - rpos;
692 isamp = hw->samples - rpos;
700 if (audio_bug (AUDIO_FUNC, osamp < 0)) {
701 dolog ("osamp=%d\n", osamp);
704 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
706 rpos = (rpos + isamp) % hw->samples;
712 sw->clip (buf, sw->conv_buf, ret);
713 sw->total_hw_samples_acquired += total;
714 return ret << sw->info.shift;
718 * Hard voice (playback)
720 static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
726 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
727 if (sw->active || !sw->empty) {
728 m = audio_MIN (m, sw->total_hw_samples_mixed);
737 static void audio_pcm_hw_free_resources_out (HWVoiceOut *hw)
740 qemu_free (hw->mix_buf);
746 static int audio_pcm_hw_alloc_resources_out (HWVoiceOut *hw)
748 hw->mix_buf = qemu_mallocz (hw->samples * sizeof (st_sample_t));
756 static int audio_pcm_hw_init_out (HWVoiceOut *hw, int freq,
757 int nchannels, audfmt_e fmt)
759 audio_pcm_hw_fini_out (hw);
760 if (hw->pcm_ops->init_out (hw, freq, nchannels, fmt)) {
761 memset (hw, 0, audio_state.drv->voice_size_out);
765 LIST_INIT (&hw->sw_head);
767 hw->samples = hw->bufsize >> hw->info.shift;
772 [hw->info.swap_endian]
773 [hw->info.bits == 16];
774 if (audio_pcm_hw_alloc_resources_out (hw)) {
775 audio_pcm_hw_fini_out (hw);
781 int audio_pcm_hw_get_live_out2 (HWVoiceOut *hw, int *nb_live)
785 smin = audio_pcm_hw_find_min_out (hw, nb_live);
793 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
794 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
801 int audio_pcm_hw_get_live_out (HWVoiceOut *hw)
806 live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
807 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
808 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
815 * Soft voice (playback)
817 static void audio_pcm_sw_free_resources_out (SWVoiceOut *sw)
824 st_rate_stop (sw->rate);
831 static int audio_pcm_sw_alloc_resources_out (SWVoiceOut *sw)
833 sw->buf = qemu_mallocz (sw->hw->samples * sizeof (st_sample_t));
838 sw->rate = st_rate_start (sw->info.freq, sw->hw->info.freq);
847 static int audio_pcm_sw_init_out (SWVoiceOut *sw, HWVoiceOut *hw,
848 const char *name, int freq,
849 int nchannels, audfmt_e fmt)
851 audio_pcm_init_info (&sw->info, freq, nchannels, fmt,
852 /* None of the cards emulated by QEMU are big-endian
853 hence following shortcut */
854 audio_need_to_swap_endian (0));
858 sw->ratio = ((int64_t) sw->hw->info.freq << 32) / sw->info.freq;
859 sw->total_hw_samples_mixed = 0;
865 [sw->info.swap_endian]
866 [sw->info.bits == 16];
867 sw->name = qemu_strdup (name);
869 audio_pcm_sw_free_resources_out (sw);
870 return audio_pcm_sw_alloc_resources_out (sw);
873 int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
875 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
876 int ret = 0, pos = 0, total = 0;
882 hwsamples = sw->hw->samples;
884 live = sw->total_hw_samples_mixed;
885 if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
886 dolog ("live=%d hw->samples=%d\n", live, hwsamples);
890 if (live == hwsamples) {
894 wpos = (sw->hw->rpos + live) % hwsamples;
895 samples = size >> sw->info.shift;
897 dead = hwsamples - live;
898 swlim = ((int64_t) dead << 32) / sw->ratio;
899 swlim = audio_MIN (swlim, samples);
901 sw->conv (sw->buf, buf, swlim, &sw->vol);
905 dead = hwsamples - live;
906 left = hwsamples - wpos;
907 blck = audio_MIN (dead, left);
916 sw->hw->mix_buf + wpos,
924 wpos = (wpos + osamp) % hwsamples;
928 sw->total_hw_samples_mixed += total;
929 sw->empty = sw->total_hw_samples_mixed == 0;
933 "%s: write size %d ret %d total sw %d, hw %d\n",
935 size >> sw->info.shift,
937 sw->total_hw_samples_mixed,
938 sw->hw->total_samples_played
942 return ret << sw->info.shift;
946 static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
948 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
949 cap, info->bits, info->sign, info->freq, info->nchannels);
954 #include "audio_template.h"
956 #include "audio_template.h"
958 int AUD_write (SWVoiceOut *sw, void *buf, int size)
963 /* XXX: Consider options */
967 if (!sw->hw->enabled) {
968 dolog ("Writing to disabled voice %s\n", sw->name);
972 bytes = sw->hw->pcm_ops->write (sw, buf, size);
976 int AUD_read (SWVoiceIn *sw, void *buf, int size)
981 /* XXX: Consider options */
985 if (!sw->hw->enabled) {
986 dolog ("Reading from disabled voice %s\n", sw->name);
990 bytes = sw->hw->pcm_ops->read (sw, buf, size);
994 int AUD_get_buffer_size_out (SWVoiceOut *sw)
996 return sw->hw->bufsize;
999 void AUD_set_active_out (SWVoiceOut *sw, int on)
1008 if (sw->active != on) {
1009 SWVoiceOut *temp_sw;
1014 hw->pending_disable = 0;
1017 hw->pcm_ops->ctl_out (hw, VOICE_ENABLE);
1028 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1029 temp_sw = temp_sw->entries.le_next) {
1030 nb_active += temp_sw->active != 0;
1033 hw->pending_disable = nb_active == 1;
1040 void AUD_set_active_in (SWVoiceIn *sw, int on)
1049 if (sw->active != on) {
1055 hw->pcm_ops->ctl_in (hw, VOICE_ENABLE);
1057 sw->total_hw_samples_acquired = hw->total_samples_captured;
1063 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1064 temp_sw = temp_sw->entries.le_next) {
1065 nb_active += temp_sw->active != 0;
1068 if (nb_active == 1) {
1070 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
1078 static int audio_get_avail (SWVoiceIn *sw)
1086 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1087 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1088 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1093 "%s: get_avail live %d ret %lld\n",
1095 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1098 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1101 static int audio_get_free (SWVoiceOut *sw)
1109 live = sw->total_hw_samples_mixed;
1111 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1112 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1115 dead = sw->hw->samples - live;
1118 dolog ("%s: get_free live %d dead %d ret %lld\n",
1120 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1123 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1126 static void audio_run_out (void)
1128 HWVoiceOut *hw = NULL;
1131 while ((hw = audio_pcm_hw_find_any_active_enabled_out (hw))) {
1133 int live, free, nb_live;
1135 live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
1139 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1140 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1143 if (hw->pending_disable && !nb_live) {
1145 dolog ("Disabling voice\n");
1148 hw->pending_disable = 0;
1149 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
1154 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1156 free = audio_get_free (sw);
1158 sw->callback.fn (sw->callback.opaque, free);
1165 played = hw->pcm_ops->run_out (hw);
1166 if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1167 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1168 hw->rpos, hw->samples, played);
1173 dolog ("played = %d total %d\n", played, hw->total_samples_played);
1177 hw->ts_helper += played;
1180 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1182 if (!sw->active && sw->empty) {
1186 if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1187 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1188 played, sw->total_hw_samples_mixed);
1189 played = sw->total_hw_samples_mixed;
1192 sw->total_hw_samples_mixed -= played;
1194 if (!sw->total_hw_samples_mixed) {
1197 if (!sw->active && !sw->callback.fn) {
1198 SWVoiceOut *temp = sw->entries.le_next;
1201 dolog ("Finishing with old voice\n");
1215 free = audio_get_free (sw);
1217 sw->callback.fn (sw->callback.opaque, free);
1224 static void audio_run_in (void)
1226 HWVoiceIn *hw = NULL;
1228 while ((hw = audio_pcm_hw_find_any_active_enabled_in (hw))) {
1232 captured = hw->pcm_ops->run_in (hw);
1234 min = audio_pcm_hw_find_min_in (hw);
1235 hw->total_samples_captured += captured - min;
1236 hw->ts_helper += captured;
1238 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1239 sw->total_hw_samples_acquired -= min;
1244 avail = audio_get_avail (sw);
1246 sw->callback.fn (sw->callback.opaque, avail);
1253 static struct audio_option audio_options[] = {
1255 {"DAC_FIXED_SETTINGS", AUD_OPT_BOOL, &audio_state.fixed_settings_out,
1256 "Use fixed settings for host DAC", NULL, 0},
1258 {"DAC_FIXED_FREQ", AUD_OPT_INT, &audio_state.fixed_freq_out,
1259 "Frequency for fixed host DAC", NULL, 0},
1261 {"DAC_FIXED_FMT", AUD_OPT_FMT, &audio_state.fixed_fmt_out,
1262 "Format for fixed host DAC", NULL, 0},
1264 {"DAC_FIXED_CHANNELS", AUD_OPT_INT, &audio_state.fixed_channels_out,
1265 "Number of channels for fixed DAC (1 - mono, 2 - stereo)", NULL, 0},
1267 {"DAC_VOICES", AUD_OPT_INT, &audio_state.nb_hw_voices_out,
1268 "Number of voices for DAC", NULL, 0},
1271 {"ADC_FIXED_SETTINGS", AUD_OPT_BOOL, &audio_state.fixed_settings_out,
1272 "Use fixed settings for host ADC", NULL, 0},
1274 {"ADC_FIXED_FREQ", AUD_OPT_INT, &audio_state.fixed_freq_out,
1275 "Frequency for fixed ADC", NULL, 0},
1277 {"ADC_FIXED_FMT", AUD_OPT_FMT, &audio_state.fixed_fmt_out,
1278 "Format for fixed ADC", NULL, 0},
1280 {"ADC_FIXED_CHANNELS", AUD_OPT_INT, &audio_state.fixed_channels_in,
1281 "Number of channels for fixed ADC (1 - mono, 2 - stereo)", NULL, 0},
1283 {"ADC_VOICES", AUD_OPT_INT, &audio_state.nb_hw_voices_out,
1284 "Number of voices for ADC", NULL, 0},
1287 {"TIMER_PERIOD", AUD_OPT_INT, &audio_state.period.usec,
1288 "Timer period in microseconds (0 - try lowest possible)", NULL, 0},
1290 {"PLIVE", AUD_OPT_BOOL, &audio_state.plive,
1291 "(undocumented)", NULL, 0},
1293 {NULL, 0, NULL, NULL, NULL, 0}
1296 void AUD_help (void)
1300 audio_process_options ("AUDIO", audio_options);
1301 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1302 struct audio_driver *d = drvtab[i];
1304 audio_process_options (d->name, d->options);
1308 printf ("Audio options:\n");
1309 audio_print_options ("AUDIO", audio_options);
1312 printf ("Available drivers:\n");
1314 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1315 struct audio_driver *d = drvtab[i];
1317 printf ("Name: %s\n", d->name);
1318 printf ("Description: %s\n", d->descr);
1320 switch (d->max_voices_out) {
1322 printf ("Does not support DAC\n");
1325 printf ("One DAC voice\n");
1328 printf ("Theoretically supports many DAC voices\n");
1331 printf ("Theoretically supports upto %d DAC voices\n",
1336 switch (d->max_voices_in) {
1338 printf ("Does not support ADC\n");
1341 printf ("One ADC voice\n");
1344 printf ("Theoretically supports many ADC voices\n");
1347 printf ("Theoretically supports upto %d ADC voices\n",
1353 printf ("Options:\n");
1354 audio_print_options (d->name, d->options);
1357 printf ("No options\n");
1363 "Options are settable through environment variables.\n"
1366 " set QEMU_AUDIO_DRV=wav\n"
1367 " set QEMU_WAV_PATH=c:/tune.wav\n"
1369 " export QEMU_AUDIO_DRV=wav\n"
1370 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1371 "(for csh replace export with setenv in the above)\n"
1377 void audio_timer (void *opaque)
1379 AudioState *s = opaque;
1384 qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + s->period.ticks);
1387 static int audio_driver_init (struct audio_driver *drv)
1390 audio_process_options (drv->name, drv->options);
1392 audio_state.opaque = drv->init ();
1394 if (audio_state.opaque) {
1399 if (audio_state.nb_hw_voices_out > drv->max_voices_out) {
1400 if (!drv->max_voices_out) {
1401 dolog ("`%s' does not support DAC\n", drv->name);
1405 "`%s' does not support %d multiple DAC voicess\n"
1406 "Resetting to %d\n",
1408 audio_state.nb_hw_voices_out,
1412 audio_state.nb_hw_voices_out = drv->max_voices_out;
1415 LIST_INIT (&hw_head_out);
1416 hwo = qemu_mallocz (audio_state.nb_hw_voices_out * drv->voice_size_out);
1419 "Not enough memory for %d `%s' DAC voices (each %d bytes)\n",
1420 audio_state.nb_hw_voices_out,
1424 drv->fini (audio_state.opaque);
1428 for (i = 0; i < audio_state.nb_hw_voices_out; ++i) {
1429 LIST_INSERT_HEAD (&hw_head_out, hwo, entries);
1430 hwo = advance (hwo, drv->voice_size_out);
1433 if (!drv->voice_size_in && drv->max_voices_in) {
1434 ldebug ("warning: No ADC voice size defined for `%s'\n",
1436 drv->max_voices_in = 0;
1439 if (!drv->voice_size_out && drv->max_voices_out) {
1440 ldebug ("warning: No DAC voice size defined for `%s'\n",
1444 if (drv->voice_size_in && !drv->max_voices_in) {
1445 ldebug ("warning: ADC voice size is %d for ADC less driver `%s'\n",
1446 drv->voice_size_out, drv->name);
1449 if (drv->voice_size_out && !drv->max_voices_out) {
1450 ldebug ("warning: DAC voice size is %d for DAC less driver `%s'\n",
1451 drv->voice_size_in, drv->name);
1454 if (audio_state.nb_hw_voices_in > drv->max_voices_in) {
1455 if (!drv->max_voices_in) {
1456 ldebug ("`%s' does not support ADC\n", drv->name);
1460 "`%s' does not support %d multiple ADC voices\n"
1461 "Resetting to %d\n",
1463 audio_state.nb_hw_voices_in,
1467 audio_state.nb_hw_voices_in = drv->max_voices_in;
1470 LIST_INIT (&hw_head_in);
1471 hwi = qemu_mallocz (audio_state.nb_hw_voices_in * drv->voice_size_in);
1474 "Not enough memory for %d `%s' ADC voices (each %d bytes)\n",
1475 audio_state.nb_hw_voices_in,
1480 drv->fini (audio_state.opaque);
1484 for (i = 0; i < audio_state.nb_hw_voices_in; ++i) {
1485 LIST_INSERT_HEAD (&hw_head_in, hwi, entries);
1486 hwi = advance (hwi, drv->voice_size_in);
1489 audio_state.drv = drv;
1493 dolog ("Could not init `%s' audio driver\n", drv->name);
1498 static void audio_vm_stop_handler (void *opaque, int reason)
1500 HWVoiceOut *hwo = NULL;
1501 HWVoiceIn *hwi = NULL;
1502 int op = reason ? VOICE_ENABLE : VOICE_DISABLE;
1505 while ((hwo = audio_pcm_hw_find_any_out (hwo))) {
1506 if (!hwo->pcm_ops) {
1510 if (hwo->enabled != reason) {
1511 hwo->pcm_ops->ctl_out (hwo, op);
1515 while ((hwi = audio_pcm_hw_find_any_in (hwi))) {
1516 if (!hwi->pcm_ops) {
1520 if (hwi->enabled != reason) {
1521 hwi->pcm_ops->ctl_in (hwi, op);
1526 static void audio_atexit (void)
1528 HWVoiceOut *hwo = NULL;
1529 HWVoiceIn *hwi = NULL;
1531 while ((hwo = audio_pcm_hw_find_any_out (hwo))) {
1532 if (!hwo->pcm_ops) {
1537 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1539 hwo->pcm_ops->fini_out (hwo);
1542 while ((hwi = audio_pcm_hw_find_any_in (hwi))) {
1543 if (!hwi->pcm_ops) {
1548 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1550 hwi->pcm_ops->fini_in (hwi);
1552 audio_state.drv->fini (audio_state.opaque);
1555 static void audio_save (QEMUFile *f, void *opaque)
1561 static int audio_load (QEMUFile *f, void *opaque, int version_id)
1566 if (version_id != 1) {
1573 void AUD_init (void)
1577 const char *drvname;
1578 AudioState *s = &audio_state;
1580 audio_process_options ("AUDIO", audio_options);
1582 if (s->nb_hw_voices_out <= 0) {
1583 dolog ("Bogus number of DAC voices %d\n",
1584 s->nb_hw_voices_out);
1585 s->nb_hw_voices_out = 1;
1588 if (s->nb_hw_voices_in <= 0) {
1589 dolog ("Bogus number of ADC voices %d\n",
1590 s->nb_hw_voices_in);
1591 s->nb_hw_voices_in = 1;
1596 drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1599 s->ts = qemu_new_timer (vm_clock, audio_timer, s);
1601 dolog ("Can not create audio timer\n");
1608 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1609 if (!strcmp (drvname, drvtab[i]->name)) {
1610 done = !audio_driver_init (drvtab[i]);
1617 dolog ("Unknown audio driver `%s'\n", drvname);
1618 dolog ("Run with -audio-help to list available drivers\n");
1622 qemu_add_vm_stop_handler (audio_vm_stop_handler, NULL);
1623 atexit (audio_atexit);
1626 for (i = 0; !done && i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1627 if (drvtab[i]->can_be_default) {
1628 done = !audio_driver_init (drvtab[i]);
1633 register_savevm ("audio", 0, 1, audio_save, audio_load, NULL);
1635 if (audio_driver_init (&no_audio_driver)) {
1636 dolog ("Can not initialize audio subsystem\n");
1639 dolog ("warning: using timer based audio emulation\n");
1643 if (s->period.usec <= 0) {
1644 if (s->period.usec < 0) {
1645 dolog ("warning: timer period is negative - %d treating as zero\n",
1648 s->period.ticks = 1;
1651 s->period.ticks = (ticks_per_sec * s->period.usec) / 1000000;
1654 qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + s->period.ticks);