Module Name:    src
Committed By:   mrg
Date:           Mon Mar  4 06:29:35 UTC 2024

Modified Files:
        src/usr.bin/audio/play: audioplay.1 play.c

Log Message:
audioplay: add -n flag that doesn't write audio data.

this will be used in an upcoming testsuite for the wav parser.


To generate a diff of this commit:
cvs rdiff -u -r1.33 -r1.34 src/usr.bin/audio/play/audioplay.1
cvs rdiff -u -r1.63 -r1.64 src/usr.bin/audio/play/play.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/audio/play/audioplay.1
diff -u src/usr.bin/audio/play/audioplay.1:1.33 src/usr.bin/audio/play/audioplay.1:1.34
--- src/usr.bin/audio/play/audioplay.1:1.33	Sun Feb  4 05:43:07 2024
+++ src/usr.bin/audio/play/audioplay.1	Mon Mar  4 06:29:35 2024
@@ -1,6 +1,6 @@
-.\"	$NetBSD: audioplay.1,v 1.33 2024/02/04 05:43:07 mrg Exp $
+.\"	$NetBSD: audioplay.1,v 1.34 2024/03/04 06:29:35 mrg Exp $
 .\"
-.\" Copyright (c) 1998, 1999, 2002, 2010, 2019 Matthew R. Green
+.\" Copyright (c) 1998, 1999, 2002, 2010, 2019, 2024 Matthew R. Green
 .\" All rights reserved.
 .\"
 .\" Redistribution and use in source and binary forms, with or without
@@ -24,7 +24,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd April 10, 2020
+.Dd March 3, 2024
 .Dt AUDIOPLAY 1
 .Os
 .Sh NAME
@@ -32,7 +32,7 @@
 .Nd play audio files
 .Sh SYNOPSIS
 .Nm
-.Op Fl hiqV
+.Op Fl hinqV
 .Op Fl B Ar buffersize
 .Op Fl b Ar balance
 .Op Fl d Ar device
@@ -118,6 +118,8 @@ sample rate.
 Print a help message.
 .It Fl i
 If the audio device cannot be opened, exit now rather than wait for it.
+.It Fl n
+Do not write audio data, only parse files for sanity.
 .It Fl P
 When combined with the
 .Fl f

Index: src/usr.bin/audio/play/play.c
diff -u src/usr.bin/audio/play/play.c:1.63 src/usr.bin/audio/play/play.c:1.64
--- src/usr.bin/audio/play/play.c:1.63	Sat Apr 15 16:54:39 2023
+++ src/usr.bin/audio/play/play.c	Mon Mar  4 06:29:35 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: play.c,v 1.63 2023/04/15 16:54:39 mlelstv Exp $	*/
+/*	$NetBSD: play.c,v 1.64 2024/03/04 06:29:35 mrg Exp $	*/
 
 /*
  * Copyright (c) 1999, 2000, 2001, 2002, 2010, 2015, 2019, 2021 Matthew R. Green
@@ -28,7 +28,7 @@
 #include <sys/cdefs.h>
 
 #ifndef lint
-__RCSID("$NetBSD: play.c,v 1.63 2023/04/15 16:54:39 mlelstv Exp $");
+__RCSID("$NetBSD: play.c,v 1.64 2024/03/04 06:29:35 mrg Exp $");
 #endif
 
 #include <sys/param.h>
@@ -65,6 +65,7 @@ static int	volume;
 static int	balance;
 static int	port;
 static int	fflag;
+static int	nflag;
 static int	qflag;
 int	verbose;
 static int	sample_rate;
@@ -75,7 +76,7 @@ static int	channels;
 
 static char	const *play_errstring = NULL;
 static size_t	bufsize;
-static int	audiofd;
+static int	audiofd = -1;
 static int	exitstatus = EXIT_SUCCESS;
 
 int
@@ -87,7 +88,7 @@ main(int argc, char *argv[])
 	const char *defdevice = _PATH_SOUND;
 	const char *device = NULL;
 
-	while ((ch = getopt(argc, argv, "b:B:C:c:d:e:fhip:P:qs:Vv:")) != -1) {
+	while ((ch = getopt(argc, argv, "b:B:C:c:d:e:fhinp:P:qs:Vv:")) != -1) {
 		switch (ch) {
 		case 'b':
 			decode_int(optarg, &balance);
@@ -118,6 +119,9 @@ main(int argc, char *argv[])
 		case 'i':
 			iflag++;
 			break;
+		case 'n':
+			nflag++;
+			break;
 		case 'q':
 			qflag++;
 			break;
@@ -173,22 +177,22 @@ main(int argc, char *argv[])
 	    (device = getenv("AUDIODEV")) == NULL) /* Sun compatibility */
 		device = defdevice;
 
-	audiofd = open(device, O_WRONLY);
-	if (audiofd < 0 && device == defdevice) {
-		device = _PATH_SOUND0;
+	if (!nflag) {
 		audiofd = open(device, O_WRONLY);
-	}
-
-	if (audiofd < 0)
-		err(1, "failed to open %s", device);
+		if (audiofd < 0 && device == defdevice) {
+			device = _PATH_SOUND0;
+			audiofd = open(device, O_WRONLY);
+		}
+		if (audiofd < 0)
+			err(1, "failed to open %s", device);
 
-	if (ioctl(audiofd, AUDIO_GETINFO, &info) < 0)
-		err(1, "failed to get audio info");
-	if (bufsize == 0) {
-		bufsize = info.play.buffer_size;
-		if (bufsize < 32 * 1024)
-			bufsize = 32 * 1024;
+		if (ioctl(audiofd, AUDIO_GETINFO, &info) < 0)
+			err(1, "failed to get audio info");
+		if (bufsize == 0)
+			bufsize = info.play.buffer_size;
 	}
+	if (bufsize == 0)
+		bufsize = 32 * 1024;
 
 	signal(SIGINT, cleanup);
 	signal(SIGTERM, cleanup);
@@ -208,9 +212,12 @@ static void
 cleanup(int signo)
 {
 
-	(void)ioctl(audiofd, AUDIO_FLUSH, NULL);
-	(void)ioctl(audiofd, AUDIO_SETINFO, &info);
-	close(audiofd);
+	if (audiofd != -1) {
+		(void)ioctl(audiofd, AUDIO_FLUSH, NULL);
+		(void)ioctl(audiofd, AUDIO_SETINFO, &info);
+		close(audiofd);
+		audiofd = -1;
+	}
 	if (signo != 0) {
 		(void)raise_default_signal(signo);
 	}
@@ -283,6 +290,9 @@ audio_write(int fd, void *buf, size_t le
 	static void *convert_buffer;
 	static size_t convert_buffer_size;
 
+	if (nflag)
+		return len;
+
 	if (conv == NULL)
 		return write(fd, buf, len);
 
@@ -317,8 +327,7 @@ play(char *file)
 
 	fd = open(file, O_RDONLY);
 	if (fd < 0) {
-		if (!qflag)
-			warn("could not open %s", file);
+		warn("could not open %s", file);
 		exitstatus = EXIT_FAILURE;
 		return;
 	}
@@ -359,6 +368,8 @@ play(char *file)
 		else
 			errx(1, "unknown audio file: %s", file);
 	}
+	if (verbose)
+		printf("header length: %zd\n", hdrlen);
 
 	filesize -= hdrlen;
 	addr = (char *)addr + hdrlen;
@@ -383,7 +394,7 @@ play(char *file)
 	if ((off_t)nw != datasize)
 		errx(1, "final write failed");
 
-	if (ioctl(audiofd, AUDIO_DRAIN) < 0 && !qflag)
+	if (!nflag && ioctl(audiofd, AUDIO_DRAIN) < 0 && !qflag)
 		warn("audio drain ioctl failed");
 	if (munmap(oaddr, sizet_filesize) < 0)
 		err(1, "munmap failed");
@@ -446,7 +457,7 @@ play_fd(const char *file, int fd)
 			break;
 	}
 	/* something to think about: no message given for dataout < datasize */
-	if (ioctl(audiofd, AUDIO_DRAIN) < 0 && !qflag)
+	if (!nflag && ioctl(audiofd, AUDIO_DRAIN) < 0 && !qflag)
 		warn("audio drain ioctl failed");
 	return;
 read_error:
@@ -571,7 +582,7 @@ set_audio_mode:
 	}
 #endif /* __vax__ */
 
-	if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
+	if (!nflag && ioctl(fd, AUDIO_SETINFO, &info) < 0)
 		err(1, "failed to set audio info");
 
 	return (hdr_len);
@@ -581,7 +592,7 @@ static void
 usage(void)
 {
 
-	fprintf(stderr, "Usage: %s [-hiqV] [options] files\n", getprogname());
+	fprintf(stderr, "Usage: %s [-hinqV] [options] files\n", getprogname());
 	fprintf(stderr, "Options:\n\t"
 	    "-B buffer size\n\t"
 	    "-b balance (0-63)\n\t"

Reply via email to