chiark / gitweb /
Use libsamplerate in disorder-normalize, if available. If it's not
authorRichard Kettlewell <rjk@greenend.org.uk>
Fri, 20 Nov 2009 18:58:18 +0000 (18:58 +0000)
committerRichard Kettlewell <rjk@greenend.org.uk>
Fri, 20 Nov 2009 18:58:18 +0000 (18:58 +0000)
installed then back off to the old way (which is to run an external
copy of sox).

1  2 
clients/resample.c
server/normalize.c

diff --combined clients/resample.c
index 0000000000000000000000000000000000000000,8ba1843bc5ee3a1aeb3e4aa06efe967669b3d7c8..bc41013b6fb343b55d7b0b759b475aab0271a661
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,170 +1,170 @@@
 -    default: fatal(0, "invalid option");
+ /*
+  * This file is part of DisOrder.
+  * Copyright (C) 2009 Richard Kettlewell
+  *
+  * This program is free software: you can redistribute it and/or modify
+  * it under the terms of the GNU General Public License as published by
+  * the Free Software Foundation, either version 3 of the License, or
+  * (at your option) any later version.
+  *
+  * This program is distributed in the hope that it will be useful,
+  * but WITHOUT ANY WARRANTY; without even the implied warranty of
+  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  * GNU General Public License for more details.
+  *
+  * You should have received a copy of the GNU General Public License
+  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+  */
+ #include "common.h"
+ #include <unistd.h>
+ #include <locale.h>
+ #include <errno.h>
+ #include <getopt.h>
+ #include "resample.h"
+ #include "mem.h"
+ #include "syscalls.h"
+ #include "log.h"
+ static int input_bits = 16;
+ static int input_channels = 2;
+ static int input_rate = 44100;
+ static int input_signed = 1;
+ static int input_endian = ENDIAN_NATIVE;
+ static int output_bits = 16;
+ static int output_channels = 2;
+ static int output_rate = 44100;
+ static int output_signed = 1;
+ static int output_endian = ENDIAN_NATIVE;
+ static const struct option options[] = {
+   { "help", no_argument, 0, 'h' },
+   { "input-bits", required_argument, 0, 'b' },
+   { "input-channels", required_argument, 0, 'c' },
+   { "input-rate", required_argument, 0, 'r' },
+   { "input-signed", no_argument, 0, 's' },
+   { "input-unsigned", no_argument, 0, 'u' },
+   { "input-endian", required_argument, 0, 'e' },
+   { "output-bits", required_argument, 0, 'B' },
+   { "output-channels", required_argument, 0, 'C' },
+   { "output-rate", required_argument, 0, 'R' },
+   { "output-signed", no_argument, 0, 'S' },
+   { "output-unsigned", no_argument, 0, 'U' },
+   { "output-endian", required_argument, 0, 'E' },
+   { 0, 0, 0, 0 }
+ };
+ /* display usage message and terminate */
+ static void help(void) {
+   xprintf("Usage:\n"
+         "  resample [OPTIONS] < INPUT > OUTPUT\n"
+         "Options:\n"
+         "  --help, -h                      Display usage message\n"
+           "Input format:\n"
+         "  --input-bits, -b N              Bits/sample (16)\n"
+         "  --input-channels, -c N          Samples/frame (2)\n"
+         "  --input-rate, -r N              Frames/second (44100)\n"
+         "  --input-signed, -s              Signed samples (yes)\n"
+         "  --input-unsigned, -u            Unsigned samples\n"
+         "  --input-endian, -e big|little   Sample endianness (native)\n"
+           "Output format:\n"
+         "  --output-bits, -B N             Bits/sample (16)\n"
+         "  --output-channels, -C N         Samples/frame (2)\n"
+         "  --output-rate, -R N             Frames/second (44100)\n"
+         "  --output-signed, -S             Signed samples (yes)\n"
+         "  --output-unsigned, -U           Unsigned samples\n"
+         "  --output-endian, -E big|little  Sample endianness (native)\n"
+           "Defaults are in brackets.\n"
+           "\n"
+           "Feeds raw sample data through resample_convert().\n");
+   xfclose(stdout);
+   exit(0);
+ }
+ static void converted(uint8_t *bytes,
+                       size_t nbytes,
+                       void attribute((unused)) *cd) {
+   while(nbytes > 0) {
+     ssize_t n = write(1, bytes, nbytes);
+     if(n < 0)
+       disorder_fatal(errno, "writing to stdout");
+     bytes += n;
+     nbytes -= n;
+   }
+ }
+ int main(int argc, char **argv) {
+   int n;
+   mem_init();
+   if(!setlocale(LC_CTYPE, "")) 
+     disorder_fatal(errno, "error calling setlocale");
+   while((n = getopt_long(argc, argv, "+hb:c:r:sue:B:C:R:SUE:", 
+                          options, 0)) >= 0) {
+     switch(n) {
+     case 'h': help();
+     case 'b': input_bits = atoi(optarg); break;
+     case 'c': input_channels = atoi(optarg); break;
+     case 'r': input_rate = atoi(optarg); break;
+     case 's': input_signed = 1; break;
+     case 'u': input_signed = 1; break;
+     case 'e':
+       switch(optarg[0]) {
+       case 'b': case 'B': input_endian = ENDIAN_BIG; break;
+       case 'l': case 'L': input_endian = ENDIAN_LITTLE; break;
+       case 'n': case 'N': input_endian = ENDIAN_NATIVE; break;
+       default: disorder_fatal(0, "unknown endianness '%s'", optarg);
+       }
+       break;
+     case 'B': output_bits = atoi(optarg); break;
+     case 'C': output_channels = atoi(optarg); break;
+     case 'R': output_rate = atoi(optarg); break;
+     case 'S': output_signed = 1; break;
+     case 'U': output_signed = 1; break;
+     case 'E':
+       switch(optarg[0]) {
+       case 'b': case 'B': output_endian = ENDIAN_BIG; break;
+       case 'l': case 'L': output_endian = ENDIAN_LITTLE; break;
+       case 'n': case 'N': output_endian = ENDIAN_NATIVE; break;
+       default: disorder_fatal(0, "unknown endianness '%s'", optarg);
+       }
+       break;
++    default: disorder_fatal(0, "invalid option");
+     }
+   }
+   struct resampler rs[1];
+   resample_init(rs, input_bits, input_channels, input_rate, input_signed,
+                 input_endian, output_bits, output_channels, output_rate,
+                 output_signed, output_endian);
+ #define BUFFER_SIZE (1024 * 1024)
+   uint8_t *buffer = xmalloc_noptr(BUFFER_SIZE);
+   size_t used = 0;
+   int eof = 0;
+   while(used || !eof) {
+     if(!eof) {
+       ssize_t r = read(0, buffer + used, BUFFER_SIZE - used);
+       if(r < 0)
+         disorder_fatal(errno, "reading from stdin");
+       if(r == 0)
+         eof = 1;
+       used += r;
+     }
+     size_t consumed = resample_convert(rs, buffer, used, eof, converted, 0);
+     memmove(buffer, buffer + consumed, used - consumed);
+     used -= consumed;
+   }
+   if(close(1) < 0)
+     disorder_fatal(errno, "closing stdout");
+   return 0;
+ }
+ /*
+ Local Variables:
+ c-basic-offset:2
+ comment-column:40
+ fill-column:79
+ indent-tabs-mode:nil
+ End:
+ */
diff --combined server/normalize.c
index 52df3d77edae0e70578d3586c91484dadcfdea71,2a28c1f5c5778db3a147dfe6757fcb48159519dd..bcfc3a8847b311c2788f19b03eb480b80b81d368
@@@ -1,6 -1,6 +1,6 @@@
  /*
   * This file is part of DisOrder
-  * Copyright (C) 2007, 2008 Richard Kettlewell
+  * Copyright (C) 2007-2009 Richard Kettlewell
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
  /** @file server/normalize.c
   * @brief Convert "raw" format output to the configured format
   *
-  * Currently we invoke sox even for trivial conversions such as byte-swapping.
-  * Ideally we would do all conversion including resampling in this one process
-  * and eliminate the dependency on sox.
+  * If libsamplerate is available then resample_convert() is used to do all
+  * conversions.  If not then we invoke sox (even for trivial conversions such
+  * as byte-swapping).  The sox support might be removed in a future version.
   */
  
  #include "disorder-server.h"
+ #include "resample.h"
+ static char buffer[1024 * 1024];
  
  static const struct option options[] = {
    { "help", no_argument, 0, 'h' },
@@@ -59,7 -62,6 +62,6 @@@ static void help(void) 
   * @param n Number of bytes to copy
   */
  static void copy(int infd, int outfd, size_t n) {
-   char buffer[4096];
    ssize_t written;
  
    while(n > 0) {
        if(errno == EINTR)
        continue;
        else
 -      fatal(errno, "read error");
 +      disorder_fatal(errno, "read error");
      }
      if(readden == 0)
 -      fatal(0, "unexpected EOF");
 +      disorder_fatal(0, "unexpected EOF");
      n -= readden;
      written = 0;
      while(written < readden) {
        const ssize_t w = write(outfd, buffer + written, readden - written);
        if(w < 0)
 -      fatal(errno, "write error");
 +      disorder_fatal(errno, "write error");
        written += w;
      }
    }
  }
  
+ #if !HAVE_SAMPLERATE_H
  static void soxargs(const char ***pp, char **qq,
                      const struct stream_header *header) {
    *(*pp)++ = "-t.raw";
      case 16: *(*pp)++ = "-w"; break;
      case 32: *(*pp)++ = "-l"; break;
      case 64: *(*pp)++ = "-d"; break;
 -    default: fatal(0, "cannot handle sample size %d", header->bits);
 +    default: disorder_fatal(0, "cannot handle sample size %d", header->bits);
      }
      break;
    case 1:
        case ENDIAN_LITTLE: *(*pp)++ = "-L"; break;
        }
      if(header->bits % 8)
 -      fatal(0, "cannot handle sample size %d", header->bits);
 +      disorder_fatal(0, "cannot handle sample size %d", header->bits);
      *qq += sprintf((char *)(*(*pp)++ = *qq), "-%d", header->bits / 8) + 1;
      break;
    default:
 -    fatal(0, "unknown sox_generation %ld", config->sox_generation);
 +    disorder_fatal(0, "unknown sox_generation %ld", config->sox_generation);
    }
  }
+ #else
+ static void converted(uint8_t *bytes,
+                       size_t nbytes,
+                       void attribute((unused)) *cd) {
+   /*syslog(LOG_INFO, "out: %02x %02x %02x %02x",
+          bytes[0],
+          bytes[1],
+          bytes[2],
+          bytes[3]);*/
+   while(nbytes > 0) {
+     ssize_t n = write(1, bytes, nbytes);
+     if(n < 0)
+       disorder_fatal(errno, "writing to stdout");
+     bytes += n;
+     nbytes -= n;
+   }
+ }
+ #endif
  
  int main(int argc, char attribute((unused)) **argv) {
    struct stream_header header, latest_format;
-   int n, p[2], outfd = -1, logsyslog = !isatty(2);
+   int n, outfd = -1, logsyslog = !isatty(2), rs_in_use = 0;
    pid_t pid = -1;
+   struct resampler rs[1];
  
    set_progname(argv);
    if(!setlocale(LC_CTYPE, ""))
 -    fatal(errno, "error calling setlocale");
 +    disorder_fatal(errno, "error calling setlocale");
    while((n = getopt_long(argc, argv, "hVc:dDSs", options, 0)) >= 0) {
      switch(n) {
      case 'h': help();
      case 'D': debugging = 0; break;
      case 'S': logsyslog = 0; break;
      case 's': logsyslog = 1; break;
 -    default: fatal(0, "invalid option");
 +    default: disorder_fatal(0, "invalid option");
      }
    }
    if(config_read(1, NULL))
 -    fatal(0, "cannot read configuration");
 +    disorder_fatal(0, "cannot read configuration");
    if(logsyslog) {
      openlog(progname, LOG_PID, LOG_DAEMON);
      log_default = &log_syslog;
    }
    memset(&latest_format, 0, sizeof latest_format);
    for(;;) {
+     /* Read one header */
      n = 0;
      while((size_t)n < sizeof header) {
        int r = read(0, (char *)&header + n, sizeof header - n);
  
        if(r < 0) {
          if(errno != EINTR)
 -          fatal(errno, "error reading header");
 +          disorder_fatal(errno, "error reading header");
        } else if(r == 0) {
          if(n)
 -          fatal(0, "EOF reading header");
 +          disorder_fatal(0, "EOF reading header");
          break;
        } else
          n += r;
        break;
      /* Sanity check the header */
      if(header.rate < 100 || header.rate > 1000000)
 -      fatal(0, "implausible rate %"PRId32"Hz (%#"PRIx32")",
 -            header.rate, header.rate);
 +      disorder_fatal(0, "implausible rate %"PRId32"Hz (%#"PRIx32")",
 +                     header.rate, header.rate);
      if(header.channels < 1 || header.channels > 2)
 -      fatal(0, "unsupported channel count %d", header.channels);
 +      disorder_fatal(0, "unsupported channel count %d", header.channels);
      if(header.bits % 8 || !header.bits || header.bits > 64)
 -      fatal(0, "unsupported sample size %d bits", header.bits);
 +      disorder_fatal(0, "unsupported sample size %d bits", header.bits);
      if(header.endian != ENDIAN_BIG && header.endian != ENDIAN_LITTLE)
-       disorder_fatal(0, "unsupported byte order %x", header.bits);
 -      fatal(0, "unsupported byte order %d", header.endian);
++      disorder_fatal(0, "unsupported byte order %d", header.endian);
      /* Skip empty chunks regardless of their alleged format */
      if(header.nbytes == 0)
        continue;
      /* If the format has changed we stop/start the converter */
+ #if HAVE_SAMPLERATE_H
+     /* We have libsamplerate */
+     if(formats_equal(&header, &config->sample_format))
+       /* If the format is already correct then we just write out the data */
+       copy(0, 1, header.nbytes);
+     else {
+       /* If we have a resampler active already check it is suitable and destroy
+        * it if not */
+       if(!formats_equal(&header, &latest_format) && rs_in_use) {
+         resample_close(rs);
+         rs_in_use = 0;
+       }
+       /*syslog(LOG_INFO, "%d/%d/%d/%d/%d -> %d/%d/%d/%d/%d",
+              header.bits,
+              header.channels, 
+              header.rate,
+              1,
+              header.endian,
+              config->sample_format.bits,
+              config->sample_format.channels, 
+              config->sample_format.rate,
+              1,
+              config->sample_format.endian);*/
+       if(!rs_in_use) {
+         /* Create a suitable resampler. */
+         resample_init(rs,
+                       header.bits,
+                       header.channels, 
+                       header.rate,
+                       1,                /* signed */
+                       header.endian,
+                       config->sample_format.bits,
+                       config->sample_format.channels, 
+                       config->sample_format.rate,
+                       1,                /* signed */
+                       config->sample_format.endian);
+         latest_format = header;
+         rs_in_use = 1;
+         /* TODO speaker protocol does not record signedness of samples.  It's
+          * assumed that they are always signed.  This should be fixed in the
+          * future (and the sample format syntax extended in a compatible
+          * way). */
+       }
+       /* Feed data through the resampler */
+       size_t used = 0, left = header.nbytes;
+       while(used || left) {
+         if(left) {
+           size_t limit = (sizeof buffer) - used;
+           if(limit > left)
+             limit = left;
+           ssize_t r = read(0, buffer + used, limit);
+           if(r < 0)
+             disorder_fatal(errno, "reading from stdin");
+           if(r == 0)
+             disorder_fatal(0, "unexpected EOF");
+           left -= r;
+           used += r;
+           //syslog(LOG_INFO, "read %zd bytes", r);
+         }
+         /*syslog(LOG_INFO, " in: %02x %02x %02x %02x",
+                (uint8_t)buffer[0],
+                (uint8_t)buffer[1], 
+                (uint8_t)buffer[2],
+                (uint8_t)buffer[3]);*/
+         const size_t consumed = resample_convert(rs,
+                                                  (uint8_t *)buffer, used,
+                                                  !left,
+                                                  converted, 0);
+         //syslog(LOG_INFO, "used=%zu consumed=%zu", used, consumed);
+         memmove(buffer, buffer + consumed, used - consumed);
+         used -= consumed;
+       }
+     }
+ #else
+     /* We do not have libsamplerate.  We will use sox instead. */
      if(!formats_equal(&header, &latest_format)) {
        if(pid != -1) {
          /* There's a running converter, stop it */
          xclose(outfd);
          if(waitpid(pid, &n, 0) < 0)
 -          fatal(errno, "error calling waitpid");
 +          disorder_fatal(errno, "error calling waitpid");
          if(n)
 -          fatal(0, "sox failed: %#x", n);
 +          disorder_fatal(0, "sox failed: %#x", n);
          pid = -1;
          outfd = -1;
        }
          *pp++ = "-";                  /* stdout */
          *pp = 0;
          /* This pipe will be sox's stdin */
+         int p[2];
          xpipe(p);
          if(!(pid = xfork())) {
            exitfn = _exit;
            xclose(p[0]);
            xclose(p[1]);
            execvp(av[0], (char **)av);
 -          fatal(errno, "sox");
 +          disorder_fatal(errno, "sox");
          }
          xclose(p[0]);
          outfd = p[1];
      }
      /* Convert or copy this chunk */
      copy(0, outfd, header.nbytes);
+ #endif
    }
    if(outfd != -1)
      xclose(outfd);
    if(pid != -1) {
      /* There's still a converter running */
      if(waitpid(pid, &n, 0) < 0)
 -      fatal(errno, "error calling waitpid");
 +      disorder_fatal(errno, "error calling waitpid");
      if(n)
 -      fatal(0, "sox failed: %#x", n);
 +      disorder_fatal(0, "sox failed: %#x", n);
    }
+   if(rs_in_use)
+     resample_close(rs);
    return 0;
  }