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"
26 #include "qemu-char.h"
29 #if QEMU_GNUC_PREREQ(4, 3)
30 #pragma GCC diagnostic ignored "-Waddress"
33 #define AUDIO_CAP "alsa"
34 #include "audio_int.h"
43 typedef struct ALSAVoiceOut
{
49 struct pollhlp pollhlp
;
52 typedef struct ALSAVoiceIn
{
56 struct pollhlp pollhlp
;
62 const char *pcm_name_in
;
63 const char *pcm_name_out
;
64 unsigned int buffer_size_in
;
65 unsigned int period_size_in
;
66 unsigned int buffer_size_out
;
67 unsigned int period_size_out
;
68 unsigned int threshold
;
70 int buffer_size_in_overridden
;
71 int period_size_in_overridden
;
73 int buffer_size_out_overridden
;
74 int period_size_out_overridden
;
77 .buffer_size_out
= 4096,
78 .period_size_out
= 1024,
79 .pcm_name_out
= "default",
80 .pcm_name_in
= "default",
83 struct alsa_params_req
{
89 unsigned int buffer_size
;
90 unsigned int period_size
;
93 struct alsa_params_obt
{
98 snd_pcm_uframes_t samples
;
101 static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err
, const char *fmt
, ...)
106 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
109 AUD_log (AUDIO_CAP
, "Reason: %s\n", snd_strerror (err
));
112 static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
121 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
124 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
127 AUD_log (AUDIO_CAP
, "Reason: %s\n", snd_strerror (err
));
130 static void alsa_fini_poll (struct pollhlp
*hlp
)
133 struct pollfd
*pfds
= hlp
->pfds
;
136 for (i
= 0; i
< hlp
->count
; ++i
) {
137 qemu_set_fd_handler (pfds
[i
].fd
, NULL
, NULL
, NULL
);
146 static void alsa_anal_close1 (snd_pcm_t
**handlep
)
148 int err
= snd_pcm_close (*handlep
);
150 alsa_logerr (err
, "Failed to close PCM handle %p\n", *handlep
);
155 static void alsa_anal_close (snd_pcm_t
**handlep
, struct pollhlp
*hlp
)
157 alsa_fini_poll (hlp
);
158 alsa_anal_close1 (handlep
);
161 static int alsa_recover (snd_pcm_t
*handle
)
163 int err
= snd_pcm_prepare (handle
);
165 alsa_logerr (err
, "Failed to prepare handle %p\n", handle
);
171 static int alsa_resume (snd_pcm_t
*handle
)
173 int err
= snd_pcm_resume (handle
);
175 alsa_logerr (err
, "Failed to resume handle %p\n", handle
);
181 static void alsa_poll_handler (void *opaque
)
184 snd_pcm_state_t state
;
185 struct pollhlp
*hlp
= opaque
;
186 unsigned short revents
;
188 count
= poll (hlp
->pfds
, hlp
->count
, 0);
190 dolog ("alsa_poll_handler: poll %s\n", strerror (errno
));
198 /* XXX: ALSA example uses initial count, not the one returned by
200 err
= snd_pcm_poll_descriptors_revents (hlp
->handle
, hlp
->pfds
,
201 hlp
->count
, &revents
);
203 alsa_logerr (err
, "snd_pcm_poll_descriptors_revents");
207 if (!(revents
& hlp
->mask
)) {
209 dolog ("revents = %d\n", revents
);
214 state
= snd_pcm_state (hlp
->handle
);
216 case SND_PCM_STATE_SETUP
:
217 alsa_recover (hlp
->handle
);
220 case SND_PCM_STATE_XRUN
:
221 alsa_recover (hlp
->handle
);
224 case SND_PCM_STATE_SUSPENDED
:
225 alsa_resume (hlp
->handle
);
228 case SND_PCM_STATE_PREPARED
:
229 audio_run ("alsa run (prepared)");
232 case SND_PCM_STATE_RUNNING
:
233 audio_run ("alsa run (running)");
237 dolog ("Unexpected state %d\n", state
);
241 static int alsa_poll_helper (snd_pcm_t
*handle
, struct pollhlp
*hlp
, int mask
)
246 count
= snd_pcm_poll_descriptors_count (handle
);
248 dolog ("Could not initialize poll mode\n"
249 "Invalid number of poll descriptors %d\n", count
);
253 pfds
= audio_calloc ("alsa_poll_helper", count
, sizeof (*pfds
));
255 dolog ("Could not initialize poll mode\n");
259 err
= snd_pcm_poll_descriptors (handle
, pfds
, count
);
261 alsa_logerr (err
, "Could not initialize poll mode\n"
262 "Could not obtain poll descriptors\n");
267 for (i
= 0; i
< count
; ++i
) {
268 if (pfds
[i
].events
& POLLIN
) {
269 err
= qemu_set_fd_handler (pfds
[i
].fd
, alsa_poll_handler
,
272 if (pfds
[i
].events
& POLLOUT
) {
274 dolog ("POLLOUT %d %d\n", i
, pfds
[i
].fd
);
276 err
= qemu_set_fd_handler (pfds
[i
].fd
, NULL
,
277 alsa_poll_handler
, hlp
);
280 dolog ("Set handler events=%#x index=%d fd=%d err=%d\n",
281 pfds
[i
].events
, i
, pfds
[i
].fd
, err
);
285 dolog ("Failed to set handler events=%#x index=%d fd=%d err=%d\n",
286 pfds
[i
].events
, i
, pfds
[i
].fd
, err
);
289 qemu_set_fd_handler (pfds
[i
].fd
, NULL
, NULL
, NULL
);
297 hlp
->handle
= handle
;
302 static int alsa_poll_out (HWVoiceOut
*hw
)
304 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
306 return alsa_poll_helper (alsa
->handle
, &alsa
->pollhlp
, POLLOUT
);
309 static int alsa_poll_in (HWVoiceIn
*hw
)
311 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
313 return alsa_poll_helper (alsa
->handle
, &alsa
->pollhlp
, POLLIN
);
316 static int alsa_write (SWVoiceOut
*sw
, void *buf
, int len
)
318 return audio_pcm_sw_write (sw
, buf
, len
);
321 static snd_pcm_format_t
aud_to_alsafmt (audfmt_e fmt
)
325 return SND_PCM_FORMAT_S8
;
328 return SND_PCM_FORMAT_U8
;
331 return SND_PCM_FORMAT_S16_LE
;
334 return SND_PCM_FORMAT_U16_LE
;
337 return SND_PCM_FORMAT_S32_LE
;
340 return SND_PCM_FORMAT_U32_LE
;
343 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
347 return SND_PCM_FORMAT_U8
;
351 static int alsa_to_audfmt (snd_pcm_format_t alsafmt
, audfmt_e
*fmt
,
355 case SND_PCM_FORMAT_S8
:
360 case SND_PCM_FORMAT_U8
:
365 case SND_PCM_FORMAT_S16_LE
:
370 case SND_PCM_FORMAT_U16_LE
:
375 case SND_PCM_FORMAT_S16_BE
:
380 case SND_PCM_FORMAT_U16_BE
:
385 case SND_PCM_FORMAT_S32_LE
:
390 case SND_PCM_FORMAT_U32_LE
:
395 case SND_PCM_FORMAT_S32_BE
:
400 case SND_PCM_FORMAT_U32_BE
:
406 dolog ("Unrecognized audio format %d\n", alsafmt
);
413 static void alsa_dump_info (struct alsa_params_req
*req
,
414 struct alsa_params_obt
*obt
,
415 snd_pcm_format_t obtfmt
)
417 dolog ("parameter | requested value | obtained value\n");
418 dolog ("format | %10d | %10d\n", req
->fmt
, obtfmt
);
419 dolog ("channels | %10d | %10d\n",
420 req
->nchannels
, obt
->nchannels
);
421 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
422 dolog ("============================================\n");
423 dolog ("requested: buffer size %d period size %d\n",
424 req
->buffer_size
, req
->period_size
);
425 dolog ("obtained: samples %ld\n", obt
->samples
);
428 static void alsa_set_threshold (snd_pcm_t
*handle
, snd_pcm_uframes_t threshold
)
431 snd_pcm_sw_params_t
*sw_params
;
433 snd_pcm_sw_params_alloca (&sw_params
);
435 err
= snd_pcm_sw_params_current (handle
, sw_params
);
437 dolog ("Could not fully initialize DAC\n");
438 alsa_logerr (err
, "Failed to get current software parameters\n");
442 err
= snd_pcm_sw_params_set_start_threshold (handle
, sw_params
, threshold
);
444 dolog ("Could not fully initialize DAC\n");
445 alsa_logerr (err
, "Failed to set software threshold to %ld\n",
450 err
= snd_pcm_sw_params (handle
, sw_params
);
452 dolog ("Could not fully initialize DAC\n");
453 alsa_logerr (err
, "Failed to set software parameters\n");
458 static int alsa_open (int in
, struct alsa_params_req
*req
,
459 struct alsa_params_obt
*obt
, snd_pcm_t
**handlep
)
462 snd_pcm_hw_params_t
*hw_params
;
465 unsigned int freq
, nchannels
;
466 const char *pcm_name
= in
? conf
.pcm_name_in
: conf
.pcm_name_out
;
467 snd_pcm_uframes_t obt_buffer_size
;
468 const char *typ
= in
? "ADC" : "DAC";
469 snd_pcm_format_t obtfmt
;
472 nchannels
= req
->nchannels
;
473 size_in_usec
= req
->size_in_usec
;
475 snd_pcm_hw_params_alloca (&hw_params
);
480 in
? SND_PCM_STREAM_CAPTURE
: SND_PCM_STREAM_PLAYBACK
,
484 alsa_logerr2 (err
, typ
, "Failed to open `%s':\n", pcm_name
);
488 err
= snd_pcm_hw_params_any (handle
, hw_params
);
490 alsa_logerr2 (err
, typ
, "Failed to initialize hardware parameters\n");
494 err
= snd_pcm_hw_params_set_access (
497 SND_PCM_ACCESS_RW_INTERLEAVED
500 alsa_logerr2 (err
, typ
, "Failed to set access type\n");
504 err
= snd_pcm_hw_params_set_format (handle
, hw_params
, req
->fmt
);
505 if (err
< 0 && conf
.verbose
) {
506 alsa_logerr2 (err
, typ
, "Failed to set format %d\n", req
->fmt
);
509 err
= snd_pcm_hw_params_set_rate_near (handle
, hw_params
, &freq
, 0);
511 alsa_logerr2 (err
, typ
, "Failed to set frequency %d\n", req
->freq
);
515 err
= snd_pcm_hw_params_set_channels_near (
521 alsa_logerr2 (err
, typ
, "Failed to set number of channels %d\n",
526 if (nchannels
!= 1 && nchannels
!= 2) {
527 alsa_logerr2 (err
, typ
,
528 "Can not handle obtained number of channels %d\n",
533 if (req
->buffer_size
) {
538 unsigned int btime
= req
->buffer_size
;
540 err
= snd_pcm_hw_params_set_buffer_time_near (
549 snd_pcm_uframes_t bsize
= req
->buffer_size
;
551 err
= snd_pcm_hw_params_set_buffer_size_near (
559 alsa_logerr2 (err
, typ
, "Failed to set buffer %s to %d\n",
560 size_in_usec
? "time" : "size", req
->buffer_size
);
564 if ((req
->override_mask
& 2) && (obt
- req
->buffer_size
))
565 dolog ("Requested buffer %s %u was rejected, using %lu\n",
566 size_in_usec
? "time" : "size", req
->buffer_size
, obt
);
569 if (req
->period_size
) {
574 unsigned int ptime
= req
->period_size
;
576 err
= snd_pcm_hw_params_set_period_time_near (
586 snd_pcm_uframes_t psize
= req
->period_size
;
588 err
= snd_pcm_hw_params_set_period_size_near (
598 alsa_logerr2 (err
, typ
, "Failed to set period %s to %d\n",
599 size_in_usec
? "time" : "size", req
->period_size
);
603 if (((req
->override_mask
& 1) && (obt
- req
->period_size
)))
604 dolog ("Requested period %s %u was rejected, using %lu\n",
605 size_in_usec
? "time" : "size", req
->period_size
, obt
);
608 err
= snd_pcm_hw_params (handle
, hw_params
);
610 alsa_logerr2 (err
, typ
, "Failed to apply audio parameters\n");
614 err
= snd_pcm_hw_params_get_buffer_size (hw_params
, &obt_buffer_size
);
616 alsa_logerr2 (err
, typ
, "Failed to get buffer size\n");
620 err
= snd_pcm_hw_params_get_format (hw_params
, &obtfmt
);
622 alsa_logerr2 (err
, typ
, "Failed to get format\n");
626 if (alsa_to_audfmt (obtfmt
, &obt
->fmt
, &obt
->endianness
)) {
627 dolog ("Invalid format was returned %d\n", obtfmt
);
631 err
= snd_pcm_prepare (handle
);
633 alsa_logerr2 (err
, typ
, "Could not prepare handle %p\n", handle
);
637 if (!in
&& conf
.threshold
) {
638 snd_pcm_uframes_t threshold
;
641 bytes_per_sec
= freq
<< (nchannels
== 2);
659 threshold
= (conf
.threshold
* bytes_per_sec
) / 1000;
660 alsa_set_threshold (handle
, threshold
);
663 obt
->nchannels
= nchannels
;
665 obt
->samples
= obt_buffer_size
;
670 (obtfmt
!= req
->fmt
||
671 obt
->nchannels
!= req
->nchannels
||
672 obt
->freq
!= req
->freq
)) {
673 dolog ("Audio parameters for %s\n", typ
);
674 alsa_dump_info (req
, obt
, obtfmt
);
678 alsa_dump_info (req
, obt
, obtfmt
);
683 alsa_anal_close1 (&handle
);
687 static snd_pcm_sframes_t
alsa_get_avail (snd_pcm_t
*handle
)
689 snd_pcm_sframes_t avail
;
691 avail
= snd_pcm_avail_update (handle
);
693 if (avail
== -EPIPE
) {
694 if (!alsa_recover (handle
)) {
695 avail
= snd_pcm_avail_update (handle
);
701 "Could not obtain number of available frames\n");
709 static void alsa_write_pending (ALSAVoiceOut
*alsa
)
711 HWVoiceOut
*hw
= &alsa
->hw
;
713 while (alsa
->pending
) {
714 int left_till_end_samples
= hw
->samples
- alsa
->wpos
;
715 int len
= audio_MIN (alsa
->pending
, left_till_end_samples
);
716 char *src
= advance (alsa
->pcm_buf
, alsa
->wpos
<< hw
->info
.shift
);
719 snd_pcm_sframes_t written
;
721 written
= snd_pcm_writei (alsa
->handle
, src
, len
);
727 dolog ("Failed to write %d frames (wrote zero)\n", len
);
732 if (alsa_recover (alsa
->handle
)) {
733 alsa_logerr (written
, "Failed to write %d frames\n",
738 dolog ("Recovering from playback xrun\n");
743 /* stream is suspended and waiting for an
744 application recovery */
745 if (alsa_resume (alsa
->handle
)) {
746 alsa_logerr (written
, "Failed to write %d frames\n",
751 dolog ("Resuming suspended output stream\n");
759 alsa_logerr (written
, "Failed to write %d frames from %p\n",
765 alsa
->wpos
= (alsa
->wpos
+ written
) % hw
->samples
;
766 alsa
->pending
-= written
;
772 static int alsa_run_out (HWVoiceOut
*hw
, int live
)
774 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
776 snd_pcm_sframes_t avail
;
778 avail
= alsa_get_avail (alsa
->handle
);
780 dolog ("Could not get number of available playback frames\n");
784 decr
= audio_MIN (live
, avail
);
785 decr
= audio_pcm_hw_clip_out (hw
, alsa
->pcm_buf
, decr
, alsa
->pending
);
786 alsa
->pending
+= decr
;
787 alsa_write_pending (alsa
);
791 static void alsa_fini_out (HWVoiceOut
*hw
)
793 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
795 ldebug ("alsa_fini\n");
796 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
799 qemu_free (alsa
->pcm_buf
);
800 alsa
->pcm_buf
= NULL
;
804 static int alsa_init_out (HWVoiceOut
*hw
, struct audsettings
*as
)
806 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
807 struct alsa_params_req req
;
808 struct alsa_params_obt obt
;
810 struct audsettings obt_as
;
812 req
.fmt
= aud_to_alsafmt (as
->fmt
);
814 req
.nchannels
= as
->nchannels
;
815 req
.period_size
= conf
.period_size_out
;
816 req
.buffer_size
= conf
.buffer_size_out
;
817 req
.size_in_usec
= conf
.size_in_usec_out
;
819 (conf
.period_size_out_overridden
? 1 : 0) |
820 (conf
.buffer_size_out_overridden
? 2 : 0);
822 if (alsa_open (0, &req
, &obt
, &handle
)) {
826 obt_as
.freq
= obt
.freq
;
827 obt_as
.nchannels
= obt
.nchannels
;
828 obt_as
.fmt
= obt
.fmt
;
829 obt_as
.endianness
= obt
.endianness
;
831 audio_pcm_init_info (&hw
->info
, &obt_as
);
832 hw
->samples
= obt
.samples
;
834 alsa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, obt
.samples
, 1 << hw
->info
.shift
);
835 if (!alsa
->pcm_buf
) {
836 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
837 hw
->samples
, 1 << hw
->info
.shift
);
838 alsa_anal_close1 (&handle
);
842 alsa
->handle
= handle
;
846 #define VOICE_CTL_PAUSE 0
847 #define VOICE_CTL_PREPARE 1
848 #define VOICE_CTL_START 2
850 static int alsa_voice_ctl (snd_pcm_t
*handle
, const char *typ
, int ctl
)
854 if (ctl
== VOICE_CTL_PAUSE
) {
855 err
= snd_pcm_drop (handle
);
857 alsa_logerr (err
, "Could not stop %s\n", typ
);
862 err
= snd_pcm_prepare (handle
);
864 alsa_logerr (err
, "Could not prepare handle for %s\n", typ
);
867 if (ctl
== VOICE_CTL_START
) {
868 err
= snd_pcm_start(handle
);
870 alsa_logerr (err
, "Could not start handle for %s\n", typ
);
879 static int alsa_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
881 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
890 poll_mode
= va_arg (ap
, int);
893 ldebug ("enabling voice\n");
894 if (poll_mode
&& alsa_poll_out (hw
)) {
897 hw
->poll_mode
= poll_mode
;
898 return alsa_voice_ctl (alsa
->handle
, "playback", VOICE_CTL_PREPARE
);
902 ldebug ("disabling voice\n");
905 alsa_fini_poll (&alsa
->pollhlp
);
907 return alsa_voice_ctl (alsa
->handle
, "playback", VOICE_CTL_PAUSE
);
913 static int alsa_init_in (HWVoiceIn
*hw
, struct audsettings
*as
)
915 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
916 struct alsa_params_req req
;
917 struct alsa_params_obt obt
;
919 struct audsettings obt_as
;
921 req
.fmt
= aud_to_alsafmt (as
->fmt
);
923 req
.nchannels
= as
->nchannels
;
924 req
.period_size
= conf
.period_size_in
;
925 req
.buffer_size
= conf
.buffer_size_in
;
926 req
.size_in_usec
= conf
.size_in_usec_in
;
928 (conf
.period_size_in_overridden
? 1 : 0) |
929 (conf
.buffer_size_in_overridden
? 2 : 0);
931 if (alsa_open (1, &req
, &obt
, &handle
)) {
935 obt_as
.freq
= obt
.freq
;
936 obt_as
.nchannels
= obt
.nchannels
;
937 obt_as
.fmt
= obt
.fmt
;
938 obt_as
.endianness
= obt
.endianness
;
940 audio_pcm_init_info (&hw
->info
, &obt_as
);
941 hw
->samples
= obt
.samples
;
943 alsa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
944 if (!alsa
->pcm_buf
) {
945 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
946 hw
->samples
, 1 << hw
->info
.shift
);
947 alsa_anal_close1 (&handle
);
951 alsa
->handle
= handle
;
955 static void alsa_fini_in (HWVoiceIn
*hw
)
957 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
959 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
962 qemu_free (alsa
->pcm_buf
);
963 alsa
->pcm_buf
= NULL
;
967 static int alsa_run_in (HWVoiceIn
*hw
)
969 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
970 int hwshift
= hw
->info
.shift
;
972 int live
= audio_pcm_hw_get_live_in (hw
);
973 int dead
= hw
->samples
- live
;
979 { .add
= hw
->wpos
, .len
= 0 },
980 { .add
= 0, .len
= 0 }
982 snd_pcm_sframes_t avail
;
983 snd_pcm_uframes_t read_samples
= 0;
989 avail
= alsa_get_avail (alsa
->handle
);
991 dolog ("Could not get number of captured frames\n");
996 snd_pcm_state_t state
;
998 state
= snd_pcm_state (alsa
->handle
);
1000 case SND_PCM_STATE_PREPARED
:
1001 avail
= hw
->samples
;
1003 case SND_PCM_STATE_SUSPENDED
:
1004 /* stream is suspended and waiting for an application recovery */
1005 if (alsa_resume (alsa
->handle
)) {
1006 dolog ("Failed to resume suspended input stream\n");
1010 dolog ("Resuming suspended input stream\n");
1015 dolog ("No frames available and ALSA state is %d\n", state
);
1021 decr
= audio_MIN (dead
, avail
);
1026 if (hw
->wpos
+ decr
> hw
->samples
) {
1027 bufs
[0].len
= (hw
->samples
- hw
->wpos
);
1028 bufs
[1].len
= (decr
- (hw
->samples
- hw
->wpos
));
1034 for (i
= 0; i
< 2; ++i
) {
1036 struct st_sample
*dst
;
1037 snd_pcm_sframes_t nread
;
1038 snd_pcm_uframes_t len
;
1042 src
= advance (alsa
->pcm_buf
, bufs
[i
].add
<< hwshift
);
1043 dst
= hw
->conv_buf
+ bufs
[i
].add
;
1046 nread
= snd_pcm_readi (alsa
->handle
, src
, len
);
1052 dolog ("Failed to read %ld frames (read zero)\n", len
);
1057 if (alsa_recover (alsa
->handle
)) {
1058 alsa_logerr (nread
, "Failed to read %ld frames\n", len
);
1062 dolog ("Recovering from capture xrun\n");
1072 "Failed to read %ld frames from %p\n",
1080 hw
->conv (dst
, src
, nread
, &nominal_volume
);
1082 src
= advance (src
, nread
<< hwshift
);
1085 read_samples
+= nread
;
1091 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
1092 return read_samples
;
1095 static int alsa_read (SWVoiceIn
*sw
, void *buf
, int size
)
1097 return audio_pcm_sw_read (sw
, buf
, size
);
1100 static int alsa_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
1102 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
1111 poll_mode
= va_arg (ap
, int);
1114 ldebug ("enabling voice\n");
1115 if (poll_mode
&& alsa_poll_in (hw
)) {
1118 hw
->poll_mode
= poll_mode
;
1120 return alsa_voice_ctl (alsa
->handle
, "capture", VOICE_CTL_START
);
1124 ldebug ("disabling voice\n");
1125 if (hw
->poll_mode
) {
1127 alsa_fini_poll (&alsa
->pollhlp
);
1129 return alsa_voice_ctl (alsa
->handle
, "capture", VOICE_CTL_PAUSE
);
1135 static void *alsa_audio_init (void)
1140 static void alsa_audio_fini (void *opaque
)
1145 static struct audio_option alsa_options
[] = {
1147 .name
= "DAC_SIZE_IN_USEC",
1148 .tag
= AUD_OPT_BOOL
,
1149 .valp
= &conf
.size_in_usec_out
,
1150 .descr
= "DAC period/buffer size in microseconds (otherwise in frames)"
1153 .name
= "DAC_PERIOD_SIZE",
1155 .valp
= &conf
.period_size_out
,
1156 .descr
= "DAC period size (0 to go with system default)",
1157 .overriddenp
= &conf
.period_size_out_overridden
1160 .name
= "DAC_BUFFER_SIZE",
1162 .valp
= &conf
.buffer_size_out
,
1163 .descr
= "DAC buffer size (0 to go with system default)",
1164 .overriddenp
= &conf
.buffer_size_out_overridden
1167 .name
= "ADC_SIZE_IN_USEC",
1168 .tag
= AUD_OPT_BOOL
,
1169 .valp
= &conf
.size_in_usec_in
,
1171 "ADC period/buffer size in microseconds (otherwise in frames)"
1174 .name
= "ADC_PERIOD_SIZE",
1176 .valp
= &conf
.period_size_in
,
1177 .descr
= "ADC period size (0 to go with system default)",
1178 .overriddenp
= &conf
.period_size_in_overridden
1181 .name
= "ADC_BUFFER_SIZE",
1183 .valp
= &conf
.buffer_size_in
,
1184 .descr
= "ADC buffer size (0 to go with system default)",
1185 .overriddenp
= &conf
.buffer_size_in_overridden
1188 .name
= "THRESHOLD",
1190 .valp
= &conf
.threshold
,
1191 .descr
= "(undocumented)"
1196 .valp
= &conf
.pcm_name_out
,
1197 .descr
= "DAC device name (for instance dmix)"
1202 .valp
= &conf
.pcm_name_in
,
1203 .descr
= "ADC device name"
1207 .tag
= AUD_OPT_BOOL
,
1208 .valp
= &conf
.verbose
,
1209 .descr
= "Behave in a more verbose way"
1211 { /* End of list */ }
1214 static struct audio_pcm_ops alsa_pcm_ops
= {
1215 .init_out
= alsa_init_out
,
1216 .fini_out
= alsa_fini_out
,
1217 .run_out
= alsa_run_out
,
1218 .write
= alsa_write
,
1219 .ctl_out
= alsa_ctl_out
,
1221 .init_in
= alsa_init_in
,
1222 .fini_in
= alsa_fini_in
,
1223 .run_in
= alsa_run_in
,
1225 .ctl_in
= alsa_ctl_in
,
1228 struct audio_driver alsa_audio_driver
= {
1230 .descr
= "ALSA http://www.alsa-project.org",
1231 .options
= alsa_options
,
1232 .init
= alsa_audio_init
,
1233 .fini
= alsa_audio_fini
,
1234 .pcm_ops
= &alsa_pcm_ops
,
1235 .can_be_default
= 1,
1236 .max_voices_out
= INT_MAX
,
1237 .max_voices_in
= INT_MAX
,
1238 .voice_size_out
= sizeof (ALSAVoiceOut
),
1239 .voice_size_in
= sizeof (ALSAVoiceIn
)