48#define ALSA_PCM_NEW_HW_PARAMS_API
65#include <alsa/asoundlib.h>
67#define MOD_NAME "[ALSA cap.] "
86 const char *whitelist[] = {
"pulse",
"dsnoop"};
88 strcpy((*available_devices)[0].dev,
"");
89 strcpy((*available_devices)[0].
name,
"Default Linux audio input");
102 printf(
"\nAvailable ALSA capture devices\n");
108#define DEFAULT_SAMPLE_RATE 48000
113 if (strcmp(cfg,
"help") == 0) {
118 snd_pcm_hw_params_t *
params;
121 const char *
name =
"default";
128 if (strlen(cfg) > 0) {
130 if (strncmp(tmp,
"opts=", strlen(
"opts")) == 0) {
131 opts = tmp + strlen(
"opts=");
134 if (strstr(tmp,
":opts=") != NULL) {
135 opts = strstr(tmp,
":opts=") + strlen(
":opts=");
136 *strstr(tmp,
":opts=") =
'\0';
151 char *
item, *save_ptr;
152 while ((
item = strtok_r(opts,
":", &save_ptr)) != NULL) {
153 if (strncmp(
item,
"frames=", strlen(
"frames=")) == 0) {
156 fprintf(stderr,
"[ALSA cap.] Unknown option: %s\n",
item);
165 SND_PCM_STREAM_CAPTURE, 0);
167 fprintf(stderr,
MOD_NAME "unable to open pcm device: %s\n",
174 snd_pcm_hw_params_alloca(&
params);
179 fprintf(stderr,
MOD_NAME "unable to set default parameters: %s\n",
200 "Cannot find suitable sample format. ");
213 if (!snd_pcm_hw_params_test_access(s->
handle,
params, SND_PCM_ACCESS_RW_INTERLEAVED)) {
215 }
else if (!snd_pcm_hw_params_test_access(s->
handle,
params, SND_PCM_ACCESS_RW_NONINTERLEAVED)) {
218 "available only when capturing mono!\n");
233 s->
non_interleaved ? SND_PCM_ACCESS_RW_NONINTERLEAVED : SND_PCM_ACCESS_RW_INTERLEAVED);
235 fprintf(stderr,
MOD_NAME "unable to set interleaved mode: %s\n",
245 fprintf(stderr,
MOD_NAME "unable to set capture format: %s\n",
252 rc = snd_pcm_hw_params_set_rate_resample(s->
handle,
255 fprintf(stderr,
MOD_NAME "Warning: Unable to set resampling: %s\n",
265 fprintf(stderr,
"[ALSA cap.] unable to set sampling rate (%s %d): %s\n",
266 dir == 0 ?
"=" : (dir == -1 ?
"<" :
">"),
267 val, snd_strerror(rc));
277 fprintf(stderr,
MOD_NAME "unable to set channel count: %s\n",
288 rc = snd_pcm_hw_params_set_period_size_near(s->
handle,
291 fprintf(stderr,
"[ALSA cap.] unable to set frame period (%ld): %s\n",
292 s->
frames, snd_strerror(rc));
298 fprintf(stderr,
MOD_NAME "unable to set hw parameters: %s\n",
304 snd_pcm_hw_params_get_period_size(
params, &s->
frames, &dir);
342 rc = snd_pcm_readn(s->
handle, (
void **) read_ptr, s->
frames);
349 snd_pcm_prepare(s->
handle);
357 }
else if (rc != (
int)s->
frames) {
386 gettimeofday(&t, NULL);
387 printf(
"[ALSA cap.] Captured %lld samples in %f seconds (%f samples per second).\n",
int void(** deleter)(void *))
static const snd_pcm_format_t bps_to_snd_fmts[]
static int get_rate_near(snd_pcm_t *handle, snd_pcm_hw_params_t *params, unsigned int approx_val)
static void audio_alsa_list_devices(void)
static void audio_alsa_probe(struct device_info **available_devices, int *count, const char **whitelist, size_t whitelist_size)
static void print_alsa_device_info(snd_pcm_t *handle, const char *module_name)
This file contains definition of some common audio types.
#define AUDIO_CAPTURE_ABI_VERSION
static void * audio_cap_alsa_init(struct module *parent, const char *cfg)
static const struct audio_capture_info acap_alsa_info
static struct audio_frame * audio_cap_alsa_read(void *state)
static void audio_cap_alsa_done(void *state)
static void audio_cap_alsa_probe(struct device_info **available_devices, int *count, void(**deleter)(void *))
#define DEFAULT_SAMPLE_RATE
static const int bps_preference[]
static void audio_cap_alsa_help(void)
int color_printf(const char *format,...)
void log_msg(int level, const char *format,...)
void bug_msg(int level, const char *format,...)
prints the error with the advice to report the bug
#define LOG_LEVEL_NOTICE
information that may be interesting
#define LOG_LEVEL_ERROR
general errors
#define LOG_LEVEL_FATAL
errors that prevent UG run
#define LOG_LEVEL_WARNING
less severe errors
unsigned int audio_capture_bps
unsigned int audio_capture_sample_rate
static struct @19 params[100]
unsigned int audio_capture_channels
#define DEFAULT_AUDIO_CAPTURE_CHANNELS
@ LIBRARY_CLASS_AUDIO_CAPTURE
#define REGISTER_MODULE(name, info, lclass, abi)
Registers module to global modules' registry.
struct used to comunicate with audio capture/playback device
unsigned int min_device_channels
long long int captured_samples
struct timeval start_time
double tv_diff(struct timeval curr_time, struct timeval prev_time)
void signed2unsigned(char *out, const char *in, int in_len)
void demux_channel(char *out, char *in, int bps, int in_len, int in_stream_channels, int pos_in_stream)