4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <asoundlib.h>
28 #include "alsa-util.h"
32 int pa_alsa_set_hw_params(snd_pcm_t
*pcm_handle
, struct pa_sample_spec
*ss
, uint32_t *periods
, snd_pcm_uframes_t
*buffer_size
) {
34 snd_pcm_hw_params_t
*hwparams
= NULL
;
35 static const snd_pcm_format_t format_trans
[] = {
36 [PA_SAMPLE_U8
] = SND_PCM_FORMAT_U8
,
37 [PA_SAMPLE_ALAW
] = SND_PCM_FORMAT_A_LAW
,
38 [PA_SAMPLE_ULAW
] = SND_PCM_FORMAT_MU_LAW
,
39 [PA_SAMPLE_S16LE
] = SND_PCM_FORMAT_S16_LE
,
40 [PA_SAMPLE_S16BE
] = SND_PCM_FORMAT_S16_BE
,
41 [PA_SAMPLE_FLOAT32LE
] = SND_PCM_FORMAT_FLOAT_LE
,
42 [PA_SAMPLE_FLOAT32BE
] = SND_PCM_FORMAT_FLOAT_BE
,
45 if (snd_pcm_hw_params_malloc(&hwparams
) < 0 ||
46 snd_pcm_hw_params_any(pcm_handle
, hwparams
) < 0 ||
47 snd_pcm_hw_params_set_access(pcm_handle
, hwparams
, SND_PCM_ACCESS_RW_INTERLEAVED
) < 0 ||
48 snd_pcm_hw_params_set_format(pcm_handle
, hwparams
, format_trans
[ss
->format
]) < 0 ||
49 snd_pcm_hw_params_set_rate_near(pcm_handle
, hwparams
, &ss
->rate
, NULL
) < 0 ||
50 snd_pcm_hw_params_set_channels(pcm_handle
, hwparams
, ss
->channels
) < 0 ||
51 snd_pcm_hw_params_set_periods_near(pcm_handle
, hwparams
, periods
, NULL
) < 0 ||
52 snd_pcm_hw_params_set_buffer_size_near(pcm_handle
, hwparams
, buffer_size
) < 0 ||
53 snd_pcm_hw_params(pcm_handle
, hwparams
) < 0) {
58 snd_pcm_hw_params_free(hwparams
);
62 int pa_create_io_events(snd_pcm_t
*pcm_handle
, struct pa_mainloop_api
* m
, struct pa_io_event
***io_events
, unsigned *n_io_events
, void (*cb
)(struct pa_mainloop_api
*a
, struct pa_io_event
*e
, int fd
, enum pa_io_event_flags events
, void *userdata
), void *userdata
) {
64 struct pollfd
*pfds
, *ppfd
;
65 struct pa_io_event
**ios
;
66 assert(pcm_handle
&& m
&& io_events
&& n_io_events
&& cb
);
68 *n_io_events
= snd_pcm_poll_descriptors_count(pcm_handle
);
70 pfds
= pa_xmalloc(sizeof(struct pollfd
) * *n_io_events
);
71 if (snd_pcm_poll_descriptors(pcm_handle
, pfds
, *n_io_events
) < 0) {
76 *io_events
= pa_xmalloc(sizeof(void*) * *n_io_events
);
78 for (i
= 0, ios
= *io_events
, ppfd
= pfds
; i
< *n_io_events
; i
++, ios
++, ppfd
++) {
79 *ios
= m
->io_new(m
, ppfd
->fd
,
80 ((ppfd
->events
& POLLIN
) ? PA_IO_EVENT_INPUT
: 0) |
81 ((ppfd
->events
& POLLOUT
) ? PA_IO_EVENT_OUTPUT
: 0), cb
, userdata
);
89 void pa_free_io_events(struct pa_mainloop_api
* m
, struct pa_io_event
**io_events
, unsigned n_io_events
) {
91 struct pa_io_event
**ios
;
92 assert(m
&& io_events
);
94 for (ios
= io_events
, i
= 0; i
< n_io_events
; i
++, ios
++)