xref: /src/share/examples/sound/simple.c (revision ebe7b241662be3941e462be9f228fcad1b67073a)
1 /*
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2024 The FreeBSD Foundation
5  * Copyright (c) 2025 Goran Mekić
6  *
7  * Portions of this software were developed by Christos Margiolis
8  * <christos@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/soundcard.h>
33 #include "oss.h"
34 
35 /*
36  * Split input buffer into channels. The input buffer is in interleaved format,
37  * which means if we have 2 channels (L and R), this is what the buffer of 8
38  * samples would contain: L,R,L,R,L,R,L,R. The result of this function is a
39  * buffer containing: L,L,L,L,R,R,R,R.
40  */
41 static void
to_channels(struct config * config,void * output)42 to_channels(struct config *config, void *output)
43 {
44 	uint8_t *in = config->buf;
45 	uint8_t *out = output;
46 	int i, channel, index, offset, byte;
47 
48 	/* Iterate over bytes in the input buffer */
49 	for (byte = 0; byte < config->buffer_info.bytes;
50 	    byte += config->sample_size) {
51 		/*
52 		 * Get index of a sample in the input buffer measured in
53 		 * samples
54 		 */
55 		i = byte / config->sample_size;
56 
57 		/* Get which channel is being processed */
58 		channel = i % config->audio_info.max_channels;
59 
60 		/* Get offset of the sample inside a single channel */
61 		offset = i / config->audio_info.max_channels;
62 
63 		/* Get index of a sample in the output buffer */
64 		index = (channel * config->chsamples + offset) *
65 		    config->sample_size;
66 
67 		/* Copy singe sample from input to output */
68 		memcpy(out+index, in+byte, config->sample_size);
69 	}
70 }
71 
72 /*
73  * Convert channels into interleaved format and put into output buffer
74  */
75 static void
to_interleaved(struct config * config,void * input)76 to_interleaved(struct config *config, void *input)
77 {
78 	uint8_t *out = config->buf;
79 	uint8_t *in = input;
80 	int i, index, offset, channel, byte;
81 
82 	/* Iterate over bytes in the input buffer */
83 	for (byte = 0; byte < config->buffer_info.bytes;
84 	    byte += config->sample_size) {
85 		/*
86 		 * Get index of a sample in the input buffer measured in
87 		 * samples
88 		 */
89 		index = byte / config->sample_size;
90 
91 		/* Get which channel is being processed */
92 		channel = index / config->chsamples;
93 
94 		/* Get offset of the sample inside a single channel */
95 		offset = index % config->chsamples;
96 
97 		/* Get index of a sample in the output buffer */
98 		i = (config->audio_info.max_channels * offset + channel) *
99 		    config->sample_size;
100 
101 		/* Copy singe sample from input to output */
102 		memcpy(out+i, in+byte, config->sample_size);
103 	}
104 }
105 
106 int
main(int argc,char * argv[])107 main(int argc, char *argv[])
108 {
109 	struct config config = {
110 		.device = "/dev/dsp",
111 		.mode = O_RDWR,
112 		.format = AFMT_S32_NE,
113 		.sample_rate = 48000,
114 	};
115 	int32_t *channels;
116 	int rc, bytes;
117 
118 	oss_init(&config);
119 	if (config.format != AFMT_S32_NE)
120 		errx(1, "Device doesn't support signed 32bit samples. "
121 			"Check with 'sndctl' if it can be configured for 's32le' format.");
122 	bytes = config.buffer_info.bytes;
123 	channels = malloc(bytes);
124 
125 	for (;;) {
126 		if ((rc = read(config.fd, config.buf, bytes)) < bytes) {
127 			warn("Requested %d bytes, but read %d!\n", bytes, rc);
128 			break;
129 		}
130 		/*
131 		 * Strictly speaking, we could omit "channels" and operate only
132 		 * using config->buf, but this example tries to show the real
133 		 * world application usage. The problem is that the buffer is
134 		 * in interleaved format, and if you'd like to do any
135 		 * processing and/or mixing, it is easier to do that if samples
136 		 * are grouped per channel.
137 		 */
138 		to_channels(&config, channels);
139 		to_interleaved(&config, channels);
140 		if ((rc = write(config.fd, config.buf, bytes)) < bytes) {
141 			warn("Requested %d bytes, but wrote %d!\n", bytes, rc);
142 			break;
143 		}
144 	}
145 
146 	free(channels);
147 	free(config.buf);
148 	close(config.fd);
149 
150 	return (0);
151 }
152