Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/audio Add support for recording 24bit wav files.



details:   https://anonhg.NetBSD.org/src/rev/f60fb37b64ea
branches:  trunk
changeset: 374260:f60fb37b64ea
user:      mlelstv <mlelstv%NetBSD.org@localhost>
date:      Sat Apr 15 12:39:44 2023 +0000

description:
Add support for recording 24bit wav files.

diffstat:

 usr.bin/audio/common/auconv.h |  58 +++++++++++++++++++++++++++++++-
 usr.bin/audio/common/wav.c    |   6 ++-
 usr.bin/audio/play/play.c     |  79 +++++++++++++++++++++++++++++++++++++++---
 usr.bin/audio/record/record.c |  14 +++++-
 4 files changed, 145 insertions(+), 12 deletions(-)

diffs (truncated from 336 to 300 lines):

diff -r 2279bf9dee4d -r f60fb37b64ea usr.bin/audio/common/auconv.h
--- a/usr.bin/audio/common/auconv.h     Sat Apr 15 12:29:43 2023 +0000
+++ b/usr.bin/audio/common/auconv.h     Sat Apr 15 12:39:44 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: auconv.h,v 1.5 2008/04/28 20:24:12 martin Exp $        */
+/*     $NetBSD: auconv.h,v 1.6 2023/04/15 12:39:44 mlelstv Exp $       */
 
 /*-
  * Copyright (c) 1997 The NetBSD Foundation, Inc.
@@ -78,6 +78,24 @@ change_sign16_be(u_char *p, int cc)
 }
 
 static inline void
+change_sign24_le(u_char *p, int cc)
+{
+       while ((cc -= 3) >= 0) {
+               p[2] ^= 0x80;
+               p += 3;
+       }
+}
+
+static inline void
+change_sign24_be(u_char *p, int cc)
+{
+       while ((cc -= 3) >= 0) {
+               p[0] ^= 0x80;
+               p += 3;
+       }
+}
+
+static inline void
 change_sign32_le(u_char *p, int cc)
 {
        while ((cc -= 4) >= 0) {
@@ -163,6 +181,44 @@ change_sign16_swap_bytes_be(u_char *p, i
 }
 
 static inline void
+swap_bytes_change_sign24_le(u_char *p, int cc)
+{
+       u_char t;
+
+       while ((cc -= 3) >= 0) {
+               t = p[2];
+               p[2] = p[0] ^ 0x80;
+               p[0] = t;
+               p += 3;
+       }
+}
+
+static inline void
+swap_bytes_change_sign24_be(u_char *p, int cc)
+{
+       u_char t;
+
+       while ((cc -= 3) >= 0) {
+               t = p[0];
+               p[0] = p[2] ^ 0x80;
+               p[2] = t;
+               p += 3;
+       }
+}
+
+static inline void
+change_sign24_swap_bytes_le(u_char *p, int cc)
+{
+       swap_bytes_change_sign24_be(p, cc);
+}
+
+static inline void
+change_sign24_swap_bytes_be(u_char *p, int cc)
+{
+       swap_bytes_change_sign24_le(p, cc);
+}
+
+static inline void
 swap_bytes_change_sign32_le(u_char *p, int cc)
 {
        u_char t;
diff -r 2279bf9dee4d -r f60fb37b64ea usr.bin/audio/common/wav.c
--- a/usr.bin/audio/common/wav.c        Sat Apr 15 12:29:43 2023 +0000
+++ b/usr.bin/audio/common/wav.c        Sat Apr 15 12:39:44 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: wav.c,v 1.15 2019/11/09 12:46:44 mrg Exp $     */
+/*     $NetBSD: wav.c,v 1.16 2023/04/15 12:39:44 mlelstv Exp $ */
 
 /*
  * Copyright (c) 2002, 2009, 2013, 2015, 2019 Matthew R. Green
@@ -33,7 +33,7 @@
 #include <sys/cdefs.h>
 
 #ifndef lint
-__RCSID("$NetBSD: wav.c,v 1.15 2019/11/09 12:46:44 mrg Exp $");
+__RCSID("$NetBSD: wav.c,v 1.16 2023/04/15 12:39:44 mlelstv Exp $");
 #endif
 
 
@@ -287,6 +287,8 @@ wav_prepare_header(struct track_info *ti
                break;
        case 16:
                break;
+       case 24:
+               break;
        case 32:
                break;
        default:
diff -r 2279bf9dee4d -r f60fb37b64ea usr.bin/audio/play/play.c
--- a/usr.bin/audio/play/play.c Sat Apr 15 12:29:43 2023 +0000
+++ b/usr.bin/audio/play/play.c Sat Apr 15 12:39:44 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: play.c,v 1.61 2022/05/15 02:16:06 mrg Exp $    */
+/*     $NetBSD: play.c,v 1.62 2023/04/15 12:39:44 mlelstv 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.61 2022/05/15 02:16:06 mrg Exp $");
+__RCSID("$NetBSD: play.c,v 1.62 2023/04/15 12:39:44 mlelstv Exp $");
 #endif
 
 #include <sys/param.h>
@@ -65,6 +65,7 @@ static int    volume;
 static int     balance;
 static int     port;
 static int     fflag;
+static int     lflag;
 static int     qflag;
 int    verbose;
 static int     sample_rate;
@@ -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:fhilp: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 'l':
+                       lflag++;
+                       break;
                case 'q':
                        qflag++;
                        break;
@@ -297,6 +301,57 @@ audio_write(int fd, void *buf, size_t le
        return write(fd, convert_buffer, len);
 }
 
+/*
+ * print audio output offset
+ */
+static void
+print_offset(off_t written, int ratelimit)
+{
+       static time_t last;
+       time_t now;
+       static off_t base = 0;
+       static off_t played = 0;
+       off_t bps;
+       audio_offset_t aoff;
+       u_int blocksize;
+
+       if (!lflag)
+               return;
+
+       if (ioctl(audiofd, AUDIO_GETOOFFS, &aoff))
+               return;
+
+       bps = info.play.sample_rate
+                 * info.play.channels
+                 * info.play.precision / NBBY;
+       blocksize = info.blocksize > 0 ? info.blocksize : 1;
+
+       /* Check if aoff.samples overflowed */
+       if (aoff.samples < played) {
+               base += UINT_MAX;
+               base += 1;
+       }
+
+       /* Overflow base + number of samples in completed blocks + offset in currently played block */
+       played = base + aoff.samples + (aoff.offset % blocksize);
+
+       /* Print only every second */
+       if (ratelimit) {
+               time(&now);
+               if (now == last)
+                       return;
+               last = now;
+       }
+
+       if (bps > 0) {
+               printf("%jdms\n", (written - played) * 1000 / bps);
+       } else {
+               /* unknown rate, report bytes */
+               printf("%jd\n", written - played);
+       }
+       fflush(stdout);
+}
+
 static void
 play(char *file)
 {
@@ -305,7 +360,7 @@ play(char *file)
        void *addr, *oaddr;
        off_t   filesize;
        size_t  sizet_filesize;
-       off_t datasize = 0;
+       off_t datasize = 0, written = 0;
        ssize_t hdrlen;
        int fd;
        int nw;
@@ -369,6 +424,7 @@ play(char *file)
        }
 
        while ((uint64_t)datasize > bufsize) {
+               print_offset(written, 0);
                nw = audio_write(audiofd, addr, bufsize, conv);
                if (nw == -1)
                        err(1, "write failed");
@@ -376,13 +432,17 @@ play(char *file)
                        errx(1, "write failed");
                addr = (char *)addr + bufsize;
                datasize -= bufsize;
+               written += nw;
        }
+       print_offset(written, 0);
        nw = audio_write(audiofd, addr, datasize, conv);
        if (nw == -1)
                err(1, "final write failed");
        if ((off_t)nw != datasize)
                errx(1, "final write failed");
+       written += nw;
 
+       print_offset(written, 0);
        if (ioctl(audiofd, AUDIO_DRAIN) < 0 && !qflag)
                warn("audio drain ioctl failed");
        if (munmap(oaddr, sizet_filesize) < 0)
@@ -401,7 +461,7 @@ play_fd(const char *file, int fd)
        char    *buffer = malloc(bufsize);
        ssize_t hdrlen;
        int     nr, nw;
-       off_t   datasize = 0;
+       off_t   datasize = 0, written = 0;
        off_t   dataout = 0;
 
        if (buffer == NULL)
@@ -430,6 +490,8 @@ play_fd(const char *file, int fd)
                memmove(buffer, buffer + hdrlen, nr - hdrlen);
                nr -= hdrlen;
        }
+
+       print_offset(written, 0);
        while (datasize == 0 || dataout < datasize) {
                if (datasize != 0 && dataout + nr > datasize)
                        nr = datasize - dataout;
@@ -444,10 +506,13 @@ play_fd(const char *file, int fd)
                        goto read_error;
                if (nr == 0)
                        break;
+               print_offset(written, 1);
+               written += nw;
        }
        /* something to think about: no message given for dataout < datasize */
        if (ioctl(audiofd, AUDIO_DRAIN) < 0 && !qflag)
                warn("audio drain ioctl failed");
+       print_offset(written, 0);
        return;
 read_error:
        err(1, "read of standard input failed");
@@ -573,6 +638,8 @@ set_audio_mode:
 
        if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
                err(1, "failed to set audio info");
+       if (ioctl(fd, AUDIO_GETINFO, &info) < 0)
+               err(1, "failed to re-get audio info");
 
        return (hdr_len);
 }
@@ -581,7 +648,7 @@ static void
 usage(void)
 {
 
-       fprintf(stderr, "Usage: %s [-hiqV] [options] files\n", getprogname());
+       fprintf(stderr, "Usage: %s [-hilqV] [options] files\n", getprogname());
        fprintf(stderr, "Options:\n\t"
            "-B buffer size\n\t"
            "-b balance (0-63)\n\t"
diff -r 2279bf9dee4d -r f60fb37b64ea usr.bin/audio/record/record.c
--- a/usr.bin/audio/record/record.c     Sat Apr 15 12:29:43 2023 +0000
+++ b/usr.bin/audio/record/record.c     Sat Apr 15 12:39:44 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: record.c,v 1.56 2022/01/09 06:33:13 mlelstv Exp $      */
+/*     $NetBSD: record.c,v 1.57 2023/04/15 12:39:44 mlelstv Exp $      */



Home | Main Index | Thread Index | Old Index