2 * QEMU ALSA audio driver
4 * Copyright (c) 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
24 #include <alsa/asoundlib.h>
25 #include "qemu-common.h"
28 #define AUDIO_CAP "alsa"
29 #include "audio_int.h"
31 typedef struct ALSAVoiceOut {
37 typedef struct ALSAVoiceIn {
46 const char *pcm_name_in;
47 const char *pcm_name_out;
48 unsigned int buffer_size_in;
49 unsigned int period_size_in;
50 unsigned int buffer_size_out;
51 unsigned int period_size_out;
52 unsigned int threshold;
54 int buffer_size_in_overridden;
55 int period_size_in_overridden;
57 int buffer_size_out_overridden;
58 int period_size_out_overridden;
61 .buffer_size_out = 1024,
62 .pcm_name_out = "default",
63 .pcm_name_in = "default",
66 struct alsa_params_req {
72 unsigned int buffer_size;
73 unsigned int period_size;
76 struct alsa_params_obt {
81 snd_pcm_uframes_t samples;
84 static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err, const char *fmt, ...)
89 AUD_vlog (AUDIO_CAP, fmt, ap);
92 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
95 static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
104 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
107 AUD_vlog (AUDIO_CAP, fmt, ap);
110 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
113 static void alsa_anal_close (snd_pcm_t **handlep)
115 int err = snd_pcm_close (*handlep);
117 alsa_logerr (err, "Failed to close PCM handle %p\n", *handlep);
122 static int alsa_write (SWVoiceOut *sw, void *buf, int len)
124 return audio_pcm_sw_write (sw, buf, len);
127 static snd_pcm_format_t aud_to_alsafmt (audfmt_e fmt)
131 return SND_PCM_FORMAT_S8;
134 return SND_PCM_FORMAT_U8;
137 return SND_PCM_FORMAT_S16_LE;
140 return SND_PCM_FORMAT_U16_LE;
143 return SND_PCM_FORMAT_S32_LE;
146 return SND_PCM_FORMAT_U32_LE;
149 dolog ("Internal logic error: Bad audio format %d\n", fmt);
153 return SND_PCM_FORMAT_U8;
157 static int alsa_to_audfmt (snd_pcm_format_t alsafmt, audfmt_e *fmt,
161 case SND_PCM_FORMAT_S8:
166 case SND_PCM_FORMAT_U8:
171 case SND_PCM_FORMAT_S16_LE:
176 case SND_PCM_FORMAT_U16_LE:
181 case SND_PCM_FORMAT_S16_BE:
186 case SND_PCM_FORMAT_U16_BE:
191 case SND_PCM_FORMAT_S32_LE:
196 case SND_PCM_FORMAT_U32_LE:
201 case SND_PCM_FORMAT_S32_BE:
206 case SND_PCM_FORMAT_U32_BE:
212 dolog ("Unrecognized audio format %d\n", alsafmt);
219 static void alsa_dump_info (struct alsa_params_req *req,
220 struct alsa_params_obt *obt)
222 dolog ("parameter | requested value | obtained value\n");
223 dolog ("format | %10d | %10d\n", req->fmt, obt->fmt);
224 dolog ("channels | %10d | %10d\n",
225 req->nchannels, obt->nchannels);
226 dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
227 dolog ("============================================\n");
228 dolog ("requested: buffer size %d period size %d\n",
229 req->buffer_size, req->period_size);
230 dolog ("obtained: samples %ld\n", obt->samples);
233 static void alsa_set_threshold (snd_pcm_t *handle, snd_pcm_uframes_t threshold)
236 snd_pcm_sw_params_t *sw_params;
238 snd_pcm_sw_params_alloca (&sw_params);
240 err = snd_pcm_sw_params_current (handle, sw_params);
242 dolog ("Could not fully initialize DAC\n");
243 alsa_logerr (err, "Failed to get current software parameters\n");
247 err = snd_pcm_sw_params_set_start_threshold (handle, sw_params, threshold);
249 dolog ("Could not fully initialize DAC\n");
250 alsa_logerr (err, "Failed to set software threshold to %ld\n",
255 err = snd_pcm_sw_params (handle, sw_params);
257 dolog ("Could not fully initialize DAC\n");
258 alsa_logerr (err, "Failed to set software parameters\n");
263 static int alsa_open (int in, struct alsa_params_req *req,
264 struct alsa_params_obt *obt, snd_pcm_t **handlep)
267 snd_pcm_hw_params_t *hw_params;
270 unsigned int freq, nchannels;
271 const char *pcm_name = in ? conf.pcm_name_in : conf.pcm_name_out;
272 snd_pcm_uframes_t obt_buffer_size;
273 const char *typ = in ? "ADC" : "DAC";
274 snd_pcm_format_t obtfmt;
277 nchannels = req->nchannels;
278 size_in_usec = req->size_in_usec;
280 snd_pcm_hw_params_alloca (&hw_params);
285 in ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK,
289 alsa_logerr2 (err, typ, "Failed to open `%s':\n", pcm_name);
293 err = snd_pcm_hw_params_any (handle, hw_params);
295 alsa_logerr2 (err, typ, "Failed to initialize hardware parameters\n");
299 err = snd_pcm_hw_params_set_access (
302 SND_PCM_ACCESS_RW_INTERLEAVED
305 alsa_logerr2 (err, typ, "Failed to set access type\n");
309 err = snd_pcm_hw_params_set_format (handle, hw_params, req->fmt);
310 if (err < 0 && conf.verbose) {
311 alsa_logerr2 (err, typ, "Failed to set format %d\n", req->fmt);
314 err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &freq, 0);
316 alsa_logerr2 (err, typ, "Failed to set frequency %d\n", req->freq);
320 err = snd_pcm_hw_params_set_channels_near (
326 alsa_logerr2 (err, typ, "Failed to set number of channels %d\n",
331 if (nchannels != 1 && nchannels != 2) {
332 alsa_logerr2 (err, typ,
333 "Can not handle obtained number of channels %d\n",
338 if (req->buffer_size) {
343 unsigned int btime = req->buffer_size;
345 err = snd_pcm_hw_params_set_buffer_time_near (
354 snd_pcm_uframes_t bsize = req->buffer_size;
356 err = snd_pcm_hw_params_set_buffer_size_near (
364 alsa_logerr2 (err, typ, "Failed to set buffer %s to %d\n",
365 size_in_usec ? "time" : "size", req->buffer_size);
369 if ((req->override_mask & 2) && (obt - req->buffer_size))
370 dolog ("Requested buffer %s %u was rejected, using %lu\n",
371 size_in_usec ? "time" : "size", req->buffer_size, obt);
374 if (req->period_size) {
379 unsigned int ptime = req->period_size;
381 err = snd_pcm_hw_params_set_period_time_near (
391 snd_pcm_uframes_t psize = req->period_size;
393 err = snd_pcm_hw_params_set_period_size_near (
403 alsa_logerr2 (err, typ, "Failed to set period %s to %d\n",
404 size_in_usec ? "time" : "size", req->period_size);
408 if ((req->override_mask & 1) && (obt - req->period_size))
409 dolog ("Requested period %s %u was rejected, using %lu\n",
410 size_in_usec ? "time" : "size", req->period_size, obt);
413 err = snd_pcm_hw_params (handle, hw_params);
415 alsa_logerr2 (err, typ, "Failed to apply audio parameters\n");
419 err = snd_pcm_hw_params_get_buffer_size (hw_params, &obt_buffer_size);
421 alsa_logerr2 (err, typ, "Failed to get buffer size\n");
425 err = snd_pcm_hw_params_get_format (hw_params, &obtfmt);
427 alsa_logerr2 (err, typ, "Failed to get format\n");
431 if (alsa_to_audfmt (obtfmt, &obt->fmt, &obt->endianness)) {
432 dolog ("Invalid format was returned %d\n", obtfmt);
436 err = snd_pcm_prepare (handle);
438 alsa_logerr2 (err, typ, "Could not prepare handle %p\n", handle);
442 if (!in && conf.threshold) {
443 snd_pcm_uframes_t threshold;
446 bytes_per_sec = freq << (nchannels == 2);
464 threshold = (conf.threshold * bytes_per_sec) / 1000;
465 alsa_set_threshold (handle, threshold);
468 obt->nchannels = nchannels;
470 obt->samples = obt_buffer_size;
475 (obt->fmt != req->fmt ||
476 obt->nchannels != req->nchannels ||
477 obt->freq != req->freq)) {
478 dolog ("Audio paramters for %s\n", typ);
479 alsa_dump_info (req, obt);
483 alsa_dump_info (req, obt);
488 alsa_anal_close (&handle);
492 static int alsa_recover (snd_pcm_t *handle)
494 int err = snd_pcm_prepare (handle);
496 alsa_logerr (err, "Failed to prepare handle %p\n", handle);
502 static snd_pcm_sframes_t alsa_get_avail (snd_pcm_t *handle)
504 snd_pcm_sframes_t avail;
506 avail = snd_pcm_avail_update (handle);
508 if (avail == -EPIPE) {
509 if (!alsa_recover (handle)) {
510 avail = snd_pcm_avail_update (handle);
516 "Could not obtain number of available frames\n");
524 static int alsa_run_out (HWVoiceOut *hw)
526 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
527 int rpos, live, decr;
531 snd_pcm_sframes_t avail;
533 live = audio_pcm_hw_get_live_out (hw);
538 avail = alsa_get_avail (alsa->handle);
540 dolog ("Could not get number of available playback frames\n");
544 decr = audio_MIN (live, avail);
548 int left_till_end_samples = hw->samples - rpos;
549 int len = audio_MIN (samples, left_till_end_samples);
550 snd_pcm_sframes_t written;
552 src = hw->mix_buf + rpos;
553 dst = advance (alsa->pcm_buf, rpos << hw->info.shift);
555 hw->clip (dst, src, len);
558 written = snd_pcm_writei (alsa->handle, dst, len);
564 dolog ("Failed to write %d frames (wrote zero)\n", len);
569 if (alsa_recover (alsa->handle)) {
570 alsa_logerr (written, "Failed to write %d frames\n",
575 dolog ("Recovering from playback xrun\n");
583 alsa_logerr (written, "Failed to write %d frames to %p\n",
589 rpos = (rpos + written) % hw->samples;
592 dst = advance (dst, written << hw->info.shift);
602 static void alsa_fini_out (HWVoiceOut *hw)
604 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
606 ldebug ("alsa_fini\n");
607 alsa_anal_close (&alsa->handle);
610 qemu_free (alsa->pcm_buf);
611 alsa->pcm_buf = NULL;
615 static int alsa_init_out (HWVoiceOut *hw, audsettings_t *as)
617 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
618 struct alsa_params_req req;
619 struct alsa_params_obt obt;
621 audsettings_t obt_as;
623 req.fmt = aud_to_alsafmt (as->fmt);
625 req.nchannels = as->nchannels;
626 req.period_size = conf.period_size_out;
627 req.buffer_size = conf.buffer_size_out;
628 req.size_in_usec = conf.size_in_usec_out;
629 req.override_mask = !!conf.period_size_out_overridden
630 | (!!conf.buffer_size_out_overridden << 1);
632 if (alsa_open (0, &req, &obt, &handle)) {
636 obt_as.freq = obt.freq;
637 obt_as.nchannels = obt.nchannels;
638 obt_as.fmt = obt.fmt;
639 obt_as.endianness = obt.endianness;
641 audio_pcm_init_info (&hw->info, &obt_as);
642 hw->samples = obt.samples;
644 alsa->pcm_buf = audio_calloc (AUDIO_FUNC, obt.samples, 1 << hw->info.shift);
645 if (!alsa->pcm_buf) {
646 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
647 hw->samples, 1 << hw->info.shift);
648 alsa_anal_close (&handle);
652 alsa->handle = handle;
656 static int alsa_voice_ctl (snd_pcm_t *handle, const char *typ, int pause)
661 err = snd_pcm_drop (handle);
663 alsa_logerr (err, "Could not stop %s\n", typ);
668 err = snd_pcm_prepare (handle);
670 alsa_logerr (err, "Could not prepare handle for %s\n", typ);
678 static int alsa_ctl_out (HWVoiceOut *hw, int cmd, ...)
680 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
684 ldebug ("enabling voice\n");
685 return alsa_voice_ctl (alsa->handle, "playback", 0);
688 ldebug ("disabling voice\n");
689 return alsa_voice_ctl (alsa->handle, "playback", 1);
695 static int alsa_init_in (HWVoiceIn *hw, audsettings_t *as)
697 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
698 struct alsa_params_req req;
699 struct alsa_params_obt obt;
701 audsettings_t obt_as;
703 req.fmt = aud_to_alsafmt (as->fmt);
705 req.nchannels = as->nchannels;
706 req.period_size = conf.period_size_in;
707 req.buffer_size = conf.buffer_size_in;
708 req.size_in_usec = conf.size_in_usec_in;
709 req.override_mask = !!conf.period_size_in_overridden
710 | (!!conf.buffer_size_in_overridden << 1);
712 if (alsa_open (1, &req, &obt, &handle)) {
716 obt_as.freq = obt.freq;
717 obt_as.nchannels = obt.nchannels;
718 obt_as.fmt = obt.fmt;
719 obt_as.endianness = obt.endianness;
721 audio_pcm_init_info (&hw->info, &obt_as);
722 hw->samples = obt.samples;
724 alsa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
725 if (!alsa->pcm_buf) {
726 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
727 hw->samples, 1 << hw->info.shift);
728 alsa_anal_close (&handle);
732 alsa->handle = handle;
736 static void alsa_fini_in (HWVoiceIn *hw)
738 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
740 alsa_anal_close (&alsa->handle);
743 qemu_free (alsa->pcm_buf);
744 alsa->pcm_buf = NULL;
748 static int alsa_run_in (HWVoiceIn *hw)
750 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
751 int hwshift = hw->info.shift;
753 int live = audio_pcm_hw_get_live_in (hw);
754 int dead = hw->samples - live;
763 snd_pcm_sframes_t avail;
764 snd_pcm_uframes_t read_samples = 0;
770 avail = alsa_get_avail (alsa->handle);
772 dolog ("Could not get number of captured frames\n");
776 if (!avail && (snd_pcm_state (alsa->handle) == SND_PCM_STATE_PREPARED)) {
780 decr = audio_MIN (dead, avail);
785 if (hw->wpos + decr > hw->samples) {
786 bufs[0].len = (hw->samples - hw->wpos);
787 bufs[1].len = (decr - (hw->samples - hw->wpos));
793 for (i = 0; i < 2; ++i) {
796 snd_pcm_sframes_t nread;
797 snd_pcm_uframes_t len;
801 src = advance (alsa->pcm_buf, bufs[i].add << hwshift);
802 dst = hw->conv_buf + bufs[i].add;
805 nread = snd_pcm_readi (alsa->handle, src, len);
811 dolog ("Failed to read %ld frames (read zero)\n", len);
816 if (alsa_recover (alsa->handle)) {
817 alsa_logerr (nread, "Failed to read %ld frames\n", len);
821 dolog ("Recovering from capture xrun\n");
831 "Failed to read %ld frames from %p\n",
839 hw->conv (dst, src, nread, &nominal_volume);
841 src = advance (src, nread << hwshift);
844 read_samples += nread;
850 hw->wpos = (hw->wpos + read_samples) % hw->samples;
854 static int alsa_read (SWVoiceIn *sw, void *buf, int size)
856 return audio_pcm_sw_read (sw, buf, size);
859 static int alsa_ctl_in (HWVoiceIn *hw, int cmd, ...)
861 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
865 ldebug ("enabling voice\n");
866 return alsa_voice_ctl (alsa->handle, "capture", 0);
869 ldebug ("disabling voice\n");
870 return alsa_voice_ctl (alsa->handle, "capture", 1);
876 static void *alsa_audio_init (void)
881 static void alsa_audio_fini (void *opaque)
886 static struct audio_option alsa_options[] = {
887 {"DAC_SIZE_IN_USEC", AUD_OPT_BOOL, &conf.size_in_usec_out,
888 "DAC period/buffer size in microseconds (otherwise in frames)", NULL, 0},
889 {"DAC_PERIOD_SIZE", AUD_OPT_INT, &conf.period_size_out,
890 "DAC period size (0 to go with system default)",
891 &conf.period_size_out_overridden, 0},
892 {"DAC_BUFFER_SIZE", AUD_OPT_INT, &conf.buffer_size_out,
893 "DAC buffer size (0 to go with system default)",
894 &conf.buffer_size_out_overridden, 0},
896 {"ADC_SIZE_IN_USEC", AUD_OPT_BOOL, &conf.size_in_usec_in,
897 "ADC period/buffer size in microseconds (otherwise in frames)", NULL, 0},
898 {"ADC_PERIOD_SIZE", AUD_OPT_INT, &conf.period_size_in,
899 "ADC period size (0 to go with system default)",
900 &conf.period_size_in_overridden, 0},
901 {"ADC_BUFFER_SIZE", AUD_OPT_INT, &conf.buffer_size_in,
902 "ADC buffer size (0 to go with system default)",
903 &conf.buffer_size_in_overridden, 0},
905 {"THRESHOLD", AUD_OPT_INT, &conf.threshold,
906 "(undocumented)", NULL, 0},
908 {"DAC_DEV", AUD_OPT_STR, &conf.pcm_name_out,
909 "DAC device name (for instance dmix)", NULL, 0},
911 {"ADC_DEV", AUD_OPT_STR, &conf.pcm_name_in,
912 "ADC device name", NULL, 0},
914 {"VERBOSE", AUD_OPT_BOOL, &conf.verbose,
915 "Behave in a more verbose way", NULL, 0},
917 {NULL, 0, NULL, NULL, NULL, 0}
920 static struct audio_pcm_ops alsa_pcm_ops = {
934 struct audio_driver alsa_audio_driver = {
935 INIT_FIELD (name = ) "alsa",
936 INIT_FIELD (descr = ) "ALSA http://www.alsa-project.org",
937 INIT_FIELD (options = ) alsa_options,
938 INIT_FIELD (init = ) alsa_audio_init,
939 INIT_FIELD (fini = ) alsa_audio_fini,
940 INIT_FIELD (pcm_ops = ) &alsa_pcm_ops,
941 INIT_FIELD (can_be_default = ) 1,
942 INIT_FIELD (max_voices_out = ) INT_MAX,
943 INIT_FIELD (max_voices_in = ) INT_MAX,
944 INIT_FIELD (voice_size_out = ) sizeof (ALSAVoiceOut),
945 INIT_FIELD (voice_size_in = ) sizeof (ALSAVoiceIn)