2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2021 Goran Mekić
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/soundcard.h>
38 #define SAMPLE_SIZE 16
41 /* Format can be unsigned, in which case replace S with U */
43 typedef int32_t sample_t;
44 int format = AFMT_S32_NE; /* Signed 32bit native endian format */
45 #elif SAMPLE_SIZE == 16
46 typedef int16_t sample_t;
47 int format = AFMT_S16_NE; /* Signed 16bit native endian format */
48 #elif SAMPLE_SIZE == 8
49 typedef int8_t sample_t;
50 int format = AFMT_S8_NE; /* Signed 8bit native endian format */
52 #error Unsupported sample format!
53 typedef int32_t sample_t;
54 int format = AFMT_S32_NE; /* Not a real value, just silencing
61 * Minimal configuration for OSS
62 * For real world applications, this structure will probably contain many
65 typedef struct config {
76 oss_audioinfo audio_info;
77 audio_buf_info buffer_info;
82 * Error state is indicated by value=-1 in which case application exits
86 check_error(const int value, const char *message)
89 fprintf(stderr, "OSS error: %s %s\n", message, strerror(errno));
96 /* Calculate frag by giving it minimal size of buffer */
102 while ((1 << frag) < x) {
110 * Split input buffer into channels. Input buffer is in interleaved format
111 * which means if we have 2 channels (L and R), this is what the buffer of
112 * 8 samples would contain: L,R,L,R,L,R,L,R. The result are two channels
113 * containing: L,L,L,L and R,R,R,R.
116 oss_split(config_t *config, sample_t *input, sample_t *output)
121 for (int i = 0; i < config->sample_count; ++i) {
122 channel = i % config->channels;
123 index = i / config->channels;
124 output[channel * index] = input[i];
130 * Convert channels into interleaved format and place it in output
134 oss_merge(config_t *config, sample_t *input, sample_t *output)
136 for (int channel = 0; channel < config->channels; ++channel) {
137 for (int index = 0; index < config->chsamples; ++index) {
138 output[index * config->channels + channel] = input[channel * index];
144 oss_init(config_t *config)
149 /* Open the device for read and write */
150 config->fd = open(config->device, O_RDWR);
151 check_error(config->fd, "open");
153 /* Get device information */
154 config->audio_info.dev = -1;
155 error = ioctl(config->fd, SNDCTL_ENGINEINFO, &(config->audio_info));
156 check_error(error, "SNDCTL_ENGINEINFO");
157 printf("min_channels: %d\n", config->audio_info.min_channels);
158 printf("max_channels: %d\n", config->audio_info.max_channels);
159 printf("latency: %d\n", config->audio_info.latency);
160 printf("handle: %s\n", config->audio_info.handle);
161 if (config->audio_info.min_rate > config->sample_rate || config->sample_rate > config->audio_info.max_rate) {
162 fprintf(stderr, "%s doesn't support chosen ", config->device);
163 fprintf(stderr, "samplerate of %dHz!\n", config->sample_rate);
166 if (config->channels < 1) {
167 config->channels = config->audio_info.max_channels;
171 * If device is going to be used in mmap mode, disable all format
172 * conversions. Official OSS documentation states error code should not be
173 * checked. http://manuals.opensound.com/developer/mmap_test.c.html#LOC10
177 ioctl(config->fd, SNDCTL_DSP_COOKEDMODE, &tmp);
181 * Set number of channels. If number of channels is chosen to the value
182 * near the one wanted, save it in config
184 tmp = config->channels;
185 error = ioctl(config->fd, SNDCTL_DSP_CHANNELS, &tmp);
186 check_error(error, "SNDCTL_DSP_CHANNELS");
187 if (tmp != config->channels) { /* or check if tmp is close enough? */
188 fprintf(stderr, "%s doesn't support chosen ", config->device);
189 fprintf(stderr, "channel count of %d", config->channels);
190 fprintf(stderr, ", set to %d!\n", tmp);
192 config->channels = tmp;
194 /* Set format, or bit size: 8, 16, 24 or 32 bit sample */
195 tmp = config->format;
196 error = ioctl(config->fd, SNDCTL_DSP_SETFMT, &tmp);
197 check_error(error, "SNDCTL_DSP_SETFMT");
198 if (tmp != config->format) {
199 fprintf(stderr, "%s doesn't support chosen sample format!\n", config->device);
203 /* Most common values for samplerate (in kHz): 44.1, 48, 88.2, 96 */
204 tmp = config->sample_rate;
205 error = ioctl(config->fd, SNDCTL_DSP_SPEED, &tmp);
206 check_error(error, "SNDCTL_DSP_SPEED");
208 /* Get and check device capabilities */
209 error = ioctl(config->fd, SNDCTL_DSP_GETCAPS, &(config->audio_info.caps));
210 check_error(error, "SNDCTL_DSP_GETCAPS");
211 if (!(config->audio_info.caps & PCM_CAP_DUPLEX)) {
212 fprintf(stderr, "Device doesn't support full duplex!\n");
216 if (!(config->audio_info.caps & PCM_CAP_TRIGGER)) {
217 fprintf(stderr, "Device doesn't support triggering!\n");
220 if (!(config->audio_info.caps & PCM_CAP_MMAP)) {
221 fprintf(stderr, "Device doesn't support mmap mode!\n");
227 * If desired frag is smaller than minimum, based on number of channels
228 * and format (size in bits: 8, 16, 24, 32), set that as frag. Buffer size
229 * is 2^frag, but the real size of the buffer will be read when the
230 * configuration of the device is successfull
232 int min_frag = size2frag(config->sample_size * config->channels);
234 if (config->frag < min_frag) {
235 config->frag = min_frag;
239 * Allocate buffer in fragments. Total buffer will be split in number
240 * of fragments (2 by default)
242 if (config->buffer_info.fragments < 0) {
243 config->buffer_info.fragments = 2;
245 tmp = ((config->buffer_info.fragments) << 16) | config->frag;
246 error = ioctl(config->fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
247 check_error(error, "SNDCTL_DSP_SETFRAGMENT");
249 /* When all is set and ready to go, get the size of buffer */
250 error = ioctl(config->fd, SNDCTL_DSP_GETOSPACE, &(config->buffer_info));
251 check_error(error, "SNDCTL_DSP_GETOSPACE");
252 if (config->buffer_info.bytes < 1) {
255 "OSS buffer error: buffer size can not be %d\n",
256 config->buffer_info.bytes
260 config->sample_count = config->buffer_info.bytes / config->sample_size;
261 config->chsamples = config->sample_count / config->channels;