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
{
47 struct pollhlp pollhlp
;
50 typedef struct ALSAVoiceIn
{
54 struct pollhlp pollhlp
;
60 const char *pcm_name_in
;
61 const char *pcm_name_out
;
62 unsigned int buffer_size_in
;
63 unsigned int period_size_in
;
64 unsigned int buffer_size_out
;
65 unsigned int period_size_out
;
66 unsigned int threshold
;
68 int buffer_size_in_overridden
;
69 int period_size_in_overridden
;
71 int buffer_size_out_overridden
;
72 int period_size_out_overridden
;
75 .buffer_size_out
= 1024,
76 .pcm_name_out
= "default",
77 .pcm_name_in
= "default",
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
)) {
206 dolog ("revents = %d\n", revents
);
211 state
= snd_pcm_state (hlp
->handle
);
213 case SND_PCM_STATE_XRUN
:
214 alsa_recover (hlp
->handle
);
217 case SND_PCM_STATE_SUSPENDED
:
218 alsa_resume (hlp
->handle
);
221 case SND_PCM_STATE_PREPARED
:
222 audio_run ("alsa run (prepared)");
225 case SND_PCM_STATE_RUNNING
:
226 audio_run ("alsa run (running)");
230 dolog ("Unexpected state %d\n", state
);
234 static int alsa_poll_helper (snd_pcm_t
*handle
, struct pollhlp
*hlp
, int mask
)
239 count
= snd_pcm_poll_descriptors_count (handle
);
241 dolog ("Could not initialize poll mode\n"
242 "Invalid number of poll descriptors %d\n", count
);
246 pfds
= audio_calloc ("alsa_poll_helper", count
, sizeof (*pfds
));
248 dolog ("Could not initialize poll mode\n");
252 err
= snd_pcm_poll_descriptors (handle
, pfds
, count
);
254 alsa_logerr (err
, "Could not initialize poll mode\n"
255 "Could not obtain poll descriptors\n");
260 for (i
= 0; i
< count
; ++i
) {
261 if (pfds
[i
].events
& POLLIN
) {
262 err
= qemu_set_fd_handler (pfds
[i
].fd
, alsa_poll_handler
,
265 if (pfds
[i
].events
& POLLOUT
) {
267 dolog ("POLLOUT %d %d\n", i
, pfds
[i
].fd
);
269 err
= qemu_set_fd_handler (pfds
[i
].fd
, NULL
,
270 alsa_poll_handler
, hlp
);
273 dolog ("Set handler events=%#x index=%d fd=%d err=%d\n",
274 pfds
[i
].events
, i
, pfds
[i
].fd
, err
);
278 dolog ("Failed to set handler events=%#x index=%d fd=%d err=%d\n",
279 pfds
[i
].events
, i
, pfds
[i
].fd
, err
);
282 qemu_set_fd_handler (pfds
[i
].fd
, NULL
, NULL
, NULL
);
290 hlp
->handle
= handle
;
295 static int alsa_poll_out (HWVoiceOut
*hw
)
297 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
299 return alsa_poll_helper (alsa
->handle
, &alsa
->pollhlp
, POLLOUT
);
302 static int alsa_poll_in (HWVoiceIn
*hw
)
304 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
306 return alsa_poll_helper (alsa
->handle
, &alsa
->pollhlp
, POLLIN
);
309 static int alsa_write (SWVoiceOut
*sw
, void *buf
, int len
)
311 return audio_pcm_sw_write (sw
, buf
, len
);
314 static snd_pcm_format_t
aud_to_alsafmt (audfmt_e fmt
)
318 return SND_PCM_FORMAT_S8
;
321 return SND_PCM_FORMAT_U8
;
324 return SND_PCM_FORMAT_S16_LE
;
327 return SND_PCM_FORMAT_U16_LE
;
330 return SND_PCM_FORMAT_S32_LE
;
333 return SND_PCM_FORMAT_U32_LE
;
336 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
340 return SND_PCM_FORMAT_U8
;
344 static int alsa_to_audfmt (snd_pcm_format_t alsafmt
, audfmt_e
*fmt
,
348 case SND_PCM_FORMAT_S8
:
353 case SND_PCM_FORMAT_U8
:
358 case SND_PCM_FORMAT_S16_LE
:
363 case SND_PCM_FORMAT_U16_LE
:
368 case SND_PCM_FORMAT_S16_BE
:
373 case SND_PCM_FORMAT_U16_BE
:
378 case SND_PCM_FORMAT_S32_LE
:
383 case SND_PCM_FORMAT_U32_LE
:
388 case SND_PCM_FORMAT_S32_BE
:
393 case SND_PCM_FORMAT_U32_BE
:
399 dolog ("Unrecognized audio format %d\n", alsafmt
);
406 static void alsa_dump_info (struct alsa_params_req
*req
,
407 struct alsa_params_obt
*obt
)
409 dolog ("parameter | requested value | obtained value\n");
410 dolog ("format | %10d | %10d\n", req
->fmt
, obt
->fmt
);
411 dolog ("channels | %10d | %10d\n",
412 req
->nchannels
, obt
->nchannels
);
413 dolog ("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
414 dolog ("============================================\n");
415 dolog ("requested: buffer size %d period size %d\n",
416 req
->buffer_size
, req
->period_size
);
417 dolog ("obtained: samples %ld\n", obt
->samples
);
420 static void alsa_set_threshold (snd_pcm_t
*handle
, snd_pcm_uframes_t threshold
)
423 snd_pcm_sw_params_t
*sw_params
;
425 snd_pcm_sw_params_alloca (&sw_params
);
427 err
= snd_pcm_sw_params_current (handle
, sw_params
);
429 dolog ("Could not fully initialize DAC\n");
430 alsa_logerr (err
, "Failed to get current software parameters\n");
434 err
= snd_pcm_sw_params_set_start_threshold (handle
, sw_params
, threshold
);
436 dolog ("Could not fully initialize DAC\n");
437 alsa_logerr (err
, "Failed to set software threshold to %ld\n",
442 err
= snd_pcm_sw_params (handle
, sw_params
);
444 dolog ("Could not fully initialize DAC\n");
445 alsa_logerr (err
, "Failed to set software parameters\n");
450 static int alsa_open (int in
, struct alsa_params_req
*req
,
451 struct alsa_params_obt
*obt
, snd_pcm_t
**handlep
)
454 snd_pcm_hw_params_t
*hw_params
;
457 unsigned int freq
, nchannels
;
458 const char *pcm_name
= in
? conf
.pcm_name_in
: conf
.pcm_name_out
;
459 snd_pcm_uframes_t obt_buffer_size
;
460 const char *typ
= in
? "ADC" : "DAC";
461 snd_pcm_format_t obtfmt
;
464 nchannels
= req
->nchannels
;
465 size_in_usec
= req
->size_in_usec
;
467 snd_pcm_hw_params_alloca (&hw_params
);
472 in
? SND_PCM_STREAM_CAPTURE
: SND_PCM_STREAM_PLAYBACK
,
476 alsa_logerr2 (err
, typ
, "Failed to open `%s':\n", pcm_name
);
480 err
= snd_pcm_hw_params_any (handle
, hw_params
);
482 alsa_logerr2 (err
, typ
, "Failed to initialize hardware parameters\n");
486 err
= snd_pcm_hw_params_set_access (
489 SND_PCM_ACCESS_RW_INTERLEAVED
492 alsa_logerr2 (err
, typ
, "Failed to set access type\n");
496 err
= snd_pcm_hw_params_set_format (handle
, hw_params
, req
->fmt
);
497 if (err
< 0 && conf
.verbose
) {
498 alsa_logerr2 (err
, typ
, "Failed to set format %d\n", req
->fmt
);
501 err
= snd_pcm_hw_params_set_rate_near (handle
, hw_params
, &freq
, 0);
503 alsa_logerr2 (err
, typ
, "Failed to set frequency %d\n", req
->freq
);
507 err
= snd_pcm_hw_params_set_channels_near (
513 alsa_logerr2 (err
, typ
, "Failed to set number of channels %d\n",
518 if (nchannels
!= 1 && nchannels
!= 2) {
519 alsa_logerr2 (err
, typ
,
520 "Can not handle obtained number of channels %d\n",
525 if (req
->buffer_size
) {
530 unsigned int btime
= req
->buffer_size
;
532 err
= snd_pcm_hw_params_set_buffer_time_near (
541 snd_pcm_uframes_t bsize
= req
->buffer_size
;
543 err
= snd_pcm_hw_params_set_buffer_size_near (
551 alsa_logerr2 (err
, typ
, "Failed to set buffer %s to %d\n",
552 size_in_usec
? "time" : "size", req
->buffer_size
);
556 if ((req
->override_mask
& 2) && (obt
- req
->buffer_size
))
557 dolog ("Requested buffer %s %u was rejected, using %lu\n",
558 size_in_usec
? "time" : "size", req
->buffer_size
, obt
);
561 if (req
->period_size
) {
566 unsigned int ptime
= req
->period_size
;
568 err
= snd_pcm_hw_params_set_period_time_near (
578 snd_pcm_uframes_t psize
= req
->period_size
;
580 err
= snd_pcm_hw_params_set_period_size_near (
590 alsa_logerr2 (err
, typ
, "Failed to set period %s to %d\n",
591 size_in_usec
? "time" : "size", req
->period_size
);
595 if ((req
->override_mask
& 1) && (obt
- req
->period_size
))
596 dolog ("Requested period %s %u was rejected, using %lu\n",
597 size_in_usec
? "time" : "size", req
->period_size
, obt
);
600 err
= snd_pcm_hw_params (handle
, hw_params
);
602 alsa_logerr2 (err
, typ
, "Failed to apply audio parameters\n");
606 err
= snd_pcm_hw_params_get_buffer_size (hw_params
, &obt_buffer_size
);
608 alsa_logerr2 (err
, typ
, "Failed to get buffer size\n");
612 err
= snd_pcm_hw_params_get_format (hw_params
, &obtfmt
);
614 alsa_logerr2 (err
, typ
, "Failed to get format\n");
618 if (alsa_to_audfmt (obtfmt
, &obt
->fmt
, &obt
->endianness
)) {
619 dolog ("Invalid format was returned %d\n", obtfmt
);
623 err
= snd_pcm_prepare (handle
);
625 alsa_logerr2 (err
, typ
, "Could not prepare handle %p\n", handle
);
629 if (!in
&& conf
.threshold
) {
630 snd_pcm_uframes_t threshold
;
633 bytes_per_sec
= freq
<< (nchannels
== 2);
651 threshold
= (conf
.threshold
* bytes_per_sec
) / 1000;
652 alsa_set_threshold (handle
, threshold
);
655 obt
->nchannels
= nchannels
;
657 obt
->samples
= obt_buffer_size
;
662 (obt
->fmt
!= req
->fmt
||
663 obt
->nchannels
!= req
->nchannels
||
664 obt
->freq
!= req
->freq
)) {
665 dolog ("Audio paramters for %s\n", typ
);
666 alsa_dump_info (req
, obt
);
670 alsa_dump_info (req
, obt
);
675 alsa_anal_close1 (&handle
);
679 static snd_pcm_sframes_t
alsa_get_avail (snd_pcm_t
*handle
)
681 snd_pcm_sframes_t avail
;
683 avail
= snd_pcm_avail_update (handle
);
685 if (avail
== -EPIPE
) {
686 if (!alsa_recover (handle
)) {
687 avail
= snd_pcm_avail_update (handle
);
693 "Could not obtain number of available frames\n");
701 static int alsa_run_out (HWVoiceOut
*hw
)
703 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
704 int rpos
, live
, decr
;
707 struct st_sample
*src
;
708 snd_pcm_sframes_t avail
;
710 live
= audio_pcm_hw_get_live_out (hw
);
715 avail
= alsa_get_avail (alsa
->handle
);
717 dolog ("Could not get number of available playback frames\n");
721 decr
= audio_MIN (live
, avail
);
725 int left_till_end_samples
= hw
->samples
- rpos
;
726 int len
= audio_MIN (samples
, left_till_end_samples
);
727 snd_pcm_sframes_t written
;
729 src
= hw
->mix_buf
+ rpos
;
730 dst
= advance (alsa
->pcm_buf
, rpos
<< hw
->info
.shift
);
732 hw
->clip (dst
, src
, len
);
735 written
= snd_pcm_writei (alsa
->handle
, dst
, len
);
741 dolog ("Failed to write %d frames (wrote zero)\n", len
);
746 if (alsa_recover (alsa
->handle
)) {
747 alsa_logerr (written
, "Failed to write %d frames\n",
752 dolog ("Recovering from playback xrun\n");
757 /* stream is suspended and waiting for an
758 application recovery */
759 if (alsa_resume (alsa
->handle
)) {
760 alsa_logerr (written
, "Failed to write %d frames\n",
765 dolog ("Resuming suspended output stream\n");
773 alsa_logerr (written
, "Failed to write %d frames to %p\n",
779 rpos
= (rpos
+ written
) % hw
->samples
;
782 dst
= advance (dst
, written
<< hw
->info
.shift
);
792 static void alsa_fini_out (HWVoiceOut
*hw
)
794 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
796 ldebug ("alsa_fini\n");
797 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
800 qemu_free (alsa
->pcm_buf
);
801 alsa
->pcm_buf
= NULL
;
805 static int alsa_init_out (HWVoiceOut
*hw
, struct audsettings
*as
)
807 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
808 struct alsa_params_req req
;
809 struct alsa_params_obt obt
;
811 struct audsettings obt_as
;
813 req
.fmt
= aud_to_alsafmt (as
->fmt
);
815 req
.nchannels
= as
->nchannels
;
816 req
.period_size
= conf
.period_size_out
;
817 req
.buffer_size
= conf
.buffer_size_out
;
818 req
.size_in_usec
= conf
.size_in_usec_out
;
820 (conf
.period_size_out_overridden
? 1 : 0) |
821 (conf
.buffer_size_out_overridden
? 2 : 0);
823 if (alsa_open (0, &req
, &obt
, &handle
)) {
827 obt_as
.freq
= obt
.freq
;
828 obt_as
.nchannels
= obt
.nchannels
;
829 obt_as
.fmt
= obt
.fmt
;
830 obt_as
.endianness
= obt
.endianness
;
832 audio_pcm_init_info (&hw
->info
, &obt_as
);
833 hw
->samples
= obt
.samples
;
835 alsa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, obt
.samples
, 1 << hw
->info
.shift
);
836 if (!alsa
->pcm_buf
) {
837 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
838 hw
->samples
, 1 << hw
->info
.shift
);
839 alsa_anal_close1 (&handle
);
843 alsa
->handle
= handle
;
847 static int alsa_voice_ctl (snd_pcm_t
*handle
, const char *typ
, int pause
)
852 err
= snd_pcm_drop (handle
);
854 alsa_logerr (err
, "Could not stop %s\n", typ
);
859 err
= snd_pcm_prepare (handle
);
861 alsa_logerr (err
, "Could not prepare handle for %s\n", typ
);
869 static int alsa_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
873 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
876 poll_mode
= va_arg (ap
, int);
881 ldebug ("enabling voice\n");
882 if (poll_mode
&& alsa_poll_out (hw
)) {
885 hw
->poll_mode
= poll_mode
;
886 return alsa_voice_ctl (alsa
->handle
, "playback", 0);
889 ldebug ("disabling voice\n");
890 return alsa_voice_ctl (alsa
->handle
, "playback", 1);
896 static int alsa_init_in (HWVoiceIn
*hw
, struct audsettings
*as
)
898 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
899 struct alsa_params_req req
;
900 struct alsa_params_obt obt
;
902 struct audsettings obt_as
;
904 req
.fmt
= aud_to_alsafmt (as
->fmt
);
906 req
.nchannels
= as
->nchannels
;
907 req
.period_size
= conf
.period_size_in
;
908 req
.buffer_size
= conf
.buffer_size_in
;
909 req
.size_in_usec
= conf
.size_in_usec_in
;
911 (conf
.period_size_in_overridden
? 1 : 0) |
912 (conf
.buffer_size_in_overridden
? 2 : 0);
914 if (alsa_open (1, &req
, &obt
, &handle
)) {
918 obt_as
.freq
= obt
.freq
;
919 obt_as
.nchannels
= obt
.nchannels
;
920 obt_as
.fmt
= obt
.fmt
;
921 obt_as
.endianness
= obt
.endianness
;
923 audio_pcm_init_info (&hw
->info
, &obt_as
);
924 hw
->samples
= obt
.samples
;
926 alsa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
927 if (!alsa
->pcm_buf
) {
928 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
929 hw
->samples
, 1 << hw
->info
.shift
);
930 alsa_anal_close1 (&handle
);
934 alsa
->handle
= handle
;
938 static void alsa_fini_in (HWVoiceIn
*hw
)
940 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
942 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
945 qemu_free (alsa
->pcm_buf
);
946 alsa
->pcm_buf
= NULL
;
950 static int alsa_run_in (HWVoiceIn
*hw
)
952 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
953 int hwshift
= hw
->info
.shift
;
955 int live
= audio_pcm_hw_get_live_in (hw
);
956 int dead
= hw
->samples
- live
;
962 { .add
= hw
->wpos
, .len
= 0 },
963 { .add
= 0, .len
= 0 }
965 snd_pcm_sframes_t avail
;
966 snd_pcm_uframes_t read_samples
= 0;
972 avail
= alsa_get_avail (alsa
->handle
);
974 dolog ("Could not get number of captured frames\n");
979 snd_pcm_state_t state
;
981 state
= snd_pcm_state (alsa
->handle
);
983 case SND_PCM_STATE_PREPARED
:
986 case SND_PCM_STATE_SUSPENDED
:
987 /* stream is suspended and waiting for an application recovery */
988 if (alsa_resume (alsa
->handle
)) {
989 dolog ("Failed to resume suspended input stream\n");
993 dolog ("Resuming suspended input stream\n");
998 dolog ("No frames available and ALSA state is %d\n", state
);
1004 decr
= audio_MIN (dead
, avail
);
1009 if (hw
->wpos
+ decr
> hw
->samples
) {
1010 bufs
[0].len
= (hw
->samples
- hw
->wpos
);
1011 bufs
[1].len
= (decr
- (hw
->samples
- hw
->wpos
));
1017 for (i
= 0; i
< 2; ++i
) {
1019 struct st_sample
*dst
;
1020 snd_pcm_sframes_t nread
;
1021 snd_pcm_uframes_t len
;
1025 src
= advance (alsa
->pcm_buf
, bufs
[i
].add
<< hwshift
);
1026 dst
= hw
->conv_buf
+ bufs
[i
].add
;
1029 nread
= snd_pcm_readi (alsa
->handle
, src
, len
);
1035 dolog ("Failed to read %ld frames (read zero)\n", len
);
1040 if (alsa_recover (alsa
->handle
)) {
1041 alsa_logerr (nread
, "Failed to read %ld frames\n", len
);
1045 dolog ("Recovering from capture xrun\n");
1055 "Failed to read %ld frames from %p\n",
1063 hw
->conv (dst
, src
, nread
, &nominal_volume
);
1065 src
= advance (src
, nread
<< hwshift
);
1068 read_samples
+= nread
;
1074 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
1075 return read_samples
;
1078 static int alsa_read (SWVoiceIn
*sw
, void *buf
, int size
)
1080 return audio_pcm_sw_read (sw
, buf
, size
);
1083 static int alsa_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
1087 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
1090 poll_mode
= va_arg (ap
, int);
1095 ldebug ("enabling voice\n");
1096 if (poll_mode
&& alsa_poll_in (hw
)) {
1099 hw
->poll_mode
= poll_mode
;
1101 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
)