Fri Oct 30 21:44:49 2020 UTC ()
ossaudio(3): Simplify setting rate (try to supply the nearest one possible)


(nia)
diff -r1.58 -r1.59 src/lib/libossaudio/ossaudio.c

cvs diff -r1.58 -r1.59 src/lib/libossaudio/Attic/ossaudio.c (expand / switch to unified diff)

--- src/lib/libossaudio/Attic/ossaudio.c 2020/10/24 14:43:53 1.58
+++ src/lib/libossaudio/Attic/ossaudio.c 2020/10/30 21:44:49 1.59
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: ossaudio.c,v 1.58 2020/10/24 14:43:53 roy Exp $ */ 1/* $NetBSD: ossaudio.c,v 1.59 2020/10/30 21:44:49 nia Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1997, 2020 The NetBSD Foundation, Inc. 4 * Copyright (c) 1997, 2020 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -17,27 +17,27 @@ @@ -17,27 +17,27 @@
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29#include <sys/cdefs.h> 29#include <sys/cdefs.h>
30__RCSID("$NetBSD: ossaudio.c,v 1.58 2020/10/24 14:43:53 roy Exp $"); 30__RCSID("$NetBSD: ossaudio.c,v 1.59 2020/10/30 21:44:49 nia Exp $");
31 31
32/* 32/*
33 * This is an Open Sound System compatibility layer, which provides 33 * This is an Open Sound System compatibility layer, which provides
34 * fairly complete ioctl emulation for OSSv3 and some of OSSv4. 34 * fairly complete ioctl emulation for OSSv3 and some of OSSv4.
35 * 35 *
36 * The canonical OSS specification is available at 36 * The canonical OSS specification is available at
37 * http://manuals.opensound.com/developer/ 37 * http://manuals.opensound.com/developer/
38 *  38 *
39 * This file is similar to sys/compat/ossaudio.c with additional OSSv4 39 * This file is similar to sys/compat/ossaudio.c with additional OSSv4
40 * compatibility. 40 * compatibility.
41 */ 41 */
42 42
43#include <string.h> 43#include <string.h>
@@ -170,56 +170,35 @@ audio_ioctl(int fd, unsigned long com, v @@ -170,56 +170,35 @@ audio_ioctl(int fd, unsigned long com, v
170 break; 170 break;
171 case SNDCTL_DSP_COOKEDMODE: 171 case SNDCTL_DSP_COOKEDMODE:
172 /* 172 /*
173 * NetBSD is always running in "cooked mode" - the kernel 173 * NetBSD is always running in "cooked mode" - the kernel
174 * always performs format conversions. 174 * always performs format conversions.
175 */ 175 */
176 INTARG = 1; 176 INTARG = 1;
177 break; 177 break;
178 case SNDCTL_DSP_POST: 178 case SNDCTL_DSP_POST:
179 /* This call is merely advisory, and may be a nop. */ 179 /* This call is merely advisory, and may be a nop. */
180 break; 180 break;
181 case SNDCTL_DSP_SPEED: 181 case SNDCTL_DSP_SPEED:
182 AUDIO_INITINFO(&tmpinfo); 182 AUDIO_INITINFO(&tmpinfo);
 183 /* Conform to kernel limits. */
 184 if (INTARG < 1000)
 185 INTARG = 1000;
 186 if (INTARG > 192000)
 187 INTARG = 192000;
183 tmpinfo.play.sample_rate = 188 tmpinfo.play.sample_rate =
184 tmpinfo.record.sample_rate = INTARG; 189 tmpinfo.record.sample_rate = INTARG;
185 /* 
186 * The default NetBSD behavior if an unsupported sample rate 
187 * is set is to return an error code and keep the rate at the 
188 * default of 8000 Hz. 
189 *  
190 * However, OSS specifies that a sample rate supported by the 
191 * hardware is returned if the exact rate could not be set. 
192 *  
193 * So, if the chosen sample rate is invalid, set and return 
194 * the current hardware rate. 
195 */ 
196 if (ioctl(fd, AUDIO_SETINFO, &tmpinfo) < 0) { 190 if (ioctl(fd, AUDIO_SETINFO, &tmpinfo) < 0) {
197 /* Don't care that SETINFO failed the first time... */ 191 return retval;
198 errno = 0; 
199 retval = ioctl(fd, AUDIO_GETFORMAT, &hwfmt); 
200 if (retval < 0) 
201 return retval; 
202 retval = ioctl(fd, AUDIO_GETINFO, &tmpinfo); 
203 if (retval < 0) 
204 return retval; 
205 tmpinfo.play.sample_rate = 
206 tmpinfo.record.sample_rate = 
207 (tmpinfo.mode == AUMODE_RECORD) ? 
208 hwfmt.record.sample_rate : 
209 hwfmt.play.sample_rate; 
210 retval = ioctl(fd, AUDIO_SETINFO, &tmpinfo); 
211 if (retval < 0) 
212 return retval; 
213 } 192 }
214 /* FALLTHRU */ 193 /* FALLTHRU */
215 case SOUND_PCM_READ_RATE: 194 case SOUND_PCM_READ_RATE:
216 retval = ioctl(fd, AUDIO_GETBUFINFO, &tmpinfo); 195 retval = ioctl(fd, AUDIO_GETBUFINFO, &tmpinfo);
217 if (retval < 0) 196 if (retval < 0)
218 return retval; 197 return retval;
219 INTARG = GETPRINFO(&tmpinfo, sample_rate); 198 INTARG = GETPRINFO(&tmpinfo, sample_rate);
220 break; 199 break;
221 case SNDCTL_DSP_STEREO: 200 case SNDCTL_DSP_STEREO:
222 AUDIO_INITINFO(&tmpinfo); 201 AUDIO_INITINFO(&tmpinfo);
223 tmpinfo.play.channels = 202 tmpinfo.play.channels =
224 tmpinfo.record.channels = INTARG ? 2 : 1; 203 tmpinfo.record.channels = INTARG ? 2 : 1;
225 (void) ioctl(fd, AUDIO_SETINFO, &tmpinfo); 204 (void) ioctl(fd, AUDIO_SETINFO, &tmpinfo);