/*1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2026 Goran Mekić4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627/*28* This program demonstrates low-latency audio pass-through using mmap.29* Opens input and output audio devices using memory-mapped I/O,30* synchronizes them in a sync group for simultaneous start,31* then continuously copies audio data from input to output.32*/3334#include <time.h>3536#include "oss.h"3738/*39* Get current time in nanoseconds using monotonic clock.40* Monotonic clock is not affected by system time changes.41*/42static int64_t43gettime_ns(void)44{45struct timespec ts;4647if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)48err(1, "clock_gettime failed");49return ((int64_t)ts.tv_sec * 1000000000LL + ts.tv_nsec);50}5152/*53* Sleep until the specified absolute time (in nanoseconds).54* Uses TIMER_ABSTIME for precise timing synchronization.55*/56static void57sleep_until_ns(int64_t target_ns)58{59struct timespec ts;6061ts.tv_sec = target_ns / 1000000000LL;62ts.tv_nsec = target_ns % 1000000000LL;63if (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL) != 0)64err(1, "clock_nanosleep failed");65}6667/*68* Calculate the number of frames to process per iteration.69* Higher sample rates require larger steps to maintain efficiency.70*/71static unsigned72frame_stepping(unsigned sample_rate)73{74return (16U * (1U + (sample_rate / 50000U)));75}7677/*78* Update the mmap pointer and calculate progress.79* Returns the absolute progress in bytes.80*81* fd: file descriptor for the audio device82* request: ioctl request (SNDCTL_DSP_GETIPTR or SNDCTL_DSP_GETOPTR)83* map_pointer: current pointer position in the ring buffer84* map_progress: absolute progress in bytes85* buffer_bytes: total size of the ring buffer86* frag_size: size of each fragment87* frame_size: size of one audio frame in bytes88*/89static int64_t90update_map_progress(int fd, unsigned long request, int *map_pointer,91int64_t *map_progress, int buffer_bytes, int frag_size, int frame_size)92{93count_info info = {};94unsigned delta, max_bytes, cycles;95int fragments;9697if (ioctl(fd, request, &info) < 0)98err(1, "Failed to get mmap pointer");99if (info.ptr < 0 || info.ptr >= buffer_bytes)100errx(1, "Pointer out of bounds: %d", info.ptr);101if ((info.ptr % frame_size) != 0)102errx(1, "Pointer %d not aligned to frame size %d", info.ptr,103frame_size);104if (info.blocks < 0)105errx(1, "Invalid block count %d", info.blocks);106107/*108* Calculate delta: how many bytes have been processed since last check.109* Handle ring buffer wraparound using modulo arithmetic.110*/111delta = (info.ptr + buffer_bytes - *map_pointer) % buffer_bytes;112113/*114* Adjust delta based on reported blocks available.115* This accounts for cases where the pointer has wrapped multiple times.116*/117max_bytes = (info.blocks + 1) * frag_size - 1;118if (max_bytes >= delta) {119cycles = max_bytes - delta;120cycles -= cycles % buffer_bytes;121delta += cycles;122}123124/* Verify fragment count matches expected value */125fragments = delta / frag_size;126if (info.blocks < fragments || info.blocks > fragments + 1)127warnx("Pointer block mismatch: ptr=%d blocks=%d delta=%u",128info.ptr, info.blocks, delta);129130/* Update pointer and progress tracking */131*map_pointer = info.ptr;132*map_progress += delta;133return (*map_progress);134}135136/*137* Copy data between ring buffers, handling wraparound.138* The copy starts at 'offset' and copies 'length' bytes.139* If the copy crosses the buffer boundary, it wraps to the beginning.140*/141static void142copy_ring(void *dstv, const void *srcv, int buffer_bytes, int offset,143int length)144{145uint8_t *dst = dstv;146const uint8_t *src = srcv;147int first;148149if (length <= 0)150return;151152/* Calculate bytes to copy before wraparound */153first = buffer_bytes - offset;154if (first > length)155first = length;156157/* Copy first part (up to buffer end or length) */158memcpy(dst + offset, src + offset, first);159160/* Copy remaining part from beginning of buffer if needed */161if (first < length)162memcpy(dst, src, length - first);163}164165int166main(int argc, char *argv[])167{168int ch, bytes;169int frag_size, frame_size, verbose = 0;170int map_pointer = 0;171unsigned step_frames;172int64_t frame_ns, start_ns, next_wakeup_ns;173int64_t read_progress = 0, write_progress = 0;174oss_syncgroup sync_group = { 0, 0, { 0 } };175struct config config_in = {176.device = "/dev/dsp",177.mode = O_RDONLY | O_EXCL | O_NONBLOCK,178.format = AFMT_S32_NE,179.sample_rate = 48000,180.mmap = 1,181};182struct config config_out = {183.device = "/dev/dsp",184.mode = O_WRONLY | O_EXCL | O_NONBLOCK,185.format = AFMT_S32_NE,186.sample_rate = 48000,187.mmap = 1,188};189190while ((ch = getopt(argc, argv, "v")) != -1) {191switch (ch) {192case 'v':193verbose = 1;194break;195}196}197argc -= optind;198argv += optind;199200if (!verbose)201printf("Use -v for verbose mode\n");202203oss_init(&config_in);204oss_init(&config_out);205206/*207* Verify input and output have matching ring-buffer geometry.208* The passthrough loop copies raw bytes at the same offset in both mmap209* buffers, so both devices must expose the same total byte count.210* They must also use the same max_channels because frame_size is211* derived from that value and all mmap pointers/lengths are expected to212* stay aligned to whole frames on both sides. If channels differed, the213* same byte offset could land in the middle of a frame on one device.214*/215if (config_in.buffer_info.bytes != config_out.buffer_info.bytes)216errx(1,217"Input and output configurations have different buffer sizes");218if (config_in.audio_info.max_channels !=219config_out.audio_info.max_channels)220errx(1,221"Input and output configurations have different number of channels");222223bytes = config_in.buffer_info.bytes;224frag_size = config_in.buffer_info.fragsize;225frame_size = config_in.sample_size * config_in.audio_info.max_channels;226if (frag_size != config_out.buffer_info.fragsize)227errx(1,228"Input and output configurations have different fragment sizes");229230/* Calculate timing parameters */231step_frames = frame_stepping(config_in.sample_rate);232frame_ns = 1000000000LL / config_in.sample_rate;233234/* Clear output buffer to prevent noise on startup */235memset(config_out.buf, 0, bytes);236237/* Configure and start sync group */238sync_group.mode = PCM_ENABLE_INPUT;239if (ioctl(config_in.fd, SNDCTL_DSP_SYNCGROUP, &sync_group) < 0)240err(1, "Failed to add input to syncgroup");241sync_group.mode = PCM_ENABLE_OUTPUT;242if (ioctl(config_out.fd, SNDCTL_DSP_SYNCGROUP, &sync_group) < 0)243err(1, "Failed to add output to syncgroup");244if (ioctl(config_in.fd, SNDCTL_DSP_SYNCSTART, &sync_group.id) < 0)245err(1, "Starting sync group failed");246247/* Initialize timing and progress tracking */248start_ns = gettime_ns();249read_progress = update_map_progress(config_in.fd, SNDCTL_DSP_GETIPTR,250&map_pointer, &read_progress, bytes, frag_size, frame_size);251write_progress = read_progress;252next_wakeup_ns = start_ns;253254/*255* Main processing loop:256* 1. Sleep until next scheduled wakeup257* 2. Check how much new audio data is available258* 3. Copy available data from input to output buffer259* 4. Schedule next wakeup260*/261for (;;) {262sleep_until_ns(next_wakeup_ns);263read_progress = update_map_progress(config_in.fd,264SNDCTL_DSP_GETIPTR, &map_pointer, &read_progress, bytes,265frag_size, frame_size);266267/* Copy new audio data if available */268if (read_progress > write_progress) {269int offset = write_progress % bytes;270int length = read_progress - write_progress;271272copy_ring(config_out.buf, config_in.buf, bytes, offset,273length);274write_progress = read_progress;275if (verbose)276printf("copied %d bytes at %d (abs %lld)\n",277length, offset, (long long)write_progress);278}279280/* Schedule next wakeup based on frame timing */281next_wakeup_ns += (int64_t)step_frames * frame_ns;282if (next_wakeup_ns < gettime_ns())283next_wakeup_ns = gettime_ns();284}285286if (munmap(config_in.buf, bytes) != 0)287err(1, "Memory unmap failed");288config_in.buf = NULL;289if (munmap(config_out.buf, bytes) != 0)290err(1, "Memory unmap failed");291config_out.buf = NULL;292close(config_in.fd);293close(config_out.fd);294295return (0);296}297298299