2004-07-16 19:56:36 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-02-13 15:35:19 +00:00
|
|
|
Copyright 2004-2006 Lennart Poettering
|
|
|
|
|
Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio 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.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2004-07-16 19:56:36 +00:00
|
|
|
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.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-11-14 14:58:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2006-06-19 21:53:48 +00:00
|
|
|
along with PulseAudio; if not, write to the Free Software
|
2004-07-16 19:56:36 +00:00
|
|
|
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-06-19 21:53:48 +00:00
|
|
|
#include <pulse/sample.h>
|
|
|
|
|
#include <pulse/xmalloc.h>
|
2006-05-17 16:34:18 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/log.h>
|
2006-02-16 22:08:06 +00:00
|
|
|
|
|
|
|
|
#include "alsa-util.h"
|
2004-07-16 17:03:11 +00:00
|
|
|
|
2006-06-16 19:33:05 +00:00
|
|
|
static int set_format(snd_pcm_t *pcm_handle, snd_pcm_hw_params_t *hwparams, pa_sample_format_t *f) {
|
|
|
|
|
|
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,
|
|
|
|
|
};
|
2006-06-16 19:33:05 +00:00
|
|
|
|
|
|
|
|
static const pa_sample_format_t try_order[] = {
|
|
|
|
|
PA_SAMPLE_S16NE,
|
|
|
|
|
PA_SAMPLE_S16RE,
|
|
|
|
|
PA_SAMPLE_FLOAT32NE,
|
|
|
|
|
PA_SAMPLE_FLOAT32RE,
|
|
|
|
|
PA_SAMPLE_ULAW,
|
|
|
|
|
PA_SAMPLE_ALAW,
|
|
|
|
|
PA_SAMPLE_U8,
|
|
|
|
|
PA_SAMPLE_INVALID
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int i, ret;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-16 19:33:05 +00:00
|
|
|
assert(pcm_handle);
|
|
|
|
|
assert(f);
|
|
|
|
|
|
|
|
|
|
if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
if (*f == PA_SAMPLE_FLOAT32BE)
|
|
|
|
|
*f = PA_SAMPLE_FLOAT32LE;
|
|
|
|
|
else if (*f == PA_SAMPLE_FLOAT32LE)
|
|
|
|
|
*f = PA_SAMPLE_FLOAT32BE;
|
|
|
|
|
else if (*f == PA_SAMPLE_S16BE)
|
|
|
|
|
*f = PA_SAMPLE_S16LE;
|
|
|
|
|
else if (*f == PA_SAMPLE_S16LE)
|
|
|
|
|
*f = PA_SAMPLE_S16BE;
|
|
|
|
|
else
|
|
|
|
|
goto try_auto;
|
|
|
|
|
|
|
|
|
|
if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
|
|
|
|
|
return ret;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-16 19:33:05 +00:00
|
|
|
try_auto:
|
|
|
|
|
|
|
|
|
|
for (i = 0; try_order[i] != PA_SAMPLE_INVALID; i++) {
|
|
|
|
|
*f = try_order[i];
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-16 19:33:05 +00:00
|
|
|
if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Set the hardware parameters of the given ALSA device. Returns the
|
|
|
|
|
* selected fragment settings in *period and *period_size */
|
2007-07-28 17:24:28 +00:00
|
|
|
int pa_alsa_set_hw_params(snd_pcm_t *pcm_handle, pa_sample_spec *ss, uint32_t *periods, snd_pcm_uframes_t *period_size, int *use_mmap) {
|
2006-06-16 19:33:05 +00:00
|
|
|
int ret = -1;
|
|
|
|
|
snd_pcm_uframes_t buffer_size;
|
|
|
|
|
unsigned int r = ss->rate;
|
|
|
|
|
unsigned int c = ss->channels;
|
|
|
|
|
pa_sample_format_t f = ss->format;
|
|
|
|
|
snd_pcm_hw_params_t *hwparams;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-16 19:33:05 +00:00
|
|
|
assert(pcm_handle);
|
|
|
|
|
assert(ss);
|
|
|
|
|
assert(periods);
|
|
|
|
|
assert(period_size);
|
2006-03-05 18:37:13 +00:00
|
|
|
|
|
|
|
|
buffer_size = *periods * *period_size;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-03-05 18:35:45 +00:00
|
|
|
if ((ret = snd_pcm_hw_params_malloc(&hwparams)) < 0 ||
|
|
|
|
|
(ret = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0 ||
|
2007-07-28 17:24:28 +00:00
|
|
|
(ret = snd_pcm_hw_params_set_rate_resample(pcm_handle, hwparams, 0)) < 0)
|
2006-06-17 23:36:03 +00:00
|
|
|
goto finish;
|
|
|
|
|
|
2007-07-28 17:24:28 +00:00
|
|
|
if (use_mmap && *use_mmap) {
|
|
|
|
|
if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0)
|
|
|
|
|
if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
|
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
|
|
} else if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
|
|
|
|
|
goto finish;
|
|
|
|
|
else if (*use_mmap)
|
|
|
|
|
*use_mmap = 0;
|
|
|
|
|
|
2006-06-17 23:36:03 +00:00
|
|
|
if ((ret = set_format(pcm_handle, hwparams, &f)) < 0)
|
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
|
|
if ((ret = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &r, NULL)) < 0)
|
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
|
|
if ((ret = snd_pcm_hw_params_set_channels_near(pcm_handle, hwparams, &c)) < 0)
|
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
|
|
if ((*period_size > 0 && (ret = snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, period_size, NULL)) < 0) ||
|
|
|
|
|
(*periods > 0 && (ret = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffer_size)) < 0))
|
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
|
|
if ((ret = snd_pcm_hw_params(pcm_handle, hwparams)) < 0)
|
2004-11-20 22:17:31 +00:00
|
|
|
goto finish;
|
2006-02-20 23:47:46 +00:00
|
|
|
|
2006-05-30 22:05:07 +00:00
|
|
|
if (ss->rate != r) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log_warn("device doesn't support %u Hz, changed to %u Hz.", ss->rate, r);
|
2006-05-30 22:05:07 +00:00
|
|
|
|
|
|
|
|
/* If the sample rate deviates too much, we need to resample */
|
2006-05-31 00:05:38 +00:00
|
|
|
if (r < ss->rate*.95 || r > ss->rate*1.05)
|
2006-05-30 22:05:07 +00:00
|
|
|
ss->rate = r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ss->channels != c) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log_warn("device doesn't support %u channels, changed to %u.", ss->channels, c);
|
2006-05-30 22:05:07 +00:00
|
|
|
ss->channels = c;
|
|
|
|
|
}
|
2006-06-16 19:33:05 +00:00
|
|
|
|
|
|
|
|
if (ss->format != f) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log_warn("device doesn't support sample format %s, changed to %s.", pa_sample_format_to_string(ss->format), pa_sample_format_to_string(f));
|
2006-06-16 19:33:05 +00:00
|
|
|
ss->format = f;
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-03-05 18:35:45 +00:00
|
|
|
if ((ret = snd_pcm_prepare(pcm_handle)) < 0)
|
2004-11-20 22:17:31 +00:00
|
|
|
goto finish;
|
|
|
|
|
|
2006-03-05 18:35:45 +00:00
|
|
|
if ((ret = snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size)) < 0 ||
|
|
|
|
|
(ret = snd_pcm_hw_params_get_period_size(hwparams, period_size, NULL)) < 0)
|
2004-11-20 22:17:31 +00:00
|
|
|
goto finish;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
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);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-11-20 22:17:31 +00:00
|
|
|
ret = 0;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-11-20 22:17:31 +00:00
|
|
|
finish:
|
2004-07-16 17:03:11 +00:00
|
|
|
if (hwparams)
|
|
|
|
|
snd_pcm_hw_params_free(hwparams);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-07-16 17:03:11 +00:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-26 17:57:58 +00:00
|
|
|
int pa_alsa_prepare_mixer(snd_mixer_t *mixer, const char *dev) {
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
assert(mixer && dev);
|
|
|
|
|
|
|
|
|
|
if ((err = snd_mixer_attach(mixer, dev)) < 0) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log_warn("Unable to attach to mixer %s: %s", dev, snd_strerror(err));
|
2006-02-26 17:57:58 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((err = snd_mixer_selem_register(mixer, NULL, NULL)) < 0) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log_warn("Unable to register mixer: %s", snd_strerror(err));
|
2006-02-26 17:57:58 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((err = snd_mixer_load(mixer)) < 0) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log_warn("Unable to load mixer: %s", snd_strerror(err));
|
2006-02-26 17:57:58 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-30 22:48:17 +00:00
|
|
|
snd_mixer_elem_t *pa_alsa_find_elem(snd_mixer_t *mixer, const char *name, const char *fallback) {
|
2006-02-26 17:57:58 +00:00
|
|
|
snd_mixer_elem_t *elem;
|
2006-05-30 22:48:17 +00:00
|
|
|
snd_mixer_selem_id_t *sid = NULL;
|
2006-02-26 17:57:58 +00:00
|
|
|
snd_mixer_selem_id_alloca(&sid);
|
|
|
|
|
|
2006-05-30 22:48:17 +00:00
|
|
|
assert(mixer);
|
|
|
|
|
assert(name);
|
2006-02-26 17:57:58 +00:00
|
|
|
|
|
|
|
|
snd_mixer_selem_id_set_name(sid, name);
|
|
|
|
|
|
2006-05-30 22:48:17 +00:00
|
|
|
if (!(elem = snd_mixer_find_selem(mixer, sid))) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log_warn("Cannot find mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
|
2006-05-30 22:48:17 +00:00
|
|
|
|
|
|
|
|
if (fallback) {
|
|
|
|
|
snd_mixer_selem_id_set_name(sid, fallback);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-05-30 22:48:17 +00:00
|
|
|
if (!(elem = snd_mixer_find_selem(mixer, sid)))
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log_warn("Cannot find fallback mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
|
2006-05-30 22:48:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (elem)
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log_info("Using mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
|
2006-02-26 17:57:58 +00:00
|
|
|
|
|
|
|
|
return elem;
|
|
|
|
|
}
|