Files
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

108 lines
3.0 KiB
C

/*
* audio.c -- MCDE: the PCM streams of McdeSys.h, "Sound"
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* The public calls live here; the device is the system's own (audio_linux.c
* with ALSA, audio_netbsd.c with audio(4)), the quiet one is audio_null.c.
* A handle is opened for one direction only: a call reads its microphone and
* writes its output, two streams.
*/
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <Mcde/McdeSys.h>
#include "private.h"
int
mcde_sys_pcm_open(McdeSysPcm **pcm, int direction, int backend, unsigned rate,
unsigned channels)
{
McdeSysPcm *p;
if (pcm == NULL)
return mcde_sys_fail(MCDE_SYS_EINVAL, "No place for the stream.");
*pcm = NULL;
if (direction != MCDE_SYS_PCM_PLAYBACK &&
direction != MCDE_SYS_PCM_CAPTURE)
return mcde_sys_fail(MCDE_SYS_EINVAL,
"Unknown stream direction %d.", direction);
if (rate < 4000 || rate > 192000)
return mcde_sys_fail(MCDE_SYS_EINVAL,
"The sample rate %u is out of range.", rate);
if (channels < 1 || channels > 2)
return mcde_sys_fail(MCDE_SYS_EINVAL,
"Only one or two channels are supported, not %u.",
channels);
if (backend == MCDE_SYS_PCM_NULL)
p = mcde_sys_pcm_null_open(direction, rate, channels);
else if (backend == MCDE_SYS_PCM_DEFAULT)
p = mcde_sys_pcm_device_open(direction, rate, channels);
else
return mcde_sys_fail(MCDE_SYS_EINVAL,
"Unknown audio backend %d.", backend);
if (p == NULL) {
if (mcde_sys_error() == NULL || *mcde_sys_error() == '\0')
(void) mcde_sys_fail(MCDE_SYS_ENOTSUP,
"No audio device for %s.",
direction == MCDE_SYS_PCM_PLAYBACK ?
"playback" : "capture");
return -1;
}
*pcm = p;
return MCDE_SYS_OK;
}
int
mcde_sys_pcm_wait(McdeSysPcm *pcm, int timeout_ms)
{
if (pcm == NULL || pcm->ops == NULL || pcm->ops->wait == NULL)
return mcde_sys_fail(MCDE_SYS_EINVAL, "No audio stream.");
return pcm->ops->wait(pcm, timeout_ms);
}
ssize_t
mcde_sys_pcm_read(McdeSysPcm *pcm, int16_t *frames, size_t count)
{
if (pcm == NULL || pcm->ops == NULL || pcm->ops->read == NULL ||
pcm->direction != MCDE_SYS_PCM_CAPTURE)
return mcde_sys_fail(MCDE_SYS_EINVAL,
"Not a capture stream.");
if (frames == NULL || count == 0)
return 0;
return pcm->ops->read(pcm, frames, count);
}
ssize_t
mcde_sys_pcm_write(McdeSysPcm *pcm, const int16_t *frames, size_t count)
{
if (pcm == NULL || pcm->ops == NULL || pcm->ops->write == NULL ||
pcm->direction != MCDE_SYS_PCM_PLAYBACK)
return mcde_sys_fail(MCDE_SYS_EINVAL,
"Not a playback stream.");
if (frames == NULL || count == 0)
return 0;
return pcm->ops->write(pcm, frames, count);
}
void
mcde_sys_pcm_close(McdeSysPcm *pcm)
{
if (pcm == NULL)
return;
if (pcm->ops != NULL && pcm->ops->close != NULL)
pcm->ops->close(pcm);
free(pcm);
}
const char *
mcde_sys_pcm_backend(const McdeSysPcm *pcm)
{
return pcm != NULL && pcm->name != NULL ? pcm->name : "none";
}