]> code.delx.au - pulseaudio/blob - polyp/alsa-util.c
Make the whole stuff LGPL only
[pulseaudio] / polyp / alsa-util.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser 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.
10
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.
15
16 You should have received a copy of the GNU Lesser 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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <asoundlib.h>
27
28 #include "alsa-util.h"
29 #include "sample.h"
30 #include "xmalloc.h"
31
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) {
33 int ret = 0;
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,
43 };
44
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) {
54 ret = -1;
55 }
56
57 if (hwparams)
58 snd_pcm_hw_params_free(hwparams);
59 return ret;
60 }
61
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) {
63 unsigned i;
64 struct pollfd *pfds, *ppfd;
65 struct pa_io_event **ios;
66 assert(pcm_handle && m && io_events && n_io_events && cb);
67
68 *n_io_events = snd_pcm_poll_descriptors_count(pcm_handle);
69
70 pfds = pa_xmalloc(sizeof(struct pollfd) * *n_io_events);
71 if (snd_pcm_poll_descriptors(pcm_handle, pfds, *n_io_events) < 0) {
72 pa_xfree(pfds);
73 return -1;
74 }
75
76 *io_events = pa_xmalloc(sizeof(void*) * *n_io_events);
77
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);
82 assert(*ios);
83 }
84
85 pa_xfree(pfds);
86 return 0;
87 }
88
89 void pa_free_io_events(struct pa_mainloop_api* m, struct pa_io_event **io_events, unsigned n_io_events) {
90 unsigned i;
91 struct pa_io_event **ios;
92 assert(m && io_events);
93
94 for (ios = io_events, i = 0; i < n_io_events; i++, ios++)
95 m->io_free(*ios);
96 pa_xfree(io_events);
97 }