]> code.delx.au - pulseaudio/blob - src/modules/alsa-util.c
fix snd_pcm_hw_params_set_rate_near() usage
[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 unsigned int r = ss->rate;
42
43 static const snd_pcm_format_t format_trans[] = {
44 [PA_SAMPLE_U8] = SND_PCM_FORMAT_U8,
45 [PA_SAMPLE_ALAW] = SND_PCM_FORMAT_A_LAW,
46 [PA_SAMPLE_ULAW] = SND_PCM_FORMAT_MU_LAW,
47 [PA_SAMPLE_S16LE] = SND_PCM_FORMAT_S16_LE,
48 [PA_SAMPLE_S16BE] = SND_PCM_FORMAT_S16_BE,
49 [PA_SAMPLE_FLOAT32LE] = SND_PCM_FORMAT_FLOAT_LE,
50 [PA_SAMPLE_FLOAT32BE] = SND_PCM_FORMAT_FLOAT_BE,
51 };
52 assert(pcm_handle && ss && periods && period_size);
53
54 if (snd_pcm_hw_params_malloc(&hwparams) < 0 ||
55 snd_pcm_hw_params_any(pcm_handle, hwparams) < 0 ||
56 snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0 ||
57 snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[ss->format]) < 0 ||
58 snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &r, NULL) < 0 ||
59 snd_pcm_hw_params_set_channels(pcm_handle, hwparams, ss->channels) < 0 ||
60 (*periods > 0 && snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, periods, NULL) < 0) ||
61 (*period_size > 0 && snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, period_size, NULL) < 0) ||
62 snd_pcm_hw_params(pcm_handle, hwparams) < 0)
63 goto finish;
64
65 if (ss->rate != r)
66 pa_log_info(__FILE__": device doesn't support %u Hz, changed to %u Hz.\n", ss->rate, r);
67
68 if (snd_pcm_prepare(pcm_handle) < 0)
69 goto finish;
70
71 if (snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size) < 0 ||
72 snd_pcm_hw_params_get_period_size(hwparams, period_size, NULL) < 0)
73 goto finish;
74
75 assert(buffer_size > 0);
76 assert(*period_size > 0);
77 *periods = buffer_size / *period_size;
78 assert(*periods > 0);
79
80 ret = 0;
81
82 finish:
83 if (hwparams)
84 snd_pcm_hw_params_free(hwparams);
85
86 return ret;
87 }
88
89 /* Allocate an IO event for every ALSA poll descriptor for the
90 * specified ALSA device. Return a pointer to such an array in
91 * *io_events. Store the length of that array in *n_io_events. Use the
92 * specified callback function and userdata. The array has to be freed
93 * with pa_free_io_events(). */
94 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) {
95 unsigned i;
96 struct pollfd *pfds, *ppfd;
97 pa_io_event **ios;
98 assert(pcm_handle && m && io_events && n_io_events && cb);
99
100 *n_io_events = snd_pcm_poll_descriptors_count(pcm_handle);
101
102 pfds = pa_xmalloc(sizeof(struct pollfd) * *n_io_events);
103 if (snd_pcm_poll_descriptors(pcm_handle, pfds, *n_io_events) < 0) {
104 pa_xfree(pfds);
105 return -1;
106 }
107
108 *io_events = pa_xmalloc(sizeof(void*) * *n_io_events);
109
110 for (i = 0, ios = *io_events, ppfd = pfds; i < *n_io_events; i++, ios++, ppfd++) {
111 *ios = m->io_new(m, ppfd->fd,
112 ((ppfd->events & POLLIN) ? PA_IO_EVENT_INPUT : 0) |
113 ((ppfd->events & POLLOUT) ? PA_IO_EVENT_OUTPUT : 0), cb, userdata);
114 assert(*ios);
115 }
116
117 pa_xfree(pfds);
118 return 0;
119 }
120
121 /* Free the memory allocated by pa_create_io_events() */
122 void pa_free_io_events(pa_mainloop_api* m, pa_io_event **io_events, unsigned n_io_events) {
123 unsigned i;
124 pa_io_event **ios;
125 assert(m && io_events);
126
127 for (ios = io_events, i = 0; i < n_io_events; i++, ios++)
128 m->io_free(*ios);
129 pa_xfree(io_events);
130 }