2004-07-16 19:56:36 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
|
This file is part of polypaudio.
|
|
|
|
|
|
|
|
|
|
polypaudio is free software; you can redistribute it and/or modify
|
2004-11-14 14:58:54 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
2004-07-16 19:56:36 +00:00
|
|
|
by the Free Software Foundation; either version 2 of the License,
|
|
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
polypaudio is distributed in the hope that it will be useful, but
|
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
2004-11-14 14:58:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2004-07-16 19:56:36 +00:00
|
|
|
along with polypaudio; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
2004-07-16 19:16:42 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-11-17 23:11:34 +00:00
|
|
|
#include <sys/types.h>
|
2004-07-16 17:03:11 +00:00
|
|
|
#include <asoundlib.h>
|
|
|
|
|
|
2006-02-16 19:19:58 +00:00
|
|
|
#include <polyp/sample.h>
|
2006-02-16 22:08:06 +00:00
|
|
|
#include <polypcore/xmalloc.h>
|
|
|
|
|
#include <polypcore/log.h>
|
|
|
|
|
|
|
|
|
|
#include "alsa-util.h"
|
2004-07-16 17:03:11 +00:00
|
|
|
|
2004-11-21 02:43:05 +00:00
|
|
|
/* Set the hardware parameters of the given ALSA device. Returns the
|
2004-11-21 16:27:51 +00:00
|
|
|
* selected fragment settings in *period and *period_size */
|
2006-01-11 01:17:39 +00:00
|
|
|
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) {
|
2004-11-20 22:17:31 +00:00
|
|
|
int ret = -1;
|
|
|
|
|
snd_pcm_uframes_t buffer_size;
|
2004-07-16 17:03:11 +00:00
|
|
|
snd_pcm_hw_params_t *hwparams = NULL;
|
2006-02-20 23:47:46 +00:00
|
|
|
unsigned int r = ss->rate;
|
|
|
|
|
|
2004-07-16 17:03:11 +00:00
|
|
|
static const snd_pcm_format_t format_trans[] = {
|
|
|
|
|
[PA_SAMPLE_U8] = SND_PCM_FORMAT_U8,
|
|
|
|
|
[PA_SAMPLE_ALAW] = SND_PCM_FORMAT_A_LAW,
|
|
|
|
|
[PA_SAMPLE_ULAW] = SND_PCM_FORMAT_MU_LAW,
|
|
|
|
|
[PA_SAMPLE_S16LE] = SND_PCM_FORMAT_S16_LE,
|
|
|
|
|
[PA_SAMPLE_S16BE] = SND_PCM_FORMAT_S16_BE,
|
|
|
|
|
[PA_SAMPLE_FLOAT32LE] = SND_PCM_FORMAT_FLOAT_LE,
|
|
|
|
|
[PA_SAMPLE_FLOAT32BE] = SND_PCM_FORMAT_FLOAT_BE,
|
|
|
|
|
};
|
2004-11-20 22:17:31 +00:00
|
|
|
assert(pcm_handle && ss && periods && period_size);
|
|
|
|
|
|
2004-07-16 17:03:11 +00:00
|
|
|
if (snd_pcm_hw_params_malloc(&hwparams) < 0 ||
|
|
|
|
|
snd_pcm_hw_params_any(pcm_handle, hwparams) < 0 ||
|
|
|
|
|
snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0 ||
|
|
|
|
|
snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[ss->format]) < 0 ||
|
2006-02-20 23:47:46 +00:00
|
|
|
snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &r, NULL) < 0 ||
|
2004-07-16 17:03:11 +00:00
|
|
|
snd_pcm_hw_params_set_channels(pcm_handle, hwparams, ss->channels) < 0 ||
|
2004-11-20 22:17:31 +00:00
|
|
|
(*periods > 0 && snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, periods, NULL) < 0) ||
|
|
|
|
|
(*period_size > 0 && snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, period_size, NULL) < 0) ||
|
|
|
|
|
snd_pcm_hw_params(pcm_handle, hwparams) < 0)
|
|
|
|
|
goto finish;
|
2006-02-20 23:47:46 +00:00
|
|
|
|
|
|
|
|
if (ss->rate != r)
|
2006-02-23 02:27:19 +00:00
|
|
|
pa_log_info(__FILE__": device doesn't support %u Hz, changed to %u Hz.", ss->rate, r);
|
2004-11-20 22:17:31 +00:00
|
|
|
|
|
|
|
|
if (snd_pcm_prepare(pcm_handle) < 0)
|
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
|
|
if (snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size) < 0 ||
|
|
|
|
|
snd_pcm_hw_params_get_period_size(hwparams, period_size, NULL) < 0)
|
|
|
|
|
goto finish;
|
2004-12-11 16:48:45 +00:00
|
|
|
|
|
|
|
|
assert(buffer_size > 0);
|
|
|
|
|
assert(*period_size > 0);
|
2004-11-20 22:17:31 +00:00
|
|
|
*periods = buffer_size / *period_size;
|
|
|
|
|
assert(*periods > 0);
|
|
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
|
|
finish:
|
2004-07-16 17:03:11 +00:00
|
|
|
if (hwparams)
|
|
|
|
|
snd_pcm_hw_params_free(hwparams);
|
2004-11-20 22:17:31 +00:00
|
|
|
|
2004-07-16 17:03:11 +00:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-21 02:43:05 +00:00
|
|
|
/* Allocate an IO event for every ALSA poll descriptor for the
|
|
|
|
|
* specified ALSA device. Return a pointer to such an array in
|
|
|
|
|
* *io_events. Store the length of that array in *n_io_events. Use the
|
|
|
|
|
* specified callback function and userdata. The array has to be freed
|
|
|
|
|
* with pa_free_io_events(). */
|
2006-01-27 16:25:31 +00:00
|
|
|
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) {
|
2004-07-16 17:03:11 +00:00
|
|
|
unsigned i;
|
|
|
|
|
struct pollfd *pfds, *ppfd;
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_io_event **ios;
|
2004-08-05 19:53:57 +00:00
|
|
|
assert(pcm_handle && m && io_events && n_io_events && cb);
|
2004-07-16 17:03:11 +00:00
|
|
|
|
2004-08-05 19:53:57 +00:00
|
|
|
*n_io_events = snd_pcm_poll_descriptors_count(pcm_handle);
|
2004-07-16 17:03:11 +00:00
|
|
|
|
2004-08-05 19:53:57 +00:00
|
|
|
pfds = pa_xmalloc(sizeof(struct pollfd) * *n_io_events);
|
|
|
|
|
if (snd_pcm_poll_descriptors(pcm_handle, pfds, *n_io_events) < 0) {
|
2004-08-04 16:39:30 +00:00
|
|
|
pa_xfree(pfds);
|
2004-07-16 17:03:11 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-05 19:53:57 +00:00
|
|
|
*io_events = pa_xmalloc(sizeof(void*) * *n_io_events);
|
2004-07-16 17:03:11 +00:00
|
|
|
|
2004-08-05 19:53:57 +00:00
|
|
|
for (i = 0, ios = *io_events, ppfd = pfds; i < *n_io_events; i++, ios++, ppfd++) {
|
|
|
|
|
*ios = m->io_new(m, ppfd->fd,
|
|
|
|
|
((ppfd->events & POLLIN) ? PA_IO_EVENT_INPUT : 0) |
|
|
|
|
|
((ppfd->events & POLLOUT) ? PA_IO_EVENT_OUTPUT : 0), cb, userdata);
|
2004-07-16 17:03:11 +00:00
|
|
|
assert(*ios);
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-04 16:39:30 +00:00
|
|
|
pa_xfree(pfds);
|
2004-07-16 17:03:11 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-21 02:43:05 +00:00
|
|
|
/* Free the memory allocated by pa_create_io_events() */
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_free_io_events(pa_mainloop_api* m, pa_io_event **io_events, unsigned n_io_events) {
|
2004-07-16 17:03:11 +00:00
|
|
|
unsigned i;
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_io_event **ios;
|
2004-08-05 19:53:57 +00:00
|
|
|
assert(m && io_events);
|
2004-07-16 17:03:11 +00:00
|
|
|
|
2004-08-05 19:53:57 +00:00
|
|
|
for (ios = io_events, i = 0; i < n_io_events; i++, ios++)
|
|
|
|
|
m->io_free(*ios);
|
|
|
|
|
pa_xfree(io_events);
|
2004-07-16 17:03:11 +00:00
|
|
|
}
|