Index | Thread | Search

From:
Alexandre Ratchov <alex@caoua.org>
Subject:
sndiod: Make the device sample rate and buffer sizes global
To:
tech@openbsd.org
Date:
Wed, 20 May 2026 18:17:50 +0200

Download raw body.

Thread
  • Alexandre Ratchov:

    sndiod: Make the device sample rate and buffer sizes global

To switch between sound cards (enabled by default since a couple of
years) they must use the same rate, block size and buffer size. So,
beside being useless, per-device parameters (i.e. two or more -brz
options) make no sense.

Furthermore, note that a given set of parameters may make sense for
one sound card but not for another. With USB the same device number
may correspond to different sound cards, so per-device-number
parameters make no sense these days.

Here's a minimal diff to make the rate and buffer size global.

OK?

Index: dev.c
===================================================================
RCS file: /cvs/src/usr.bin/sndiod/dev.c,v
diff -u -p -r1.133 dev.c
--- dev.c	20 May 2026 13:02:04 -0000	1.133
+++ dev.c	20 May 2026 13:29:52 -0000
@@ -76,6 +76,11 @@ struct ctl *ctl_list = NULL;
 struct dev *dev_list = NULL;
 unsigned int dev_sndnum = 0;
 
+/*
+ * Preferred sample rate, buffer size, and block size
+ */
+int dev_rate, dev_bufsz, dev_round;
+
 struct ctlslot ctlslot_array[DEV_NCTLSLOT];
 struct slot slot_array[DEV_NSLOT];
 
@@ -746,8 +751,7 @@ dev_master(struct dev *d, unsigned int m
  * Create a sndio device
  */
 struct dev *
-dev_new(char *path, struct aparams *par, unsigned int bufsz, unsigned int round,
-    unsigned int rate, unsigned int hold, unsigned int autovol)
+dev_new(char *path, struct aparams *par, unsigned int hold, unsigned int autovol)
 {
 	struct dev *d, **pd;
 
@@ -761,9 +765,6 @@ dev_new(char *path, struct aparams *par,
 
 	d->reqpar = *par;
 	d->reqpchan = d->reqrchan = 0;
-	d->reqbufsz = bufsz;
-	d->reqround = round;
-	d->reqrate = rate;
 	d->hold = hold;
 	d->autovol = autovol;
 	d->refcnt = 0;
@@ -859,9 +860,9 @@ int
 dev_open(struct dev *d)
 {
 	d->mode = MODE_AUDIOMASK;
-	d->round = d->reqround;
-	d->bufsz = d->reqbufsz;
-	d->rate = d->reqrate;
+	d->round = dev_round;
+	d->bufsz = dev_bufsz;
+	d->rate = dev_rate;
 	d->pchan = d->reqpchan;
 	d->rchan = d->reqrchan;
 	d->par = d->reqpar;
Index: dev.h
===================================================================
RCS file: /cvs/src/usr.bin/sndiod/dev.h,v
diff -u -p -r1.54 dev.h
--- dev.h	20 May 2026 13:02:04 -0000	1.54
+++ dev.h	20 May 2026 13:29:52 -0000
@@ -247,9 +247,6 @@ struct dev {
 	 */
 	struct aparams reqpar;			/* parameters */
 	int reqpchan, reqrchan;			/* play & rec chans */
-	unsigned int reqbufsz;			/* buffer size */
-	unsigned int reqround;			/* block size */
-	unsigned int reqrate;			/* sample rate */
 	unsigned int hold;			/* hold the device open ? */
 	unsigned int autovol;			/* auto adjust playvol ? */
 	unsigned int refcnt;			/* number of openers */
@@ -278,14 +275,14 @@ extern struct ctl *ctl_list;
 extern struct slot slot_array[DEV_NSLOT];
 extern struct ctlslot ctlslot_array[DEV_NCTLSLOT];
 extern struct mtc mtc_array[1];
+extern int dev_rate, dev_bufsz, dev_round;
 
 size_t chans_fmt(char *, size_t, int, int, int, int, int);
 int dev_open(struct dev *);
 void dev_close(struct dev *);
 void dev_abort(struct dev *);
 void dev_migrate(struct dev *);
-struct dev *dev_new(char *, struct aparams *, unsigned int,
-    unsigned int, unsigned int, unsigned int, unsigned int);
+struct dev *dev_new(char *, struct aparams *, unsigned int, unsigned int);
 struct dev *dev_bynum(int);
 void dev_del(struct dev *);
 void dev_adjpar(struct dev *, int, int);
Index: sndiod.8
===================================================================
RCS file: /cvs/src/usr.bin/sndiod/sndiod.8,v
diff -u -p -r1.20 sndiod.8
--- sndiod.8	15 Mar 2026 14:24:43 -0000	1.20
+++ sndiod.8	20 May 2026 13:29:53 -0000
@@ -366,7 +366,7 @@ if the buffer size is set.
 .Pp
 On the command line,
 per-device parameters
-.Pq Fl aberwz
+.Pq Fl aew
 must precede the device definition
 .Pq Fl f ,
 and per-sub-device parameters
Index: sndiod.c
===================================================================
RCS file: /cvs/src/usr.bin/sndiod/sndiod.c,v
diff -u -p -r1.54 sndiod.c
--- sndiod.c	20 May 2026 13:02:04 -0000	1.54
+++ sndiod.c	20 May 2026 13:29:53 -0000
@@ -103,7 +103,7 @@ unsigned int opt_mode(void);
 void getbasepath(char *);
 void setsig(void);
 void unsetsig(void);
-struct dev *mkdev(char *, struct aparams *, int, int, int, int, int);
+struct dev *mkdev(char *, struct aparams *, int, int);
 struct port *mkport(char *, int);
 struct opt *mkopt(char *, struct dev *, struct opt_alt *,
     int, int, int, int, int, int, int, int);
@@ -364,8 +364,7 @@ unsetsig(void)
 }
 
 struct dev *
-mkdev(char *path, struct aparams *par,
-    int bufsz, int round, int rate, int hold, int autovol)
+mkdev(char *path, struct aparams *par, int hold, int autovol)
 {
 	struct dev *d;
 
@@ -373,14 +372,7 @@ mkdev(char *path, struct aparams *par,
 		if (strcmp(d->path, path) == 0)
 			return d;
 	}
-	if (!bufsz && !round) {
-		round = DEFAULT_ROUND;
-		bufsz = DEFAULT_BUFSZ;
-	} else if (!bufsz) {
-		bufsz = round * 2;
-	} else if (!round)
-		round = bufsz / 2;
-	d = dev_new(path, par, bufsz, round, rate, hold, autovol);
+	d = dev_new(path, par, hold, autovol);
 	if (d == NULL)
 		exit(1);
 	return d;
@@ -601,8 +593,7 @@ main(int argc, char **argv)
 		case 's':
 			if (d == NULL) {
 				for (i = 0; default_devs[i] != NULL; i++) {
-					mkdev(default_devs[i], &par,
-					    bufsz, round, rate, 0, autovol);
+					mkdev(default_devs[i], &par, 0, autovol);
 				}
 				d = dev_list;
 			}
@@ -641,8 +632,7 @@ main(int argc, char **argv)
 				errx(1, "%s: block size is %s", optarg, str);
 			break;
 		case 'f':
-			d = mkdev(optarg, &par, bufsz, round,
-			    rate, hold, autovol);
+			d = mkdev(optarg, &par, hold, autovol);
 			while ((a = alt_list) != NULL) {
 				alt_list = a->next;
 				xfree(a);
@@ -652,8 +642,7 @@ main(int argc, char **argv)
 			if (d == NULL)
 				errx(1, "-F %s: no devices defined", optarg);
 			a = xmalloc(sizeof(struct opt_alt));
-			a->dev = mkdev(optarg, &par, bufsz, round,
-			    rate, hold, autovol);
+			a->dev = mkdev(optarg, &par, hold, autovol);
 			for (pa = &alt_list; *pa != NULL; pa = &(*pa)->next)
 				;
 			a->next = NULL;
@@ -670,14 +659,26 @@ main(int argc, char **argv)
 		fputs(usagestr, stderr);
 		return 1;
 	}
+
+	if (!bufsz && !round) {
+		round = DEFAULT_ROUND;
+		bufsz = DEFAULT_BUFSZ;
+	} else if (!bufsz) {
+		bufsz = round * 2;
+	} else if (!round) {
+		round = bufsz / 2;
+	}
+	dev_rate = rate;
+	dev_round = round;
+	dev_bufsz = bufsz;
+
 	if (port_list == NULL) {
 		for (i = 0; default_ports[i] != NULL; i++)
 			mkport(default_ports[i], 0);
 	}
 	if (dev_list == NULL) {
 		for (i = 0; default_devs[i] != NULL; i++) {
-			mkdev(default_devs[i], &par,
-			    bufsz, round, rate, 0, autovol);
+			mkdev(default_devs[i], &par, 0, autovol);
 		}
 	}