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 .pcm_name_out
= "default",
62 .pcm_name_in
= "default",
65 struct alsa_params_req
{
70 unsigned int buffer_size
;
71 unsigned int period_size
;
74 struct alsa_params_obt
{
79 snd_pcm_uframes_t samples
;
82 static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err
, const char *fmt
, ...)
87 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
90 AUD_log (AUDIO_CAP
, "Reason: %s\n", snd_strerror (err
));
93 static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
102 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
105 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
108 AUD_log (AUDIO_CAP
, "Reason: %s\n", snd_strerror (err
));
111 static void alsa_anal_close (snd_pcm_t
**handlep
)
113 int err
= snd_pcm_close (*handlep
);
115 alsa_logerr (err
, "Failed to close PCM handle %p\n", *handlep
);
120 static int alsa_write (SWVoiceOut
*sw
, void *buf
, int len
)
122 return audio_pcm_sw_write (sw
, buf
, len
);
125 static snd_pcm_format_t
aud_to_alsafmt (audfmt_e fmt
)
129 return SND_PCM_FORMAT_S8
;
132 return SND_PCM_FORMAT_U8
;
135 return SND_PCM_FORMAT_S16_LE
;
138 return SND_PCM_FORMAT_U16_LE
;
141 return SND_PCM_FORMAT_S32_LE
;
144 return SND_PCM_FORMAT_U32_LE
;
147 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
151 return SND_PCM_FORMAT_U8
;
155 static int alsa_to_audfmt (snd_pcm_format_t alsafmt
, audfmt_e
*fmt
,
159 case SND_PCM_FORMAT_S8
:
164 case SND_PCM_FORMAT_U8
:
169 case SND_PCM_FORMAT_S16_LE
:
174 case SND_PCM_FORMAT_U16_LE
:
179 case SND_PCM_FORMAT_S16_BE
:
184 case SND_PCM_FORMAT_U16_BE
:
189 case SND_PCM_FORMAT_S32_LE
:
194 case SND_PCM_FORMAT_U32_LE
:
199 case SND_PCM_FORMAT_S32_BE
:
204 case SND_PCM_FORMAT_U32_BE
:
210 dolog ("Unrecognized audio format %d\n", alsafmt
);
217 static void alsa_dump_info (struct alsa_params_req
*req
,
218 struct alsa_params_obt
*obt
)
220 dolog ("parameter | requested value | obtained value\n");
221 dolog ("format | %10d | %10d\n", req
->fmt
, obt
->fmt
);
222 dolog ("channels | %10d | %10d\n",
223 req
->nchannels
, obt
->nchannels
);
224 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
225 dolog ("============================================\n");
226 dolog ("requested: buffer size %d period size %d\n",
227 req
->buffer_size
, req
->period_size
);
228 dolog ("obtained: samples %ld\n", obt
->samples
);
231 static void alsa_set_threshold (snd_pcm_t
*handle
, snd_pcm_uframes_t threshold
)
234 snd_pcm_sw_params_t
*sw_params
;
236 snd_pcm_sw_params_alloca (&sw_params
);
238 err
= snd_pcm_sw_params_current (handle
, sw_params
);
240 dolog ("Could not fully initialize DAC\n");
241 alsa_logerr (err
, "Failed to get current software parameters\n");
245 err
= snd_pcm_sw_params_set_start_threshold (handle
, sw_params
, threshold
);
247 dolog ("Could not fully initialize DAC\n");
248 alsa_logerr (err
, "Failed to set software threshold to %ld\n",
253 err
= snd_pcm_sw_params (handle
, sw_params
);
255 dolog ("Could not fully initialize DAC\n");
256 alsa_logerr (err
, "Failed to set software parameters\n");
261 static int alsa_open (int in
, struct alsa_params_req
*req
,
262 struct alsa_params_obt
*obt
, snd_pcm_t
**handlep
)
265 snd_pcm_hw_params_t
*hw_params
;
268 unsigned int freq
, nchannels
;
269 const char *pcm_name
= in
? conf
.pcm_name_in
: conf
.pcm_name_out
;
270 snd_pcm_uframes_t obt_buffer_size
;
271 const char *typ
= in
? "ADC" : "DAC";
272 snd_pcm_format_t obtfmt
;
275 nchannels
= req
->nchannels
;
276 size_in_usec
= req
->size_in_usec
;
278 snd_pcm_hw_params_alloca (&hw_params
);
283 in
? SND_PCM_STREAM_CAPTURE
: SND_PCM_STREAM_PLAYBACK
,
287 alsa_logerr2 (err
, typ
, "Failed to open `%s':\n", pcm_name
);
291 err
= snd_pcm_hw_params_any (handle
, hw_params
);
293 alsa_logerr2 (err
, typ
, "Failed to initialize hardware parameters\n");
297 err
= snd_pcm_hw_params_set_access (
300 SND_PCM_ACCESS_RW_INTERLEAVED
303 alsa_logerr2 (err
, typ
, "Failed to set access type\n");
307 err
= snd_pcm_hw_params_set_format (handle
, hw_params
, req
->fmt
);
308 if (err
< 0 && conf
.verbose
) {
309 alsa_logerr2 (err
, typ
, "Failed to set format %d\n", req
->fmt
);
312 err
= snd_pcm_hw_params_set_rate_near (handle
, hw_params
, &freq
, 0);
314 alsa_logerr2 (err
, typ
, "Failed to set frequency %d\n", req
->freq
);
318 err
= snd_pcm_hw_params_set_channels_near (
324 alsa_logerr2 (err
, typ
, "Failed to set number of channels %d\n",
329 if (nchannels
!= 1 && nchannels
!= 2) {
330 alsa_logerr2 (err
, typ
,
331 "Can not handle obtained number of channels %d\n",
336 if (req
->buffer_size
) {
341 unsigned int btime
= req
->buffer_size
;
343 err
= snd_pcm_hw_params_set_buffer_time_near (
352 snd_pcm_uframes_t bsize
= req
->buffer_size
;
354 err
= snd_pcm_hw_params_set_buffer_size_near (
362 alsa_logerr2 (err
, typ
, "Failed to set buffer %s to %d\n",
363 size_in_usec
? "time" : "size", req
->buffer_size
);
367 if (obt
- req
->buffer_size
)
368 dolog ("Requested buffer %s %u was rejected, using %lu\n",
369 size_in_usec
? "time" : "size", req
->buffer_size
, obt
);
372 if (req
->period_size
) {
377 unsigned int ptime
= req
->period_size
;
379 err
= snd_pcm_hw_params_set_period_time_near (
389 snd_pcm_uframes_t psize
= req
->period_size
;
391 err
= snd_pcm_hw_params_set_period_size_near (
401 alsa_logerr2 (err
, typ
, "Failed to set period %s to %d\n",
402 size_in_usec
? "time" : "size", req
->period_size
);
406 if (obt
- req
->period_size
)
407 dolog ("Requested period %s %u was rejected, using %lu\n",
408 size_in_usec
? "time" : "size", req
->period_size
, obt
);
411 err
= snd_pcm_hw_params (handle
, hw_params
);
413 alsa_logerr2 (err
, typ
, "Failed to apply audio parameters\n");
417 err
= snd_pcm_hw_params_get_buffer_size (hw_params
, &obt_buffer_size
);
419 alsa_logerr2 (err
, typ
, "Failed to get buffer size\n");
423 err
= snd_pcm_hw_params_get_format (hw_params
, &obtfmt
);
425 alsa_logerr2 (err
, typ
, "Failed to get format\n");
429 if (alsa_to_audfmt (obtfmt
, &obt
->fmt
, &obt
->endianness
)) {
430 dolog ("Invalid format was returned %d\n", obtfmt
);
434 err
= snd_pcm_prepare (handle
);
436 alsa_logerr2 (err
, typ
, "Could not prepare handle %p\n", handle
);
440 if (!in
&& conf
.threshold
) {
441 snd_pcm_uframes_t threshold
;
444 bytes_per_sec
= freq
<< (nchannels
== 2);
462 threshold
= (conf
.threshold
* bytes_per_sec
) / 1000;
463 alsa_set_threshold (handle
, threshold
);
466 obt
->nchannels
= nchannels
;
468 obt
->samples
= obt_buffer_size
;
473 (obt
->fmt
!= req
->fmt
||
474 obt
->nchannels
!= req
->nchannels
||
475 obt
->freq
!= req
->freq
)) {
476 dolog ("Audio paramters for %s\n", typ
);
477 alsa_dump_info (req
, obt
);
481 alsa_dump_info (req
, obt
);
486 alsa_anal_close (&handle
);
490 static int alsa_recover (snd_pcm_t
*handle
)
492 int err
= snd_pcm_prepare (handle
);
494 alsa_logerr (err
, "Failed to prepare handle %p\n", handle
);
500 static snd_pcm_sframes_t
alsa_get_avail (snd_pcm_t
*handle
)
502 snd_pcm_sframes_t avail
;
504 avail
= snd_pcm_avail_update (handle
);
506 if (avail
== -EPIPE
) {
507 if (!alsa_recover (handle
)) {
508 avail
= snd_pcm_avail_update (handle
);
514 "Could not obtain number of available frames\n");
522 static int alsa_run_out (HWVoiceOut
*hw
)
524 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
525 int rpos
, live
, decr
;
529 snd_pcm_sframes_t avail
;
531 live
= audio_pcm_hw_get_live_out (hw
);
536 avail
= alsa_get_avail (alsa
->handle
);
538 dolog ("Could not get number of available playback frames\n");
542 decr
= audio_MIN (live
, avail
);
546 int left_till_end_samples
= hw
->samples
- rpos
;
547 int len
= audio_MIN (samples
, left_till_end_samples
);
548 snd_pcm_sframes_t written
;
550 src
= hw
->mix_buf
+ rpos
;
551 dst
= advance (alsa
->pcm_buf
, rpos
<< hw
->info
.shift
);
553 hw
->clip (dst
, src
, len
);
556 written
= snd_pcm_writei (alsa
->handle
, dst
, len
);
562 dolog ("Failed to write %d frames (wrote zero)\n", len
);
567 if (alsa_recover (alsa
->handle
)) {
568 alsa_logerr (written
, "Failed to write %d frames\n",
573 dolog ("Recovering from playback xrun\n");
581 alsa_logerr (written
, "Failed to write %d frames to %p\n",
587 rpos
= (rpos
+ written
) % hw
->samples
;
590 dst
= advance (dst
, written
<< hw
->info
.shift
);
600 static void alsa_fini_out (HWVoiceOut
*hw
)
602 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
604 ldebug ("alsa_fini\n");
605 alsa_anal_close (&alsa
->handle
);
608 qemu_free (alsa
->pcm_buf
);
609 alsa
->pcm_buf
= NULL
;
613 static int alsa_init_out (HWVoiceOut
*hw
, audsettings_t
*as
)
615 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
616 struct alsa_params_req req
;
617 struct alsa_params_obt obt
;
619 audsettings_t obt_as
;
621 req
.fmt
= aud_to_alsafmt (as
->fmt
);
623 req
.nchannels
= as
->nchannels
;
624 req
.period_size
= conf
.period_size_out
;
625 req
.buffer_size
= conf
.buffer_size_out
;
626 req
.size_in_usec
= conf
.size_in_usec_out
;
628 if (alsa_open (0, &req
, &obt
, &handle
)) {
632 obt_as
.freq
= obt
.freq
;
633 obt_as
.nchannels
= obt
.nchannels
;
634 obt_as
.fmt
= obt
.fmt
;
635 obt_as
.endianness
= obt
.endianness
;
637 audio_pcm_init_info (&hw
->info
, &obt_as
);
638 hw
->samples
= obt
.samples
;
640 alsa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, obt
.samples
, 1 << hw
->info
.shift
);
641 if (!alsa
->pcm_buf
) {
642 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
643 hw
->samples
, 1 << hw
->info
.shift
);
644 alsa_anal_close (&handle
);
648 alsa
->handle
= handle
;
652 static int alsa_voice_ctl (snd_pcm_t
*handle
, const char *typ
, int pause
)
657 err
= snd_pcm_drop (handle
);
659 alsa_logerr (err
, "Could not stop %s\n", typ
);
664 err
= snd_pcm_prepare (handle
);
666 alsa_logerr (err
, "Could not prepare handle for %s\n", typ
);
674 static int alsa_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
676 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
680 ldebug ("enabling voice\n");
681 return alsa_voice_ctl (alsa
->handle
, "playback", 0);
684 ldebug ("disabling voice\n");
685 return alsa_voice_ctl (alsa
->handle
, "playback", 1);
691 static int alsa_init_in (HWVoiceIn
*hw
, audsettings_t
*as
)
693 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
694 struct alsa_params_req req
;
695 struct alsa_params_obt obt
;
697 audsettings_t obt_as
;
699 req
.fmt
= aud_to_alsafmt (as
->fmt
);
701 req
.nchannels
= as
->nchannels
;
702 req
.period_size
= conf
.period_size_in
;
703 req
.buffer_size
= conf
.buffer_size_in
;
704 req
.size_in_usec
= conf
.size_in_usec_in
;
706 if (alsa_open (1, &req
, &obt
, &handle
)) {
710 obt_as
.freq
= obt
.freq
;
711 obt_as
.nchannels
= obt
.nchannels
;
712 obt_as
.fmt
= obt
.fmt
;
713 obt_as
.endianness
= obt
.endianness
;
715 audio_pcm_init_info (&hw
->info
, &obt_as
);
716 hw
->samples
= obt
.samples
;
718 alsa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
719 if (!alsa
->pcm_buf
) {
720 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
721 hw
->samples
, 1 << hw
->info
.shift
);
722 alsa_anal_close (&handle
);
726 alsa
->handle
= handle
;
730 static void alsa_fini_in (HWVoiceIn
*hw
)
732 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
734 alsa_anal_close (&alsa
->handle
);
737 qemu_free (alsa
->pcm_buf
);
738 alsa
->pcm_buf
= NULL
;
742 static int alsa_run_in (HWVoiceIn
*hw
)
744 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
745 int hwshift
= hw
->info
.shift
;
747 int live
= audio_pcm_hw_get_live_in (hw
);
748 int dead
= hw
->samples
- live
;
757 snd_pcm_sframes_t avail
;
758 snd_pcm_uframes_t read_samples
= 0;
764 avail
= alsa_get_avail (alsa
->handle
);
766 dolog ("Could not get number of captured frames\n");
770 if (!avail
&& (snd_pcm_state (alsa
->handle
) == SND_PCM_STATE_PREPARED
)) {
774 decr
= audio_MIN (dead
, avail
);
779 if (hw
->wpos
+ decr
> hw
->samples
) {
780 bufs
[0].len
= (hw
->samples
- hw
->wpos
);
781 bufs
[1].len
= (decr
- (hw
->samples
- hw
->wpos
));
787 for (i
= 0; i
< 2; ++i
) {
790 snd_pcm_sframes_t nread
;
791 snd_pcm_uframes_t len
;
795 src
= advance (alsa
->pcm_buf
, bufs
[i
].add
<< hwshift
);
796 dst
= hw
->conv_buf
+ bufs
[i
].add
;
799 nread
= snd_pcm_readi (alsa
->handle
, src
, len
);
805 dolog ("Failed to read %ld frames (read zero)\n", len
);
810 if (alsa_recover (alsa
->handle
)) {
811 alsa_logerr (nread
, "Failed to read %ld frames\n", len
);
815 dolog ("Recovering from capture xrun\n");
825 "Failed to read %ld frames from %p\n",
833 hw
->conv (dst
, src
, nread
, &nominal_volume
);
835 src
= advance (src
, nread
<< hwshift
);
838 read_samples
+= nread
;
844 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
848 static int alsa_read (SWVoiceIn
*sw
, void *buf
, int size
)
850 return audio_pcm_sw_read (sw
, buf
, size
);
853 static int alsa_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
855 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
859 ldebug ("enabling voice\n");
860 return alsa_voice_ctl (alsa
->handle
, "capture", 0);
863 ldebug ("disabling voice\n");
864 return alsa_voice_ctl (alsa
->handle
, "capture", 1);
870 static void *alsa_audio_init (void)
875 static void alsa_audio_fini (void *opaque
)
880 static struct audio_option alsa_options
[] = {
881 {"DAC_SIZE_IN_USEC", AUD_OPT_BOOL
, &conf
.size_in_usec_out
,
882 "DAC period/buffer size in microseconds (otherwise in frames)", NULL
, 0},
883 {"DAC_PERIOD_SIZE", AUD_OPT_INT
, &conf
.period_size_out
,
884 "DAC period size (0 to go with system default)",
885 &conf
.period_size_out_overridden
, 0},
886 {"DAC_BUFFER_SIZE", AUD_OPT_INT
, &conf
.buffer_size_out
,
887 "DAC buffer size (0 to go with system default)",
888 &conf
.buffer_size_out_overridden
, 0},
890 {"ADC_SIZE_IN_USEC", AUD_OPT_BOOL
, &conf
.size_in_usec_in
,
891 "ADC period/buffer size in microseconds (otherwise in frames)", NULL
, 0},
892 {"ADC_PERIOD_SIZE", AUD_OPT_INT
, &conf
.period_size_in
,
893 "ADC period size (0 to go with system default)",
894 &conf
.period_size_in_overridden
, 0},
895 {"ADC_BUFFER_SIZE", AUD_OPT_INT
, &conf
.buffer_size_in
,
896 "ADC buffer size (0 to go with system default)",
897 &conf
.buffer_size_in_overridden
, 0},
899 {"THRESHOLD", AUD_OPT_INT
, &conf
.threshold
,
900 "(undocumented)", NULL
, 0},
902 {"DAC_DEV", AUD_OPT_STR
, &conf
.pcm_name_out
,
903 "DAC device name (for instance dmix)", NULL
, 0},
905 {"ADC_DEV", AUD_OPT_STR
, &conf
.pcm_name_in
,
906 "ADC device name", NULL
, 0},
908 {"VERBOSE", AUD_OPT_BOOL
, &conf
.verbose
,
909 "Behave in a more verbose way", NULL
, 0},
911 {NULL
, 0, NULL
, NULL
, NULL
, 0}
914 static struct audio_pcm_ops alsa_pcm_ops
= {
928 struct audio_driver alsa_audio_driver
= {
929 INIT_FIELD (name
= ) "alsa",
930 INIT_FIELD (descr
= ) "ALSA http://www.alsa-project.org",
931 INIT_FIELD (options
= ) alsa_options
,
932 INIT_FIELD (init
= ) alsa_audio_init
,
933 INIT_FIELD (fini
= ) alsa_audio_fini
,
934 INIT_FIELD (pcm_ops
= ) &alsa_pcm_ops
,
935 INIT_FIELD (can_be_default
= ) 1,
936 INIT_FIELD (max_voices_out
= ) INT_MAX
,
937 INIT_FIELD (max_voices_in
= ) INT_MAX
,
938 INIT_FIELD (voice_size_out
= ) sizeof (ALSAVoiceOut
),
939 INIT_FIELD (voice_size_in
= ) sizeof (ALSAVoiceIn
)