/* * audio_linux.c -- MCDE: PCM streams through ALSA (McdeSys.h, "Sound") * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * One stream per handle, signed 16-bit little-endian, interleaved, 20 ms * periods and four periods of buffer (80 ms). The handle is non-blocking: * readi/writei answer EAGAIN when nothing fits, an xrun is recovered and the * call answers 0, and mcde_sys_pcm_wait() uses snd_pcm_wait(), which polls * the device's descriptors itself. */ #define _POSIX_C_SOURCE 200809L #include #include #include #include #ifdef HAVE_ALSA #include #endif #include #include "private.h" #ifndef HAVE_ALSA /* A build without ALSA: the desktop still works, a call has no device and * the caller can ask for MCDE_SYS_PCM_NULL instead. */ McdeSysPcm * mcde_sys_pcm_device_open(int direction, unsigned rate, unsigned channels) { (void) direction; (void) rate; (void) channels; (void) mcde_sys_fail(MCDE_SYS_ENOTSUP, "This build has no ALSA; audio has no device."); return NULL; } #else /* HAVE_ALSA */ typedef struct { snd_pcm_t *handle; unsigned rate; unsigned channels; snd_pcm_uframes_t period; } AlsaPcm; static void AlsaReport(const char *what, int err) { (void) mcde_sys_fail(MCDE_SYS_EFAIL, "%s: %s.", what, snd_strerror(err)); } static ssize_t AlsaRead(McdeSysPcm *pcm, int16_t *frames, size_t count) { AlsaPcm *a = pcm->priv; snd_pcm_sframes_t n; n = snd_pcm_readi(a->handle, frames, (snd_pcm_uframes_t) count); if (n == -EAGAIN) return 0; if (n == -EPIPE || n == -ESTRPIPE) { (void) snd_pcm_recover(a->handle, (int) n, 1); return 0; } if (n < 0) { AlsaReport("Cannot read from the audio device", (int) n); return -1; } return (ssize_t) n; } static ssize_t AlsaWrite(McdeSysPcm *pcm, const int16_t *frames, size_t count) { AlsaPcm *a = pcm->priv; snd_pcm_sframes_t n; n = snd_pcm_writei(a->handle, frames, (snd_pcm_uframes_t) count); if (n == -EAGAIN) return 0; if (n == -EPIPE || n == -ESTRPIPE) { (void) snd_pcm_recover(a->handle, (int) n, 1); return 0; } if (n < 0) { AlsaReport("Cannot write to the audio device", (int) n); return -1; } return (ssize_t) n; } static int AlsaWait(McdeSysPcm *pcm, int timeout_ms) { AlsaPcm *a = pcm->priv; int rc; rc = snd_pcm_wait(a->handle, timeout_ms); if (rc == 0) return 0; if (rc < 0) { if (rc == -EPIPE || rc == -ESTRPIPE) { (void) snd_pcm_recover(a->handle, rc, 1); return 0; } AlsaReport("Cannot wait for the audio device", rc); return -1; } return 1; } static void AlsaClose(McdeSysPcm *pcm) { AlsaPcm *a = pcm->priv; if (a != NULL) { if (a->handle != NULL) { (void) snd_pcm_drop(a->handle); (void) snd_pcm_close(a->handle); } free(a); } pcm->priv = NULL; } static const McdeSysPcmOps alsaOps = { AlsaRead, AlsaWrite, AlsaWait, AlsaClose }; McdeSysPcm * mcde_sys_pcm_device_open(int direction, unsigned rate, unsigned channels) { snd_pcm_t *handle = NULL; snd_pcm_hw_params_t *params = NULL; snd_pcm_stream_t stream = direction == MCDE_SYS_PCM_PLAYBACK ? SND_PCM_STREAM_PLAYBACK : SND_PCM_STREAM_CAPTURE; McdeSysPcm *pcm = NULL; AlsaPcm *a = NULL; snd_pcm_uframes_t period; snd_pcm_uframes_t buffer; unsigned r = rate; int dir = 0; int err; err = snd_pcm_open(&handle, "default", stream, SND_PCM_NONBLOCK); if (err < 0) { AlsaReport("Cannot open the audio device", err); return NULL; } pcm = calloc(1, sizeof *pcm); a = calloc(1, sizeof *a); if (pcm == NULL || a == NULL) { free(pcm); free(a); (void) snd_pcm_close(handle); (void) mcde_sys_fail(MCDE_SYS_EFAIL, "Out of memory."); return NULL; } a->handle = handle; a->rate = rate; a->channels = channels; pcm->priv = a; if ((err = snd_pcm_hw_params_malloc(¶ms)) < 0 || (err = snd_pcm_hw_params_any(handle, params)) < 0 || (err = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0 || (err = snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE)) < 0 || (err = snd_pcm_hw_params_set_channels(handle, params, channels)) < 0 || (err = snd_pcm_hw_params_set_rate_near(handle, params, &r, &dir)) < 0) { AlsaReport("Cannot set the audio format", err); goto fail; } if (r != rate) { (void) mcde_sys_fail(MCDE_SYS_EFAIL, "The audio device does not take %u Hz.", rate); goto fail; } period = rate / 50; /* 20 ms */ buffer = period * 4; if ((err = snd_pcm_hw_params_set_period_size_near(handle, params, &period, &dir)) < 0 || (err = snd_pcm_hw_params_set_buffer_size_near(handle, params, &buffer)) < 0 || (err = snd_pcm_hw_params(handle, params)) < 0) { AlsaReport("Cannot set the audio buffer", err); goto fail; } snd_pcm_hw_params_free(params); params = NULL; a->period = period; if ((err = snd_pcm_prepare(handle)) < 0) { AlsaReport("Cannot prepare the audio device", err); goto fail; } pcm->ops = &alsaOps; pcm->name = "alsa"; pcm->direction = direction; return pcm; fail: if (params != NULL) snd_pcm_hw_params_free(params); AlsaClose(pcm); free(pcm); return NULL; } #endif /* HAVE_ALSA */