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.
This commit is contained in:
lotherk committed 2026-11-17 07:23:23 +00:00
1 parent e3e4e95d07
commit 233da2bc3f
9 files changed
+884 -5

No files matched your search

+16
View File
@@ -604,6 +604,22 @@ AC_CHECK_LIB(crypto, EVP_PKEY_new_raw_private_key,
[CRYPTOLIB="-lcrypto"], [AC_MSG_ERROR([libcrypto (OpenSSL 1.1.1+) not found])])
AC_SUBST(CRYPTOLIB)
dnl MCDE: ALSA for McdeSys' PCM streams on Linux (audio_linux.c). NetBSD
dnl uses audio(4) and needs no library. Without ALSA the desktop still
dnl builds: the device stream answers MCDE_SYS_ENOTSUP and the null stream
dnl remains, so dtchat calls are silent there.
ALSALIB=""
ALSA_CPPFLAGS=""
if test "$build_netbsd" != "yes"; then
AC_CHECK_HEADERS([alsa/asoundlib.h])
AC_CHECK_LIB(asound, snd_pcm_open,
[ALSALIB="-lasound"
ALSA_CPPFLAGS="-DHAVE_ALSA"],
[AC_MSG_WARN([ALSA not found: dtchat audio will have no device on this system])])
fi
AC_SUBST(ALSALIB)
AC_SUBST(ALSA_CPPFLAGS)
dnl MCDE: libopus for dtchat's audio (48 kHz mono, 20 ms, 24 kbit/s).
OPUSLIB=""
AC_CHECK_LIB(opus, opus_encoder_create,
+34
View File
@@ -188,6 +188,40 @@ int mcde_sys_volume_mute(int muted);
* player left running on its own; MCDE_SYS_ENOTSUP when there is none */
int mcde_sys_sound_play(const char *file);
/*
* PCM streams, for the calls of dtchat: signed 16-bit samples, interleaved,
* one stream per direction. A frame is one sample per channel; COUNT in
* read/write is frames. The system's own device is used (ALSA on Linux,
* audio(4) with /dev/audio on NetBSD); MCDE_SYS_PCM_NULL is silence in and
* a discard out -- it is what a test, or a session without a device, uses.
* The handle is non-blocking: read/write answer 0 when no frame fits (a
* negative value is an error, see mcde_sys_error()), and mcde_sys_pcm_wait
* blocks until the stream can take or give frames.
*/
typedef struct McdeSysPcm McdeSysPcm;
enum {
MCDE_SYS_PCM_PLAYBACK = 0,
MCDE_SYS_PCM_CAPTURE = 1
};
enum {
MCDE_SYS_PCM_DEFAULT = 0,
MCDE_SYS_PCM_NULL = 1
};
int mcde_sys_pcm_open(McdeSysPcm **pcm, int direction, int backend,
unsigned rate, unsigned channels);
/* wait up to TIMEOUT_MS (0: do not wait, negative: no limit) */
int mcde_sys_pcm_wait(McdeSysPcm *pcm, int timeout_ms);
/* frames read or written; 0 when the stream would block */
ssize_t mcde_sys_pcm_read(McdeSysPcm *pcm, int16_t *frames, size_t count);
ssize_t mcde_sys_pcm_write(McdeSysPcm *pcm, const int16_t *frames,
size_t count);
void mcde_sys_pcm_close(McdeSysPcm *pcm);
/* "alsa", "audio(4)" or "null", for a status line and the tests */
const char *mcde_sys_pcm_backend(const McdeSysPcm *pcm);
/*
* Bluetooth: the first controller
*/
+14 -5
View File
@@ -9,20 +9,26 @@ lib_LTLIBRARIES = libMcdeSys.la
libMcdeSys_la_CPPFLAGS = -I$(top_srcdir)/include \
-DMCDE_PKG_PREFIX='"$(MCDE_PKG_PREFIX)"' \
-DMCDE_PKG_SYSCONFDIR='"$(MCDE_PKG_SYSCONFDIR)"' \
-DMCDE_DT_TOP='"$(prefix)"'
libMcdeSys_la_SOURCES = common.c text.c net.c sound.c users.c share.c netmount.c backup.c printers.c logs.c events.c mount.c private.h
-DMCDE_DT_TOP='"$(prefix)"' $(ALSA_CPPFLAGS)
libMcdeSys_la_SOURCES = common.c text.c net.c sound.c audio.c audio_null.c \
users.c share.c netmount.c backup.c printers.c logs.c events.c mount.c \
private.h
if NETBSD
libMcdeSys_la_SOURCES += netbsd.c disk_netbsd.c mount_netbsd.c
libMcdeSys_la_SOURCES += netbsd.c disk_netbsd.c mount_netbsd.c audio_netbsd.c
# crypt(3), for the password hash; proplib(3), for the battery; libdns_sd
# (base), for the computers on the network (netbsd.c)
libMcdeSys_la_LIBADD = -lcrypt -lprop -ldns_sd
else
libMcdeSys_la_SOURCES += linux.c disk_linux.c mount_linux.c
libMcdeSys_la_SOURCES += linux.c disk_linux.c mount_linux.c audio_linux.c
# ALSA, when configure found it (audio_linux.c); empty otherwise
libMcdeSys_la_LIBADD = $(ALSALIB)
endif
libMcdeSys_la_LDFLAGS = -version-info 1:0:0
# what the system tells about itself, for a look by hand: make check runs it
check_PROGRAMS = test_sysinfo test_netmount test_updatesource test_processes test_job test_netinput test_sysload test_disk_events
# built by make mcde-host as well, so tools/mcde-tests.sh finds it there
noinst_PROGRAMS = test_audio
test_sysinfo_SOURCES = test_sysinfo.c
test_sysinfo_CPPFLAGS = -I$(top_srcdir)/include
test_sysinfo_LDADD = libMcdeSys.la
@@ -47,4 +53,7 @@ test_sysload_LDADD = libMcdeSys.la
test_disk_events_SOURCES = test_disk_events.c
test_disk_events_CPPFLAGS = -I$(top_srcdir)/include
test_disk_events_LDADD = libMcdeSys.la
TESTS = test_sysinfo test_netmount test_updatesource test_processes test_job test_netinput test_sysload test_disk_events
test_audio_SOURCES = test_audio.c
test_audio_CPPFLAGS = -I$(top_srcdir)/include
test_audio_LDADD = libMcdeSys.la -lm
TESTS = test_sysinfo test_netmount test_updatesource test_processes test_job test_netinput test_sysload test_disk_events test_audio
+107
View File
@@ -0,0 +1,107 @@
/*
* 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";
}
+223
View File
@@ -0,0 +1,223 @@
/*
* 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 */
+206
View File
@@ -0,0 +1,206 @@
/*
* audio_netbsd.c -- MCDE: PCM streams through audio(4) (McdeSys.h, "Sound")
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* The device is /dev/audio (or $AUDIODEV, or /dev/sound), set to signed
* 16-bit little-endian, the wanted rate and channels with AUDIO_SETINFO;
* the answer is checked, because a device may round the rate. The smaller
* of the wanted and the device's block size is one period. The descriptor is
* non-blocking: read/write answer EAGAIN when nothing fits, mcde_sys_pcm_wait
* polls it. Compiled for the NetBSD sysroot, not yet run on NetBSD.
*/
/* audio(4) uses the BSD types (u_int) that a strict POSIX feature set hides;
* NetBSD's own programs ask for _NETBSD_SOURCE, so this file does too. */
#define _NETBSD_SOURCE 1
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/audioio.h>
#include <sys/ioctl.h>
#include <Mcde/McdeSys.h>
#include "private.h"
typedef struct {
int fd;
size_t block; /* frames per period */
} BsdPcm;
static ssize_t
BsdRead(McdeSysPcm *pcm, int16_t *frames, size_t count)
{
BsdPcm *b = pcm->priv;
size_t bytes = count * sizeof *frames;
ssize_t n;
n = read(b->fd, frames, bytes);
if (n < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK)
return 0;
(void) mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot read the audio "
"device: %s.", strerror(errno));
return -1;
}
return n / (ssize_t) sizeof *frames;
}
static ssize_t
BsdWrite(McdeSysPcm *pcm, const int16_t *frames, size_t count)
{
BsdPcm *b = pcm->priv;
ssize_t n;
n = write(b->fd, frames, count * sizeof *frames);
if (n < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK)
return 0;
(void) mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot write the audio "
"device: %s.", strerror(errno));
return -1;
}
return n / (ssize_t) sizeof *frames;
}
static int
BsdWait(McdeSysPcm *pcm, int timeout_ms)
{
BsdPcm *b = pcm->priv;
struct pollfd p;
int rc;
p.fd = b->fd;
p.events = pcm->direction == MCDE_SYS_PCM_CAPTURE ? POLLIN : POLLOUT;
p.revents = 0;
rc = poll(&p, 1, timeout_ms);
if (rc < 0) {
if (errno == EINTR)
return 0;
(void) mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot wait for the "
"audio device: %s.", strerror(errno));
return -1;
}
if (rc == 0)
return 0;
if (p.revents & (POLLERR | POLLHUP | POLLNVAL)) {
(void) mcde_sys_fail(MCDE_SYS_EFAIL, "The audio device went "
"away.");
return -1;
}
return 1;
}
static void
BsdClose(McdeSysPcm *pcm)
{
BsdPcm *b = pcm->priv;
if (b != NULL) {
if (b->fd >= 0)
(void) close(b->fd);
free(b);
}
pcm->priv = NULL;
}
static const McdeSysPcmOps bsdOps = {
BsdRead,
BsdWrite,
BsdWait,
BsdClose
};
McdeSysPcm *
mcde_sys_pcm_device_open(int direction, unsigned rate, unsigned channels)
{
const char *dev = getenv("AUDIODEV");
audio_info_t info;
McdeSysPcm *pcm = NULL;
BsdPcm *b = NULL;
int fd = -1;
int flags;
if (dev == NULL || *dev == '\0')
dev = "/dev/audio";
flags = direction == MCDE_SYS_PCM_PLAYBACK ? O_WRONLY : O_RDONLY;
fd = open(dev, flags | O_NONBLOCK | O_CLOEXEC);
if (fd < 0 && errno == ENOENT && strcmp(dev, "/dev/audio") == 0) {
dev = "/dev/sound";
fd = open(dev, flags | O_NONBLOCK | O_CLOEXEC);
}
if (fd < 0) {
(void) mcde_sys_fail(MCDE_SYS_ENOTSUP, "No audio device: %s.",
strerror(errno));
return NULL;
}
AUDIO_INITINFO(&info);
info.mode = direction == MCDE_SYS_PCM_PLAYBACK ?
AUMODE_PLAY : AUMODE_RECORD;
{
audio_prinfo_t *pr = direction == MCDE_SYS_PCM_PLAYBACK ?
&info.play : &info.record;
pr->encoding = AUDIO_ENCODING_SLINEAR_LE;
pr->precision = 16;
pr->channels = (u_int) channels;
pr->sample_rate = rate;
}
/* one period, in bytes, for the device's own blocking writes */
info.blocksize = (u_int) (rate / 50 * channels * 2);
if (ioctl(fd, AUDIO_SETINFO, &info) < 0) {
(void) mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot set the audio "
"format on %s: %s.", dev, strerror(errno));
(void) close(fd);
return NULL;
}
AUDIO_INITINFO(&info);
if (ioctl(fd, AUDIO_GETINFO, &info) < 0) {
(void) mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot read the audio "
"format of %s: %s.", dev, strerror(errno));
(void) close(fd);
return NULL;
}
{
audio_prinfo_t *pr = direction == MCDE_SYS_PCM_PLAYBACK ?
&info.play : &info.record;
if (pr->encoding != AUDIO_ENCODING_SLINEAR_LE ||
pr->precision != 16 ||
pr->channels != (u_int) channels ||
pr->sample_rate != rate) {
(void) mcde_sys_fail(MCDE_SYS_EFAIL, "The audio "
"device did not take %u Hz, %u channel(s), "
"16 bit.", rate, channels);
(void) close(fd);
return NULL;
}
}
pcm = calloc(1, sizeof *pcm);
b = calloc(1, sizeof *b);
if (pcm == NULL || b == NULL) {
free(pcm);
free(b);
(void) close(fd);
(void) mcde_sys_fail(MCDE_SYS_EFAIL, "Out of memory.");
return NULL;
}
b->fd = fd;
b->block = info.blocksize / (channels * 2) != 0 ?
info.blocksize / (channels * 2) : rate / 50;
pcm->ops = &bsdOps;
pcm->name = "audio(4)";
pcm->direction = direction;
pcm->priv = b;
return pcm;
}
+86
View File
@@ -0,0 +1,86 @@
/*
* audio_null.c -- MCDE: the silent PCM stream (McdeSys.h, "Sound")
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* Capture reads silence, playback discards, a wait is over at once. It is
* what a test uses (no sound card in a test VM) and what a session without a
* device falls back to when the caller asks for it; nothing here needs ALSA
* or /dev/audio, so it builds on every system.
*/
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <Mcde/McdeSys.h>
#include "private.h"
typedef struct {
unsigned rate;
unsigned channels;
} NullPcm;
static ssize_t
NullRead(McdeSysPcm *pcm, int16_t *frames, size_t count)
{
(void) pcm;
memset(frames, 0, count * sizeof *frames);
return (ssize_t) count;
}
static ssize_t
NullWrite(McdeSysPcm *pcm, const int16_t *frames, size_t count)
{
(void) pcm;
(void) frames;
return (ssize_t) count;
}
static int
NullWait(McdeSysPcm *pcm, int timeout_ms)
{
(void) pcm;
(void) timeout_ms;
return 1;
}
static void
NullClose(McdeSysPcm *pcm)
{
free(pcm->priv);
pcm->priv = NULL;
}
static const McdeSysPcmOps nullOps = {
NullRead,
NullWrite,
NullWait,
NullClose
};
McdeSysPcm *
mcde_sys_pcm_null_open(int direction, unsigned rate, unsigned channels)
{
McdeSysPcm *pcm;
NullPcm *priv;
pcm = calloc(1, sizeof *pcm);
priv = calloc(1, sizeof *priv);
if (pcm == NULL || priv == NULL) {
free(pcm);
free(priv);
(void) mcde_sys_fail(MCDE_SYS_EFAIL, "Out of memory.");
return NULL;
}
priv->rate = rate;
priv->channels = channels;
pcm->ops = &nullOps;
pcm->name = "null";
pcm->direction = direction;
pcm->priv = priv;
return pcm;
}
+24
View File
@@ -381,4 +381,28 @@ int mcde_sys_mixer_mute(int muted);
* when no sound server runs (audioplay(1) on NetBSD), or NULL */
const char *mcde_sys_player(void);
/* audio.c: the PCM streams of McdeSys.h. A backend fills PRIV and the ops;
* audio.c owns the handle and the public calls. */
typedef struct McdeSysPcmOps {
ssize_t (*read)(McdeSysPcm *pcm, int16_t *frames, size_t count);
ssize_t (*write)(McdeSysPcm *pcm, const int16_t *frames, size_t count);
int (*wait)(McdeSysPcm *pcm, int timeout_ms);
void (*close)(McdeSysPcm *pcm);
} McdeSysPcmOps;
struct McdeSysPcm {
const McdeSysPcmOps *ops;
const char *name; /* "alsa", "audio(4)" or "null" */
int direction;
void *priv;
};
/* audio_null.c: silence in, discard out; never fails but for memory */
McdeSysPcm *mcde_sys_pcm_null_open(int direction, unsigned rate,
unsigned channels);
/* audio_linux.c (ALSA) and audio_netbsd.c (audio(4)): NULL when the device
* cannot be opened or set up, the sentence in mcde_sys_error() */
McdeSysPcm *mcde_sys_pcm_device_open(int direction, unsigned rate,
unsigned channels);
#endif /* _McdeSys_private_h */
+174
View File
@@ -0,0 +1,174 @@
/*
* test_audio.c -- McdeSys: the PCM streams (T6.1)
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* The null stream is checked to the frame (silence in, discard out, both
* directions kept apart); the system's own device is tried and reported, but
* a machine without a sound card is no failure (a build host, a test VM).
*/
#define _POSIX_C_SOURCE 200809L
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <Mcde/McdeSys.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define RATE 48000
#define PERIOD 960 /* 20 ms */
static int fails;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \
#cond); \
fails++; \
} \
} while (0)
static void
fill_tone(int16_t *pcm, int n, int offset)
{
int i;
for (i = 0; i < n; i++) {
double t = (double) (offset + i) / (double) RATE;
pcm[i] = (int16_t) (sin(2.0 * M_PI * 440.0 * t) * 8000.0);
}
}
static void
test_null(void)
{
McdeSysPcm *play = NULL;
McdeSysPcm *cap = NULL;
int16_t pcm[PERIOD];
ssize_t n;
int i;
int nonzero;
CHECK(mcde_sys_pcm_open(&play, MCDE_SYS_PCM_PLAYBACK,
MCDE_SYS_PCM_NULL, RATE, 1) == MCDE_SYS_OK);
CHECK(play != NULL);
CHECK(mcde_sys_pcm_open(&cap, MCDE_SYS_PCM_CAPTURE,
MCDE_SYS_PCM_NULL, RATE, 1) == MCDE_SYS_OK);
CHECK(cap != NULL);
if (play == NULL || cap == NULL)
goto out;
CHECK(strcmp(mcde_sys_pcm_backend(play), "null") == 0);
CHECK(mcde_sys_pcm_wait(play, 0) == 1);
CHECK(mcde_sys_pcm_wait(cap, 0) == 1);
fill_tone(pcm, PERIOD, 0);
n = mcde_sys_pcm_write(play, pcm, PERIOD);
CHECK(n == PERIOD);
n = mcde_sys_pcm_write(play, pcm, PERIOD);
CHECK(n == PERIOD);
memset(pcm, 0x5a, sizeof pcm);
n = mcde_sys_pcm_read(cap, pcm, PERIOD);
CHECK(n == PERIOD);
nonzero = 0;
for (i = 0; i < PERIOD; i++)
if (pcm[i] != 0)
nonzero++;
CHECK(nonzero == 0);
/* the directions are not mixed up */
CHECK(mcde_sys_pcm_write(cap, pcm, PERIOD) < 0);
CHECK(mcde_sys_pcm_read(play, pcm, PERIOD) < 0);
/* the backend is chosen, not guessed */
{
McdeSysPcm *bad = NULL;
CHECK(mcde_sys_pcm_open(&bad, 7, MCDE_SYS_PCM_NULL, RATE, 1)
!= MCDE_SYS_OK);
CHECK(bad == NULL);
CHECK(mcde_sys_pcm_open(&bad, MCDE_SYS_PCM_PLAYBACK,
MCDE_SYS_PCM_NULL, 0, 1) != MCDE_SYS_OK);
CHECK(bad == NULL);
}
out:
mcde_sys_pcm_close(play);
mcde_sys_pcm_close(cap);
}
static void
test_device(int direction)
{
const char *what = direction == MCDE_SYS_PCM_PLAYBACK ?
"playback" : "capture";
McdeSysPcm *pcm = NULL;
int16_t buf[PERIOD];
long long started;
long long elapsed;
int periods = 0;
ssize_t n;
if (mcde_sys_pcm_open(&pcm, direction, MCDE_SYS_PCM_DEFAULT, RATE, 1)
!= MCDE_SYS_OK) {
printf("audio: no %s device (%s), skipped\n", what,
mcde_sys_error() != NULL ? mcde_sys_error() : "");
return;
}
printf("audio: %s device is %s\n", what, mcde_sys_pcm_backend(pcm));
started = (long long) time(NULL);
if (direction == MCDE_SYS_PCM_PLAYBACK) {
int offset = 0;
while (periods < 5) {
fill_tone(buf, PERIOD, offset);
offset += PERIOD;
if (mcde_sys_pcm_wait(pcm, 1000) < 0)
break;
n = mcde_sys_pcm_write(pcm, buf, PERIOD);
if (n < 0)
break;
if (n == PERIOD)
periods++;
else
(void) mcde_sys_pcm_wait(pcm, 100);
}
CHECK(periods == 5);
} else {
while (periods < 5) {
if (mcde_sys_pcm_wait(pcm, 1000) < 0)
break;
n = mcde_sys_pcm_read(pcm, buf, PERIOD);
if (n < 0)
break;
if (n > 0)
periods++;
}
CHECK(periods > 0);
}
elapsed = (long long) time(NULL) - started;
printf("audio: %s: %d periods in %lld s\n", what, periods, elapsed);
mcde_sys_pcm_close(pcm);
}
int
main(void)
{
test_null();
test_device(MCDE_SYS_PCM_PLAYBACK);
test_device(MCDE_SYS_PCM_CAPTURE);
if (fails != 0) {
fprintf(stderr, "%d test(s) failed\n", fails);
return 1;
}
printf("test_audio: all ok\n");
return 0;
}