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"
31 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
) {
33 snd_pcm_hw_params_t
*hwparams
= NULL
;
34 static const snd_pcm_format_t format_trans
[] = {
35 [PA_SAMPLE_U8
] = SND_PCM_FORMAT_U8
,
36 [PA_SAMPLE_ALAW
] = SND_PCM_FORMAT_A_LAW
,
37 [PA_SAMPLE_ULAW
] = SND_PCM_FORMAT_MU_LAW
,
38 [PA_SAMPLE_S16LE
] = SND_PCM_FORMAT_S16_LE
,
39 [PA_SAMPLE_S16BE
] = SND_PCM_FORMAT_S16_BE
,
40 [PA_SAMPLE_FLOAT32LE
] = SND_PCM_FORMAT_FLOAT_LE
,
41 [PA_SAMPLE_FLOAT32BE
] = SND_PCM_FORMAT_FLOAT_BE
,
44 if (snd_pcm_hw_params_malloc(&hwparams
) < 0 ||
45 snd_pcm_hw_params_any(pcm_handle
, hwparams
) < 0 ||
46 snd_pcm_hw_params_set_access(pcm_handle
, hwparams
, SND_PCM_ACCESS_RW_INTERLEAVED
) < 0 ||
47 snd_pcm_hw_params_set_format(pcm_handle
, hwparams
, format_trans
[ss
->format
]) < 0 ||
48 snd_pcm_hw_params_set_rate_near(pcm_handle
, hwparams
, &ss
->rate
, NULL
) < 0 ||
49 snd_pcm_hw_params_set_channels(pcm_handle
, hwparams
, ss
->channels
) < 0 ||
50 snd_pcm_hw_params_set_periods_near(pcm_handle
, hwparams
, periods
, NULL
) < 0 ||
51 snd_pcm_hw_params_set_buffer_size_near(pcm_handle
, hwparams
, buffer_size
) < 0 ||
52 snd_pcm_hw_params(pcm_handle
, hwparams
) < 0) {
57 snd_pcm_hw_params_free(hwparams
);
61 int pa_create_io_sources(snd_pcm_t
*pcm_handle
, struct pa_mainloop_api
* m
, void ***io_sources
, unsigned *n_io_sources
, void (*cb
)(struct pa_mainloop_api
*a
, void *id
, int fd
, enum pa_mainloop_api_io_events events
, void *userdata
), void *userdata
) {
63 struct pollfd
*pfds
, *ppfd
;
65 assert(pcm_handle
&& m
&& io_sources
&& n_io_sources
&& cb
);
67 *n_io_sources
= snd_pcm_poll_descriptors_count(pcm_handle
);
69 pfds
= malloc(sizeof(struct pollfd
) * *n_io_sources
);
71 if (snd_pcm_poll_descriptors(pcm_handle
, pfds
, *n_io_sources
) < 0) {
76 *io_sources
= malloc(sizeof(void*) * *n_io_sources
);
79 for (i
= 0, ios
= *io_sources
, ppfd
= pfds
; i
< *n_io_sources
; i
++, ios
++, ppfd
++) {
80 *ios
= m
->source_io(m
, ppfd
->fd
,
81 ((ppfd
->events
& POLLIN
) ? PA_MAINLOOP_API_IO_EVENT_INPUT
: 0) |
82 ((ppfd
->events
& POLLOUT
) ? PA_MAINLOOP_API_IO_EVENT_OUTPUT
: 0), cb
, userdata
);
90 void pa_free_io_sources(struct pa_mainloop_api
* m
, void **io_sources
, unsigned n_io_sources
) {
93 assert(m
&& io_sources
);
95 for (ios
= io_sources
, i
= 0; i
< n_io_sources
; i
++, ios
++)
96 m
->cancel_io(m
, *ios
);