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

175 lines
3.9 KiB
C

/*
* 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;
}