chiark / gitweb /
Move ENDIAN_ constants to new byte-order.h.
authorRichard Kettlewell <rjk@greenend.org.uk>
Fri, 20 Nov 2009 16:59:48 +0000 (16:59 +0000)
committerRichard Kettlewell <rjk@greenend.org.uk>
Fri, 20 Nov 2009 16:59:48 +0000 (16:59 +0000)
New clients/resample, which just wraps resample_convert() in the
obvious way.

.bzrignore
clients/Makefile.am
clients/resample.c [new file with mode: 0644]
lib/Makefile.am
lib/byte-order.h [new file with mode: 0644]
lib/resample.c
lib/resample.h
lib/speaker-protocol.h

index 7695a2b191cfa7586f17bb623f906b3266b6cd6c..21c00de21f16faa083d901db6ca8bbe9cf0d1d0f 100644 (file)
@@ -204,3 +204,4 @@ config.aux/compile
 server/endian
 clients/rtpmon
 libtests/t-resample
+clients/resample
index 1817f1519056ee28652bb0138dd027b92d842edf..91afc1c4f06ed604840741ce985fa49b6baaaf27 100644 (file)
@@ -17,7 +17,7 @@
 #
 
 bin_PROGRAMS=disorder disorderfm disorder-playrtp
-noinst_PROGRAMS=filename-bytes rtpmon
+noinst_PROGRAMS=filename-bytes rtpmon resample
 noinst_SCRIPTS=dump2wav
 
 AM_CPPFLAGS=-I${top_srcdir}/lib -I../lib
@@ -44,6 +44,9 @@ rtpmon_LDADD=$(LIBOBJS) ../lib/libdisorder.a
 
 filename_bytes_SOURCES=filename-bytes.c
 
+resample_SOURCES=resample.c
+resample_LDADD=$(LIBOBJS) ../lib/libdisorder.a $(LIBSAMPLERATE)
+
 install-exec-hook:
        $(LIBTOOL) --mode=finish $(DESTDIR)$(libdir)
 
diff --git a/clients/resample.c b/clients/resample.c
new file mode 100644 (file)
index 0000000..6c0bd3f
--- /dev/null
@@ -0,0 +1,168 @@
+/*
+ * 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);
+      }
+    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);
+      }
+    default: 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:
+*/
index fa5b389272e74a4cd55a492c6ac8bce9952d8c63..3c1e2f107af9ccd903b74224ed9229e5776b6c5e 100644 (file)
@@ -32,6 +32,7 @@ libdisorder_a_SOURCES=charset.c charset.h             \
        basen.c basen.h                                 \
        base64.c base64.h                               \
        bits.c bits.h                                   \
+       byte-order.h                                    \
        cache.c cache.h                                 \
        cgi.c cgi.h                                     \
        client.c client.h                               \
diff --git a/lib/byte-order.h b/lib/byte-order.h
new file mode 100644 (file)
index 0000000..e80e51f
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * 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/>.
+ */
+/** @file lib/byte-order.h
+ * @brief Byte order macros
+ */
+
+#ifndef BYTE_ORDER_H
+#define BYTE_ORDER_H
+
+#include <config.h>
+
+#define ENDIAN_BIG 1
+#define ENDIAN_LITTLE 2
+#ifdef WORDS_BIGENDIAN
+# define ENDIAN_NATIVE ENDIAN_BIG
+#else
+# define ENDIAN_NATIVE ENDIAN_LITTLE
+#endif
+
+#endif /* BYTE_ORDER_H */
+
+/*
+Local Variables:
+c-basic-offset:2
+comment-column:40
+fill-column:79
+indent-tabs-mode:nil
+End:
+*/
index 11cb6960f359a187d91cde94110efae43c90b5f7..99879a4a87423558d3da1ff50364eadbc6e03442 100644 (file)
@@ -274,7 +274,7 @@ size_t resample_convert(const struct resampler *rs,
     data.input_frames = nframesin;
     data.output_frames = maxframesout;
     data.end_of_input = eof;
-    data.src_ratio = rs->output_rate / rs->input_rate;
+    data.src_ratio = (double)rs->output_rate / rs->input_rate;
     int error_ = src_process(rs->state, &data);
     if(error_)
       disorder_fatal(0, "calling src_process: %s", src_strerror(error_));
@@ -303,6 +303,7 @@ size_t resample_convert(const struct resampler *rs,
   xfree(input);
   eof = 0;             /* quieten compiler */
   /* Report how many input bytes were actually consumed */
+  //fprintf(stderr, "converted %zu frames\n", nframesin);
   return nframesin * rs->input_bytes_per_frame;
 }
 
index 477efd9285962f0f2fefc737475e7ab264b142e1..e99aaeb3455de23d77fef9a981f61bcf0157120a 100644 (file)
@@ -27,8 +27,7 @@
 #include <samplerate.h>
 #endif
 
-#define ENDIAN_BIG 1
-#define ENDIAN_LITTLE 2
+#include "byte-order.h"
 
 struct resampler {
   int input_bits, input_channels, input_rate, input_signed, input_endian;
index bd80e7d0249f45363d54b4a0c1eda0eeae3fd857..3b21f0a8ef8821f21f5a25a3fa844e2a1964c520 100644 (file)
@@ -25,6 +25,8 @@
 #ifndef SPEAKER_PROTOCOL_H
 #define SPEAKER_PROTOCOL_H
 
+#include "byte-order.h"
+
 /** @brief A message from the main server to the speaker, or vica versa */
 struct speaker_message {
   /** @brief Message type
@@ -123,13 +125,6 @@ struct stream_header {
 
   /** @brief Endianness */
   uint8_t endian;
-#define ENDIAN_BIG 1
-#define ENDIAN_LITTLE 2
-#ifdef WORDS_BIGENDIAN
-# define ENDIAN_NATIVE ENDIAN_BIG
-#else
-# define ENDIAN_NATIVE ENDIAN_LITTLE
-#endif
 } attribute((packed));
 
 static inline int formats_equal(const struct stream_header *a,