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>
27 #define AUDIO_CAP "alsa"
28 #include "audio_int.h"
30 typedef struct ALSAVoiceOut {
38 typedef struct ALSAVoiceIn {
48 const char *pcm_name_in;
49 const char *pcm_name_out;
50 unsigned int buffer_size_in;
51 unsigned int period_size_in;
52 unsigned int buffer_size_out;
53 unsigned int period_size_out;
54 unsigned int threshold;
56 int buffer_size_in_overriden;
57 int period_size_in_overriden;
59 int buffer_size_out_overriden;
60 int period_size_out_overriden;
64 .size_in_usec_out = 1,
66 .pcm_name_out = "hw:0,0",
67 .pcm_name_in = "hw:0,0",
69 .buffer_size_in = 400000,
70 .period_size_in = 400000 / 4,
71 .buffer_size_out = 400000,
72 .period_size_out = 400000 / 4,
74 #define DEFAULT_BUFFER_SIZE 1024
75 #define DEFAULT_PERIOD_SIZE 256
76 .buffer_size_in = DEFAULT_BUFFER_SIZE,
77 .period_size_in = DEFAULT_PERIOD_SIZE,
78 .buffer_size_out = DEFAULT_BUFFER_SIZE,
79 .period_size_out = DEFAULT_PERIOD_SIZE,
80 .buffer_size_in_overriden = 0,
81 .buffer_size_out_overriden = 0,
82 .period_size_in_overriden = 0,
83 .period_size_out_overriden = 0,
88 struct alsa_params_req {
92 unsigned int buffer_size;
93 unsigned int period_size;
96 struct alsa_params_obt {
101 snd_pcm_uframes_t buffer_size;
104 static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err, const char *fmt, ...)
109 AUD_vlog (AUDIO_CAP, fmt, ap);
112 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
115 static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
124 AUD_log (AUDIO_CAP, "Can not initialize %s\n", typ);
127 AUD_vlog (AUDIO_CAP, fmt, ap);
130 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
133 static void alsa_anal_close (snd_pcm_t **handlep)
135 int err = snd_pcm_close (*handlep);
137 alsa_logerr (err, "Failed to close PCM handle %p\n", *handlep);
142 static int alsa_write (SWVoiceOut *sw, void *buf, int len)
144 return audio_pcm_sw_write (sw, buf, len);
147 static int aud_to_alsafmt (audfmt_e fmt)
151 return SND_PCM_FORMAT_S8;
154 return SND_PCM_FORMAT_U8;
157 return SND_PCM_FORMAT_S16_LE;
160 return SND_PCM_FORMAT_U16_LE;
163 dolog ("Internal logic error: Bad audio format %d\n", fmt);
167 return SND_PCM_FORMAT_U8;
171 static int alsa_to_audfmt (int alsafmt, audfmt_e *fmt, int *endianness)
174 case SND_PCM_FORMAT_S8:
179 case SND_PCM_FORMAT_U8:
184 case SND_PCM_FORMAT_S16_LE:
189 case SND_PCM_FORMAT_U16_LE:
194 case SND_PCM_FORMAT_S16_BE:
199 case SND_PCM_FORMAT_U16_BE:
205 dolog ("Unrecognized audio format %d\n", alsafmt);
212 #ifdef DEBUG_MISMATCHES
213 static void alsa_dump_info (struct alsa_params_req *req,
214 struct alsa_params_obt *obt)
216 dolog ("parameter | requested value | obtained value\n");
217 dolog ("format | %10d | %10d\n", req->fmt, obt->fmt);
218 dolog ("channels | %10d | %10d\n",
219 req->nchannels, obt->nchannels);
220 dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
221 dolog ("============================================\n");
222 dolog ("requested: buffer size %d period size %d\n",
223 req->buffer_size, req->period_size);
224 dolog ("obtained: buffer size %ld\n", obt->buffer_size);
228 static void alsa_set_threshold (snd_pcm_t *handle, snd_pcm_uframes_t threshold)
231 snd_pcm_sw_params_t *sw_params;
233 snd_pcm_sw_params_alloca (&sw_params);
235 err = snd_pcm_sw_params_current (handle, sw_params);
237 dolog ("Can not fully initialize DAC\n");
238 alsa_logerr (err, "Failed to get current software parameters\n");
242 err = snd_pcm_sw_params_set_start_threshold (handle, sw_params, threshold);
244 dolog ("Can not fully initialize DAC\n");
245 alsa_logerr (err, "Failed to set software threshold to %ld\n",
250 err = snd_pcm_sw_params (handle, sw_params);
252 dolog ("Can not fully initialize DAC\n");
253 alsa_logerr (err, "Failed to set software parameters\n");
258 static int alsa_open (int in, struct alsa_params_req *req,
259 struct alsa_params_obt *obt, snd_pcm_t **handlep)
262 snd_pcm_hw_params_t *hw_params;
263 int err, freq, nchannels;
264 const char *pcm_name = in ? conf.pcm_name_in : conf.pcm_name_out;
265 unsigned int period_size, buffer_size;
266 snd_pcm_uframes_t obt_buffer_size;
267 const char *typ = in ? "ADC" : "DAC";
270 period_size = req->period_size;
271 buffer_size = req->buffer_size;
272 nchannels = req->nchannels;
274 snd_pcm_hw_params_alloca (&hw_params);
279 in ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK,
283 alsa_logerr2 (err, typ, "Failed to open `%s':\n", pcm_name);
287 err = snd_pcm_hw_params_any (handle, hw_params);
289 alsa_logerr2 (err, typ, "Failed to initialize hardware parameters\n");
293 err = snd_pcm_hw_params_set_access (
296 SND_PCM_ACCESS_RW_INTERLEAVED
299 alsa_logerr2 (err, typ, "Failed to set access type\n");
303 err = snd_pcm_hw_params_set_format (handle, hw_params, req->fmt);
305 alsa_logerr2 (err, typ, "Failed to set format %d\n", req->fmt);
309 err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &freq, 0);
311 alsa_logerr2 (err, typ, "Failed to set frequency %d\n", req->freq);
315 err = snd_pcm_hw_params_set_channels_near (
321 alsa_logerr2 (err, typ, "Failed to set number of channels %d\n",
326 if (nchannels != 1 && nchannels != 2) {
327 alsa_logerr2 (err, typ,
328 "Can not handle obtained number of channels %d\n",
333 if (!((in && conf.size_in_usec_in) || (!in && conf.size_in_usec_out))) {
335 buffer_size = DEFAULT_BUFFER_SIZE;
336 period_size= DEFAULT_PERIOD_SIZE;
341 if ((in && conf.size_in_usec_in) || (!in && conf.size_in_usec_out)) {
343 err = snd_pcm_hw_params_set_period_time_near (
349 alsa_logerr2 (err, typ,
350 "Failed to set period time %d\n",
356 err = snd_pcm_hw_params_set_buffer_time_near (
363 alsa_logerr2 (err, typ,
364 "Failed to set buffer time %d\n",
371 snd_pcm_uframes_t minval;
374 minval = period_size;
377 err = snd_pcm_hw_params_get_period_size_min (
385 "Can not get minmal period size for %s\n",
390 if (period_size < minval) {
391 if ((in && conf.period_size_in_overriden)
392 || (!in && conf.period_size_out_overriden)) {
393 dolog ("%s period size(%d) is less "
394 "than minmal period size(%ld)\n",
399 period_size = minval;
403 err = snd_pcm_hw_params_set_period_size (
410 alsa_logerr2 (err, typ, "Failed to set period size %d\n",
416 minval = buffer_size;
417 err = snd_pcm_hw_params_get_buffer_size_min (
422 alsa_logerr (err, "Can not get minmal buffer size for %s\n",
426 if (buffer_size < minval) {
427 if ((in && conf.buffer_size_in_overriden)
428 || (!in && conf.buffer_size_out_overriden)) {
430 "%s buffer size(%d) is less "
431 "than minimal buffer size(%ld)\n",
437 buffer_size = minval;
441 err = snd_pcm_hw_params_set_buffer_size (
447 alsa_logerr2 (err, typ, "Failed to set buffer size %d\n",
454 dolog ("warning: buffer size is not set\n");
457 err = snd_pcm_hw_params (handle, hw_params);
459 alsa_logerr2 (err, typ, "Failed to apply audio parameters\n");
463 err = snd_pcm_hw_params_get_buffer_size (hw_params, &obt_buffer_size);
465 alsa_logerr2 (err, typ, "Failed to get buffer size\n");
469 err = snd_pcm_prepare (handle);
471 alsa_logerr2 (err, typ, "Can not prepare handle %p\n", handle);
475 obt->can_pause = snd_pcm_hw_params_can_pause (hw_params);
476 if (obt->can_pause < 0) {
477 alsa_logerr (err, "Can not get pause capability for %s\n", typ);
481 if (!in && conf.threshold) {
482 snd_pcm_uframes_t threshold;
487 << (req->fmt == AUD_FMT_S16 || req->fmt == AUD_FMT_U16);
489 threshold = (conf.threshold * bytes_per_sec) / 1000;
490 alsa_set_threshold (handle, threshold);
494 obt->nchannels = nchannels;
496 obt->buffer_size = snd_pcm_frames_to_bytes (handle, obt_buffer_size);
499 if (obt->fmt != req->fmt ||
500 obt->nchannels != req->nchannels ||
501 obt->freq != req->freq) {
502 #ifdef DEBUG_MISMATCHES
503 dolog ("Audio paramters mismatch for %s\n", typ);
504 alsa_dump_info (req, obt);
509 alsa_dump_info (req, obt);
514 alsa_anal_close (&handle);
518 static int alsa_recover (snd_pcm_t *handle)
520 int err = snd_pcm_prepare (handle);
522 alsa_logerr (err, "Failed to prepare handle %p\n", handle);
528 static int alsa_run_out (HWVoiceOut *hw)
530 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
531 int rpos, live, decr;
535 snd_pcm_sframes_t avail;
537 live = audio_pcm_hw_get_live_out (hw);
542 avail = snd_pcm_avail_update (alsa->handle);
544 if (avail == -EPIPE) {
545 if (!alsa_recover (alsa->handle)) {
546 avail = snd_pcm_avail_update (alsa->handle);
553 alsa_logerr (avail, "Can not get amount free space\n");
558 decr = audio_MIN (live, avail);
562 int left_till_end_samples = hw->samples - rpos;
563 int convert_samples = audio_MIN (samples, left_till_end_samples);
564 snd_pcm_sframes_t written;
566 src = hw->mix_buf + rpos;
567 dst = advance (alsa->pcm_buf, rpos << hw->info.shift);
569 hw->clip (dst, src, convert_samples);
572 written = snd_pcm_writei (alsa->handle, dst, convert_samples);
577 if (!alsa_recover (alsa->handle)) {
581 "Failed to write %d frames to %p, handle %p not prepared\n",
592 alsa_logerr (written, "Failed to write %d frames to %p\n",
593 convert_samples, dst);
598 mixeng_clear (src, written);
599 rpos = (rpos + written) % hw->samples;
608 static void alsa_fini_out (HWVoiceOut *hw)
610 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
612 ldebug ("alsa_fini\n");
613 alsa_anal_close (&alsa->handle);
616 qemu_free (alsa->pcm_buf);
617 alsa->pcm_buf = NULL;
621 static int alsa_init_out (HWVoiceOut *hw, int freq, int nchannels, audfmt_e fmt)
623 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
624 struct alsa_params_req req;
625 struct alsa_params_obt obt;
626 audfmt_e effective_fmt;
631 req.fmt = aud_to_alsafmt (fmt);
633 req.nchannels = nchannels;
634 req.period_size = conf.period_size_out;
635 req.buffer_size = conf.buffer_size_out;
637 if (alsa_open (0, &req, &obt, &handle)) {
641 err = alsa_to_audfmt (obt.fmt, &effective_fmt, &endianness);
643 alsa_anal_close (&handle);
647 audio_pcm_init_info (
652 audio_need_to_swap_endian (endianness)
654 alsa->can_pause = obt.can_pause;
655 hw->bufsize = obt.buffer_size;
657 alsa->pcm_buf = qemu_mallocz (hw->bufsize);
658 if (!alsa->pcm_buf) {
659 alsa_anal_close (&handle);
663 alsa->handle = handle;
664 alsa->was_enabled = 0;
668 static int alsa_ctl_out (HWVoiceOut *hw, int cmd, ...)
671 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
675 ldebug ("enabling voice\n");
676 audio_pcm_info_clear_buf (&hw->info, alsa->pcm_buf, hw->samples);
677 if (alsa->can_pause) {
678 /* Why this was_enabled madness is needed at all?? */
679 if (alsa->was_enabled) {
680 err = snd_pcm_pause (alsa->handle, 0);
682 alsa_logerr (err, "Failed to resume playing\n");
683 /* not fatal really */
687 alsa->was_enabled = 1;
693 ldebug ("disabling voice\n");
694 if (alsa->can_pause) {
695 err = snd_pcm_pause (alsa->handle, 1);
697 alsa_logerr (err, "Failed to stop playing\n");
698 /* not fatal really */
706 static int alsa_init_in (HWVoiceIn *hw,
707 int freq, int nchannels, audfmt_e fmt)
709 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
710 struct alsa_params_req req;
711 struct alsa_params_obt obt;
714 audfmt_e effective_fmt;
717 req.fmt = aud_to_alsafmt (fmt);
719 req.nchannels = nchannels;
720 req.period_size = conf.period_size_in;
721 req.buffer_size = conf.buffer_size_in;
723 if (alsa_open (1, &req, &obt, &handle)) {
727 err = alsa_to_audfmt (obt.fmt, &effective_fmt, &endianness);
729 alsa_anal_close (&handle);
733 audio_pcm_init_info (
738 audio_need_to_swap_endian (endianness)
740 alsa->can_pause = obt.can_pause;
741 hw->bufsize = obt.buffer_size;
742 alsa->pcm_buf = qemu_mallocz (hw->bufsize);
743 if (!alsa->pcm_buf) {
744 alsa_anal_close (&handle);
748 alsa->handle = handle;
752 static void alsa_fini_in (HWVoiceIn *hw)
754 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
756 alsa_anal_close (&alsa->handle);
759 qemu_free (alsa->pcm_buf);
760 alsa->pcm_buf = NULL;
764 static int alsa_run_in (HWVoiceIn *hw)
766 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
767 int hwshift = hw->info.shift;
769 int live = audio_pcm_hw_get_live_in (hw);
770 int dead = hw->samples - live;
779 snd_pcm_uframes_t read_samples = 0;
785 if (hw->wpos + dead > hw->samples) {
786 bufs[0].len = (hw->samples - hw->wpos);
787 bufs[1].len = (dead - (hw->samples - hw->wpos));
794 for (i = 0; i < 2; ++i) {
797 snd_pcm_sframes_t nread;
798 snd_pcm_uframes_t len;
802 src = advance (alsa->pcm_buf, bufs[i].add << hwshift);
803 dst = hw->conv_buf + bufs[i].add;
806 nread = snd_pcm_readi (alsa->handle, src, len);
811 if (!alsa_recover (alsa->handle)) {
815 "Failed to read %ld frames from %p, "
816 "handle %p not prepared\n",
829 "Failed to read %ld frames from %p\n",
837 hw->conv (dst, src, nread, &nominal_volume);
839 src = advance (src, nread << hwshift);
842 read_samples += nread;
848 hw->wpos = (hw->wpos + read_samples) % hw->samples;
852 static int alsa_read (SWVoiceIn *sw, void *buf, int size)
854 return audio_pcm_sw_read (sw, buf, size);
857 static int alsa_ctl_in (HWVoiceIn *hw, int cmd, ...)
864 static void *alsa_audio_init (void)
869 static void alsa_audio_fini (void *opaque)
874 static struct audio_option alsa_options[] = {
875 {"DAC_SIZE_IN_USEC", AUD_OPT_BOOL, &conf.size_in_usec_out,
876 "DAC period/buffer size in microseconds (otherwise in frames)", NULL, 0},
877 {"DAC_PERIOD_SIZE", AUD_OPT_INT, &conf.period_size_out,
878 "DAC period size", &conf.period_size_out_overriden, 0},
879 {"DAC_BUFFER_SIZE", AUD_OPT_INT, &conf.buffer_size_out,
880 "DAC buffer size", &conf.buffer_size_out_overriden, 0},
882 {"ADC_SIZE_IN_USEC", AUD_OPT_BOOL, &conf.size_in_usec_in,
883 "ADC period/buffer size in microseconds (otherwise in frames)", NULL, 0},
884 {"ADC_PERIOD_SIZE", AUD_OPT_INT, &conf.period_size_in,
885 "ADC period size", &conf.period_size_in_overriden, 0},
886 {"ADC_BUFFER_SIZE", AUD_OPT_INT, &conf.buffer_size_in,
887 "ADC buffer size", &conf.buffer_size_in_overriden, 0},
889 {"THRESHOLD", AUD_OPT_INT, &conf.threshold,
890 "(undocumented)", NULL, 0},
892 {"DAC_DEV", AUD_OPT_STR, &conf.pcm_name_out,
893 "DAC device name (for instance dmix)", NULL, 0},
895 {"ADC_DEV", AUD_OPT_STR, &conf.pcm_name_in,
896 "ADC device name", NULL, 0},
897 {NULL, 0, NULL, NULL, NULL, 0}
900 static struct audio_pcm_ops alsa_pcm_ops = {
914 struct audio_driver alsa_audio_driver = {
915 INIT_FIELD (name = ) "alsa",
916 INIT_FIELD (descr = ) "ALSA http://www.alsa-project.org",
917 INIT_FIELD (options = ) alsa_options,
918 INIT_FIELD (init = ) alsa_audio_init,
919 INIT_FIELD (fini = ) alsa_audio_fini,
920 INIT_FIELD (pcm_ops = ) &alsa_pcm_ops,
921 INIT_FIELD (can_be_default = ) 1,
922 INIT_FIELD (max_voices_out = ) INT_MAX,
923 INIT_FIELD (max_voices_in = ) INT_MAX,
924 INIT_FIELD (voice_size_out = ) sizeof (ALSAVoiceOut),
925 INIT_FIELD (voice_size_in = ) sizeof (ALSAVoiceIn)