]> code.delx.au - pulseaudio/blob - src/modules/alsa-util.c
move alsa-util.[ch], oss-util.[ch] and howl-wrap.[ch] to the modules directory since...
[pulseaudio] / src / modules / 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 <sys/types.h>
27 #include <asoundlib.h>
28
29 #include <polyp/sample.h>
30 #include <polypcore/xmalloc.h>
31 #include <polypcore/log.h>
32
33 #include "alsa-util.h"
34
35 /* Set the hardware parameters of the given ALSA device. Returns the
36 * selected fragment settings in *period and *period_size */
37 int pa_alsa_set_hw_params(snd_pcm_t *pcm_handle, const pa_sample_spec *ss, uint32_t *periods, snd_pcm_uframes_t *period_size) {
38 int ret = -1;
39 snd_pcm_uframes_t buffer_size;
40 snd_pcm_hw_params_t *hwparams = NULL;
41 static const snd_pcm_format_t format_trans[] = {
42 [PA_SAMPLE_U8] = SND_PCM_FORMAT_U8,
43 [PA_SAMPLE_ALAW] = SND_PCM_FORMAT_A_LAW,
44 [PA_SAMPLE_ULAW] = SND_PCM_FORMAT_MU_LAW,
45 [PA_SAMPLE_S16LE] = SND_PCM_FORMAT_S16_LE,
46 [PA_SAMPLE_S16BE] = SND_PCM_FORMAT_S16_BE,
47 [PA_SAMPLE_FLOAT32LE] = SND_PCM_FORMAT_FLOAT_LE,
48 [PA_SAMPLE_FLOAT32BE] = SND_PCM_FORMAT_FLOAT_BE,
49 };
50 assert(pcm_handle && ss && periods && period_size);
51
52 if (snd_pcm_hw_params_malloc(&hwparams) < 0 ||
53 snd_pcm_hw_params_any(pcm_handle, hwparams) < 0 ||
54 snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0 ||
55 snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[ss->format]) < 0 ||
56 snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &ss->rate, NULL) < 0 ||
57 snd_pcm_hw_params_set_channels(pcm_handle, hwparams, ss->channels) < 0 ||
58 (*periods > 0 && snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, periods, NULL) < 0) ||
59 (*period_size > 0 && snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, period_size, NULL) < 0) ||
60 snd_pcm_hw_params(pcm_handle, hwparams) < 0)
61 goto finish;
62
63 if (snd_pcm_prepare(pcm_handle) < 0)
64 goto finish;
65
66 if (snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size) < 0 ||
67 snd_pcm_hw_params_get_period_size(hwparams, period_size, NULL) < 0)
68 goto finish;
69
70 assert(buffer_size > 0);
71 assert(*period_size > 0);
72 *periods = buffer_size / *period_size;
73 assert(*periods > 0);
74
75 ret = 0;
76
77 finish:
78 if (hwparams)
79 snd_pcm_hw_params_free(hwparams);
80
81 return ret;
82 }
83
84 /* Allocate an IO event for every ALSA poll descriptor for the
85 * specified ALSA device. Return a pointer to such an array in
86 * *io_events. Store the length of that array in *n_io_events. Use the
87 * specified callback function and userdata. The array has to be freed
88 * with pa_free_io_events(). */
89 int pa_create_io_events(snd_pcm_t *pcm_handle, pa_mainloop_api* m, pa_io_event ***io_events, unsigned *n_io_events, void (*cb)(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t events, void *userdata), void *userdata) {
90 unsigned i;
91 struct pollfd *pfds, *ppfd;
92 pa_io_event **ios;
93 assert(pcm_handle && m && io_events && n_io_events && cb);
94
95 *n_io_events = snd_pcm_poll_descriptors_count(pcm_handle);
96
97 pfds = pa_xmalloc(sizeof(struct pollfd) * *n_io_events);
98 if (snd_pcm_poll_descriptors(pcm_handle, pfds, *n_io_events) < 0) {
99 pa_xfree(pfds);
100 return -1;
101 }
102
103 *io_events = pa_xmalloc(sizeof(void*) * *n_io_events);
104
105 for (i = 0, ios = *io_events, ppfd = pfds; i < *n_io_events; i++, ios++, ppfd++) {
106 *ios = m->io_new(m, ppfd->fd,
107 ((ppfd->events & POLLIN) ? PA_IO_EVENT_INPUT : 0) |
108 ((ppfd->events & POLLOUT) ? PA_IO_EVENT_OUTPUT : 0), cb, userdata);
109 assert(*ios);
110 }
111
112 pa_xfree(pfds);
113 return 0;
114 }
115
116 /* Free the memory allocated by pa_create_io_events() */
117 void pa_free_io_events(pa_mainloop_api* m, pa_io_event **io_events, unsigned n_io_events) {
118 unsigned i;
119 pa_io_event **ios;
120 assert(m && io_events);
121
122 for (ios = io_events, i = 0; i < n_io_events; i++, ios++)
123 m->io_free(*ios);
124 pa_xfree(io_events);
125 }