This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch master
in repository wav_play.

View the commit online.

commit f29a2e3fdabb58498161e5dc90cfa23783f64f48
Author: Vincent Torri <[email protected]>
AuthorDate: Sat Nov 21 18:51:51 2026 +0100

    add freebsd OSS backend
---
 src/bin/wav_audio_oss.c | 161 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 161 insertions(+)

diff --git a/src/bin/wav_audio_oss.c b/src/bin/wav_audio_oss.c
new file mode 100644
index 0000000..84eabf8
--- /dev/null
+++ b/src/bin/wav_audio_oss.c
@@ -0,0 +1,161 @@
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <sys/ioctl.h>
+#include <sys/soundcard.h>
+
+#include "wav_audio.h"
+#include "wav_log.h"
+
+#define WAV_AUDIO_DEVICE "/dev/dsp"
+
+struct Device
+{
+    int fd;                  /* audio device */
+    const uint8_t *data;     /* PCM data (inside the mapped file) */
+    uint32_t data_size;      /* in bytes */
+    uint32_t block_align;    /* bytes per frame */
+};
+
+Device *
+wav_audio_init(void)
+{
+    Device *wav_dev;
+
+    wav_dev = (Device *)calloc(1, sizeof(Device));
+    if (!wav_dev)
+    {
+        WAV_LOG_ERR("calloc() failed");
+        return NULL;
+    }
+
+    /* blocking mode: write() waits for room in the device buffer */
+    wav_dev->fd = open(WAV_AUDIO_DEVICE, O_WRONLY);
+    if (wav_dev->fd < 0)
+    {
+        WAV_LOG_ERR("can not open %s", WAV_AUDIO_DEVICE);
+        free(wav_dev);
+        return NULL;
+    }
+
+    return wav_dev;
+}
+
+void
+wav_audio_shutdown(Device *dev)
+{
+    if (!dev)
+        return;
+
+    if (dev->fd >= 0)
+        close(dev->fd);
+    free(dev);
+}
+
+bool
+wav_audio_set(Device *dev, const Wav_File *wav)
+{
+    int fmt;
+    int want_fmt;
+    int channels;
+    int rate;
+
+    if (!dev || !wav || (wav->block_align == 0))
+        return false;
+
+    /* WAV PCM is little endian, 8 bits samples are unsigned, the others
+     * are signed. 24 bits packed samples are not handled: the OSS 24 bits
+     * format uses a 32 bits container. */
+    switch (wav->bits_per_sample)
+    {
+        case 8:
+            want_fmt = AFMT_U8;
+            break;
+        case 16:
+            want_fmt = AFMT_S16_LE;
+            break;
+        case 32:
+            want_fmt = AFMT_S32_LE;
+            break;
+        default:
+            WAV_LOG_ERR("unsupported bits per sample: %u", wav->bits_per_sample);
+            return false;
+    }
+
+    /* the order matters: format, channels, then rate */
+    fmt = want_fmt;
+    if ((ioctl(dev->fd, SNDCTL_DSP_SETFMT, &fmt) == -1) || (fmt != want_fmt))
+    {
+        WAV_LOG_ERR("the device does not support this sample format "
+                    "(%u bits)", wav->bits_per_sample);
+        return false;
+    }
+
+    channels = wav->num_channels;
+    if ((ioctl(dev->fd, SNDCTL_DSP_CHANNELS, &channels) == -1) ||
+        (channels != (int)wav->num_channels))
+    {
+        WAV_LOG_ERR("the device does not support %u channels", wav->num_channels);
+        return false;
+    }
+
+    /* the device may adjust the rate; a small difference is tolerated
+     * (the pitch is slightly different), not a large one. */
+    rate = (int)wav->sample_rate;
+    if (ioctl(dev->fd, SNDCTL_DSP_SPEED, &rate) == -1)
+    {
+        WAV_LOG_ERR("SNDCTL_DSP_SPEED failed");
+        return false;
+    }
+    if (rate != (int)wav->sample_rate)
+        WAV_LOG_WARN("rate adjusted by the device: %u Hz -> %d Hz",
+                     wav->sample_rate, rate);
+
+    dev->data = "" uint8_t *)wav->samples;
+    dev->data_size = wav->subchunk_2_size;
+    dev->block_align = wav->block_align;
+
+    return true;
+}
+
+bool
+wav_audio_play(Device *dev)
+{
+    const uint8_t *ptr;
+    uint32_t remaining;
+    ssize_t w;
+
+    if (!dev || (dev->fd < 0) || !dev->data || (dev->block_align == 0))
+        return false;
+
+    ptr = dev->data;
+    /* whole frames only */
+    remaining = dev->data_size - (dev->data_size % dev->block_align);
+
+    while (remaining > 0)
+    {
+        /* write() can write less than asked */
+        w = write(dev->fd, ptr, remaining);
+        if (w < 0)
+        {
+            if (errno == EINTR)
+                continue;
+            WAV_LOG_ERR("write() on %s failed", WAV_AUDIO_DEVICE);
+            return false;
+        }
+
+        ptr += w;
+        remaining -= (uint32_t)w;
+    }
+
+    /* wait until everything has been played */
+    if (ioctl(dev->fd, SNDCTL_DSP_SYNC, NULL) == -1)
+        WAV_LOG_WARN("SNDCTL_DSP_SYNC failed");
+
+    return true;
+}

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to