Page MenuHomeFreeBSD

D53749.diff
No OneTemporary

D53749.diff

diff --git a/share/examples/sound/mmap-usleep.c b/share/examples/sound/mmap-usleep.c
new file mode 100644
--- /dev/null
+++ b/share/examples/sound/mmap-usleep.c
@@ -0,0 +1,121 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 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 "oss.h"
+
+int
+main(int argc, char *argv[])
+{
+ int ch, rc, bytes, position;
+ int verbose = 0, oldptr = 0;
+ count_info ci = {0, 0, -1};
+ oss_syncgroup sync_group = {0, 0, {0}};
+ struct config config_in = {
+ .device = "/dev/dsp",
+ .mode = O_RDONLY | O_EXCL | O_NONBLOCK,
+ .format = AFMT_S32_NE,
+ .sample_rate = 48000,
+ .mmap = 1,
+ };
+ struct config config_out = {
+ .device = "/dev/dsp",
+ .mode = O_WRONLY | O_EXCL | O_NONBLOCK,
+ .format = AFMT_S32_NE,
+ .sample_rate = 48000,
+ .mmap = 1,
+ };
+
+ while ((ch = getopt(argc, argv, "v")) != -1) {
+ switch (ch) {
+ case 'v':
+ verbose = 1;
+ break;
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
+ if (!verbose)
+ printf("Use -v for verbose mode\n");
+ oss_init(&config_in);
+ oss_init(&config_out);
+ if (config_in.buffer_info.bytes != config_out.buffer_info.bytes)
+ errx(1, "Input and output configurations have different buffer sizes");
+ bytes = config_in.buffer_info.bytes;
+ if (config_in.audio_info.max_channels != config_out.audio_info.max_channels)
+ errx(1, "Input and output configurations have different number of channels");
+
+ /* Configure and start sync group */
+ sync_group.mode = PCM_ENABLE_INPUT;
+ if (ioctl(config_in.fd, SNDCTL_DSP_SYNCGROUP, &sync_group) < 0)
+ err(1, "Failed to add input to syncgroup");
+ sync_group.mode = PCM_ENABLE_OUTPUT;
+ if (ioctl(config_out.fd, SNDCTL_DSP_SYNCGROUP, &sync_group) < 0)
+ err(1, "Failed to add output to syncgroup");
+ if (ioctl(config_in.fd, SNDCTL_DSP_SYNCSTART, &sync_group.id) < 0)
+ err(1, "Starting sync group failed");
+
+ while (ci.ptr != 0) {
+ if (ioctl(config_out.fd, SNDCTL_DSP_GETOPTR, &ci) < 0)
+ err(1, "Failed to get output pointer");
+ }
+ /* Main loop */
+ for (;;) {
+ if (ioctl(config_out.fd, SNDCTL_DSP_GETOPTR, &ci) < 0)
+ err(1, "Failed to get output pointer");
+ if (verbose) {
+ if (oldptr > ci.ptr)
+ printf("\nO: ");
+ printf("%d, ", ci.ptr);
+ }
+ if (oldptr < ci.ptr)
+ memcpy(config_out.buf + oldptr,
+ config_in.buf + oldptr,
+ ci.ptr - oldptr);
+ else if (oldptr > ci.ptr) {
+ memcpy(config_out.buf + oldptr,
+ config_in.buf + oldptr,
+ bytes - oldptr);
+ if (ci.ptr > 0)
+ memcpy(config_out.buf, config_in.buf, ci.ptr);
+ }
+ oldptr = ci.ptr;
+ usleep(400);
+ }
+
+ /* Cleanup */
+ if (munmap(config_in.buf, bytes) != 0)
+ err(1, "Memory unmap failed");
+ config_in.buf = NULL;
+ if (munmap(config_out.buf, bytes) != 0)
+ err(1, "Memory unmap failed");
+ config_out.buf = NULL;
+ close(config_in.fd);
+ close(config_out.fd);
+
+ return (0);
+}

File Metadata

Mime Type
text/plain
Expires
Thu, Mar 5, 9:43 AM (10 h, 32 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
27046185
Default Alt Text
D53749.diff (4 KB)

Event Timeline