]> code.delx.au - pulseaudio/blob - src/alsa-util.c
rename configuration file
[pulseaudio] / src / 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 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 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
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) {
32 int ret = 0;
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,
42 };
43
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) {
53 ret = -1;
54 }
55
56 if (hwparams)
57 snd_pcm_hw_params_free(hwparams);
58 return ret;
59 }
60
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) {
62 unsigned i;
63 struct pollfd *pfds, *ppfd;
64 void **ios;
65 assert(pcm_handle && m && io_sources && n_io_sources && cb);
66
67 *n_io_sources = snd_pcm_poll_descriptors_count(pcm_handle);
68
69 pfds = malloc(sizeof(struct pollfd) * *n_io_sources);
70 assert(pfds);
71 if (snd_pcm_poll_descriptors(pcm_handle, pfds, *n_io_sources) < 0) {
72 free(pfds);
73 return -1;
74 }
75
76 *io_sources = malloc(sizeof(void*) * *n_io_sources);
77 assert(io_sources);
78
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);
83 assert(*ios);
84 }
85
86 free(pfds);
87 return 0;
88 }
89
90 void pa_free_io_sources(struct pa_mainloop_api* m, void **io_sources, unsigned n_io_sources) {
91 unsigned i;
92 void **ios;
93 assert(m && io_sources);
94
95 for (ios = io_sources, i = 0; i < n_io_sources; i++, ios++)
96 m->cancel_io(m, *ios);
97 free(io_sources);
98 }