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
25 #include "qemu/osdep.h"
26 #include <alsa/asoundlib.h>
27 #include "qemu/main-loop.h"
28 #include "qemu/module.h"
32 #pragma GCC diagnostic ignored "-Waddress"
34 #define AUDIO_CAP "alsa"
35 #include "audio_int.h"
44 typedef struct ALSAVoiceOut
{
50 struct pollhlp pollhlp
;
54 typedef struct ALSAVoiceIn
{
58 struct pollhlp pollhlp
;
62 struct alsa_params_req
{
68 struct alsa_params_obt
{
73 snd_pcm_uframes_t samples
;
76 static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err
, const char *fmt
, ...)
81 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
84 AUD_log (AUDIO_CAP
, "Reason: %s\n", snd_strerror (err
));
87 static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
96 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
99 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
102 AUD_log (AUDIO_CAP
, "Reason: %s\n", snd_strerror (err
));
105 static void alsa_fini_poll (struct pollhlp
*hlp
)
108 struct pollfd
*pfds
= hlp
->pfds
;
111 for (i
= 0; i
< hlp
->count
; ++i
) {
112 qemu_set_fd_handler (pfds
[i
].fd
, NULL
, NULL
, NULL
);
121 static void alsa_anal_close1 (snd_pcm_t
**handlep
)
123 int err
= snd_pcm_close (*handlep
);
125 alsa_logerr (err
, "Failed to close PCM handle %p\n", *handlep
);
130 static void alsa_anal_close (snd_pcm_t
**handlep
, struct pollhlp
*hlp
)
132 alsa_fini_poll (hlp
);
133 alsa_anal_close1 (handlep
);
136 static int alsa_recover (snd_pcm_t
*handle
)
138 int err
= snd_pcm_prepare (handle
);
140 alsa_logerr (err
, "Failed to prepare handle %p\n", handle
);
146 static int alsa_resume (snd_pcm_t
*handle
)
148 int err
= snd_pcm_resume (handle
);
150 alsa_logerr (err
, "Failed to resume handle %p\n", handle
);
156 static void alsa_poll_handler (void *opaque
)
159 snd_pcm_state_t state
;
160 struct pollhlp
*hlp
= opaque
;
161 unsigned short revents
;
163 count
= poll (hlp
->pfds
, hlp
->count
, 0);
165 dolog ("alsa_poll_handler: poll %s\n", strerror (errno
));
173 /* XXX: ALSA example uses initial count, not the one returned by
175 err
= snd_pcm_poll_descriptors_revents (hlp
->handle
, hlp
->pfds
,
176 hlp
->count
, &revents
);
178 alsa_logerr (err
, "snd_pcm_poll_descriptors_revents");
182 if (!(revents
& hlp
->mask
)) {
183 trace_alsa_revents(revents
);
187 state
= snd_pcm_state (hlp
->handle
);
189 case SND_PCM_STATE_SETUP
:
190 alsa_recover (hlp
->handle
);
193 case SND_PCM_STATE_XRUN
:
194 alsa_recover (hlp
->handle
);
197 case SND_PCM_STATE_SUSPENDED
:
198 alsa_resume (hlp
->handle
);
201 case SND_PCM_STATE_PREPARED
:
202 audio_run ("alsa run (prepared)");
205 case SND_PCM_STATE_RUNNING
:
206 audio_run ("alsa run (running)");
210 dolog ("Unexpected state %d\n", state
);
214 static int alsa_poll_helper (snd_pcm_t
*handle
, struct pollhlp
*hlp
, int mask
)
219 count
= snd_pcm_poll_descriptors_count (handle
);
221 dolog ("Could not initialize poll mode\n"
222 "Invalid number of poll descriptors %d\n", count
);
226 pfds
= audio_calloc ("alsa_poll_helper", count
, sizeof (*pfds
));
228 dolog ("Could not initialize poll mode\n");
232 err
= snd_pcm_poll_descriptors (handle
, pfds
, count
);
234 alsa_logerr (err
, "Could not initialize poll mode\n"
235 "Could not obtain poll descriptors\n");
240 for (i
= 0; i
< count
; ++i
) {
241 if (pfds
[i
].events
& POLLIN
) {
242 qemu_set_fd_handler (pfds
[i
].fd
, alsa_poll_handler
, NULL
, hlp
);
244 if (pfds
[i
].events
& POLLOUT
) {
245 trace_alsa_pollout(i
, pfds
[i
].fd
);
246 qemu_set_fd_handler (pfds
[i
].fd
, NULL
, alsa_poll_handler
, hlp
);
248 trace_alsa_set_handler(pfds
[i
].events
, i
, pfds
[i
].fd
, err
);
253 hlp
->handle
= handle
;
258 static int alsa_poll_out (HWVoiceOut
*hw
)
260 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
262 return alsa_poll_helper (alsa
->handle
, &alsa
->pollhlp
, POLLOUT
);
265 static int alsa_poll_in (HWVoiceIn
*hw
)
267 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
269 return alsa_poll_helper (alsa
->handle
, &alsa
->pollhlp
, POLLIN
);
272 static int alsa_write (SWVoiceOut
*sw
, void *buf
, int len
)
274 return audio_pcm_sw_write (sw
, buf
, len
);
277 static snd_pcm_format_t
aud_to_alsafmt (AudioFormat fmt
, int endianness
)
280 case AUDIO_FORMAT_S8
:
281 return SND_PCM_FORMAT_S8
;
283 case AUDIO_FORMAT_U8
:
284 return SND_PCM_FORMAT_U8
;
286 case AUDIO_FORMAT_S16
:
288 return SND_PCM_FORMAT_S16_BE
;
291 return SND_PCM_FORMAT_S16_LE
;
294 case AUDIO_FORMAT_U16
:
296 return SND_PCM_FORMAT_U16_BE
;
299 return SND_PCM_FORMAT_U16_LE
;
302 case AUDIO_FORMAT_S32
:
304 return SND_PCM_FORMAT_S32_BE
;
307 return SND_PCM_FORMAT_S32_LE
;
310 case AUDIO_FORMAT_U32
:
312 return SND_PCM_FORMAT_U32_BE
;
315 return SND_PCM_FORMAT_U32_LE
;
319 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
323 return SND_PCM_FORMAT_U8
;
327 static int alsa_to_audfmt (snd_pcm_format_t alsafmt
, AudioFormat
*fmt
,
331 case SND_PCM_FORMAT_S8
:
333 *fmt
= AUDIO_FORMAT_S8
;
336 case SND_PCM_FORMAT_U8
:
338 *fmt
= AUDIO_FORMAT_U8
;
341 case SND_PCM_FORMAT_S16_LE
:
343 *fmt
= AUDIO_FORMAT_S16
;
346 case SND_PCM_FORMAT_U16_LE
:
348 *fmt
= AUDIO_FORMAT_U16
;
351 case SND_PCM_FORMAT_S16_BE
:
353 *fmt
= AUDIO_FORMAT_S16
;
356 case SND_PCM_FORMAT_U16_BE
:
358 *fmt
= AUDIO_FORMAT_U16
;
361 case SND_PCM_FORMAT_S32_LE
:
363 *fmt
= AUDIO_FORMAT_S32
;
366 case SND_PCM_FORMAT_U32_LE
:
368 *fmt
= AUDIO_FORMAT_U32
;
371 case SND_PCM_FORMAT_S32_BE
:
373 *fmt
= AUDIO_FORMAT_S32
;
376 case SND_PCM_FORMAT_U32_BE
:
378 *fmt
= AUDIO_FORMAT_U32
;
382 dolog ("Unrecognized audio format %d\n", alsafmt
);
389 static void alsa_dump_info (struct alsa_params_req
*req
,
390 struct alsa_params_obt
*obt
,
391 snd_pcm_format_t obtfmt
,
392 AudiodevAlsaPerDirectionOptions
*apdo
)
394 dolog("parameter | requested value | obtained value\n");
395 dolog("format | %10d | %10d\n", req
->fmt
, obtfmt
);
396 dolog("channels | %10d | %10d\n",
397 req
->nchannels
, obt
->nchannels
);
398 dolog("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
399 dolog("============================================\n");
400 dolog("requested: buffer len %" PRId32
" period len %" PRId32
"\n",
401 apdo
->buffer_length
, apdo
->period_length
);
402 dolog("obtained: samples %ld\n", obt
->samples
);
405 static void alsa_set_threshold (snd_pcm_t
*handle
, snd_pcm_uframes_t threshold
)
408 snd_pcm_sw_params_t
*sw_params
;
410 snd_pcm_sw_params_alloca (&sw_params
);
412 err
= snd_pcm_sw_params_current (handle
, sw_params
);
414 dolog ("Could not fully initialize DAC\n");
415 alsa_logerr (err
, "Failed to get current software parameters\n");
419 err
= snd_pcm_sw_params_set_start_threshold (handle
, sw_params
, threshold
);
421 dolog ("Could not fully initialize DAC\n");
422 alsa_logerr (err
, "Failed to set software threshold to %ld\n",
427 err
= snd_pcm_sw_params (handle
, sw_params
);
429 dolog ("Could not fully initialize DAC\n");
430 alsa_logerr (err
, "Failed to set software parameters\n");
435 static int alsa_open(bool in
, struct alsa_params_req
*req
,
436 struct alsa_params_obt
*obt
, snd_pcm_t
**handlep
,
439 AudiodevAlsaOptions
*aopts
= &dev
->u
.alsa
;
440 AudiodevAlsaPerDirectionOptions
*apdo
= in
? aopts
->in
: aopts
->out
;
442 snd_pcm_hw_params_t
*hw_params
;
444 unsigned int freq
, nchannels
;
445 const char *pcm_name
= apdo
->has_dev
? apdo
->dev
: "default";
446 snd_pcm_uframes_t obt_buffer_size
;
447 const char *typ
= in
? "ADC" : "DAC";
448 snd_pcm_format_t obtfmt
;
451 nchannels
= req
->nchannels
;
453 snd_pcm_hw_params_alloca (&hw_params
);
458 in
? SND_PCM_STREAM_CAPTURE
: SND_PCM_STREAM_PLAYBACK
,
462 alsa_logerr2 (err
, typ
, "Failed to open `%s':\n", pcm_name
);
466 err
= snd_pcm_hw_params_any (handle
, hw_params
);
468 alsa_logerr2 (err
, typ
, "Failed to initialize hardware parameters\n");
472 err
= snd_pcm_hw_params_set_access (
475 SND_PCM_ACCESS_RW_INTERLEAVED
478 alsa_logerr2 (err
, typ
, "Failed to set access type\n");
482 err
= snd_pcm_hw_params_set_format (handle
, hw_params
, req
->fmt
);
484 alsa_logerr2 (err
, typ
, "Failed to set format %d\n", req
->fmt
);
487 err
= snd_pcm_hw_params_set_rate_near (handle
, hw_params
, &freq
, 0);
489 alsa_logerr2 (err
, typ
, "Failed to set frequency %d\n", req
->freq
);
493 err
= snd_pcm_hw_params_set_channels_near (
499 alsa_logerr2 (err
, typ
, "Failed to set number of channels %d\n",
504 if (nchannels
!= 1 && nchannels
!= 2) {
505 alsa_logerr2 (err
, typ
,
506 "Can not handle obtained number of channels %d\n",
511 if (apdo
->buffer_length
) {
513 unsigned int btime
= apdo
->buffer_length
;
515 err
= snd_pcm_hw_params_set_buffer_time_near(
516 handle
, hw_params
, &btime
, &dir
);
519 alsa_logerr2(err
, typ
, "Failed to set buffer time to %" PRId32
"\n",
520 apdo
->buffer_length
);
524 if (apdo
->has_buffer_length
&& btime
!= apdo
->buffer_length
) {
525 dolog("Requested buffer time %" PRId32
526 " was rejected, using %u\n", apdo
->buffer_length
, btime
);
530 if (apdo
->period_length
) {
532 unsigned int ptime
= apdo
->period_length
;
534 err
= snd_pcm_hw_params_set_period_time_near(handle
, hw_params
, &ptime
,
538 alsa_logerr2(err
, typ
, "Failed to set period time to %" PRId32
"\n",
539 apdo
->period_length
);
543 if (apdo
->has_period_length
&& ptime
!= apdo
->period_length
) {
544 dolog("Requested period time %" PRId32
" was rejected, using %d\n",
545 apdo
->period_length
, ptime
);
549 err
= snd_pcm_hw_params (handle
, hw_params
);
551 alsa_logerr2 (err
, typ
, "Failed to apply audio parameters\n");
555 err
= snd_pcm_hw_params_get_buffer_size (hw_params
, &obt_buffer_size
);
557 alsa_logerr2 (err
, typ
, "Failed to get buffer size\n");
561 err
= snd_pcm_hw_params_get_format (hw_params
, &obtfmt
);
563 alsa_logerr2 (err
, typ
, "Failed to get format\n");
567 if (alsa_to_audfmt (obtfmt
, &obt
->fmt
, &obt
->endianness
)) {
568 dolog ("Invalid format was returned %d\n", obtfmt
);
572 err
= snd_pcm_prepare (handle
);
574 alsa_logerr2 (err
, typ
, "Could not prepare handle %p\n", handle
);
578 if (!in
&& aopts
->has_threshold
&& aopts
->threshold
) {
579 struct audsettings as
= { .freq
= freq
};
582 audio_buffer_frames(qapi_AudiodevAlsaPerDirectionOptions_base(apdo
),
583 &as
, aopts
->threshold
));
586 obt
->nchannels
= nchannels
;
588 obt
->samples
= obt_buffer_size
;
592 if (obtfmt
!= req
->fmt
||
593 obt
->nchannels
!= req
->nchannels
||
594 obt
->freq
!= req
->freq
) {
595 dolog ("Audio parameters for %s\n", typ
);
596 alsa_dump_info(req
, obt
, obtfmt
, apdo
);
600 alsa_dump_info(req
, obt
, obtfmt
, pdo
);
605 alsa_anal_close1 (&handle
);
609 static snd_pcm_sframes_t
alsa_get_avail (snd_pcm_t
*handle
)
611 snd_pcm_sframes_t avail
;
613 avail
= snd_pcm_avail_update (handle
);
615 if (avail
== -EPIPE
) {
616 if (!alsa_recover (handle
)) {
617 avail
= snd_pcm_avail_update (handle
);
623 "Could not obtain number of available frames\n");
631 static void alsa_write_pending (ALSAVoiceOut
*alsa
)
633 HWVoiceOut
*hw
= &alsa
->hw
;
635 while (alsa
->pending
) {
636 int left_till_end_samples
= hw
->samples
- alsa
->wpos
;
637 int len
= audio_MIN (alsa
->pending
, left_till_end_samples
);
638 char *src
= advance (alsa
->pcm_buf
, alsa
->wpos
<< hw
->info
.shift
);
641 snd_pcm_sframes_t written
;
643 written
= snd_pcm_writei (alsa
->handle
, src
, len
);
648 trace_alsa_wrote_zero(len
);
652 if (alsa_recover (alsa
->handle
)) {
653 alsa_logerr (written
, "Failed to write %d frames\n",
657 trace_alsa_xrun_out();
661 /* stream is suspended and waiting for an
662 application recovery */
663 if (alsa_resume (alsa
->handle
)) {
664 alsa_logerr (written
, "Failed to write %d frames\n",
668 trace_alsa_resume_out();
675 alsa_logerr (written
, "Failed to write %d frames from %p\n",
681 alsa
->wpos
= (alsa
->wpos
+ written
) % hw
->samples
;
682 alsa
->pending
-= written
;
688 static int alsa_run_out (HWVoiceOut
*hw
, int live
)
690 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
692 snd_pcm_sframes_t avail
;
694 avail
= alsa_get_avail (alsa
->handle
);
696 dolog ("Could not get number of available playback frames\n");
700 decr
= audio_MIN (live
, avail
);
701 decr
= audio_pcm_hw_clip_out (hw
, alsa
->pcm_buf
, decr
, alsa
->pending
);
702 alsa
->pending
+= decr
;
703 alsa_write_pending (alsa
);
707 static void alsa_fini_out (HWVoiceOut
*hw
)
709 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
711 ldebug ("alsa_fini\n");
712 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
714 g_free(alsa
->pcm_buf
);
715 alsa
->pcm_buf
= NULL
;
718 static int alsa_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
721 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
722 struct alsa_params_req req
;
723 struct alsa_params_obt obt
;
725 struct audsettings obt_as
;
726 Audiodev
*dev
= drv_opaque
;
728 req
.fmt
= aud_to_alsafmt (as
->fmt
, as
->endianness
);
730 req
.nchannels
= as
->nchannels
;
732 if (alsa_open(0, &req
, &obt
, &handle
, dev
)) {
736 obt_as
.freq
= obt
.freq
;
737 obt_as
.nchannels
= obt
.nchannels
;
738 obt_as
.fmt
= obt
.fmt
;
739 obt_as
.endianness
= obt
.endianness
;
741 audio_pcm_init_info (&hw
->info
, &obt_as
);
742 hw
->samples
= obt
.samples
;
744 alsa
->pcm_buf
= audio_calloc(__func__
, obt
.samples
, 1 << hw
->info
.shift
);
745 if (!alsa
->pcm_buf
) {
746 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
747 hw
->samples
, 1 << hw
->info
.shift
);
748 alsa_anal_close1 (&handle
);
752 alsa
->handle
= handle
;
757 #define VOICE_CTL_PAUSE 0
758 #define VOICE_CTL_PREPARE 1
759 #define VOICE_CTL_START 2
761 static int alsa_voice_ctl (snd_pcm_t
*handle
, const char *typ
, int ctl
)
765 if (ctl
== VOICE_CTL_PAUSE
) {
766 err
= snd_pcm_drop (handle
);
768 alsa_logerr (err
, "Could not stop %s\n", typ
);
773 err
= snd_pcm_prepare (handle
);
775 alsa_logerr (err
, "Could not prepare handle for %s\n", typ
);
778 if (ctl
== VOICE_CTL_START
) {
779 err
= snd_pcm_start(handle
);
781 alsa_logerr (err
, "Could not start handle for %s\n", typ
);
790 static int alsa_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
792 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
793 AudiodevAlsaPerDirectionOptions
*apdo
= alsa
->dev
->u
.alsa
.out
;
798 bool poll_mode
= apdo
->try_poll
;
800 ldebug ("enabling voice\n");
801 if (poll_mode
&& alsa_poll_out (hw
)) {
804 hw
->poll_mode
= poll_mode
;
805 return alsa_voice_ctl (alsa
->handle
, "playback", VOICE_CTL_PREPARE
);
809 ldebug ("disabling voice\n");
812 alsa_fini_poll (&alsa
->pollhlp
);
814 return alsa_voice_ctl (alsa
->handle
, "playback", VOICE_CTL_PAUSE
);
820 static int alsa_init_in(HWVoiceIn
*hw
, struct audsettings
*as
, void *drv_opaque
)
822 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
823 struct alsa_params_req req
;
824 struct alsa_params_obt obt
;
826 struct audsettings obt_as
;
827 Audiodev
*dev
= drv_opaque
;
829 req
.fmt
= aud_to_alsafmt (as
->fmt
, as
->endianness
);
831 req
.nchannels
= as
->nchannels
;
833 if (alsa_open(1, &req
, &obt
, &handle
, dev
)) {
837 obt_as
.freq
= obt
.freq
;
838 obt_as
.nchannels
= obt
.nchannels
;
839 obt_as
.fmt
= obt
.fmt
;
840 obt_as
.endianness
= obt
.endianness
;
842 audio_pcm_init_info (&hw
->info
, &obt_as
);
843 hw
->samples
= obt
.samples
;
845 alsa
->pcm_buf
= audio_calloc(__func__
, hw
->samples
, 1 << hw
->info
.shift
);
846 if (!alsa
->pcm_buf
) {
847 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
848 hw
->samples
, 1 << hw
->info
.shift
);
849 alsa_anal_close1 (&handle
);
853 alsa
->handle
= handle
;
858 static void alsa_fini_in (HWVoiceIn
*hw
)
860 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
862 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
864 g_free(alsa
->pcm_buf
);
865 alsa
->pcm_buf
= NULL
;
868 static int alsa_run_in (HWVoiceIn
*hw
)
870 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
871 int hwshift
= hw
->info
.shift
;
873 int live
= audio_pcm_hw_get_live_in (hw
);
874 int dead
= hw
->samples
- live
;
880 { .add
= hw
->wpos
, .len
= 0 },
881 { .add
= 0, .len
= 0 }
883 snd_pcm_sframes_t avail
;
884 snd_pcm_uframes_t read_samples
= 0;
890 avail
= alsa_get_avail (alsa
->handle
);
892 dolog ("Could not get number of captured frames\n");
897 snd_pcm_state_t state
;
899 state
= snd_pcm_state (alsa
->handle
);
901 case SND_PCM_STATE_PREPARED
:
904 case SND_PCM_STATE_SUSPENDED
:
905 /* stream is suspended and waiting for an application recovery */
906 if (alsa_resume (alsa
->handle
)) {
907 dolog ("Failed to resume suspended input stream\n");
910 trace_alsa_resume_in();
913 trace_alsa_no_frames(state
);
918 decr
= audio_MIN (dead
, avail
);
923 if (hw
->wpos
+ decr
> hw
->samples
) {
924 bufs
[0].len
= (hw
->samples
- hw
->wpos
);
925 bufs
[1].len
= (decr
- (hw
->samples
- hw
->wpos
));
931 for (i
= 0; i
< 2; ++i
) {
933 struct st_sample
*dst
;
934 snd_pcm_sframes_t nread
;
935 snd_pcm_uframes_t len
;
939 src
= advance (alsa
->pcm_buf
, bufs
[i
].add
<< hwshift
);
940 dst
= hw
->conv_buf
+ bufs
[i
].add
;
943 nread
= snd_pcm_readi (alsa
->handle
, src
, len
);
948 trace_alsa_read_zero(len
);
952 if (alsa_recover (alsa
->handle
)) {
953 alsa_logerr (nread
, "Failed to read %ld frames\n", len
);
956 trace_alsa_xrun_in();
965 "Failed to read %ld frames from %p\n",
973 hw
->conv (dst
, src
, nread
);
975 src
= advance (src
, nread
<< hwshift
);
978 read_samples
+= nread
;
984 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
988 static int alsa_read (SWVoiceIn
*sw
, void *buf
, int size
)
990 return audio_pcm_sw_read (sw
, buf
, size
);
993 static int alsa_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
995 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
996 AudiodevAlsaPerDirectionOptions
*apdo
= alsa
->dev
->u
.alsa
.in
;
1001 bool poll_mode
= apdo
->try_poll
;
1003 ldebug ("enabling voice\n");
1004 if (poll_mode
&& alsa_poll_in (hw
)) {
1007 hw
->poll_mode
= poll_mode
;
1009 return alsa_voice_ctl (alsa
->handle
, "capture", VOICE_CTL_START
);
1013 ldebug ("disabling voice\n");
1014 if (hw
->poll_mode
) {
1016 alsa_fini_poll (&alsa
->pollhlp
);
1018 return alsa_voice_ctl (alsa
->handle
, "capture", VOICE_CTL_PAUSE
);
1024 static void alsa_init_per_direction(AudiodevAlsaPerDirectionOptions
*apdo
)
1026 if (!apdo
->has_try_poll
) {
1027 apdo
->try_poll
= true;
1028 apdo
->has_try_poll
= true;
1032 static void *alsa_audio_init(Audiodev
*dev
)
1034 AudiodevAlsaOptions
*aopts
;
1035 assert(dev
->driver
== AUDIODEV_DRIVER_ALSA
);
1037 aopts
= &dev
->u
.alsa
;
1038 alsa_init_per_direction(aopts
->in
);
1039 alsa_init_per_direction(aopts
->out
);
1042 * need to define them, as otherwise alsa produces no sound
1043 * doesn't set has_* so alsa_open can identify it wasn't set by the user
1045 if (!dev
->u
.alsa
.out
->has_period_length
) {
1046 /* 1024 frames assuming 44100Hz */
1047 dev
->u
.alsa
.out
->period_length
= 1024 * 1000000 / 44100;
1049 if (!dev
->u
.alsa
.out
->has_buffer_length
) {
1050 /* 4096 frames assuming 44100Hz */
1051 dev
->u
.alsa
.out
->buffer_length
= 4096ll * 1000000 / 44100;
1055 * OptsVisitor sets unspecified optional fields to zero, but do not depend
1058 if (!dev
->u
.alsa
.in
->has_period_length
) {
1059 dev
->u
.alsa
.in
->period_length
= 0;
1061 if (!dev
->u
.alsa
.in
->has_buffer_length
) {
1062 dev
->u
.alsa
.in
->buffer_length
= 0;
1068 static void alsa_audio_fini (void *opaque
)
1072 static struct audio_pcm_ops alsa_pcm_ops
= {
1073 .init_out
= alsa_init_out
,
1074 .fini_out
= alsa_fini_out
,
1075 .run_out
= alsa_run_out
,
1076 .write
= alsa_write
,
1077 .ctl_out
= alsa_ctl_out
,
1079 .init_in
= alsa_init_in
,
1080 .fini_in
= alsa_fini_in
,
1081 .run_in
= alsa_run_in
,
1083 .ctl_in
= alsa_ctl_in
,
1086 static struct audio_driver alsa_audio_driver
= {
1088 .descr
= "ALSA http://www.alsa-project.org",
1089 .init
= alsa_audio_init
,
1090 .fini
= alsa_audio_fini
,
1091 .pcm_ops
= &alsa_pcm_ops
,
1092 .can_be_default
= 1,
1093 .max_voices_out
= INT_MAX
,
1094 .max_voices_in
= INT_MAX
,
1095 .voice_size_out
= sizeof (ALSAVoiceOut
),
1096 .voice_size_in
= sizeof (ALSAVoiceIn
)
1099 static void register_audio_alsa(void)
1101 audio_driver_register(&alsa_audio_driver
);
1103 type_init(register_audio_alsa
);