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/main-loop.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
, int endianness
)
325 return SND_PCM_FORMAT_S8
;
328 return SND_PCM_FORMAT_U8
;
332 return SND_PCM_FORMAT_S16_BE
;
335 return SND_PCM_FORMAT_S16_LE
;
340 return SND_PCM_FORMAT_U16_BE
;
343 return SND_PCM_FORMAT_U16_LE
;
348 return SND_PCM_FORMAT_S32_BE
;
351 return SND_PCM_FORMAT_S32_LE
;
356 return SND_PCM_FORMAT_U32_BE
;
359 return SND_PCM_FORMAT_U32_LE
;
363 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
367 return SND_PCM_FORMAT_U8
;
371 static int alsa_to_audfmt (snd_pcm_format_t alsafmt
, audfmt_e
*fmt
,
375 case SND_PCM_FORMAT_S8
:
380 case SND_PCM_FORMAT_U8
:
385 case SND_PCM_FORMAT_S16_LE
:
390 case SND_PCM_FORMAT_U16_LE
:
395 case SND_PCM_FORMAT_S16_BE
:
400 case SND_PCM_FORMAT_U16_BE
:
405 case SND_PCM_FORMAT_S32_LE
:
410 case SND_PCM_FORMAT_U32_LE
:
415 case SND_PCM_FORMAT_S32_BE
:
420 case SND_PCM_FORMAT_U32_BE
:
426 dolog ("Unrecognized audio format %d\n", alsafmt
);
433 static void alsa_dump_info (struct alsa_params_req
*req
,
434 struct alsa_params_obt
*obt
,
435 snd_pcm_format_t obtfmt
)
437 dolog ("parameter | requested value | obtained value\n");
438 dolog ("format | %10d | %10d\n", req
->fmt
, obtfmt
);
439 dolog ("channels | %10d | %10d\n",
440 req
->nchannels
, obt
->nchannels
);
441 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
442 dolog ("============================================\n");
443 dolog ("requested: buffer size %d period size %d\n",
444 req
->buffer_size
, req
->period_size
);
445 dolog ("obtained: samples %ld\n", obt
->samples
);
448 static void alsa_set_threshold (snd_pcm_t
*handle
, snd_pcm_uframes_t threshold
)
451 snd_pcm_sw_params_t
*sw_params
;
453 snd_pcm_sw_params_alloca (&sw_params
);
455 err
= snd_pcm_sw_params_current (handle
, sw_params
);
457 dolog ("Could not fully initialize DAC\n");
458 alsa_logerr (err
, "Failed to get current software parameters\n");
462 err
= snd_pcm_sw_params_set_start_threshold (handle
, sw_params
, threshold
);
464 dolog ("Could not fully initialize DAC\n");
465 alsa_logerr (err
, "Failed to set software threshold to %ld\n",
470 err
= snd_pcm_sw_params (handle
, sw_params
);
472 dolog ("Could not fully initialize DAC\n");
473 alsa_logerr (err
, "Failed to set software parameters\n");
478 static int alsa_open (int in
, struct alsa_params_req
*req
,
479 struct alsa_params_obt
*obt
, snd_pcm_t
**handlep
)
482 snd_pcm_hw_params_t
*hw_params
;
485 unsigned int freq
, nchannels
;
486 const char *pcm_name
= in
? conf
.pcm_name_in
: conf
.pcm_name_out
;
487 snd_pcm_uframes_t obt_buffer_size
;
488 const char *typ
= in
? "ADC" : "DAC";
489 snd_pcm_format_t obtfmt
;
492 nchannels
= req
->nchannels
;
493 size_in_usec
= req
->size_in_usec
;
495 snd_pcm_hw_params_alloca (&hw_params
);
500 in
? SND_PCM_STREAM_CAPTURE
: SND_PCM_STREAM_PLAYBACK
,
504 alsa_logerr2 (err
, typ
, "Failed to open `%s':\n", pcm_name
);
508 err
= snd_pcm_hw_params_any (handle
, hw_params
);
510 alsa_logerr2 (err
, typ
, "Failed to initialize hardware parameters\n");
514 err
= snd_pcm_hw_params_set_access (
517 SND_PCM_ACCESS_RW_INTERLEAVED
520 alsa_logerr2 (err
, typ
, "Failed to set access type\n");
524 err
= snd_pcm_hw_params_set_format (handle
, hw_params
, req
->fmt
);
525 if (err
< 0 && conf
.verbose
) {
526 alsa_logerr2 (err
, typ
, "Failed to set format %d\n", req
->fmt
);
529 err
= snd_pcm_hw_params_set_rate_near (handle
, hw_params
, &freq
, 0);
531 alsa_logerr2 (err
, typ
, "Failed to set frequency %d\n", req
->freq
);
535 err
= snd_pcm_hw_params_set_channels_near (
541 alsa_logerr2 (err
, typ
, "Failed to set number of channels %d\n",
546 if (nchannels
!= 1 && nchannels
!= 2) {
547 alsa_logerr2 (err
, typ
,
548 "Can not handle obtained number of channels %d\n",
553 if (req
->buffer_size
) {
558 unsigned int btime
= req
->buffer_size
;
560 err
= snd_pcm_hw_params_set_buffer_time_near (
569 snd_pcm_uframes_t bsize
= req
->buffer_size
;
571 err
= snd_pcm_hw_params_set_buffer_size_near (
579 alsa_logerr2 (err
, typ
, "Failed to set buffer %s to %d\n",
580 size_in_usec
? "time" : "size", req
->buffer_size
);
584 if ((req
->override_mask
& 2) && (obt
- req
->buffer_size
))
585 dolog ("Requested buffer %s %u was rejected, using %lu\n",
586 size_in_usec
? "time" : "size", req
->buffer_size
, obt
);
589 if (req
->period_size
) {
594 unsigned int ptime
= req
->period_size
;
596 err
= snd_pcm_hw_params_set_period_time_near (
606 snd_pcm_uframes_t psize
= req
->period_size
;
608 err
= snd_pcm_hw_params_set_period_size_near (
618 alsa_logerr2 (err
, typ
, "Failed to set period %s to %d\n",
619 size_in_usec
? "time" : "size", req
->period_size
);
623 if (((req
->override_mask
& 1) && (obt
- req
->period_size
)))
624 dolog ("Requested period %s %u was rejected, using %lu\n",
625 size_in_usec
? "time" : "size", req
->period_size
, obt
);
628 err
= snd_pcm_hw_params (handle
, hw_params
);
630 alsa_logerr2 (err
, typ
, "Failed to apply audio parameters\n");
634 err
= snd_pcm_hw_params_get_buffer_size (hw_params
, &obt_buffer_size
);
636 alsa_logerr2 (err
, typ
, "Failed to get buffer size\n");
640 err
= snd_pcm_hw_params_get_format (hw_params
, &obtfmt
);
642 alsa_logerr2 (err
, typ
, "Failed to get format\n");
646 if (alsa_to_audfmt (obtfmt
, &obt
->fmt
, &obt
->endianness
)) {
647 dolog ("Invalid format was returned %d\n", obtfmt
);
651 err
= snd_pcm_prepare (handle
);
653 alsa_logerr2 (err
, typ
, "Could not prepare handle %p\n", handle
);
657 if (!in
&& conf
.threshold
) {
658 snd_pcm_uframes_t threshold
;
661 bytes_per_sec
= freq
<< (nchannels
== 2);
679 threshold
= (conf
.threshold
* bytes_per_sec
) / 1000;
680 alsa_set_threshold (handle
, threshold
);
683 obt
->nchannels
= nchannels
;
685 obt
->samples
= obt_buffer_size
;
690 (obtfmt
!= req
->fmt
||
691 obt
->nchannels
!= req
->nchannels
||
692 obt
->freq
!= req
->freq
)) {
693 dolog ("Audio parameters for %s\n", typ
);
694 alsa_dump_info (req
, obt
, obtfmt
);
698 alsa_dump_info (req
, obt
, obtfmt
);
703 alsa_anal_close1 (&handle
);
707 static snd_pcm_sframes_t
alsa_get_avail (snd_pcm_t
*handle
)
709 snd_pcm_sframes_t avail
;
711 avail
= snd_pcm_avail_update (handle
);
713 if (avail
== -EPIPE
) {
714 if (!alsa_recover (handle
)) {
715 avail
= snd_pcm_avail_update (handle
);
721 "Could not obtain number of available frames\n");
729 static void alsa_write_pending (ALSAVoiceOut
*alsa
)
731 HWVoiceOut
*hw
= &alsa
->hw
;
733 while (alsa
->pending
) {
734 int left_till_end_samples
= hw
->samples
- alsa
->wpos
;
735 int len
= audio_MIN (alsa
->pending
, left_till_end_samples
);
736 char *src
= advance (alsa
->pcm_buf
, alsa
->wpos
<< hw
->info
.shift
);
739 snd_pcm_sframes_t written
;
741 written
= snd_pcm_writei (alsa
->handle
, src
, len
);
747 dolog ("Failed to write %d frames (wrote zero)\n", len
);
752 if (alsa_recover (alsa
->handle
)) {
753 alsa_logerr (written
, "Failed to write %d frames\n",
758 dolog ("Recovering from playback xrun\n");
763 /* stream is suspended and waiting for an
764 application recovery */
765 if (alsa_resume (alsa
->handle
)) {
766 alsa_logerr (written
, "Failed to write %d frames\n",
771 dolog ("Resuming suspended output stream\n");
779 alsa_logerr (written
, "Failed to write %d frames from %p\n",
785 alsa
->wpos
= (alsa
->wpos
+ written
) % hw
->samples
;
786 alsa
->pending
-= written
;
792 static int alsa_run_out (HWVoiceOut
*hw
, int live
)
794 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
796 snd_pcm_sframes_t avail
;
798 avail
= alsa_get_avail (alsa
->handle
);
800 dolog ("Could not get number of available playback frames\n");
804 decr
= audio_MIN (live
, avail
);
805 decr
= audio_pcm_hw_clip_out (hw
, alsa
->pcm_buf
, decr
, alsa
->pending
);
806 alsa
->pending
+= decr
;
807 alsa_write_pending (alsa
);
811 static void alsa_fini_out (HWVoiceOut
*hw
)
813 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
815 ldebug ("alsa_fini\n");
816 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
818 g_free(alsa
->pcm_buf
);
819 alsa
->pcm_buf
= NULL
;
822 static int alsa_init_out (HWVoiceOut
*hw
, struct audsettings
*as
)
824 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
825 struct alsa_params_req req
;
826 struct alsa_params_obt obt
;
828 struct audsettings obt_as
;
830 req
.fmt
= aud_to_alsafmt (as
->fmt
, as
->endianness
);
832 req
.nchannels
= as
->nchannels
;
833 req
.period_size
= conf
.period_size_out
;
834 req
.buffer_size
= conf
.buffer_size_out
;
835 req
.size_in_usec
= conf
.size_in_usec_out
;
837 (conf
.period_size_out_overridden
? 1 : 0) |
838 (conf
.buffer_size_out_overridden
? 2 : 0);
840 if (alsa_open (0, &req
, &obt
, &handle
)) {
844 obt_as
.freq
= obt
.freq
;
845 obt_as
.nchannels
= obt
.nchannels
;
846 obt_as
.fmt
= obt
.fmt
;
847 obt_as
.endianness
= obt
.endianness
;
849 audio_pcm_init_info (&hw
->info
, &obt_as
);
850 hw
->samples
= obt
.samples
;
852 alsa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, obt
.samples
, 1 << hw
->info
.shift
);
853 if (!alsa
->pcm_buf
) {
854 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
855 hw
->samples
, 1 << hw
->info
.shift
);
856 alsa_anal_close1 (&handle
);
860 alsa
->handle
= handle
;
864 #define VOICE_CTL_PAUSE 0
865 #define VOICE_CTL_PREPARE 1
866 #define VOICE_CTL_START 2
868 static int alsa_voice_ctl (snd_pcm_t
*handle
, const char *typ
, int ctl
)
872 if (ctl
== VOICE_CTL_PAUSE
) {
873 err
= snd_pcm_drop (handle
);
875 alsa_logerr (err
, "Could not stop %s\n", typ
);
880 err
= snd_pcm_prepare (handle
);
882 alsa_logerr (err
, "Could not prepare handle for %s\n", typ
);
885 if (ctl
== VOICE_CTL_START
) {
886 err
= snd_pcm_start(handle
);
888 alsa_logerr (err
, "Could not start handle for %s\n", typ
);
897 static int alsa_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
899 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
908 poll_mode
= va_arg (ap
, int);
911 ldebug ("enabling voice\n");
912 if (poll_mode
&& alsa_poll_out (hw
)) {
915 hw
->poll_mode
= poll_mode
;
916 return alsa_voice_ctl (alsa
->handle
, "playback", VOICE_CTL_PREPARE
);
920 ldebug ("disabling voice\n");
923 alsa_fini_poll (&alsa
->pollhlp
);
925 return alsa_voice_ctl (alsa
->handle
, "playback", VOICE_CTL_PAUSE
);
931 static int alsa_init_in (HWVoiceIn
*hw
, struct audsettings
*as
)
933 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
934 struct alsa_params_req req
;
935 struct alsa_params_obt obt
;
937 struct audsettings obt_as
;
939 req
.fmt
= aud_to_alsafmt (as
->fmt
, as
->endianness
);
941 req
.nchannels
= as
->nchannels
;
942 req
.period_size
= conf
.period_size_in
;
943 req
.buffer_size
= conf
.buffer_size_in
;
944 req
.size_in_usec
= conf
.size_in_usec_in
;
946 (conf
.period_size_in_overridden
? 1 : 0) |
947 (conf
.buffer_size_in_overridden
? 2 : 0);
949 if (alsa_open (1, &req
, &obt
, &handle
)) {
953 obt_as
.freq
= obt
.freq
;
954 obt_as
.nchannels
= obt
.nchannels
;
955 obt_as
.fmt
= obt
.fmt
;
956 obt_as
.endianness
= obt
.endianness
;
958 audio_pcm_init_info (&hw
->info
, &obt_as
);
959 hw
->samples
= obt
.samples
;
961 alsa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
962 if (!alsa
->pcm_buf
) {
963 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
964 hw
->samples
, 1 << hw
->info
.shift
);
965 alsa_anal_close1 (&handle
);
969 alsa
->handle
= handle
;
973 static void alsa_fini_in (HWVoiceIn
*hw
)
975 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
977 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
979 g_free(alsa
->pcm_buf
);
980 alsa
->pcm_buf
= NULL
;
983 static int alsa_run_in (HWVoiceIn
*hw
)
985 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
986 int hwshift
= hw
->info
.shift
;
988 int live
= audio_pcm_hw_get_live_in (hw
);
989 int dead
= hw
->samples
- live
;
995 { .add
= hw
->wpos
, .len
= 0 },
996 { .add
= 0, .len
= 0 }
998 snd_pcm_sframes_t avail
;
999 snd_pcm_uframes_t read_samples
= 0;
1005 avail
= alsa_get_avail (alsa
->handle
);
1007 dolog ("Could not get number of captured frames\n");
1012 snd_pcm_state_t state
;
1014 state
= snd_pcm_state (alsa
->handle
);
1016 case SND_PCM_STATE_PREPARED
:
1017 avail
= hw
->samples
;
1019 case SND_PCM_STATE_SUSPENDED
:
1020 /* stream is suspended and waiting for an application recovery */
1021 if (alsa_resume (alsa
->handle
)) {
1022 dolog ("Failed to resume suspended input stream\n");
1026 dolog ("Resuming suspended input stream\n");
1031 dolog ("No frames available and ALSA state is %d\n", state
);
1037 decr
= audio_MIN (dead
, avail
);
1042 if (hw
->wpos
+ decr
> hw
->samples
) {
1043 bufs
[0].len
= (hw
->samples
- hw
->wpos
);
1044 bufs
[1].len
= (decr
- (hw
->samples
- hw
->wpos
));
1050 for (i
= 0; i
< 2; ++i
) {
1052 struct st_sample
*dst
;
1053 snd_pcm_sframes_t nread
;
1054 snd_pcm_uframes_t len
;
1058 src
= advance (alsa
->pcm_buf
, bufs
[i
].add
<< hwshift
);
1059 dst
= hw
->conv_buf
+ bufs
[i
].add
;
1062 nread
= snd_pcm_readi (alsa
->handle
, src
, len
);
1068 dolog ("Failed to read %ld frames (read zero)\n", len
);
1073 if (alsa_recover (alsa
->handle
)) {
1074 alsa_logerr (nread
, "Failed to read %ld frames\n", len
);
1078 dolog ("Recovering from capture xrun\n");
1088 "Failed to read %ld frames from %p\n",
1096 hw
->conv (dst
, src
, nread
);
1098 src
= advance (src
, nread
<< hwshift
);
1101 read_samples
+= nread
;
1107 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
1108 return read_samples
;
1111 static int alsa_read (SWVoiceIn
*sw
, void *buf
, int size
)
1113 return audio_pcm_sw_read (sw
, buf
, size
);
1116 static int alsa_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
1118 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
1127 poll_mode
= va_arg (ap
, int);
1130 ldebug ("enabling voice\n");
1131 if (poll_mode
&& alsa_poll_in (hw
)) {
1134 hw
->poll_mode
= poll_mode
;
1136 return alsa_voice_ctl (alsa
->handle
, "capture", VOICE_CTL_START
);
1140 ldebug ("disabling voice\n");
1141 if (hw
->poll_mode
) {
1143 alsa_fini_poll (&alsa
->pollhlp
);
1145 return alsa_voice_ctl (alsa
->handle
, "capture", VOICE_CTL_PAUSE
);
1151 static void *alsa_audio_init (void)
1156 static void alsa_audio_fini (void *opaque
)
1161 static struct audio_option alsa_options
[] = {
1163 .name
= "DAC_SIZE_IN_USEC",
1164 .tag
= AUD_OPT_BOOL
,
1165 .valp
= &conf
.size_in_usec_out
,
1166 .descr
= "DAC period/buffer size in microseconds (otherwise in frames)"
1169 .name
= "DAC_PERIOD_SIZE",
1171 .valp
= &conf
.period_size_out
,
1172 .descr
= "DAC period size (0 to go with system default)",
1173 .overriddenp
= &conf
.period_size_out_overridden
1176 .name
= "DAC_BUFFER_SIZE",
1178 .valp
= &conf
.buffer_size_out
,
1179 .descr
= "DAC buffer size (0 to go with system default)",
1180 .overriddenp
= &conf
.buffer_size_out_overridden
1183 .name
= "ADC_SIZE_IN_USEC",
1184 .tag
= AUD_OPT_BOOL
,
1185 .valp
= &conf
.size_in_usec_in
,
1187 "ADC period/buffer size in microseconds (otherwise in frames)"
1190 .name
= "ADC_PERIOD_SIZE",
1192 .valp
= &conf
.period_size_in
,
1193 .descr
= "ADC period size (0 to go with system default)",
1194 .overriddenp
= &conf
.period_size_in_overridden
1197 .name
= "ADC_BUFFER_SIZE",
1199 .valp
= &conf
.buffer_size_in
,
1200 .descr
= "ADC buffer size (0 to go with system default)",
1201 .overriddenp
= &conf
.buffer_size_in_overridden
1204 .name
= "THRESHOLD",
1206 .valp
= &conf
.threshold
,
1207 .descr
= "(undocumented)"
1212 .valp
= &conf
.pcm_name_out
,
1213 .descr
= "DAC device name (for instance dmix)"
1218 .valp
= &conf
.pcm_name_in
,
1219 .descr
= "ADC device name"
1223 .tag
= AUD_OPT_BOOL
,
1224 .valp
= &conf
.verbose
,
1225 .descr
= "Behave in a more verbose way"
1227 { /* End of list */ }
1230 static struct audio_pcm_ops alsa_pcm_ops
= {
1231 .init_out
= alsa_init_out
,
1232 .fini_out
= alsa_fini_out
,
1233 .run_out
= alsa_run_out
,
1234 .write
= alsa_write
,
1235 .ctl_out
= alsa_ctl_out
,
1237 .init_in
= alsa_init_in
,
1238 .fini_in
= alsa_fini_in
,
1239 .run_in
= alsa_run_in
,
1241 .ctl_in
= alsa_ctl_in
,
1244 struct audio_driver alsa_audio_driver
= {
1246 .descr
= "ALSA http://www.alsa-project.org",
1247 .options
= alsa_options
,
1248 .init
= alsa_audio_init
,
1249 .fini
= alsa_audio_fini
,
1250 .pcm_ops
= &alsa_pcm_ops
,
1251 .can_be_default
= 1,
1252 .max_voices_out
= INT_MAX
,
1253 .max_voices_in
= INT_MAX
,
1254 .voice_size_out
= sizeof (ALSAVoiceOut
),
1255 .voice_size_in
= sizeof (ALSAVoiceIn
)