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 #if QEMU_GNUC_PREREQ(4, 3)
29 #pragma GCC diagnostic ignored "-Waddress"
32 #define AUDIO_CAP "alsa"
33 #include "audio_int.h"
35 typedef struct ALSAVoiceOut
{
41 typedef struct ALSAVoiceIn
{
50 const char *pcm_name_in
;
51 const char *pcm_name_out
;
52 unsigned int buffer_size_in
;
53 unsigned int period_size_in
;
54 unsigned int buffer_size_out
;
55 unsigned int period_size_out
;
56 unsigned int threshold
;
58 int buffer_size_in_overridden
;
59 int period_size_in_overridden
;
61 int buffer_size_out_overridden
;
62 int period_size_out_overridden
;
65 .buffer_size_out
= 1024,
66 .pcm_name_out
= "default",
67 .pcm_name_in
= "default",
70 struct alsa_params_req
{
76 unsigned int buffer_size
;
77 unsigned int period_size
;
80 struct alsa_params_obt
{
85 snd_pcm_uframes_t samples
;
88 static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err
, const char *fmt
, ...)
93 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
96 AUD_log (AUDIO_CAP
, "Reason: %s\n", snd_strerror (err
));
99 static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
108 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
111 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
114 AUD_log (AUDIO_CAP
, "Reason: %s\n", snd_strerror (err
));
117 static void alsa_anal_close (snd_pcm_t
**handlep
)
119 int err
= snd_pcm_close (*handlep
);
121 alsa_logerr (err
, "Failed to close PCM handle %p\n", *handlep
);
126 static int alsa_write (SWVoiceOut
*sw
, void *buf
, int len
)
128 return audio_pcm_sw_write (sw
, buf
, len
);
131 static snd_pcm_format_t
aud_to_alsafmt (audfmt_e fmt
)
135 return SND_PCM_FORMAT_S8
;
138 return SND_PCM_FORMAT_U8
;
141 return SND_PCM_FORMAT_S16_LE
;
144 return SND_PCM_FORMAT_U16_LE
;
147 return SND_PCM_FORMAT_S32_LE
;
150 return SND_PCM_FORMAT_U32_LE
;
153 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
157 return SND_PCM_FORMAT_U8
;
161 static int alsa_to_audfmt (snd_pcm_format_t alsafmt
, audfmt_e
*fmt
,
165 case SND_PCM_FORMAT_S8
:
170 case SND_PCM_FORMAT_U8
:
175 case SND_PCM_FORMAT_S16_LE
:
180 case SND_PCM_FORMAT_U16_LE
:
185 case SND_PCM_FORMAT_S16_BE
:
190 case SND_PCM_FORMAT_U16_BE
:
195 case SND_PCM_FORMAT_S32_LE
:
200 case SND_PCM_FORMAT_U32_LE
:
205 case SND_PCM_FORMAT_S32_BE
:
210 case SND_PCM_FORMAT_U32_BE
:
216 dolog ("Unrecognized audio format %d\n", alsafmt
);
223 static void alsa_dump_info (struct alsa_params_req
*req
,
224 struct alsa_params_obt
*obt
)
226 dolog ("parameter | requested value | obtained value\n");
227 dolog ("format | %10d | %10d\n", req
->fmt
, obt
->fmt
);
228 dolog ("channels | %10d | %10d\n",
229 req
->nchannels
, obt
->nchannels
);
230 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
231 dolog ("============================================\n");
232 dolog ("requested: buffer size %d period size %d\n",
233 req
->buffer_size
, req
->period_size
);
234 dolog ("obtained: samples %ld\n", obt
->samples
);
237 static void alsa_set_threshold (snd_pcm_t
*handle
, snd_pcm_uframes_t threshold
)
240 snd_pcm_sw_params_t
*sw_params
;
242 snd_pcm_sw_params_alloca (&sw_params
);
244 err
= snd_pcm_sw_params_current (handle
, sw_params
);
246 dolog ("Could not fully initialize DAC\n");
247 alsa_logerr (err
, "Failed to get current software parameters\n");
251 err
= snd_pcm_sw_params_set_start_threshold (handle
, sw_params
, threshold
);
253 dolog ("Could not fully initialize DAC\n");
254 alsa_logerr (err
, "Failed to set software threshold to %ld\n",
259 err
= snd_pcm_sw_params (handle
, sw_params
);
261 dolog ("Could not fully initialize DAC\n");
262 alsa_logerr (err
, "Failed to set software parameters\n");
267 static int alsa_open (int in
, struct alsa_params_req
*req
,
268 struct alsa_params_obt
*obt
, snd_pcm_t
**handlep
)
271 snd_pcm_hw_params_t
*hw_params
;
274 unsigned int freq
, nchannels
;
275 const char *pcm_name
= in
? conf
.pcm_name_in
: conf
.pcm_name_out
;
276 snd_pcm_uframes_t obt_buffer_size
;
277 const char *typ
= in
? "ADC" : "DAC";
278 snd_pcm_format_t obtfmt
;
281 nchannels
= req
->nchannels
;
282 size_in_usec
= req
->size_in_usec
;
284 snd_pcm_hw_params_alloca (&hw_params
);
289 in
? SND_PCM_STREAM_CAPTURE
: SND_PCM_STREAM_PLAYBACK
,
293 alsa_logerr2 (err
, typ
, "Failed to open `%s':\n", pcm_name
);
297 err
= snd_pcm_hw_params_any (handle
, hw_params
);
299 alsa_logerr2 (err
, typ
, "Failed to initialize hardware parameters\n");
303 err
= snd_pcm_hw_params_set_access (
306 SND_PCM_ACCESS_RW_INTERLEAVED
309 alsa_logerr2 (err
, typ
, "Failed to set access type\n");
313 err
= snd_pcm_hw_params_set_format (handle
, hw_params
, req
->fmt
);
314 if (err
< 0 && conf
.verbose
) {
315 alsa_logerr2 (err
, typ
, "Failed to set format %d\n", req
->fmt
);
318 err
= snd_pcm_hw_params_set_rate_near (handle
, hw_params
, &freq
, 0);
320 alsa_logerr2 (err
, typ
, "Failed to set frequency %d\n", req
->freq
);
324 err
= snd_pcm_hw_params_set_channels_near (
330 alsa_logerr2 (err
, typ
, "Failed to set number of channels %d\n",
335 if (nchannels
!= 1 && nchannels
!= 2) {
336 alsa_logerr2 (err
, typ
,
337 "Can not handle obtained number of channels %d\n",
342 if (req
->buffer_size
) {
347 unsigned int btime
= req
->buffer_size
;
349 err
= snd_pcm_hw_params_set_buffer_time_near (
358 snd_pcm_uframes_t bsize
= req
->buffer_size
;
360 err
= snd_pcm_hw_params_set_buffer_size_near (
368 alsa_logerr2 (err
, typ
, "Failed to set buffer %s to %d\n",
369 size_in_usec
? "time" : "size", req
->buffer_size
);
373 if ((req
->override_mask
& 2) && (obt
- req
->buffer_size
))
374 dolog ("Requested buffer %s %u was rejected, using %lu\n",
375 size_in_usec
? "time" : "size", req
->buffer_size
, obt
);
378 if (req
->period_size
) {
383 unsigned int ptime
= req
->period_size
;
385 err
= snd_pcm_hw_params_set_period_time_near (
395 snd_pcm_uframes_t psize
= req
->period_size
;
397 err
= snd_pcm_hw_params_set_period_size_near (
407 alsa_logerr2 (err
, typ
, "Failed to set period %s to %d\n",
408 size_in_usec
? "time" : "size", req
->period_size
);
412 if ((req
->override_mask
& 1) && (obt
- req
->period_size
))
413 dolog ("Requested period %s %u was rejected, using %lu\n",
414 size_in_usec
? "time" : "size", req
->period_size
, obt
);
417 err
= snd_pcm_hw_params (handle
, hw_params
);
419 alsa_logerr2 (err
, typ
, "Failed to apply audio parameters\n");
423 err
= snd_pcm_hw_params_get_buffer_size (hw_params
, &obt_buffer_size
);
425 alsa_logerr2 (err
, typ
, "Failed to get buffer size\n");
429 err
= snd_pcm_hw_params_get_format (hw_params
, &obtfmt
);
431 alsa_logerr2 (err
, typ
, "Failed to get format\n");
435 if (alsa_to_audfmt (obtfmt
, &obt
->fmt
, &obt
->endianness
)) {
436 dolog ("Invalid format was returned %d\n", obtfmt
);
440 err
= snd_pcm_prepare (handle
);
442 alsa_logerr2 (err
, typ
, "Could not prepare handle %p\n", handle
);
446 if (!in
&& conf
.threshold
) {
447 snd_pcm_uframes_t threshold
;
450 bytes_per_sec
= freq
<< (nchannels
== 2);
468 threshold
= (conf
.threshold
* bytes_per_sec
) / 1000;
469 alsa_set_threshold (handle
, threshold
);
472 obt
->nchannels
= nchannels
;
474 obt
->samples
= obt_buffer_size
;
479 (obt
->fmt
!= req
->fmt
||
480 obt
->nchannels
!= req
->nchannels
||
481 obt
->freq
!= req
->freq
)) {
482 dolog ("Audio paramters for %s\n", typ
);
483 alsa_dump_info (req
, obt
);
487 alsa_dump_info (req
, obt
);
492 alsa_anal_close (&handle
);
496 static int alsa_recover (snd_pcm_t
*handle
)
498 int err
= snd_pcm_prepare (handle
);
500 alsa_logerr (err
, "Failed to prepare handle %p\n", handle
);
506 static int alsa_resume (snd_pcm_t
*handle
)
508 int err
= snd_pcm_resume (handle
);
510 alsa_logerr (err
, "Failed to resume handle %p\n", handle
);
516 static snd_pcm_sframes_t
alsa_get_avail (snd_pcm_t
*handle
)
518 snd_pcm_sframes_t avail
;
520 avail
= snd_pcm_avail_update (handle
);
522 if (avail
== -EPIPE
) {
523 if (!alsa_recover (handle
)) {
524 avail
= snd_pcm_avail_update (handle
);
530 "Could not obtain number of available frames\n");
538 static int alsa_run_out (HWVoiceOut
*hw
)
540 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
541 int rpos
, live
, decr
;
544 struct st_sample
*src
;
545 snd_pcm_sframes_t avail
;
547 live
= audio_pcm_hw_get_live_out (hw
);
552 avail
= alsa_get_avail (alsa
->handle
);
554 dolog ("Could not get number of available playback frames\n");
558 decr
= audio_MIN (live
, avail
);
562 int left_till_end_samples
= hw
->samples
- rpos
;
563 int len
= 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
, len
);
572 written
= snd_pcm_writei (alsa
->handle
, dst
, len
);
578 dolog ("Failed to write %d frames (wrote zero)\n", len
);
583 if (alsa_recover (alsa
->handle
)) {
584 alsa_logerr (written
, "Failed to write %d frames\n",
589 dolog ("Recovering from playback xrun\n");
594 /* stream is suspended and waiting for an
595 application recovery */
596 if (alsa_resume (alsa
->handle
)) {
597 alsa_logerr (written
, "Failed to write %d frames\n",
602 dolog ("Resuming suspended output stream\n");
610 alsa_logerr (written
, "Failed to write %d frames to %p\n",
616 rpos
= (rpos
+ written
) % hw
->samples
;
619 dst
= advance (dst
, written
<< hw
->info
.shift
);
629 static void alsa_fini_out (HWVoiceOut
*hw
)
631 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
633 ldebug ("alsa_fini\n");
634 alsa_anal_close (&alsa
->handle
);
637 qemu_free (alsa
->pcm_buf
);
638 alsa
->pcm_buf
= NULL
;
642 static int alsa_init_out (HWVoiceOut
*hw
, struct audsettings
*as
)
644 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
645 struct alsa_params_req req
;
646 struct alsa_params_obt obt
;
648 struct audsettings obt_as
;
650 req
.fmt
= aud_to_alsafmt (as
->fmt
);
652 req
.nchannels
= as
->nchannels
;
653 req
.period_size
= conf
.period_size_out
;
654 req
.buffer_size
= conf
.buffer_size_out
;
655 req
.size_in_usec
= conf
.size_in_usec_out
;
657 (conf
.period_size_out_overridden
? 1 : 0) |
658 (conf
.buffer_size_out_overridden
? 2 : 0);
660 if (alsa_open (0, &req
, &obt
, &handle
)) {
664 obt_as
.freq
= obt
.freq
;
665 obt_as
.nchannels
= obt
.nchannels
;
666 obt_as
.fmt
= obt
.fmt
;
667 obt_as
.endianness
= obt
.endianness
;
669 audio_pcm_init_info (&hw
->info
, &obt_as
);
670 hw
->samples
= obt
.samples
;
672 alsa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, obt
.samples
, 1 << hw
->info
.shift
);
673 if (!alsa
->pcm_buf
) {
674 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
675 hw
->samples
, 1 << hw
->info
.shift
);
676 alsa_anal_close (&handle
);
680 alsa
->handle
= handle
;
684 static int alsa_voice_ctl (snd_pcm_t
*handle
, const char *typ
, int pause
)
689 err
= snd_pcm_drop (handle
);
691 alsa_logerr (err
, "Could not stop %s\n", typ
);
696 err
= snd_pcm_prepare (handle
);
698 alsa_logerr (err
, "Could not prepare handle for %s\n", typ
);
706 static int alsa_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
708 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
712 ldebug ("enabling voice\n");
713 return alsa_voice_ctl (alsa
->handle
, "playback", 0);
716 ldebug ("disabling voice\n");
717 return alsa_voice_ctl (alsa
->handle
, "playback", 1);
723 static int alsa_init_in (HWVoiceIn
*hw
, struct audsettings
*as
)
725 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
726 struct alsa_params_req req
;
727 struct alsa_params_obt obt
;
729 struct audsettings obt_as
;
731 req
.fmt
= aud_to_alsafmt (as
->fmt
);
733 req
.nchannels
= as
->nchannels
;
734 req
.period_size
= conf
.period_size_in
;
735 req
.buffer_size
= conf
.buffer_size_in
;
736 req
.size_in_usec
= conf
.size_in_usec_in
;
738 (conf
.period_size_in_overridden
? 1 : 0) |
739 (conf
.buffer_size_in_overridden
? 2 : 0);
741 if (alsa_open (1, &req
, &obt
, &handle
)) {
745 obt_as
.freq
= obt
.freq
;
746 obt_as
.nchannels
= obt
.nchannels
;
747 obt_as
.fmt
= obt
.fmt
;
748 obt_as
.endianness
= obt
.endianness
;
750 audio_pcm_init_info (&hw
->info
, &obt_as
);
751 hw
->samples
= obt
.samples
;
753 alsa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
754 if (!alsa
->pcm_buf
) {
755 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
756 hw
->samples
, 1 << hw
->info
.shift
);
757 alsa_anal_close (&handle
);
761 alsa
->handle
= handle
;
765 static void alsa_fini_in (HWVoiceIn
*hw
)
767 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
769 alsa_anal_close (&alsa
->handle
);
772 qemu_free (alsa
->pcm_buf
);
773 alsa
->pcm_buf
= NULL
;
777 static int alsa_run_in (HWVoiceIn
*hw
)
779 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
780 int hwshift
= hw
->info
.shift
;
782 int live
= audio_pcm_hw_get_live_in (hw
);
783 int dead
= hw
->samples
- live
;
789 { .add
= hw
->wpos
, .len
= 0 },
790 { .add
= 0, .len
= 0 }
792 snd_pcm_sframes_t avail
;
793 snd_pcm_uframes_t read_samples
= 0;
799 avail
= alsa_get_avail (alsa
->handle
);
801 dolog ("Could not get number of captured frames\n");
806 snd_pcm_state_t state
;
808 state
= snd_pcm_state (alsa
->handle
);
810 case SND_PCM_STATE_PREPARED
:
813 case SND_PCM_STATE_SUSPENDED
:
814 /* stream is suspended and waiting for an application recovery */
815 if (alsa_resume (alsa
->handle
)) {
816 dolog ("Failed to resume suspended input stream\n");
820 dolog ("Resuming suspended input stream\n");
825 dolog ("No frames available and ALSA state is %d\n", state
);
831 decr
= audio_MIN (dead
, avail
);
836 if (hw
->wpos
+ decr
> hw
->samples
) {
837 bufs
[0].len
= (hw
->samples
- hw
->wpos
);
838 bufs
[1].len
= (decr
- (hw
->samples
- hw
->wpos
));
844 for (i
= 0; i
< 2; ++i
) {
846 struct st_sample
*dst
;
847 snd_pcm_sframes_t nread
;
848 snd_pcm_uframes_t len
;
852 src
= advance (alsa
->pcm_buf
, bufs
[i
].add
<< hwshift
);
853 dst
= hw
->conv_buf
+ bufs
[i
].add
;
856 nread
= snd_pcm_readi (alsa
->handle
, src
, len
);
862 dolog ("Failed to read %ld frames (read zero)\n", len
);
867 if (alsa_recover (alsa
->handle
)) {
868 alsa_logerr (nread
, "Failed to read %ld frames\n", len
);
872 dolog ("Recovering from capture xrun\n");
882 "Failed to read %ld frames from %p\n",
890 hw
->conv (dst
, src
, nread
, &nominal_volume
);
892 src
= advance (src
, nread
<< hwshift
);
895 read_samples
+= nread
;
901 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
905 static int alsa_read (SWVoiceIn
*sw
, void *buf
, int size
)
907 return audio_pcm_sw_read (sw
, buf
, size
);
910 static int alsa_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
912 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
916 ldebug ("enabling voice\n");
917 return alsa_voice_ctl (alsa
->handle
, "capture", 0);
920 ldebug ("disabling voice\n");
921 return alsa_voice_ctl (alsa
->handle
, "capture", 1);
927 static void *alsa_audio_init (void)
932 static void alsa_audio_fini (void *opaque
)
937 static struct audio_option alsa_options
[] = {
939 .name
= "DAC_SIZE_IN_USEC",
941 .valp
= &conf
.size_in_usec_out
,
942 .descr
= "DAC period/buffer size in microseconds (otherwise in frames)"
945 .name
= "DAC_PERIOD_SIZE",
947 .valp
= &conf
.period_size_out
,
948 .descr
= "DAC period size (0 to go with system default)",
949 .overriddenp
= &conf
.period_size_out_overridden
952 .name
= "DAC_BUFFER_SIZE",
954 .valp
= &conf
.buffer_size_out
,
955 .descr
= "DAC buffer size (0 to go with system default)",
956 .overriddenp
= &conf
.buffer_size_out_overridden
959 .name
= "ADC_SIZE_IN_USEC",
961 .valp
= &conf
.size_in_usec_in
,
963 "ADC period/buffer size in microseconds (otherwise in frames)"
966 .name
= "ADC_PERIOD_SIZE",
968 .valp
= &conf
.period_size_in
,
969 .descr
= "ADC period size (0 to go with system default)",
970 .overriddenp
= &conf
.period_size_in_overridden
973 .name
= "ADC_BUFFER_SIZE",
975 .valp
= &conf
.buffer_size_in
,
976 .descr
= "ADC buffer size (0 to go with system default)",
977 .overriddenp
= &conf
.buffer_size_in_overridden
982 .valp
= &conf
.threshold
,
983 .descr
= "(undocumented)"
988 .valp
= &conf
.pcm_name_out
,
989 .descr
= "DAC device name (for instance dmix)"
994 .valp
= &conf
.pcm_name_in
,
995 .descr
= "ADC device name"
1000 .valp
= &conf
.verbose
,
1001 .descr
= "Behave in a more verbose way"
1003 { /* End of list */ }
1006 static struct audio_pcm_ops alsa_pcm_ops
= {
1007 .init_out
= alsa_init_out
,
1008 .fini_out
= alsa_fini_out
,
1009 .run_out
= alsa_run_out
,
1010 .write
= alsa_write
,
1011 .ctl_out
= alsa_ctl_out
,
1013 .init_in
= alsa_init_in
,
1014 .fini_in
= alsa_fini_in
,
1015 .run_in
= alsa_run_in
,
1017 .ctl_in
= alsa_ctl_in
1020 struct audio_driver alsa_audio_driver
= {
1022 .descr
= "ALSA http://www.alsa-project.org",
1023 .options
= alsa_options
,
1024 .init
= alsa_audio_init
,
1025 .fini
= alsa_audio_fini
,
1026 .pcm_ops
= &alsa_pcm_ops
,
1027 .can_be_default
= 1,
1028 .max_voices_out
= INT_MAX
,
1029 .max_voices_in
= INT_MAX
,
1030 .voice_size_out
= sizeof (ALSAVoiceOut
),
1031 .voice_size_in
= sizeof (ALSAVoiceIn
)