Files
mcde/lib/McdeSys/audio_linux.c
T
lotherk 233da2bc3f mcde: add McdeSys PCM streams for dtchat's calls (T6.1)
McdeSys.h gains mcde_sys_pcm_open/wait/read/write/close/backend: signed 16-bit interleaved frames, one direction per handle, non-blocking (0 = would block). audio_linux.c drives ALSA (S16_LE, the wanted rate, 20 ms periods, four periods of buffer, EAGAIN -> 0, xrun recovery, snd_pcm_wait); it compiles to ENOTSUP without HAVE_ALSA so the desktop still builds. audio_netbsd.c sets audio(4) (/dev/audio or /dev/sound) to SLINEAR_LE 16 bit with AUDIO_SETINFO and verifies with AUDIO_GETINFO, then polls the descriptor; it compiles against the NetBSD sysroot (-D_NETBSD_SOURCE for u_int, play/record sub-structs) but has not run on NetBSD. audio_null.c is silence in/discard out. configure checks alsa/asoundlib.h+libasound on non-NetBSD (optional, with a warning) and passes ALSA_CPPFLAGS/ALSALIB. test_audio checks the null stream frame by frame (both directions, wrong-direction errors, bad arguments) and tries the real playback/capture device, skipping without a card: standalone and ASan/UBSan pass, and the container's ALSA device played and captured 5 periods each. test_audio is a noinst program and a harness entry. All four files compile with x86_64--netbsd-gcc against destdir.amd64.
2026-11-17 07:23:23 +00:00

224 lines
5.1 KiB
C

/*
* 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 <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_ALSA
#include <alsa/asoundlib.h>
#endif
#include <Mcde/McdeSys.h>
#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(&params)) < 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 */