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
= 1024,
78 .pcm_name_out
= "default",
79 .pcm_name_in
= "default",
82 struct alsa_params_req
{
88 unsigned int buffer_size
;
89 unsigned int period_size
;
92 struct alsa_params_obt
{
97 snd_pcm_uframes_t samples
;
100 static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err
, const char *fmt
, ...)
105 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
108 AUD_log (AUDIO_CAP
, "Reason: %s\n", snd_strerror (err
));
111 static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
120 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
123 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
126 AUD_log (AUDIO_CAP
, "Reason: %s\n", snd_strerror (err
));
129 static void alsa_fini_poll (struct pollhlp
*hlp
)
132 struct pollfd
*pfds
= hlp
->pfds
;
135 for (i
= 0; i
< hlp
->count
; ++i
) {
136 qemu_set_fd_handler (pfds
[i
].fd
, NULL
, NULL
, NULL
);
145 static void alsa_anal_close1 (snd_pcm_t
**handlep
)
147 int err
= snd_pcm_close (*handlep
);
149 alsa_logerr (err
, "Failed to close PCM handle %p\n", *handlep
);
154 static void alsa_anal_close (snd_pcm_t
**handlep
, struct pollhlp
*hlp
)
156 alsa_fini_poll (hlp
);
157 alsa_anal_close1 (handlep
);
160 static int alsa_recover (snd_pcm_t
*handle
)
162 int err
= snd_pcm_prepare (handle
);
164 alsa_logerr (err
, "Failed to prepare handle %p\n", handle
);
170 static int alsa_resume (snd_pcm_t
*handle
)
172 int err
= snd_pcm_resume (handle
);
174 alsa_logerr (err
, "Failed to resume handle %p\n", handle
);
180 static void alsa_poll_handler (void *opaque
)
183 snd_pcm_state_t state
;
184 struct pollhlp
*hlp
= opaque
;
185 unsigned short revents
;
187 count
= poll (hlp
->pfds
, hlp
->count
, 0);
189 dolog ("alsa_poll_handler: poll %s\n", strerror (errno
));
197 /* XXX: ALSA example uses initial count, not the one returned by
199 err
= snd_pcm_poll_descriptors_revents (hlp
->handle
, hlp
->pfds
,
200 hlp
->count
, &revents
);
202 alsa_logerr (err
, "snd_pcm_poll_descriptors_revents");
206 if (!(revents
& hlp
->mask
)) {
208 dolog ("revents = %d\n", revents
);
213 state
= snd_pcm_state (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 err
= qemu_set_fd_handler (pfds
[i
].fd
, alsa_poll_handler
,
267 if (pfds
[i
].events
& POLLOUT
) {
269 dolog ("POLLOUT %d %d\n", i
, pfds
[i
].fd
);
271 err
= qemu_set_fd_handler (pfds
[i
].fd
, NULL
,
272 alsa_poll_handler
, hlp
);
275 dolog ("Set handler events=%#x index=%d fd=%d err=%d\n",
276 pfds
[i
].events
, i
, pfds
[i
].fd
, err
);
280 dolog ("Failed to set handler events=%#x index=%d fd=%d err=%d\n",
281 pfds
[i
].events
, i
, pfds
[i
].fd
, err
);
284 qemu_set_fd_handler (pfds
[i
].fd
, NULL
, NULL
, NULL
);
292 hlp
->handle
= handle
;
297 static int alsa_poll_out (HWVoiceOut
*hw
)
299 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
301 return alsa_poll_helper (alsa
->handle
, &alsa
->pollhlp
, POLLOUT
);
304 static int alsa_poll_in (HWVoiceIn
*hw
)
306 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
308 return alsa_poll_helper (alsa
->handle
, &alsa
->pollhlp
, POLLIN
);
311 static int alsa_write (SWVoiceOut
*sw
, void *buf
, int len
)
313 return audio_pcm_sw_write (sw
, buf
, len
);
316 static snd_pcm_format_t
aud_to_alsafmt (audfmt_e fmt
)
320 return SND_PCM_FORMAT_S8
;
323 return SND_PCM_FORMAT_U8
;
326 return SND_PCM_FORMAT_S16_LE
;
329 return SND_PCM_FORMAT_U16_LE
;
332 return SND_PCM_FORMAT_S32_LE
;
335 return SND_PCM_FORMAT_U32_LE
;
338 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
342 return SND_PCM_FORMAT_U8
;
346 static int alsa_to_audfmt (snd_pcm_format_t alsafmt
, audfmt_e
*fmt
,
350 case SND_PCM_FORMAT_S8
:
355 case SND_PCM_FORMAT_U8
:
360 case SND_PCM_FORMAT_S16_LE
:
365 case SND_PCM_FORMAT_U16_LE
:
370 case SND_PCM_FORMAT_S16_BE
:
375 case SND_PCM_FORMAT_U16_BE
:
380 case SND_PCM_FORMAT_S32_LE
:
385 case SND_PCM_FORMAT_U32_LE
:
390 case SND_PCM_FORMAT_S32_BE
:
395 case SND_PCM_FORMAT_U32_BE
:
401 dolog ("Unrecognized audio format %d\n", alsafmt
);
408 static void alsa_dump_info (struct alsa_params_req
*req
,
409 struct alsa_params_obt
*obt
)
411 dolog ("parameter | requested value | obtained value\n");
412 dolog ("format | %10d | %10d\n", req
->fmt
, obt
->fmt
);
413 dolog ("channels | %10d | %10d\n",
414 req
->nchannels
, obt
->nchannels
);
415 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
416 dolog ("============================================\n");
417 dolog ("requested: buffer size %d period size %d\n",
418 req
->buffer_size
, req
->period_size
);
419 dolog ("obtained: samples %ld\n", obt
->samples
);
422 static void alsa_set_threshold (snd_pcm_t
*handle
, snd_pcm_uframes_t threshold
)
425 snd_pcm_sw_params_t
*sw_params
;
427 snd_pcm_sw_params_alloca (&sw_params
);
429 err
= snd_pcm_sw_params_current (handle
, sw_params
);
431 dolog ("Could not fully initialize DAC\n");
432 alsa_logerr (err
, "Failed to get current software parameters\n");
436 err
= snd_pcm_sw_params_set_start_threshold (handle
, sw_params
, threshold
);
438 dolog ("Could not fully initialize DAC\n");
439 alsa_logerr (err
, "Failed to set software threshold to %ld\n",
444 err
= snd_pcm_sw_params (handle
, sw_params
);
446 dolog ("Could not fully initialize DAC\n");
447 alsa_logerr (err
, "Failed to set software parameters\n");
452 static int alsa_open (int in
, struct alsa_params_req
*req
,
453 struct alsa_params_obt
*obt
, snd_pcm_t
**handlep
)
456 snd_pcm_hw_params_t
*hw_params
;
459 unsigned int freq
, nchannels
;
460 const char *pcm_name
= in
? conf
.pcm_name_in
: conf
.pcm_name_out
;
461 snd_pcm_uframes_t obt_buffer_size
;
462 const char *typ
= in
? "ADC" : "DAC";
463 snd_pcm_format_t obtfmt
;
466 nchannels
= req
->nchannels
;
467 size_in_usec
= req
->size_in_usec
;
469 snd_pcm_hw_params_alloca (&hw_params
);
474 in
? SND_PCM_STREAM_CAPTURE
: SND_PCM_STREAM_PLAYBACK
,
478 alsa_logerr2 (err
, typ
, "Failed to open `%s':\n", pcm_name
);
482 err
= snd_pcm_hw_params_any (handle
, hw_params
);
484 alsa_logerr2 (err
, typ
, "Failed to initialize hardware parameters\n");
488 err
= snd_pcm_hw_params_set_access (
491 SND_PCM_ACCESS_RW_INTERLEAVED
494 alsa_logerr2 (err
, typ
, "Failed to set access type\n");
498 err
= snd_pcm_hw_params_set_format (handle
, hw_params
, req
->fmt
);
499 if (err
< 0 && conf
.verbose
) {
500 alsa_logerr2 (err
, typ
, "Failed to set format %d\n", req
->fmt
);
503 err
= snd_pcm_hw_params_set_rate_near (handle
, hw_params
, &freq
, 0);
505 alsa_logerr2 (err
, typ
, "Failed to set frequency %d\n", req
->freq
);
509 err
= snd_pcm_hw_params_set_channels_near (
515 alsa_logerr2 (err
, typ
, "Failed to set number of channels %d\n",
520 if (nchannels
!= 1 && nchannels
!= 2) {
521 alsa_logerr2 (err
, typ
,
522 "Can not handle obtained number of channels %d\n",
527 if (req
->buffer_size
) {
532 unsigned int btime
= req
->buffer_size
;
534 err
= snd_pcm_hw_params_set_buffer_time_near (
543 snd_pcm_uframes_t bsize
= req
->buffer_size
;
545 err
= snd_pcm_hw_params_set_buffer_size_near (
553 alsa_logerr2 (err
, typ
, "Failed to set buffer %s to %d\n",
554 size_in_usec
? "time" : "size", req
->buffer_size
);
558 if ((req
->override_mask
& 2) && (obt
- req
->buffer_size
))
559 dolog ("Requested buffer %s %u was rejected, using %lu\n",
560 size_in_usec
? "time" : "size", req
->buffer_size
, obt
);
563 if (req
->period_size
) {
568 unsigned int ptime
= req
->period_size
;
570 err
= snd_pcm_hw_params_set_period_time_near (
580 snd_pcm_uframes_t psize
= req
->period_size
;
582 err
= snd_pcm_hw_params_set_period_size_near (
592 alsa_logerr2 (err
, typ
, "Failed to set period %s to %d\n",
593 size_in_usec
? "time" : "size", req
->period_size
);
597 if (((req
->override_mask
& 1) && (obt
- req
->period_size
)))
598 dolog ("Requested period %s %u was rejected, using %lu\n",
599 size_in_usec
? "time" : "size", req
->period_size
, obt
);
602 err
= snd_pcm_hw_params (handle
, hw_params
);
604 alsa_logerr2 (err
, typ
, "Failed to apply audio parameters\n");
608 err
= snd_pcm_hw_params_get_buffer_size (hw_params
, &obt_buffer_size
);
610 alsa_logerr2 (err
, typ
, "Failed to get buffer size\n");
614 err
= snd_pcm_hw_params_get_format (hw_params
, &obtfmt
);
616 alsa_logerr2 (err
, typ
, "Failed to get format\n");
620 if (alsa_to_audfmt (obtfmt
, &obt
->fmt
, &obt
->endianness
)) {
621 dolog ("Invalid format was returned %d\n", obtfmt
);
625 err
= snd_pcm_prepare (handle
);
627 alsa_logerr2 (err
, typ
, "Could not prepare handle %p\n", handle
);
631 if (!in
&& conf
.threshold
) {
632 snd_pcm_uframes_t threshold
;
635 bytes_per_sec
= freq
<< (nchannels
== 2);
653 threshold
= (conf
.threshold
* bytes_per_sec
) / 1000;
654 alsa_set_threshold (handle
, threshold
);
657 obt
->nchannels
= nchannels
;
659 obt
->samples
= obt_buffer_size
;
664 (obt
->fmt
!= req
->fmt
||
665 obt
->nchannels
!= req
->nchannels
||
666 obt
->freq
!= req
->freq
)) {
667 dolog ("Audio paramters for %s\n", typ
);
668 alsa_dump_info (req
, obt
);
672 alsa_dump_info (req
, obt
);
677 alsa_anal_close1 (&handle
);
681 static snd_pcm_sframes_t
alsa_get_avail (snd_pcm_t
*handle
)
683 snd_pcm_sframes_t avail
;
685 avail
= snd_pcm_avail_update (handle
);
687 if (avail
== -EPIPE
) {
688 if (!alsa_recover (handle
)) {
689 avail
= snd_pcm_avail_update (handle
);
695 "Could not obtain number of available frames\n");
703 static void alsa_write_pending (ALSAVoiceOut
*alsa
)
705 HWVoiceOut
*hw
= &alsa
->hw
;
707 while (alsa
->pending
) {
708 int left_till_end_samples
= hw
->samples
- alsa
->wpos
;
709 int len
= audio_MIN (alsa
->pending
, left_till_end_samples
);
710 char *src
= advance (alsa
->pcm_buf
, alsa
->wpos
<< hw
->info
.shift
);
713 snd_pcm_sframes_t written
;
715 written
= snd_pcm_writei (alsa
->handle
, src
, len
);
721 dolog ("Failed to write %d frames (wrote zero)\n", len
);
726 if (alsa_recover (alsa
->handle
)) {
727 alsa_logerr (written
, "Failed to write %d frames\n",
732 dolog ("Recovering from playback xrun\n");
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",
745 dolog ("Resuming suspended output stream\n");
753 alsa_logerr (written
, "Failed to write %d frames from %p\n",
759 alsa
->wpos
= (alsa
->wpos
+ written
) % hw
->samples
;
760 alsa
->pending
-= written
;
766 static int alsa_run_out (HWVoiceOut
*hw
, int live
)
768 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
770 snd_pcm_sframes_t avail
;
772 avail
= alsa_get_avail (alsa
->handle
);
774 dolog ("Could not get number of available playback frames\n");
778 decr
= audio_MIN (live
, avail
);
779 decr
= audio_pcm_hw_clip_out (hw
, alsa
->pcm_buf
, decr
, alsa
->pending
);
780 alsa
->pending
+= decr
;
781 alsa_write_pending (alsa
);
785 static void alsa_fini_out (HWVoiceOut
*hw
)
787 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
789 ldebug ("alsa_fini\n");
790 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
793 qemu_free (alsa
->pcm_buf
);
794 alsa
->pcm_buf
= NULL
;
798 static int alsa_init_out (HWVoiceOut
*hw
, struct audsettings
*as
)
800 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
801 struct alsa_params_req req
;
802 struct alsa_params_obt obt
;
804 struct audsettings obt_as
;
806 req
.fmt
= aud_to_alsafmt (as
->fmt
);
808 req
.nchannels
= as
->nchannels
;
809 req
.period_size
= conf
.period_size_out
;
810 req
.buffer_size
= conf
.buffer_size_out
;
811 req
.size_in_usec
= conf
.size_in_usec_out
;
813 (conf
.period_size_out_overridden
? 1 : 0) |
814 (conf
.buffer_size_out_overridden
? 2 : 0);
816 if (alsa_open (0, &req
, &obt
, &handle
)) {
820 obt_as
.freq
= obt
.freq
;
821 obt_as
.nchannels
= obt
.nchannels
;
822 obt_as
.fmt
= obt
.fmt
;
823 obt_as
.endianness
= obt
.endianness
;
825 audio_pcm_init_info (&hw
->info
, &obt_as
);
826 hw
->samples
= obt
.samples
;
828 alsa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, obt
.samples
, 1 << hw
->info
.shift
);
829 if (!alsa
->pcm_buf
) {
830 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
831 hw
->samples
, 1 << hw
->info
.shift
);
832 alsa_anal_close1 (&handle
);
836 alsa
->handle
= handle
;
840 static int alsa_voice_ctl (snd_pcm_t
*handle
, const char *typ
, int pause
)
845 err
= snd_pcm_drop (handle
);
847 alsa_logerr (err
, "Could not stop %s\n", typ
);
852 err
= snd_pcm_prepare (handle
);
854 alsa_logerr (err
, "Could not prepare handle for %s\n", typ
);
862 static int alsa_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
866 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
869 poll_mode
= va_arg (ap
, int);
874 ldebug ("enabling voice\n");
875 if (poll_mode
&& alsa_poll_out (hw
)) {
878 hw
->poll_mode
= poll_mode
;
879 return alsa_voice_ctl (alsa
->handle
, "playback", 0);
882 ldebug ("disabling voice\n");
883 return alsa_voice_ctl (alsa
->handle
, "playback", 1);
889 static int alsa_init_in (HWVoiceIn
*hw
, struct audsettings
*as
)
891 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
892 struct alsa_params_req req
;
893 struct alsa_params_obt obt
;
895 struct audsettings obt_as
;
897 req
.fmt
= aud_to_alsafmt (as
->fmt
);
899 req
.nchannels
= as
->nchannels
;
900 req
.period_size
= conf
.period_size_in
;
901 req
.buffer_size
= conf
.buffer_size_in
;
902 req
.size_in_usec
= conf
.size_in_usec_in
;
904 (conf
.period_size_in_overridden
? 1 : 0) |
905 (conf
.buffer_size_in_overridden
? 2 : 0);
907 if (alsa_open (1, &req
, &obt
, &handle
)) {
911 obt_as
.freq
= obt
.freq
;
912 obt_as
.nchannels
= obt
.nchannels
;
913 obt_as
.fmt
= obt
.fmt
;
914 obt_as
.endianness
= obt
.endianness
;
916 audio_pcm_init_info (&hw
->info
, &obt_as
);
917 hw
->samples
= obt
.samples
;
919 alsa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
920 if (!alsa
->pcm_buf
) {
921 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
922 hw
->samples
, 1 << hw
->info
.shift
);
923 alsa_anal_close1 (&handle
);
927 alsa
->handle
= handle
;
931 static void alsa_fini_in (HWVoiceIn
*hw
)
933 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
935 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
938 qemu_free (alsa
->pcm_buf
);
939 alsa
->pcm_buf
= NULL
;
943 static int alsa_run_in (HWVoiceIn
*hw
)
945 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
946 int hwshift
= hw
->info
.shift
;
948 int live
= audio_pcm_hw_get_live_in (hw
);
949 int dead
= hw
->samples
- live
;
955 { .add
= hw
->wpos
, .len
= 0 },
956 { .add
= 0, .len
= 0 }
958 snd_pcm_sframes_t avail
;
959 snd_pcm_uframes_t read_samples
= 0;
965 avail
= alsa_get_avail (alsa
->handle
);
967 dolog ("Could not get number of captured frames\n");
972 snd_pcm_state_t state
;
974 state
= snd_pcm_state (alsa
->handle
);
976 case SND_PCM_STATE_PREPARED
:
979 case SND_PCM_STATE_SUSPENDED
:
980 /* stream is suspended and waiting for an application recovery */
981 if (alsa_resume (alsa
->handle
)) {
982 dolog ("Failed to resume suspended input stream\n");
986 dolog ("Resuming suspended input stream\n");
991 dolog ("No frames available and ALSA state is %d\n", state
);
997 decr
= audio_MIN (dead
, avail
);
1002 if (hw
->wpos
+ decr
> hw
->samples
) {
1003 bufs
[0].len
= (hw
->samples
- hw
->wpos
);
1004 bufs
[1].len
= (decr
- (hw
->samples
- hw
->wpos
));
1010 for (i
= 0; i
< 2; ++i
) {
1012 struct st_sample
*dst
;
1013 snd_pcm_sframes_t nread
;
1014 snd_pcm_uframes_t len
;
1018 src
= advance (alsa
->pcm_buf
, bufs
[i
].add
<< hwshift
);
1019 dst
= hw
->conv_buf
+ bufs
[i
].add
;
1022 nread
= snd_pcm_readi (alsa
->handle
, src
, len
);
1028 dolog ("Failed to read %ld frames (read zero)\n", len
);
1033 if (alsa_recover (alsa
->handle
)) {
1034 alsa_logerr (nread
, "Failed to read %ld frames\n", len
);
1038 dolog ("Recovering from capture xrun\n");
1048 "Failed to read %ld frames from %p\n",
1056 hw
->conv (dst
, src
, nread
, &nominal_volume
);
1058 src
= advance (src
, nread
<< hwshift
);
1061 read_samples
+= nread
;
1067 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
1068 return read_samples
;
1071 static int alsa_read (SWVoiceIn
*sw
, void *buf
, int size
)
1073 return audio_pcm_sw_read (sw
, buf
, size
);
1076 static int alsa_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
1080 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
1083 poll_mode
= va_arg (ap
, int);
1088 ldebug ("enabling voice\n");
1089 if (poll_mode
&& alsa_poll_in (hw
)) {
1092 hw
->poll_mode
= poll_mode
;
1094 return alsa_voice_ctl (alsa
->handle
, "capture", 0);
1097 ldebug ("disabling voice\n");
1098 if (hw
->poll_mode
) {
1100 alsa_fini_poll (&alsa
->pollhlp
);
1102 return alsa_voice_ctl (alsa
->handle
, "capture", 1);
1108 static void *alsa_audio_init (void)
1113 static void alsa_audio_fini (void *opaque
)
1118 static struct audio_option alsa_options
[] = {
1120 .name
= "DAC_SIZE_IN_USEC",
1121 .tag
= AUD_OPT_BOOL
,
1122 .valp
= &conf
.size_in_usec_out
,
1123 .descr
= "DAC period/buffer size in microseconds (otherwise in frames)"
1126 .name
= "DAC_PERIOD_SIZE",
1128 .valp
= &conf
.period_size_out
,
1129 .descr
= "DAC period size (0 to go with system default)",
1130 .overriddenp
= &conf
.period_size_out_overridden
1133 .name
= "DAC_BUFFER_SIZE",
1135 .valp
= &conf
.buffer_size_out
,
1136 .descr
= "DAC buffer size (0 to go with system default)",
1137 .overriddenp
= &conf
.buffer_size_out_overridden
1140 .name
= "ADC_SIZE_IN_USEC",
1141 .tag
= AUD_OPT_BOOL
,
1142 .valp
= &conf
.size_in_usec_in
,
1144 "ADC period/buffer size in microseconds (otherwise in frames)"
1147 .name
= "ADC_PERIOD_SIZE",
1149 .valp
= &conf
.period_size_in
,
1150 .descr
= "ADC period size (0 to go with system default)",
1151 .overriddenp
= &conf
.period_size_in_overridden
1154 .name
= "ADC_BUFFER_SIZE",
1156 .valp
= &conf
.buffer_size_in
,
1157 .descr
= "ADC buffer size (0 to go with system default)",
1158 .overriddenp
= &conf
.buffer_size_in_overridden
1161 .name
= "THRESHOLD",
1163 .valp
= &conf
.threshold
,
1164 .descr
= "(undocumented)"
1169 .valp
= &conf
.pcm_name_out
,
1170 .descr
= "DAC device name (for instance dmix)"
1175 .valp
= &conf
.pcm_name_in
,
1176 .descr
= "ADC device name"
1180 .tag
= AUD_OPT_BOOL
,
1181 .valp
= &conf
.verbose
,
1182 .descr
= "Behave in a more verbose way"
1184 { /* End of list */ }
1187 static struct audio_pcm_ops alsa_pcm_ops
= {
1188 .init_out
= alsa_init_out
,
1189 .fini_out
= alsa_fini_out
,
1190 .run_out
= alsa_run_out
,
1191 .write
= alsa_write
,
1192 .ctl_out
= alsa_ctl_out
,
1194 .init_in
= alsa_init_in
,
1195 .fini_in
= alsa_fini_in
,
1196 .run_in
= alsa_run_in
,
1198 .ctl_in
= alsa_ctl_in
,
1201 struct audio_driver alsa_audio_driver
= {
1203 .descr
= "ALSA http://www.alsa-project.org",
1204 .options
= alsa_options
,
1205 .init
= alsa_audio_init
,
1206 .fini
= alsa_audio_fini
,
1207 .pcm_ops
= &alsa_pcm_ops
,
1208 .can_be_default
= 1,
1209 .max_voices_out
= INT_MAX
,
1210 .max_voices_in
= INT_MAX
,
1211 .voice_size_out
= sizeof (ALSAVoiceOut
),
1212 .voice_size_in
= sizeof (ALSAVoiceIn
)