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 "qemu/osdep.h"
25 #include <alsa/asoundlib.h>
26 #include "qemu-common.h"
27 #include "qemu/main-loop.h"
31 #if QEMU_GNUC_PREREQ(4, 3)
32 #pragma GCC diagnostic ignored "-Waddress"
35 #define AUDIO_CAP "alsa"
36 #include "audio_int.h"
38 typedef struct ALSAConf
{
41 const char *pcm_name_in
;
42 const char *pcm_name_out
;
43 unsigned int buffer_size_in
;
44 unsigned int period_size_in
;
45 unsigned int buffer_size_out
;
46 unsigned int period_size_out
;
47 unsigned int threshold
;
49 int buffer_size_in_overridden
;
50 int period_size_in_overridden
;
52 int buffer_size_out_overridden
;
53 int period_size_out_overridden
;
64 typedef struct ALSAVoiceOut
{
70 struct pollhlp pollhlp
;
73 typedef struct ALSAVoiceIn
{
77 struct pollhlp pollhlp
;
80 struct alsa_params_req
{
86 unsigned int buffer_size
;
87 unsigned int period_size
;
90 struct alsa_params_obt
{
95 snd_pcm_uframes_t samples
;
98 static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err
, const char *fmt
, ...)
103 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
106 AUD_log (AUDIO_CAP
, "Reason: %s\n", snd_strerror (err
));
109 static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
118 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
121 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
124 AUD_log (AUDIO_CAP
, "Reason: %s\n", snd_strerror (err
));
127 static void alsa_fini_poll (struct pollhlp
*hlp
)
130 struct pollfd
*pfds
= hlp
->pfds
;
133 for (i
= 0; i
< hlp
->count
; ++i
) {
134 qemu_set_fd_handler (pfds
[i
].fd
, NULL
, NULL
, NULL
);
143 static void alsa_anal_close1 (snd_pcm_t
**handlep
)
145 int err
= snd_pcm_close (*handlep
);
147 alsa_logerr (err
, "Failed to close PCM handle %p\n", *handlep
);
152 static void alsa_anal_close (snd_pcm_t
**handlep
, struct pollhlp
*hlp
)
154 alsa_fini_poll (hlp
);
155 alsa_anal_close1 (handlep
);
158 static int alsa_recover (snd_pcm_t
*handle
)
160 int err
= snd_pcm_prepare (handle
);
162 alsa_logerr (err
, "Failed to prepare handle %p\n", handle
);
168 static int alsa_resume (snd_pcm_t
*handle
)
170 int err
= snd_pcm_resume (handle
);
172 alsa_logerr (err
, "Failed to resume handle %p\n", handle
);
178 static void alsa_poll_handler (void *opaque
)
181 snd_pcm_state_t state
;
182 struct pollhlp
*hlp
= opaque
;
183 unsigned short revents
;
185 count
= poll (hlp
->pfds
, hlp
->count
, 0);
187 dolog ("alsa_poll_handler: poll %s\n", strerror (errno
));
195 /* XXX: ALSA example uses initial count, not the one returned by
197 err
= snd_pcm_poll_descriptors_revents (hlp
->handle
, hlp
->pfds
,
198 hlp
->count
, &revents
);
200 alsa_logerr (err
, "snd_pcm_poll_descriptors_revents");
204 if (!(revents
& hlp
->mask
)) {
205 trace_alsa_revents(revents
);
209 state
= snd_pcm_state (hlp
->handle
);
211 case SND_PCM_STATE_SETUP
:
212 alsa_recover (hlp
->handle
);
215 case SND_PCM_STATE_XRUN
:
216 alsa_recover (hlp
->handle
);
219 case SND_PCM_STATE_SUSPENDED
:
220 alsa_resume (hlp
->handle
);
223 case SND_PCM_STATE_PREPARED
:
224 audio_run ("alsa run (prepared)");
227 case SND_PCM_STATE_RUNNING
:
228 audio_run ("alsa run (running)");
232 dolog ("Unexpected state %d\n", state
);
236 static int alsa_poll_helper (snd_pcm_t
*handle
, struct pollhlp
*hlp
, int mask
)
241 count
= snd_pcm_poll_descriptors_count (handle
);
243 dolog ("Could not initialize poll mode\n"
244 "Invalid number of poll descriptors %d\n", count
);
248 pfds
= audio_calloc ("alsa_poll_helper", count
, sizeof (*pfds
));
250 dolog ("Could not initialize poll mode\n");
254 err
= snd_pcm_poll_descriptors (handle
, pfds
, count
);
256 alsa_logerr (err
, "Could not initialize poll mode\n"
257 "Could not obtain poll descriptors\n");
262 for (i
= 0; i
< count
; ++i
) {
263 if (pfds
[i
].events
& POLLIN
) {
264 qemu_set_fd_handler (pfds
[i
].fd
, alsa_poll_handler
, NULL
, hlp
);
266 if (pfds
[i
].events
& POLLOUT
) {
267 trace_alsa_pollout(i
, pfds
[i
].fd
);
268 qemu_set_fd_handler (pfds
[i
].fd
, NULL
, alsa_poll_handler
, hlp
);
270 trace_alsa_set_handler(pfds
[i
].events
, i
, pfds
[i
].fd
, err
);
275 hlp
->handle
= handle
;
280 static int alsa_poll_out (HWVoiceOut
*hw
)
282 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
284 return alsa_poll_helper (alsa
->handle
, &alsa
->pollhlp
, POLLOUT
);
287 static int alsa_poll_in (HWVoiceIn
*hw
)
289 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
291 return alsa_poll_helper (alsa
->handle
, &alsa
->pollhlp
, POLLIN
);
294 static int alsa_write (SWVoiceOut
*sw
, void *buf
, int len
)
296 return audio_pcm_sw_write (sw
, buf
, len
);
299 static snd_pcm_format_t
aud_to_alsafmt (audfmt_e fmt
, int endianness
)
303 return SND_PCM_FORMAT_S8
;
306 return SND_PCM_FORMAT_U8
;
310 return SND_PCM_FORMAT_S16_BE
;
313 return SND_PCM_FORMAT_S16_LE
;
318 return SND_PCM_FORMAT_U16_BE
;
321 return SND_PCM_FORMAT_U16_LE
;
326 return SND_PCM_FORMAT_S32_BE
;
329 return SND_PCM_FORMAT_S32_LE
;
334 return SND_PCM_FORMAT_U32_BE
;
337 return SND_PCM_FORMAT_U32_LE
;
341 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
345 return SND_PCM_FORMAT_U8
;
349 static int alsa_to_audfmt (snd_pcm_format_t alsafmt
, audfmt_e
*fmt
,
353 case SND_PCM_FORMAT_S8
:
358 case SND_PCM_FORMAT_U8
:
363 case SND_PCM_FORMAT_S16_LE
:
368 case SND_PCM_FORMAT_U16_LE
:
373 case SND_PCM_FORMAT_S16_BE
:
378 case SND_PCM_FORMAT_U16_BE
:
383 case SND_PCM_FORMAT_S32_LE
:
388 case SND_PCM_FORMAT_U32_LE
:
393 case SND_PCM_FORMAT_S32_BE
:
398 case SND_PCM_FORMAT_U32_BE
:
404 dolog ("Unrecognized audio format %d\n", alsafmt
);
411 static void alsa_dump_info (struct alsa_params_req
*req
,
412 struct alsa_params_obt
*obt
,
413 snd_pcm_format_t obtfmt
)
415 dolog ("parameter | requested value | obtained value\n");
416 dolog ("format | %10d | %10d\n", req
->fmt
, obtfmt
);
417 dolog ("channels | %10d | %10d\n",
418 req
->nchannels
, obt
->nchannels
);
419 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
420 dolog ("============================================\n");
421 dolog ("requested: buffer size %d period size %d\n",
422 req
->buffer_size
, req
->period_size
);
423 dolog ("obtained: samples %ld\n", obt
->samples
);
426 static void alsa_set_threshold (snd_pcm_t
*handle
, snd_pcm_uframes_t threshold
)
429 snd_pcm_sw_params_t
*sw_params
;
431 snd_pcm_sw_params_alloca (&sw_params
);
433 err
= snd_pcm_sw_params_current (handle
, sw_params
);
435 dolog ("Could not fully initialize DAC\n");
436 alsa_logerr (err
, "Failed to get current software parameters\n");
440 err
= snd_pcm_sw_params_set_start_threshold (handle
, sw_params
, threshold
);
442 dolog ("Could not fully initialize DAC\n");
443 alsa_logerr (err
, "Failed to set software threshold to %ld\n",
448 err
= snd_pcm_sw_params (handle
, sw_params
);
450 dolog ("Could not fully initialize DAC\n");
451 alsa_logerr (err
, "Failed to set software parameters\n");
456 static int alsa_open (int in
, struct alsa_params_req
*req
,
457 struct alsa_params_obt
*obt
, snd_pcm_t
**handlep
,
461 snd_pcm_hw_params_t
*hw_params
;
464 unsigned int freq
, nchannels
;
465 const char *pcm_name
= in
? conf
->pcm_name_in
: conf
->pcm_name_out
;
466 snd_pcm_uframes_t obt_buffer_size
;
467 const char *typ
= in
? "ADC" : "DAC";
468 snd_pcm_format_t obtfmt
;
471 nchannels
= req
->nchannels
;
472 size_in_usec
= req
->size_in_usec
;
474 snd_pcm_hw_params_alloca (&hw_params
);
479 in
? SND_PCM_STREAM_CAPTURE
: SND_PCM_STREAM_PLAYBACK
,
483 alsa_logerr2 (err
, typ
, "Failed to open `%s':\n", pcm_name
);
487 err
= snd_pcm_hw_params_any (handle
, hw_params
);
489 alsa_logerr2 (err
, typ
, "Failed to initialize hardware parameters\n");
493 err
= snd_pcm_hw_params_set_access (
496 SND_PCM_ACCESS_RW_INTERLEAVED
499 alsa_logerr2 (err
, typ
, "Failed to set access type\n");
503 err
= snd_pcm_hw_params_set_format (handle
, hw_params
, req
->fmt
);
505 alsa_logerr2 (err
, typ
, "Failed to set format %d\n", req
->fmt
);
508 err
= snd_pcm_hw_params_set_rate_near (handle
, hw_params
, &freq
, 0);
510 alsa_logerr2 (err
, typ
, "Failed to set frequency %d\n", req
->freq
);
514 err
= snd_pcm_hw_params_set_channels_near (
520 alsa_logerr2 (err
, typ
, "Failed to set number of channels %d\n",
525 if (nchannels
!= 1 && nchannels
!= 2) {
526 alsa_logerr2 (err
, typ
,
527 "Can not handle obtained number of channels %d\n",
532 if (req
->buffer_size
) {
537 unsigned int btime
= req
->buffer_size
;
539 err
= snd_pcm_hw_params_set_buffer_time_near (
548 snd_pcm_uframes_t bsize
= req
->buffer_size
;
550 err
= snd_pcm_hw_params_set_buffer_size_near (
558 alsa_logerr2 (err
, typ
, "Failed to set buffer %s to %d\n",
559 size_in_usec
? "time" : "size", req
->buffer_size
);
563 if ((req
->override_mask
& 2) && (obt
- req
->buffer_size
))
564 dolog ("Requested buffer %s %u was rejected, using %lu\n",
565 size_in_usec
? "time" : "size", req
->buffer_size
, obt
);
568 if (req
->period_size
) {
573 unsigned int ptime
= req
->period_size
;
575 err
= snd_pcm_hw_params_set_period_time_near (
585 snd_pcm_uframes_t psize
= req
->period_size
;
587 err
= snd_pcm_hw_params_set_period_size_near (
597 alsa_logerr2 (err
, typ
, "Failed to set period %s to %d\n",
598 size_in_usec
? "time" : "size", req
->period_size
);
602 if (((req
->override_mask
& 1) && (obt
- req
->period_size
)))
603 dolog ("Requested period %s %u was rejected, using %lu\n",
604 size_in_usec
? "time" : "size", req
->period_size
, obt
);
607 err
= snd_pcm_hw_params (handle
, hw_params
);
609 alsa_logerr2 (err
, typ
, "Failed to apply audio parameters\n");
613 err
= snd_pcm_hw_params_get_buffer_size (hw_params
, &obt_buffer_size
);
615 alsa_logerr2 (err
, typ
, "Failed to get buffer size\n");
619 err
= snd_pcm_hw_params_get_format (hw_params
, &obtfmt
);
621 alsa_logerr2 (err
, typ
, "Failed to get format\n");
625 if (alsa_to_audfmt (obtfmt
, &obt
->fmt
, &obt
->endianness
)) {
626 dolog ("Invalid format was returned %d\n", obtfmt
);
630 err
= snd_pcm_prepare (handle
);
632 alsa_logerr2 (err
, typ
, "Could not prepare handle %p\n", handle
);
636 if (!in
&& conf
->threshold
) {
637 snd_pcm_uframes_t threshold
;
640 bytes_per_sec
= freq
<< (nchannels
== 2);
658 threshold
= (conf
->threshold
* bytes_per_sec
) / 1000;
659 alsa_set_threshold (handle
, threshold
);
662 obt
->nchannels
= nchannels
;
664 obt
->samples
= obt_buffer_size
;
668 if (obtfmt
!= req
->fmt
||
669 obt
->nchannels
!= req
->nchannels
||
670 obt
->freq
!= req
->freq
) {
671 dolog ("Audio parameters for %s\n", typ
);
672 alsa_dump_info (req
, obt
, obtfmt
);
676 alsa_dump_info (req
, obt
, obtfmt
);
681 alsa_anal_close1 (&handle
);
685 static snd_pcm_sframes_t
alsa_get_avail (snd_pcm_t
*handle
)
687 snd_pcm_sframes_t avail
;
689 avail
= snd_pcm_avail_update (handle
);
691 if (avail
== -EPIPE
) {
692 if (!alsa_recover (handle
)) {
693 avail
= snd_pcm_avail_update (handle
);
699 "Could not obtain number of available frames\n");
707 static void alsa_write_pending (ALSAVoiceOut
*alsa
)
709 HWVoiceOut
*hw
= &alsa
->hw
;
711 while (alsa
->pending
) {
712 int left_till_end_samples
= hw
->samples
- alsa
->wpos
;
713 int len
= audio_MIN (alsa
->pending
, left_till_end_samples
);
714 char *src
= advance (alsa
->pcm_buf
, alsa
->wpos
<< hw
->info
.shift
);
717 snd_pcm_sframes_t written
;
719 written
= snd_pcm_writei (alsa
->handle
, src
, len
);
724 trace_alsa_wrote_zero(len
);
728 if (alsa_recover (alsa
->handle
)) {
729 alsa_logerr (written
, "Failed to write %d frames\n",
733 trace_alsa_xrun_out();
737 /* stream is suspended and waiting for an
738 application recovery */
739 if (alsa_resume (alsa
->handle
)) {
740 alsa_logerr (written
, "Failed to write %d frames\n",
744 trace_alsa_resume_out();
751 alsa_logerr (written
, "Failed to write %d frames from %p\n",
757 alsa
->wpos
= (alsa
->wpos
+ written
) % hw
->samples
;
758 alsa
->pending
-= written
;
764 static int alsa_run_out (HWVoiceOut
*hw
, int live
)
766 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
768 snd_pcm_sframes_t avail
;
770 avail
= alsa_get_avail (alsa
->handle
);
772 dolog ("Could not get number of available playback frames\n");
776 decr
= audio_MIN (live
, avail
);
777 decr
= audio_pcm_hw_clip_out (hw
, alsa
->pcm_buf
, decr
, alsa
->pending
);
778 alsa
->pending
+= decr
;
779 alsa_write_pending (alsa
);
783 static void alsa_fini_out (HWVoiceOut
*hw
)
785 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
787 ldebug ("alsa_fini\n");
788 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
790 g_free(alsa
->pcm_buf
);
791 alsa
->pcm_buf
= NULL
;
794 static int alsa_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
797 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
798 struct alsa_params_req req
;
799 struct alsa_params_obt obt
;
801 struct audsettings obt_as
;
802 ALSAConf
*conf
= drv_opaque
;
804 req
.fmt
= aud_to_alsafmt (as
->fmt
, as
->endianness
);
806 req
.nchannels
= as
->nchannels
;
807 req
.period_size
= conf
->period_size_out
;
808 req
.buffer_size
= conf
->buffer_size_out
;
809 req
.size_in_usec
= conf
->size_in_usec_out
;
811 (conf
->period_size_out_overridden
? 1 : 0) |
812 (conf
->buffer_size_out_overridden
? 2 : 0);
814 if (alsa_open (0, &req
, &obt
, &handle
, conf
)) {
818 obt_as
.freq
= obt
.freq
;
819 obt_as
.nchannels
= obt
.nchannels
;
820 obt_as
.fmt
= obt
.fmt
;
821 obt_as
.endianness
= obt
.endianness
;
823 audio_pcm_init_info (&hw
->info
, &obt_as
);
824 hw
->samples
= obt
.samples
;
826 alsa
->pcm_buf
= audio_calloc(__func__
, obt
.samples
, 1 << hw
->info
.shift
);
827 if (!alsa
->pcm_buf
) {
828 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
829 hw
->samples
, 1 << hw
->info
.shift
);
830 alsa_anal_close1 (&handle
);
834 alsa
->handle
= handle
;
835 alsa
->pollhlp
.conf
= conf
;
839 #define VOICE_CTL_PAUSE 0
840 #define VOICE_CTL_PREPARE 1
841 #define VOICE_CTL_START 2
843 static int alsa_voice_ctl (snd_pcm_t
*handle
, const char *typ
, int ctl
)
847 if (ctl
== VOICE_CTL_PAUSE
) {
848 err
= snd_pcm_drop (handle
);
850 alsa_logerr (err
, "Could not stop %s\n", typ
);
855 err
= snd_pcm_prepare (handle
);
857 alsa_logerr (err
, "Could not prepare handle for %s\n", typ
);
860 if (ctl
== VOICE_CTL_START
) {
861 err
= snd_pcm_start(handle
);
863 alsa_logerr (err
, "Could not start handle for %s\n", typ
);
872 static int alsa_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
874 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
883 poll_mode
= va_arg (ap
, int);
886 ldebug ("enabling voice\n");
887 if (poll_mode
&& alsa_poll_out (hw
)) {
890 hw
->poll_mode
= poll_mode
;
891 return alsa_voice_ctl (alsa
->handle
, "playback", VOICE_CTL_PREPARE
);
895 ldebug ("disabling voice\n");
898 alsa_fini_poll (&alsa
->pollhlp
);
900 return alsa_voice_ctl (alsa
->handle
, "playback", VOICE_CTL_PAUSE
);
906 static int alsa_init_in(HWVoiceIn
*hw
, struct audsettings
*as
, void *drv_opaque
)
908 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
909 struct alsa_params_req req
;
910 struct alsa_params_obt obt
;
912 struct audsettings obt_as
;
913 ALSAConf
*conf
= drv_opaque
;
915 req
.fmt
= aud_to_alsafmt (as
->fmt
, as
->endianness
);
917 req
.nchannels
= as
->nchannels
;
918 req
.period_size
= conf
->period_size_in
;
919 req
.buffer_size
= conf
->buffer_size_in
;
920 req
.size_in_usec
= conf
->size_in_usec_in
;
922 (conf
->period_size_in_overridden
? 1 : 0) |
923 (conf
->buffer_size_in_overridden
? 2 : 0);
925 if (alsa_open (1, &req
, &obt
, &handle
, conf
)) {
929 obt_as
.freq
= obt
.freq
;
930 obt_as
.nchannels
= obt
.nchannels
;
931 obt_as
.fmt
= obt
.fmt
;
932 obt_as
.endianness
= obt
.endianness
;
934 audio_pcm_init_info (&hw
->info
, &obt_as
);
935 hw
->samples
= obt
.samples
;
937 alsa
->pcm_buf
= audio_calloc(__func__
, hw
->samples
, 1 << hw
->info
.shift
);
938 if (!alsa
->pcm_buf
) {
939 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
940 hw
->samples
, 1 << hw
->info
.shift
);
941 alsa_anal_close1 (&handle
);
945 alsa
->handle
= handle
;
946 alsa
->pollhlp
.conf
= conf
;
950 static void alsa_fini_in (HWVoiceIn
*hw
)
952 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
954 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
956 g_free(alsa
->pcm_buf
);
957 alsa
->pcm_buf
= NULL
;
960 static int alsa_run_in (HWVoiceIn
*hw
)
962 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
963 int hwshift
= hw
->info
.shift
;
965 int live
= audio_pcm_hw_get_live_in (hw
);
966 int dead
= hw
->samples
- live
;
972 { .add
= hw
->wpos
, .len
= 0 },
973 { .add
= 0, .len
= 0 }
975 snd_pcm_sframes_t avail
;
976 snd_pcm_uframes_t read_samples
= 0;
982 avail
= alsa_get_avail (alsa
->handle
);
984 dolog ("Could not get number of captured frames\n");
989 snd_pcm_state_t state
;
991 state
= snd_pcm_state (alsa
->handle
);
993 case SND_PCM_STATE_PREPARED
:
996 case SND_PCM_STATE_SUSPENDED
:
997 /* stream is suspended and waiting for an application recovery */
998 if (alsa_resume (alsa
->handle
)) {
999 dolog ("Failed to resume suspended input stream\n");
1002 trace_alsa_resume_in();
1005 trace_alsa_no_frames(state
);
1010 decr
= audio_MIN (dead
, avail
);
1015 if (hw
->wpos
+ decr
> hw
->samples
) {
1016 bufs
[0].len
= (hw
->samples
- hw
->wpos
);
1017 bufs
[1].len
= (decr
- (hw
->samples
- hw
->wpos
));
1023 for (i
= 0; i
< 2; ++i
) {
1025 struct st_sample
*dst
;
1026 snd_pcm_sframes_t nread
;
1027 snd_pcm_uframes_t len
;
1031 src
= advance (alsa
->pcm_buf
, bufs
[i
].add
<< hwshift
);
1032 dst
= hw
->conv_buf
+ bufs
[i
].add
;
1035 nread
= snd_pcm_readi (alsa
->handle
, src
, len
);
1040 trace_alsa_read_zero(len
);
1044 if (alsa_recover (alsa
->handle
)) {
1045 alsa_logerr (nread
, "Failed to read %ld frames\n", len
);
1048 trace_alsa_xrun_in();
1057 "Failed to read %ld frames from %p\n",
1065 hw
->conv (dst
, src
, nread
);
1067 src
= advance (src
, nread
<< hwshift
);
1070 read_samples
+= nread
;
1076 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
1077 return read_samples
;
1080 static int alsa_read (SWVoiceIn
*sw
, void *buf
, int size
)
1082 return audio_pcm_sw_read (sw
, buf
, size
);
1085 static int alsa_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
1087 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
1096 poll_mode
= va_arg (ap
, int);
1099 ldebug ("enabling voice\n");
1100 if (poll_mode
&& alsa_poll_in (hw
)) {
1103 hw
->poll_mode
= poll_mode
;
1105 return alsa_voice_ctl (alsa
->handle
, "capture", VOICE_CTL_START
);
1109 ldebug ("disabling voice\n");
1110 if (hw
->poll_mode
) {
1112 alsa_fini_poll (&alsa
->pollhlp
);
1114 return alsa_voice_ctl (alsa
->handle
, "capture", VOICE_CTL_PAUSE
);
1120 static ALSAConf glob_conf
= {
1121 .buffer_size_out
= 4096,
1122 .period_size_out
= 1024,
1123 .pcm_name_out
= "default",
1124 .pcm_name_in
= "default",
1127 static void *alsa_audio_init (void)
1129 ALSAConf
*conf
= g_malloc(sizeof(ALSAConf
));
1134 static void alsa_audio_fini (void *opaque
)
1139 static struct audio_option alsa_options
[] = {
1141 .name
= "DAC_SIZE_IN_USEC",
1142 .tag
= AUD_OPT_BOOL
,
1143 .valp
= &glob_conf
.size_in_usec_out
,
1144 .descr
= "DAC period/buffer size in microseconds (otherwise in frames)"
1147 .name
= "DAC_PERIOD_SIZE",
1149 .valp
= &glob_conf
.period_size_out
,
1150 .descr
= "DAC period size (0 to go with system default)",
1151 .overriddenp
= &glob_conf
.period_size_out_overridden
1154 .name
= "DAC_BUFFER_SIZE",
1156 .valp
= &glob_conf
.buffer_size_out
,
1157 .descr
= "DAC buffer size (0 to go with system default)",
1158 .overriddenp
= &glob_conf
.buffer_size_out_overridden
1161 .name
= "ADC_SIZE_IN_USEC",
1162 .tag
= AUD_OPT_BOOL
,
1163 .valp
= &glob_conf
.size_in_usec_in
,
1165 "ADC period/buffer size in microseconds (otherwise in frames)"
1168 .name
= "ADC_PERIOD_SIZE",
1170 .valp
= &glob_conf
.period_size_in
,
1171 .descr
= "ADC period size (0 to go with system default)",
1172 .overriddenp
= &glob_conf
.period_size_in_overridden
1175 .name
= "ADC_BUFFER_SIZE",
1177 .valp
= &glob_conf
.buffer_size_in
,
1178 .descr
= "ADC buffer size (0 to go with system default)",
1179 .overriddenp
= &glob_conf
.buffer_size_in_overridden
1182 .name
= "THRESHOLD",
1184 .valp
= &glob_conf
.threshold
,
1185 .descr
= "(undocumented)"
1190 .valp
= &glob_conf
.pcm_name_out
,
1191 .descr
= "DAC device name (for instance dmix)"
1196 .valp
= &glob_conf
.pcm_name_in
,
1197 .descr
= "ADC device name"
1199 { /* End of list */ }
1202 static struct audio_pcm_ops alsa_pcm_ops
= {
1203 .init_out
= alsa_init_out
,
1204 .fini_out
= alsa_fini_out
,
1205 .run_out
= alsa_run_out
,
1206 .write
= alsa_write
,
1207 .ctl_out
= alsa_ctl_out
,
1209 .init_in
= alsa_init_in
,
1210 .fini_in
= alsa_fini_in
,
1211 .run_in
= alsa_run_in
,
1213 .ctl_in
= alsa_ctl_in
,
1216 struct audio_driver alsa_audio_driver
= {
1218 .descr
= "ALSA http://www.alsa-project.org",
1219 .options
= alsa_options
,
1220 .init
= alsa_audio_init
,
1221 .fini
= alsa_audio_fini
,
1222 .pcm_ops
= &alsa_pcm_ops
,
1223 .can_be_default
= 1,
1224 .max_voices_out
= INT_MAX
,
1225 .max_voices_in
= INT_MAX
,
1226 .voice_size_out
= sizeof (ALSAVoiceOut
),
1227 .voice_size_in
= sizeof (ALSAVoiceIn
)