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_XRUN
:
217 alsa_recover (hlp
->handle
);
220 case SND_PCM_STATE_SUSPENDED
:
221 alsa_resume (hlp
->handle
);
224 case SND_PCM_STATE_PREPARED
:
225 audio_run ("alsa run (prepared)");
228 case SND_PCM_STATE_RUNNING
:
229 audio_run ("alsa run (running)");
233 dolog ("Unexpected state %d\n", state
);
237 static int alsa_poll_helper (snd_pcm_t
*handle
, struct pollhlp
*hlp
, int mask
)
242 count
= snd_pcm_poll_descriptors_count (handle
);
244 dolog ("Could not initialize poll mode\n"
245 "Invalid number of poll descriptors %d\n", count
);
249 pfds
= audio_calloc ("alsa_poll_helper", count
, sizeof (*pfds
));
251 dolog ("Could not initialize poll mode\n");
255 err
= snd_pcm_poll_descriptors (handle
, pfds
, count
);
257 alsa_logerr (err
, "Could not initialize poll mode\n"
258 "Could not obtain poll descriptors\n");
263 for (i
= 0; i
< count
; ++i
) {
264 if (pfds
[i
].events
& POLLIN
) {
265 err
= qemu_set_fd_handler (pfds
[i
].fd
, alsa_poll_handler
,
268 if (pfds
[i
].events
& POLLOUT
) {
270 dolog ("POLLOUT %d %d\n", i
, pfds
[i
].fd
);
272 err
= qemu_set_fd_handler (pfds
[i
].fd
, NULL
,
273 alsa_poll_handler
, hlp
);
276 dolog ("Set handler events=%#x index=%d fd=%d err=%d\n",
277 pfds
[i
].events
, i
, pfds
[i
].fd
, err
);
281 dolog ("Failed to set handler events=%#x index=%d fd=%d err=%d\n",
282 pfds
[i
].events
, i
, pfds
[i
].fd
, err
);
285 qemu_set_fd_handler (pfds
[i
].fd
, NULL
, NULL
, NULL
);
293 hlp
->handle
= handle
;
298 static int alsa_poll_out (HWVoiceOut
*hw
)
300 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
302 return alsa_poll_helper (alsa
->handle
, &alsa
->pollhlp
, POLLOUT
);
305 static int alsa_poll_in (HWVoiceIn
*hw
)
307 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
309 return alsa_poll_helper (alsa
->handle
, &alsa
->pollhlp
, POLLIN
);
312 static int alsa_write (SWVoiceOut
*sw
, void *buf
, int len
)
314 return audio_pcm_sw_write (sw
, buf
, len
);
317 static snd_pcm_format_t
aud_to_alsafmt (audfmt_e fmt
)
321 return SND_PCM_FORMAT_S8
;
324 return SND_PCM_FORMAT_U8
;
327 return SND_PCM_FORMAT_S16_LE
;
330 return SND_PCM_FORMAT_U16_LE
;
333 return SND_PCM_FORMAT_S32_LE
;
336 return SND_PCM_FORMAT_U32_LE
;
339 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
343 return SND_PCM_FORMAT_U8
;
347 static int alsa_to_audfmt (snd_pcm_format_t alsafmt
, audfmt_e
*fmt
,
351 case SND_PCM_FORMAT_S8
:
356 case SND_PCM_FORMAT_U8
:
361 case SND_PCM_FORMAT_S16_LE
:
366 case SND_PCM_FORMAT_U16_LE
:
371 case SND_PCM_FORMAT_S16_BE
:
376 case SND_PCM_FORMAT_U16_BE
:
381 case SND_PCM_FORMAT_S32_LE
:
386 case SND_PCM_FORMAT_U32_LE
:
391 case SND_PCM_FORMAT_S32_BE
:
396 case SND_PCM_FORMAT_U32_BE
:
402 dolog ("Unrecognized audio format %d\n", alsafmt
);
409 static void alsa_dump_info (struct alsa_params_req
*req
,
410 struct alsa_params_obt
*obt
)
412 dolog ("parameter | requested value | obtained value\n");
413 dolog ("format | %10d | %10d\n", req
->fmt
, obt
->fmt
);
414 dolog ("channels | %10d | %10d\n",
415 req
->nchannels
, obt
->nchannels
);
416 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
417 dolog ("============================================\n");
418 dolog ("requested: buffer size %d period size %d\n",
419 req
->buffer_size
, req
->period_size
);
420 dolog ("obtained: samples %ld\n", obt
->samples
);
423 static void alsa_set_threshold (snd_pcm_t
*handle
, snd_pcm_uframes_t threshold
)
426 snd_pcm_sw_params_t
*sw_params
;
428 snd_pcm_sw_params_alloca (&sw_params
);
430 err
= snd_pcm_sw_params_current (handle
, sw_params
);
432 dolog ("Could not fully initialize DAC\n");
433 alsa_logerr (err
, "Failed to get current software parameters\n");
437 err
= snd_pcm_sw_params_set_start_threshold (handle
, sw_params
, threshold
);
439 dolog ("Could not fully initialize DAC\n");
440 alsa_logerr (err
, "Failed to set software threshold to %ld\n",
445 err
= snd_pcm_sw_params (handle
, sw_params
);
447 dolog ("Could not fully initialize DAC\n");
448 alsa_logerr (err
, "Failed to set software parameters\n");
453 static int alsa_open (int in
, struct alsa_params_req
*req
,
454 struct alsa_params_obt
*obt
, snd_pcm_t
**handlep
)
457 snd_pcm_hw_params_t
*hw_params
;
460 unsigned int freq
, nchannels
;
461 const char *pcm_name
= in
? conf
.pcm_name_in
: conf
.pcm_name_out
;
462 snd_pcm_uframes_t obt_buffer_size
;
463 const char *typ
= in
? "ADC" : "DAC";
464 snd_pcm_format_t obtfmt
;
467 nchannels
= req
->nchannels
;
468 size_in_usec
= req
->size_in_usec
;
470 snd_pcm_hw_params_alloca (&hw_params
);
475 in
? SND_PCM_STREAM_CAPTURE
: SND_PCM_STREAM_PLAYBACK
,
479 alsa_logerr2 (err
, typ
, "Failed to open `%s':\n", pcm_name
);
483 err
= snd_pcm_hw_params_any (handle
, hw_params
);
485 alsa_logerr2 (err
, typ
, "Failed to initialize hardware parameters\n");
489 err
= snd_pcm_hw_params_set_access (
492 SND_PCM_ACCESS_RW_INTERLEAVED
495 alsa_logerr2 (err
, typ
, "Failed to set access type\n");
499 err
= snd_pcm_hw_params_set_format (handle
, hw_params
, req
->fmt
);
500 if (err
< 0 && conf
.verbose
) {
501 alsa_logerr2 (err
, typ
, "Failed to set format %d\n", req
->fmt
);
504 err
= snd_pcm_hw_params_set_rate_near (handle
, hw_params
, &freq
, 0);
506 alsa_logerr2 (err
, typ
, "Failed to set frequency %d\n", req
->freq
);
510 err
= snd_pcm_hw_params_set_channels_near (
516 alsa_logerr2 (err
, typ
, "Failed to set number of channels %d\n",
521 if (nchannels
!= 1 && nchannels
!= 2) {
522 alsa_logerr2 (err
, typ
,
523 "Can not handle obtained number of channels %d\n",
528 if (req
->buffer_size
) {
533 unsigned int btime
= req
->buffer_size
;
535 err
= snd_pcm_hw_params_set_buffer_time_near (
544 snd_pcm_uframes_t bsize
= req
->buffer_size
;
546 err
= snd_pcm_hw_params_set_buffer_size_near (
554 alsa_logerr2 (err
, typ
, "Failed to set buffer %s to %d\n",
555 size_in_usec
? "time" : "size", req
->buffer_size
);
559 if ((req
->override_mask
& 2) && (obt
- req
->buffer_size
))
560 dolog ("Requested buffer %s %u was rejected, using %lu\n",
561 size_in_usec
? "time" : "size", req
->buffer_size
, obt
);
564 if (req
->period_size
) {
569 unsigned int ptime
= req
->period_size
;
571 err
= snd_pcm_hw_params_set_period_time_near (
581 snd_pcm_uframes_t psize
= req
->period_size
;
583 err
= snd_pcm_hw_params_set_period_size_near (
593 alsa_logerr2 (err
, typ
, "Failed to set period %s to %d\n",
594 size_in_usec
? "time" : "size", req
->period_size
);
598 if (((req
->override_mask
& 1) && (obt
- req
->period_size
)))
599 dolog ("Requested period %s %u was rejected, using %lu\n",
600 size_in_usec
? "time" : "size", req
->period_size
, obt
);
603 err
= snd_pcm_hw_params (handle
, hw_params
);
605 alsa_logerr2 (err
, typ
, "Failed to apply audio parameters\n");
609 err
= snd_pcm_hw_params_get_buffer_size (hw_params
, &obt_buffer_size
);
611 alsa_logerr2 (err
, typ
, "Failed to get buffer size\n");
615 err
= snd_pcm_hw_params_get_format (hw_params
, &obtfmt
);
617 alsa_logerr2 (err
, typ
, "Failed to get format\n");
621 if (alsa_to_audfmt (obtfmt
, &obt
->fmt
, &obt
->endianness
)) {
622 dolog ("Invalid format was returned %d\n", obtfmt
);
626 err
= snd_pcm_prepare (handle
);
628 alsa_logerr2 (err
, typ
, "Could not prepare handle %p\n", handle
);
632 if (!in
&& conf
.threshold
) {
633 snd_pcm_uframes_t threshold
;
636 bytes_per_sec
= freq
<< (nchannels
== 2);
654 threshold
= (conf
.threshold
* bytes_per_sec
) / 1000;
655 alsa_set_threshold (handle
, threshold
);
658 obt
->nchannels
= nchannels
;
660 obt
->samples
= obt_buffer_size
;
665 (obt
->fmt
!= req
->fmt
||
666 obt
->nchannels
!= req
->nchannels
||
667 obt
->freq
!= req
->freq
)) {
668 dolog ("Audio paramters for %s\n", typ
);
669 alsa_dump_info (req
, obt
);
673 alsa_dump_info (req
, obt
);
678 alsa_anal_close1 (&handle
);
682 static snd_pcm_sframes_t
alsa_get_avail (snd_pcm_t
*handle
)
684 snd_pcm_sframes_t avail
;
686 avail
= snd_pcm_avail_update (handle
);
688 if (avail
== -EPIPE
) {
689 if (!alsa_recover (handle
)) {
690 avail
= snd_pcm_avail_update (handle
);
696 "Could not obtain number of available frames\n");
704 static void alsa_write_pending (ALSAVoiceOut
*alsa
)
706 HWVoiceOut
*hw
= &alsa
->hw
;
708 while (alsa
->pending
) {
709 int left_till_end_samples
= hw
->samples
- alsa
->wpos
;
710 int len
= audio_MIN (alsa
->pending
, left_till_end_samples
);
711 char *src
= advance (alsa
->pcm_buf
, alsa
->wpos
<< hw
->info
.shift
);
714 snd_pcm_sframes_t written
;
716 written
= snd_pcm_writei (alsa
->handle
, src
, len
);
722 dolog ("Failed to write %d frames (wrote zero)\n", len
);
727 if (alsa_recover (alsa
->handle
)) {
728 alsa_logerr (written
, "Failed to write %d frames\n",
733 dolog ("Recovering from playback xrun\n");
738 /* stream is suspended and waiting for an
739 application recovery */
740 if (alsa_resume (alsa
->handle
)) {
741 alsa_logerr (written
, "Failed to write %d frames\n",
746 dolog ("Resuming suspended output stream\n");
754 alsa_logerr (written
, "Failed to write %d frames from %p\n",
760 alsa
->wpos
= (alsa
->wpos
+ written
) % hw
->samples
;
761 alsa
->pending
-= written
;
767 static int alsa_run_out (HWVoiceOut
*hw
, int live
)
769 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
771 snd_pcm_sframes_t avail
;
773 avail
= alsa_get_avail (alsa
->handle
);
775 dolog ("Could not get number of available playback frames\n");
779 decr
= audio_MIN (live
, avail
);
780 decr
= audio_pcm_hw_clip_out (hw
, alsa
->pcm_buf
, decr
, alsa
->pending
);
781 alsa
->pending
+= decr
;
782 alsa_write_pending (alsa
);
786 static void alsa_fini_out (HWVoiceOut
*hw
)
788 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
790 ldebug ("alsa_fini\n");
791 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
794 qemu_free (alsa
->pcm_buf
);
795 alsa
->pcm_buf
= NULL
;
799 static int alsa_init_out (HWVoiceOut
*hw
, struct audsettings
*as
)
801 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
802 struct alsa_params_req req
;
803 struct alsa_params_obt obt
;
805 struct audsettings obt_as
;
807 req
.fmt
= aud_to_alsafmt (as
->fmt
);
809 req
.nchannels
= as
->nchannels
;
810 req
.period_size
= conf
.period_size_out
;
811 req
.buffer_size
= conf
.buffer_size_out
;
812 req
.size_in_usec
= conf
.size_in_usec_out
;
814 (conf
.period_size_out_overridden
? 1 : 0) |
815 (conf
.buffer_size_out_overridden
? 2 : 0);
817 if (alsa_open (0, &req
, &obt
, &handle
)) {
821 obt_as
.freq
= obt
.freq
;
822 obt_as
.nchannels
= obt
.nchannels
;
823 obt_as
.fmt
= obt
.fmt
;
824 obt_as
.endianness
= obt
.endianness
;
826 audio_pcm_init_info (&hw
->info
, &obt_as
);
827 hw
->samples
= obt
.samples
;
829 alsa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, obt
.samples
, 1 << hw
->info
.shift
);
830 if (!alsa
->pcm_buf
) {
831 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
832 hw
->samples
, 1 << hw
->info
.shift
);
833 alsa_anal_close1 (&handle
);
837 alsa
->handle
= handle
;
841 static int alsa_voice_ctl (snd_pcm_t
*handle
, const char *typ
, int pause
)
846 err
= snd_pcm_drop (handle
);
848 alsa_logerr (err
, "Could not stop %s\n", typ
);
853 err
= snd_pcm_prepare (handle
);
855 alsa_logerr (err
, "Could not prepare handle for %s\n", typ
);
863 static int alsa_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
865 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
874 poll_mode
= va_arg (ap
, int);
877 ldebug ("enabling voice\n");
878 if (poll_mode
&& alsa_poll_out (hw
)) {
881 hw
->poll_mode
= poll_mode
;
882 return alsa_voice_ctl (alsa
->handle
, "playback", 0);
886 ldebug ("disabling voice\n");
887 return alsa_voice_ctl (alsa
->handle
, "playback", 1);
893 static int alsa_init_in (HWVoiceIn
*hw
, struct audsettings
*as
)
895 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
896 struct alsa_params_req req
;
897 struct alsa_params_obt obt
;
899 struct audsettings obt_as
;
901 req
.fmt
= aud_to_alsafmt (as
->fmt
);
903 req
.nchannels
= as
->nchannels
;
904 req
.period_size
= conf
.period_size_in
;
905 req
.buffer_size
= conf
.buffer_size_in
;
906 req
.size_in_usec
= conf
.size_in_usec_in
;
908 (conf
.period_size_in_overridden
? 1 : 0) |
909 (conf
.buffer_size_in_overridden
? 2 : 0);
911 if (alsa_open (1, &req
, &obt
, &handle
)) {
915 obt_as
.freq
= obt
.freq
;
916 obt_as
.nchannels
= obt
.nchannels
;
917 obt_as
.fmt
= obt
.fmt
;
918 obt_as
.endianness
= obt
.endianness
;
920 audio_pcm_init_info (&hw
->info
, &obt_as
);
921 hw
->samples
= obt
.samples
;
923 alsa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
924 if (!alsa
->pcm_buf
) {
925 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
926 hw
->samples
, 1 << hw
->info
.shift
);
927 alsa_anal_close1 (&handle
);
931 alsa
->handle
= handle
;
935 static void alsa_fini_in (HWVoiceIn
*hw
)
937 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
939 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
942 qemu_free (alsa
->pcm_buf
);
943 alsa
->pcm_buf
= NULL
;
947 static int alsa_run_in (HWVoiceIn
*hw
)
949 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
950 int hwshift
= hw
->info
.shift
;
952 int live
= audio_pcm_hw_get_live_in (hw
);
953 int dead
= hw
->samples
- live
;
959 { .add
= hw
->wpos
, .len
= 0 },
960 { .add
= 0, .len
= 0 }
962 snd_pcm_sframes_t avail
;
963 snd_pcm_uframes_t read_samples
= 0;
969 avail
= alsa_get_avail (alsa
->handle
);
971 dolog ("Could not get number of captured frames\n");
976 snd_pcm_state_t state
;
978 state
= snd_pcm_state (alsa
->handle
);
980 case SND_PCM_STATE_PREPARED
:
983 case SND_PCM_STATE_SUSPENDED
:
984 /* stream is suspended and waiting for an application recovery */
985 if (alsa_resume (alsa
->handle
)) {
986 dolog ("Failed to resume suspended input stream\n");
990 dolog ("Resuming suspended input stream\n");
995 dolog ("No frames available and ALSA state is %d\n", state
);
1001 decr
= audio_MIN (dead
, avail
);
1006 if (hw
->wpos
+ decr
> hw
->samples
) {
1007 bufs
[0].len
= (hw
->samples
- hw
->wpos
);
1008 bufs
[1].len
= (decr
- (hw
->samples
- hw
->wpos
));
1014 for (i
= 0; i
< 2; ++i
) {
1016 struct st_sample
*dst
;
1017 snd_pcm_sframes_t nread
;
1018 snd_pcm_uframes_t len
;
1022 src
= advance (alsa
->pcm_buf
, bufs
[i
].add
<< hwshift
);
1023 dst
= hw
->conv_buf
+ bufs
[i
].add
;
1026 nread
= snd_pcm_readi (alsa
->handle
, src
, len
);
1032 dolog ("Failed to read %ld frames (read zero)\n", len
);
1037 if (alsa_recover (alsa
->handle
)) {
1038 alsa_logerr (nread
, "Failed to read %ld frames\n", len
);
1042 dolog ("Recovering from capture xrun\n");
1052 "Failed to read %ld frames from %p\n",
1060 hw
->conv (dst
, src
, nread
, &nominal_volume
);
1062 src
= advance (src
, nread
<< hwshift
);
1065 read_samples
+= nread
;
1071 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
1072 return read_samples
;
1075 static int alsa_read (SWVoiceIn
*sw
, void *buf
, int size
)
1077 return audio_pcm_sw_read (sw
, buf
, size
);
1080 static int alsa_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
1082 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
1091 poll_mode
= va_arg (ap
, int);
1094 ldebug ("enabling voice\n");
1095 if (poll_mode
&& alsa_poll_in (hw
)) {
1098 hw
->poll_mode
= poll_mode
;
1100 return alsa_voice_ctl (alsa
->handle
, "capture", 0);
1104 ldebug ("disabling voice\n");
1105 if (hw
->poll_mode
) {
1107 alsa_fini_poll (&alsa
->pollhlp
);
1109 return alsa_voice_ctl (alsa
->handle
, "capture", 1);
1115 static void *alsa_audio_init (void)
1120 static void alsa_audio_fini (void *opaque
)
1125 static struct audio_option alsa_options
[] = {
1127 .name
= "DAC_SIZE_IN_USEC",
1128 .tag
= AUD_OPT_BOOL
,
1129 .valp
= &conf
.size_in_usec_out
,
1130 .descr
= "DAC period/buffer size in microseconds (otherwise in frames)"
1133 .name
= "DAC_PERIOD_SIZE",
1135 .valp
= &conf
.period_size_out
,
1136 .descr
= "DAC period size (0 to go with system default)",
1137 .overriddenp
= &conf
.period_size_out_overridden
1140 .name
= "DAC_BUFFER_SIZE",
1142 .valp
= &conf
.buffer_size_out
,
1143 .descr
= "DAC buffer size (0 to go with system default)",
1144 .overriddenp
= &conf
.buffer_size_out_overridden
1147 .name
= "ADC_SIZE_IN_USEC",
1148 .tag
= AUD_OPT_BOOL
,
1149 .valp
= &conf
.size_in_usec_in
,
1151 "ADC period/buffer size in microseconds (otherwise in frames)"
1154 .name
= "ADC_PERIOD_SIZE",
1156 .valp
= &conf
.period_size_in
,
1157 .descr
= "ADC period size (0 to go with system default)",
1158 .overriddenp
= &conf
.period_size_in_overridden
1161 .name
= "ADC_BUFFER_SIZE",
1163 .valp
= &conf
.buffer_size_in
,
1164 .descr
= "ADC buffer size (0 to go with system default)",
1165 .overriddenp
= &conf
.buffer_size_in_overridden
1168 .name
= "THRESHOLD",
1170 .valp
= &conf
.threshold
,
1171 .descr
= "(undocumented)"
1176 .valp
= &conf
.pcm_name_out
,
1177 .descr
= "DAC device name (for instance dmix)"
1182 .valp
= &conf
.pcm_name_in
,
1183 .descr
= "ADC device name"
1187 .tag
= AUD_OPT_BOOL
,
1188 .valp
= &conf
.verbose
,
1189 .descr
= "Behave in a more verbose way"
1191 { /* End of list */ }
1194 static struct audio_pcm_ops alsa_pcm_ops
= {
1195 .init_out
= alsa_init_out
,
1196 .fini_out
= alsa_fini_out
,
1197 .run_out
= alsa_run_out
,
1198 .write
= alsa_write
,
1199 .ctl_out
= alsa_ctl_out
,
1201 .init_in
= alsa_init_in
,
1202 .fini_in
= alsa_fini_in
,
1203 .run_in
= alsa_run_in
,
1205 .ctl_in
= alsa_ctl_in
,
1208 struct audio_driver alsa_audio_driver
= {
1210 .descr
= "ALSA http://www.alsa-project.org",
1211 .options
= alsa_options
,
1212 .init
= alsa_audio_init
,
1213 .fini
= alsa_audio_fini
,
1214 .pcm_ops
= &alsa_pcm_ops
,
1215 .can_be_default
= 1,
1216 .max_voices_out
= INT_MAX
,
1217 .max_voices_in
= INT_MAX
,
1218 .voice_size_out
= sizeof (ALSAVoiceOut
),
1219 .voice_size_in
= sizeof (ALSAVoiceIn
)