aboutsummaryrefslogtreecommitdiff
path: root/share/examples/sound
diff options
context:
space:
mode:
Diffstat (limited to 'share/examples/sound')
-rw-r--r--share/examples/sound/basic.c99
-rw-r--r--share/examples/sound/midi.c77
-rw-r--r--share/examples/sound/oss/README (renamed from share/examples/sound/README)0
-rw-r--r--share/examples/sound/oss/audio.c (renamed from share/examples/sound/ossinit.h)198
-rw-r--r--share/examples/sound/ossmidi.h63
-rw-r--r--share/examples/sound/sndstat_nv.c206
6 files changed, 374 insertions, 269 deletions
diff --git a/share/examples/sound/basic.c b/share/examples/sound/basic.c
deleted file mode 100644
index 83ecbe6ea9a7..000000000000
--- a/share/examples/sound/basic.c
+++ /dev/null
@@ -1,99 +0,0 @@
-/*
- * SPDX-License-Identifier: BSD-2-Clause
- *
- * Copyright (c) 2021 Goran Mekić
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include "ossinit.h"
-
-int
-main()
-{
- config_t config = {
- .device = "/dev/dsp",
- .channels = -1,
- .format = format,
- .frag = -1,
- .sample_rate = 48000,
- .sample_size = sizeof(sample_t),
- .buffer_info.fragments = -1,
- .mmap = 0,
- };
-
- /* Initialize device */
- oss_init(&config);
-
- /*
- * Allocate input and output buffers so that their size match
- * frag_size
- */
- int ret;
- int bytes = config.buffer_info.bytes;
- int8_t *ibuf = malloc(bytes);
- int8_t *obuf = malloc(bytes);
- sample_t *channels = malloc(bytes);
-
- printf(
- "bytes: %d, fragments: %d, fragsize: %d, fragstotal: %d, samples: %d\n",
- bytes,
- config.buffer_info.fragments,
- config.buffer_info.fragsize,
- config.buffer_info.fragstotal,
- config.sample_count
- );
-
- /* Minimal engine: read input and copy it to the output */
- for (;;) {
- ret = read(config.fd, ibuf, bytes);
- if (ret < bytes) {
- fprintf(
- stderr,
- "Requested %d bytes, but read %d!\n",
- bytes,
- ret
- );
- break;
- }
- oss_split(&config, (sample_t *)ibuf, channels);
- /* All processing will happen here */
- oss_merge(&config, channels, (sample_t *)obuf);
- ret = write(config.fd, obuf, bytes);
- if (ret < bytes) {
- fprintf(
- stderr,
- "Requested %d bytes, but wrote %d!\n",
- bytes,
- ret
- );
- break;
- }
- }
-
- /* Cleanup */
- free(channels);
- free(obuf);
- free(ibuf);
- close(config.fd);
- return (0);
-}
diff --git a/share/examples/sound/midi.c b/share/examples/sound/midi.c
index 6d6ac9aa0fcd..5b001ba537e5 100644
--- a/share/examples/sound/midi.c
+++ b/share/examples/sound/midi.c
@@ -2,6 +2,10 @@
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2022 Goran Mekić
+ * Copyright (c) 2024 The FreeBSD Foundation
+ *
+ * Portions of this software were developed by Christos Margiolis
+ * <christos@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -25,52 +29,61 @@
* SUCH DAMAGE.
*/
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
#include <unistd.h>
-#include "ossmidi.h"
+#define CMD_MASK 0xF0
+#define CHANNEL_MASK 0x0F
+#define NOTE_ON 0x90
+#define NOTE_OFF 0x80
+#define CTL_CHANGE 0xB0
int
-main()
+main(int argc, char *argv[])
{
- midi_event_t event;
- midi_config_t midi_config;
- int l = -1;
- unsigned char raw;
+ int fd;
+ unsigned char raw, type, channel, b1, b2;
- midi_config.device = "/dev/umidi1.0";
- oss_midi_init(&midi_config);
+ if ((fd = open("/dev/umidi0.0", O_RDWR)) < 0)
+ err(1, "Error opening MIDI device");
- while ((l = read(midi_config.fd, &raw, sizeof(raw))) != -1) {
- if (!(raw & 0x80)) {
+ for (;;) {
+ if (read(fd, &raw, sizeof(raw)) < sizeof(raw))
+ err(1, "Error reading command byte");
+ if (!(raw & 0x80))
continue;
- }
- event.type = raw & CMD_MASK;
- event.channel = raw & CHANNEL_MASK;
- switch (event.type) {
+
+ type = raw & CMD_MASK;
+ channel = raw & CHANNEL_MASK;
+
+ if (read(fd, &b1, sizeof(b1)) < sizeof(b1))
+ err(1, "Error reading byte 1");
+ if (read(fd, &b2, sizeof(b2)) < sizeof(b2))
+ err(1, "Error reading byte 2");
+
+ switch (type) {
case NOTE_ON:
- case NOTE_OFF:
- case CONTROLLER_ON:
- if ((l = read(midi_config.fd, &(event.note), sizeof(event.note))) == -1) {
- perror("Error reading MIDI note");
- exit(1);
- }
- if ((l = read(midi_config.fd, &(event.velocity), sizeof(event.velocity))) == -1) {
- perror("Error reading MIDI velocity");
- exit(1);
- }
+ printf("Channel %d, note on %d, velocity %d\n",
+ channel, b1, b2);
break;
- }
- switch (event.type) {
- case NOTE_ON:
case NOTE_OFF:
- printf("Channel %d, note %d, velocity %d\n", event.channel, event.note, event.velocity);
+ printf("Channel %d, note off %d, velocity %d\n",
+ channel, b1, b2);
break;
- case CONTROLLER_ON:
- printf("Channel %d, controller %d, value %d\n", event.channel, event.controller, event.value);
+ case CTL_CHANGE:
+ printf("Channel %d, controller change %d, value %d\n",
+ channel, b1, b2);
break;
default:
- printf("Unknown event type %d\n", event.type);
+ printf("Unknown event type %d\n", type);
+ break;
}
}
- return 0;
+
+ close(fd);
+
+ return (0);
}
diff --git a/share/examples/sound/README b/share/examples/sound/oss/README
index 0188a26348c8..0188a26348c8 100644
--- a/share/examples/sound/README
+++ b/share/examples/sound/oss/README
diff --git a/share/examples/sound/ossinit.h b/share/examples/sound/oss/audio.c
index 83920712286d..4dd3c8b82575 100644
--- a/share/examples/sound/ossinit.h
+++ b/share/examples/sound/oss/audio.c
@@ -2,6 +2,10 @@
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2021 Goran Mekić
+ * Copyright (c) 2024 The FreeBSD Foundation
+ *
+ * Portions of this software were developed by Christos Margiolis
+ * <christos@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -26,6 +30,8 @@
*/
#include <sys/soundcard.h>
+
+#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
@@ -33,7 +39,6 @@
#include <string.h>
#include <unistd.h>
-
#ifndef SAMPLE_SIZE
#define SAMPLE_SIZE 16
#endif
@@ -55,8 +60,6 @@ int format = AFMT_S32_NE; /* Not a real value, just silencing
* compiler errors */
#endif
-
-
/*
* Minimal configuration for OSS
* For real world applications, this structure will probably contain many
@@ -77,74 +80,69 @@ typedef struct config {
audio_buf_info buffer_info;
} config_t;
-
/*
- * Error state is indicated by value=-1 in which case application exits
- * with error
+ * Error state is indicated by value=-1 in which case application exits with
+ * error
*/
static inline void
check_error(const int value, const char *message)
{
- if (value == -1) {
- fprintf(stderr, "OSS error: %s %s\n", message, strerror(errno));
- exit(1);
- }
+ if (value == -1)
+ err(1, "OSS error: %s\n", message);
}
-
/* Calculate frag by giving it minimal size of buffer */
static inline int
size2frag(int x)
{
int frag = 0;
- while ((1 << frag) < x) {
+ while ((1 << frag) < x)
++frag;
- }
- return frag;
-}
+ return (frag);
+}
/*
* Split input buffer into channels. Input buffer is in interleaved format
- * which means if we have 2 channels (L and R), this is what the buffer of
- * 8 samples would contain: L,R,L,R,L,R,L,R. The result are two channels
+ * which means if we have 2 channels (L and R), this is what the buffer of 8
+ * samples would contain: L,R,L,R,L,R,L,R. The result are two channels
* containing: L,L,L,L and R,R,R,R.
*/
-void
+static void
oss_split(config_t *config, sample_t *input, sample_t *output)
{
- int channel;
- int index;
+ int channel, index, i;
- for (int i = 0; i < config->sample_count; ++i) {
+ for (i = 0; i < config->sample_count; ++i) {
channel = i % config->channels;
index = i / config->channels;
output[channel * index] = input[i];
}
}
-
/*
* Convert channels into interleaved format and place it in output
* buffer
*/
-void
+static void
oss_merge(config_t *config, sample_t *input, sample_t *output)
{
- for (int channel = 0; channel < config->channels; ++channel) {
- for (int index = 0; index < config->chsamples; ++index) {
- output[index * config->channels + channel] = input[channel * index];
+ int channel, index;
+
+ for (channel = 0; channel < config->channels; ++channel) {
+ for (index = 0; index < config->chsamples; ++index) {
+ output[index * config->channels + channel] =
+ input[channel * index];
}
}
}
-void
+static void
oss_init(config_t *config)
{
- int error;
- int tmp;
+ int error, tmp, min_frag;
/* Open the device for read and write */
config->fd = open(config->device, O_RDWR);
@@ -158,19 +156,19 @@ oss_init(config_t *config)
printf("max_channels: %d\n", config->audio_info.max_channels);
printf("latency: %d\n", config->audio_info.latency);
printf("handle: %s\n", config->audio_info.handle);
- if (config->audio_info.min_rate > config->sample_rate || config->sample_rate > config->audio_info.max_rate) {
- fprintf(stderr, "%s doesn't support chosen ", config->device);
- fprintf(stderr, "samplerate of %dHz!\n", config->sample_rate);
- exit(1);
+ if (config->audio_info.min_rate > config->sample_rate ||
+ config->sample_rate > config->audio_info.max_rate) {
+ errx(1, "%s doesn't support chosen samplerate of %dHz!\n",
+ config->device, config->sample_rate);
}
- if (config->channels < 1) {
+ if (config->channels < 1)
config->channels = config->audio_info.max_channels;
- }
/*
- * If device is going to be used in mmap mode, disable all format
- * conversions. Official OSS documentation states error code should not be
- * checked. http://manuals.opensound.com/developer/mmap_test.c.html#LOC10
+ * If device is going to be used in mmap mode, disable all format
+ * conversions. Official OSS documentation states error code should not
+ * be checked.
+ * http://manuals.opensound.com/developer/mmap_test.c.html#LOC10
*/
if (config->mmap) {
tmp = 0;
@@ -178,16 +176,16 @@ oss_init(config_t *config)
}
/*
- * Set number of channels. If number of channels is chosen to the value
- * near the one wanted, save it in config
+ * Set number of channels. If number of channels is chosen to the value
+ * near the one wanted, save it in config
*/
tmp = config->channels;
error = ioctl(config->fd, SNDCTL_DSP_CHANNELS, &tmp);
check_error(error, "SNDCTL_DSP_CHANNELS");
- if (tmp != config->channels) { /* or check if tmp is close enough? */
- fprintf(stderr, "%s doesn't support chosen ", config->device);
- fprintf(stderr, "channel count of %d", config->channels);
- fprintf(stderr, ", set to %d!\n", tmp);
+ /* Or check if tmp is close enough? */
+ if (tmp != config->channels) {
+ errx(1, "%s doesn't support chosen channel count of %d set "
+ "to %d!\n", config->device, config->channels, tmp);
}
config->channels = tmp;
@@ -196,8 +194,8 @@ oss_init(config_t *config)
error = ioctl(config->fd, SNDCTL_DSP_SETFMT, &tmp);
check_error(error, "SNDCTL_DSP_SETFMT");
if (tmp != config->format) {
- fprintf(stderr, "%s doesn't support chosen sample format!\n", config->device);
- exit(1);
+ errx(1, "%s doesn't support chosen sample format!\n",
+ config->device);
}
/* Most common values for samplerate (in kHz): 44.1, 48, 88.2, 96 */
@@ -208,40 +206,33 @@ oss_init(config_t *config)
/* Get and check device capabilities */
error = ioctl(config->fd, SNDCTL_DSP_GETCAPS, &(config->audio_info.caps));
check_error(error, "SNDCTL_DSP_GETCAPS");
- if (!(config->audio_info.caps & PCM_CAP_DUPLEX)) {
- fprintf(stderr, "Device doesn't support full duplex!\n");
- exit(1);
- }
+ if (!(config->audio_info.caps & PCM_CAP_DUPLEX))
+ errx(1, "Device doesn't support full duplex!\n");
+
if (config->mmap) {
- if (!(config->audio_info.caps & PCM_CAP_TRIGGER)) {
- fprintf(stderr, "Device doesn't support triggering!\n");
- exit(1);
- }
- if (!(config->audio_info.caps & PCM_CAP_MMAP)) {
- fprintf(stderr, "Device doesn't support mmap mode!\n");
- exit(1);
- }
+ if (!(config->audio_info.caps & PCM_CAP_TRIGGER))
+ errx(1, "Device doesn't support triggering!\n");
+ if (!(config->audio_info.caps & PCM_CAP_MMAP))
+ errx(1, "Device doesn't support mmap mode!\n");
}
/*
- * If desired frag is smaller than minimum, based on number of channels
- * and format (size in bits: 8, 16, 24, 32), set that as frag. Buffer size
- * is 2^frag, but the real size of the buffer will be read when the
- * configuration of the device is successful
+ * If desired frag is smaller than minimum, based on number of channels
+ * and format (size in bits: 8, 16, 24, 32), set that as frag. Buffer
+ * size is 2^frag, but the real size of the buffer will be read when
+ * the configuration of the device is successful
*/
- int min_frag = size2frag(config->sample_size * config->channels);
+ min_frag = size2frag(config->sample_size * config->channels);
- if (config->frag < min_frag) {
+ if (config->frag < min_frag)
config->frag = min_frag;
- }
/*
- * Allocate buffer in fragments. Total buffer will be split in number
- * of fragments (2 by default)
+ * Allocate buffer in fragments. Total buffer will be split in number
+ * of fragments (2 by default)
*/
- if (config->buffer_info.fragments < 0) {
+ if (config->buffer_info.fragments < 0)
config->buffer_info.fragments = 2;
- }
tmp = ((config->buffer_info.fragments) << 16) | config->frag;
error = ioctl(config->fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
check_error(error, "SNDCTL_DSP_SETFRAGMENT");
@@ -250,13 +241,70 @@ oss_init(config_t *config)
error = ioctl(config->fd, SNDCTL_DSP_GETOSPACE, &(config->buffer_info));
check_error(error, "SNDCTL_DSP_GETOSPACE");
if (config->buffer_info.bytes < 1) {
- fprintf(
- stderr,
- "OSS buffer error: buffer size can not be %d\n",
- config->buffer_info.bytes
- );
- exit(1);
+ errx(1, "OSS buffer error: buffer size can not be %d\n",
+ config->buffer_info.bytes);
}
config->sample_count = config->buffer_info.bytes / config->sample_size;
config->chsamples = config->sample_count / config->channels;
}
+
+int
+main(int argc, char *argv[])
+{
+ int ret, bytes;
+ int8_t *ibuf, *obuf;
+ config_t config = {
+ .device = "/dev/dsp",
+ .channels = -1,
+ .format = format,
+ .frag = -1,
+ .sample_rate = 48000,
+ .sample_size = sizeof(sample_t),
+ .buffer_info.fragments = -1,
+ .mmap = 0,
+ };
+
+ /* Initialize device */
+ oss_init(&config);
+
+ /*
+ * Allocate input and output buffers so that their size match frag_size
+ */
+ bytes = config.buffer_info.bytes;
+ ibuf = malloc(bytes);
+ obuf = malloc(bytes);
+ sample_t *channels = malloc(bytes);
+
+ printf("bytes: %d, fragments: %d, fragsize: %d, fragstotal: %d, "
+ "samples: %d\n",
+ bytes, config.buffer_info.fragments,
+ config.buffer_info.fragsize, config.buffer_info.fragstotal,
+ config.sample_count);
+
+ /* Minimal engine: read input and copy it to the output */
+ for (;;) {
+ ret = read(config.fd, ibuf, bytes);
+ if (ret < bytes) {
+ fprintf(stderr, "Requested %d bytes, but read %d!\n",
+ bytes, ret);
+ break;
+ }
+ oss_split(&config, (sample_t *)ibuf, channels);
+ /* All processing will happen here */
+ oss_merge(&config, channels, (sample_t *)obuf);
+ ret = write(config.fd, obuf, bytes);
+ if (ret < bytes) {
+ fprintf(stderr, "Requested %d bytes, but wrote %d!\n",
+ bytes, ret);
+ break;
+ }
+ }
+
+ /* Cleanup */
+ free(channels);
+ free(obuf);
+ free(ibuf);
+ close(config.fd);
+
+ return (0);
+}
diff --git a/share/examples/sound/ossmidi.h b/share/examples/sound/ossmidi.h
deleted file mode 100644
index 99a6bacffe73..000000000000
--- a/share/examples/sound/ossmidi.h
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * SPDX-License-Identifier: BSD-2-Clause
- *
- * Copyright (c) 2022 Goran Mekić
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <fcntl.h>
-
-#define CMD_MASK 0xF0
-#define NOTE_ON 0x90
-#define NOTE_OFF 0x80
-#define CHANNEL_MASK 0xF
-#define CONTROLLER_ON 0xB0
-
-typedef struct midi_event {
- unsigned char type;
- unsigned char channel;
- union {
- unsigned char note;
- unsigned controller;
- };
- union {
- unsigned char velocity;
- unsigned char value;
- };
-} midi_event_t;
-
-typedef struct midi_config {
- char *device;
- int fd;
-} midi_config_t;
-
-void
-oss_midi_init(midi_config_t *config)
-{
- if ((config->fd = open(config->device, O_RDWR)) == -1) {
- perror("Error opening MIDI device");
- exit(1);
- }
-}
diff --git a/share/examples/sound/sndstat_nv.c b/share/examples/sound/sndstat_nv.c
new file mode 100644
index 000000000000..1056c1f4a08f
--- /dev/null
+++ b/share/examples/sound/sndstat_nv.c
@@ -0,0 +1,206 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2024 The FreeBSD Foundation
+ *
+ * This software was developed by Christos Margiolis <christos@FreeBSD.org>
+ * under sponsorship from the FreeBSD Foundation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/sndstat.h>
+#include <sys/nv.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+/*
+ * Example program showcasing how to use sndstat(4)'s nvlist interface, and how
+ * to fetch all currently supported fields, with the appropriate error checks.
+ *
+ * For more detailed information on what each nvlist field represents, please
+ * read sndstat(4)'s man page.
+ */
+
+int
+main(int argc, char *argv[])
+{
+ nvlist_t *nvl;
+ const nvlist_t * const *di;
+ const nvlist_t * const *cdi;
+ struct sndstioc_nv_arg arg;
+ size_t nitems, nchans, i, j;
+ int fd, pchan, rchan;
+
+ if ((fd = open("/dev/sndstat", O_RDONLY)) < 0)
+ err(1, "open(/dev/sndstat)");
+
+ if (ioctl(fd, SNDSTIOC_REFRESH_DEVS, NULL) < 0)
+ err(1, "ioctl(SNDSTIOC_REFRESH_DEVS)");
+
+ arg.nbytes = 0;
+ arg.buf = NULL;
+ if (ioctl(fd, SNDSTIOC_GET_DEVS, &arg) < 0)
+ err(1, "ioctl(SNDSTIOC_GET_DEVS#1)");
+
+ if ((arg.buf = malloc(arg.nbytes)) == NULL)
+ err(1, "malloc");
+
+ if (ioctl(fd, SNDSTIOC_GET_DEVS, &arg) < 0)
+ err(1, "ioctl(SNDSTIOC_GET_DEVS#2)");
+
+ if ((nvl = nvlist_unpack(arg.buf, arg.nbytes, 0)) == NULL)
+ err(1, "nvlist_unpack");
+
+ if (nvlist_empty(nvl) || !nvlist_exists(nvl, SNDST_DSPS))
+ errx(1, "no soundcards attached");
+
+ di = nvlist_get_nvlist_array(nvl, SNDST_DSPS, &nitems);
+ for (i = 0; i < nitems; i++) {
+#define NV(type, item) \
+ nvlist_get_ ## type (di[i], SNDST_DSPS_ ## item)
+ printf("nameunit=%s\n", NV(string, NAMEUNIT));
+ printf("\tfrom_user=%d\n", NV(bool, FROM_USER));
+ printf("\tdevnode=%s\n", NV(string, DEVNODE));
+ printf("\tdesc=%s\n", NV(string, DESC));
+ printf("\tprovider=%s\n", NV(string, PROVIDER));
+ printf("\tpchan=%d\n", (int)NV(number, PCHAN));
+ printf("\trchan=%d\n", (int)NV(number, RCHAN));
+ pchan = NV(number, PCHAN);
+ rchan = NV(number, RCHAN);
+#undef NV
+
+ if (pchan && !nvlist_exists(di[i], SNDST_DSPS_INFO_PLAY))
+ errx(1, "playback channel list empty");
+ if (rchan && !nvlist_exists(di[i], SNDST_DSPS_INFO_REC))
+ errx(1, "recording channel list empty");
+
+#define NV(type, mode, item) \
+ nvlist_get_ ## type (nvlist_get_nvlist(di[i], \
+ SNDST_DSPS_INFO_ ## mode), SNDST_DSPS_INFO_ ## item)
+ if (pchan) {
+ printf("\tplay_min_rate=%d\n",
+ (int)NV(number, PLAY, MIN_RATE));
+ printf("\tplay_max_rate=%d\n",
+ (int)NV(number, PLAY, MAX_RATE));
+ printf("\tplay_formats=%#08x\n",
+ (int)NV(number, PLAY, FORMATS));
+ printf("\tplay_min_chn=%d\n",
+ (int)NV(number, PLAY, MIN_CHN));
+ printf("\tplay_max_chn=%d\n",
+ (int)NV(number, PLAY, MAX_CHN));
+ }
+ if (rchan) {
+ printf("\trec_min_rate=%d\n",
+ (int)NV(number, REC, MIN_RATE));
+ printf("\trec_max_rate=%d\n",
+ (int)NV(number, REC, MAX_RATE));
+ printf("\trec_formats=%#08x\n",
+ (int)NV(number, REC, FORMATS));
+ printf("\trec_min_chn=%d\n",
+ (int)NV(number, REC, MIN_CHN));
+ printf("\trec_max_chn=%d\n",
+ (int)NV(number, REC, MAX_CHN));
+ }
+#undef NV
+
+ if (!nvlist_exists(di[i], SNDST_DSPS_PROVIDER_INFO))
+ continue;
+
+#define NV(type, item) \
+ nvlist_get_ ## type (nvlist_get_nvlist(di[i], \
+ SNDST_DSPS_PROVIDER_INFO), SNDST_DSPS_SOUND4_ ## item)
+ printf("\tunit=%d\n", (int)NV(number, UNIT));
+ printf("\tstatus=%s\n", NV(string, STATUS));
+ printf("\tbitperfect=%d\n", NV(bool, BITPERFECT));
+ printf("\tpvchan=%d\n", (int)NV(number, PVCHAN));
+ printf("\tpvchanrate=%d\n", (int)NV(number, PVCHANRATE));
+ printf("\tpvchanformat=%#08x\n", (int)NV(number, PVCHANFORMAT));
+ printf("\trvchan=%d\n", (int)NV(number, RVCHAN));
+ printf("\trvchanrate=%d\n", (int)NV(number, RVCHANRATE));
+ printf("\trvchanformat=%#08x\n", (int)NV(number, RVCHANFORMAT));
+#undef NV
+
+ if (!nvlist_exists(nvlist_get_nvlist(di[i],
+ SNDST_DSPS_PROVIDER_INFO), SNDST_DSPS_SOUND4_CHAN_INFO))
+ errx(1, "channel info list empty");
+
+ cdi = nvlist_get_nvlist_array(
+ nvlist_get_nvlist(di[i], SNDST_DSPS_PROVIDER_INFO),
+ SNDST_DSPS_SOUND4_CHAN_INFO, &nchans);
+ for (j = 0; j < nchans; j++) {
+#define NV(type, item) \
+ nvlist_get_ ## type (cdi[j], SNDST_DSPS_SOUND4_CHAN_ ## item)
+ printf("\tchan=%s\n", NV(string, NAME));
+ printf("\t\tparentchan=%s\n", NV(string, PARENTCHAN));
+ printf("\t\tunit=%d\n", (int)NV(number, UNIT));
+ printf("\t\tcaps=%#08x\n", (int)NV(number, CAPS));
+ printf("\t\tlatency=%d\n", (int)NV(number, LATENCY));
+ printf("\t\trate=%d\n", (int)NV(number, RATE));
+ printf("\t\tformat=%#08x\n", (int)NV(number, FORMAT));
+ printf("\t\tpid=%d\n", (int)NV(number, PID));
+ printf("\t\tcomm=%s\n", NV(string, COMM));
+ printf("\t\tintr=%d\n", (int)NV(number, INTR));
+ printf("\t\txruns=%d\n", (int)NV(number, XRUNS));
+ printf("\t\tfeedcnt=%d\n", (int)NV(number, FEEDCNT));
+ printf("\t\tleftvol=%d\n", (int)NV(number, LEFTVOL));
+ printf("\t\trightvol=%d\n", (int)NV(number, RIGHTVOL));
+ printf("\t\thwbuf_format=%#08x\n",
+ (int)NV(number, HWBUF_FORMAT));
+ printf("\t\thwbuf_size=%d\n",
+ (int)NV(number, HWBUF_SIZE));
+ printf("\t\thwbuf_blksz=%d\n",
+ (int)NV(number, HWBUF_BLKSZ));
+ printf("\t\thwbuf_blkcnt=%d\n",
+ (int)NV(number, HWBUF_BLKCNT));
+ printf("\t\thwbuf_free=%d\n",
+ (int)NV(number, HWBUF_FREE));
+ printf("\t\thwbuf_ready=%d\n",
+ (int)NV(number, HWBUF_READY));
+ printf("\t\tswbuf_format=%#08x\n",
+ (int)NV(number, SWBUF_FORMAT));
+ printf("\t\tswbuf_size=%d\n",
+ (int)NV(number, SWBUF_SIZE));
+ printf("\t\tswbuf_blksz=%d\n",
+ (int)NV(number, SWBUF_BLKSZ));
+ printf("\t\tswbuf_blkcnt=%d\n",
+ (int)NV(number, SWBUF_BLKCNT));
+ printf("\t\tswbuf_free=%d\n",
+ (int)NV(number, SWBUF_FREE));
+ printf("\t\tswbuf_ready=%d\n",
+ (int)NV(number, SWBUF_READY));
+ printf("\t\tswbuf_feederchain=%s\n",
+ NV(string, FEEDERCHAIN));
+#undef NV
+ }
+ }
+
+ free(arg.buf);
+ nvlist_destroy(nvl);
+ close(fd);
+
+ return (0);
+}