shithub: sox

Download patch

ref: c056bbcc5c82195b6e1c422fc35f0fe059a1b25e
parent: a629631d5f8fcef3a8fc1dc2901caba0091427ba
author: robs <robs>
date: Sun Jun 28 09:08:47 EDT 2009

added example 4: concatenate audio

--- a/ChangeLog
+++ b/ChangeLog
@@ -5,46 +5,64 @@
 sox-11gamma, followed by a list of prior authors and features.
 
 
-sox-14.3.0	2009-06-14
+sox-14.x.x	2009-xx-xx
 ----------
 
 Previously deprecated features that have been removed in this release:
 
-  Deprec-  Feature    [O(ption)]
-  ated in  [F(ormat)] [E(ffect)]   Replacement
-  -------  ----------------------  ----------------------
-  14.1.0   E resample *            ~= rate
-  14.1.0   E polyphase *           ~= rate
-  14.1.0   E rabbit *              ~= rate
-  14.2.0   E dither: RPDF,scaled   dither (TPDF only)
-  14.1.0   F flac: libFLAC 1.1.1   libFLAC > 1.1.1
-
-  * But interface retained as an alias for `rate'.
-
 Newly deprecated features (to be removed in future):
 
-  Deprec-  Feature    [O(ption)]                           Removal
-  ated in  [F(ormat)] [E(ffect)]   Replacement             due after
-  -------  ----------------------  ----------------------  -------
-  14.3.0   E norm -b, norm -i      gain -B, gain -en       14.3.0 + 1 year
-  14.3.0   E filter                ~=sinc                  14.3.0 + 1 year
-  14.3.0   F flac: libFLAC 1.1.2,3 libFLAC > 1.1.3         14.3.0 + 6 months
-  14.3.0   PLAY_RATE_ARG           SOX_OPTS                14.3.0 + 6 months
-
 Previously deprecated features (to be removed in future):
 
   Deprec-  Feature    [O(ption)]                           Removal
   ated in  [F(ormat)] [E(ffect)]   Replacement             due after
   -------  ----------------------  ----------------------  -------
-  14.2.0   E key alias             pitch                   2009-05-29
-  14.2.0   E pan                   ~= mixer/remix          2009-05-29
-  14.1.0   E resample alias        rate                    14.3.0 + 1 year
-  14.1.0   E polyphase alias       rate                    14.3.0 + 1 year
-  14.1.0   E rabbit alias          rate                    14.3.0 + 1 year
+  14.2.0   E key alias             pitch                   14.3.0
+  14.2.0   E pan                   ~= mixer/remix          14.3.0
   14.1.0   F wve (native)          wve (libsndfile)        2009-07-29
   14.1.0   Behaviour whereby       soxi                    2009-07-29
            sox -V file(s) -n
            doesn't read to EOF.
+  14.3.0   F flac: libFLAC 1.1.2,3 libFLAC > 1.1.3         2009-12-14
+  14.3.0   PLAY_RATE_ARG           SOX_OPTS                2009-12-14
+  14.3.0   E norm -b, norm -i      gain -B, gain -en       2010-06-14
+  14.3.0   E filter                ~=sinc                  2010-06-14
+  14.1.0   E resample alias        rate                    2010-06-14
+  14.1.0   E polyphase alias       rate                    2010-06-14
+  14.1.0   E rabbit alias          rate                    2010-06-14
+
+LibSoX interface changes:
+
+File formats:
+
+Audio device drivers:
+
+Effects:
+
+Other new features:
+
+  o Added libSoX example #4: concatenating audio files.  (robs)
+
+Other bug fixes:
+
+Internal improvements:
+
+
+sox-14.3.0	2009-06-14
+----------
+
+Previously deprecated features that have been removed in this release:
+
+  Deprec-  Feature    [O(ption)]
+  ated in  [F(ormat)] [E(ffect)]   Replacement
+  -------  ----------------------  ----------------------
+  14.1.0   E resample *            ~= rate
+  14.1.0   E polyphase *           ~= rate
+  14.1.0   E rabbit *              ~= rate
+  14.2.0   E dither: RPDF,scaled   dither (TPDF only)
+  14.1.0   F flac: libFLAC 1.1.1   libFLAC > 1.1.1
+
+  * But interface retained as an alias for `rate'.
 
 LibSoX interface changes:
 
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -76,6 +76,8 @@
 target_link_libraries(example2 lib${PROJECT_NAME} lpc10 ${optional_libs})
 add_executable(example3 example3.c)
 target_link_libraries(example3 lib${PROJECT_NAME} lpc10 ${optional_libs})
+add_executable(example4 example4.c)
+target_link_libraries(example4 lib${PROJECT_NAME} lpc10 ${optional_libs})
 find_program(LN ln)
 if (LN)
   add_custom_target(rec ALL ${LN} -sf sox rec DEPENDS sox)
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -30,7 +30,7 @@
 #########################
 
 bin_PROGRAMS = sox
-EXTRA_PROGRAMS = example0 example1 example2 example3 sox_sample_test
+EXTRA_PROGRAMS = example0 example1 example2 example3 example4 sox_sample_test
 lib_LTLIBRARIES = libsox.la
 include_HEADERS = sox.h
 nodist_include_HEADERS = soxstdint.h
@@ -40,6 +40,7 @@
 example1_SOURCES = example1.c
 example2_SOURCES = example2.c
 example3_SOURCES = example3.c
+example4_SOURCES = example4.c
 sox_sample_test_SOURCES = sox_sample_test.c sox_sample_test.h
 
 
@@ -121,12 +122,13 @@
 example1_LDADD = ${sox_LDADD}
 example2_LDADD = ${sox_LDADD}
 example3_LDADD = ${sox_LDADD}
+example4_LDADD = ${sox_LDADD}
 
 EXTRA_DIST = monkey.au monkey.wav optional-fmts.in \
 	     CMakeLists.txt soxstdint.h.cmake soxconfig.h.cmake \
 	     tests.sh testall.sh tests.bat testall.bat test-comments
 
-all: sox$(EXEEXT) play rec soxi sox_sample_test$(EXEEXT) example0$(EXEEXT) example1$(EXEEXT) example2$(EXEEXT) example3$(EXEEXT)
+all: sox$(EXEEXT) play rec soxi sox_sample_test$(EXEEXT) example0$(EXEEXT) example1$(EXEEXT) example2$(EXEEXT) example3$(EXEEXT) example4$(EXEEXT)
 
 play rec: sox$(EXEEXT)
 	if test "$(PLAYRECLINKS)" = "yes"; then	\
@@ -157,7 +159,7 @@
 clean-local:
 	$(RM) play rec soxi
 	$(RM) sox_sample_test$(EXEEXT)
-	$(RM) example0$(EXEEXT) example1$(EXEEXT) example2$(EXEEXT) example3$(EXEEXT)
+	$(RM) example0$(EXEEXT) example1$(EXEEXT) example2$(EXEEXT) example3$(EXEEXT) example4$(EXEEXT)
 
 distclean-local:
 	$(RM) soxstdint.h
--- /dev/null
+++ b/src/example4.c
@@ -1,0 +1,97 @@
+/* Simple example of using SoX libraries
+ *
+ * Copyright (c) 2009 robs@users.sourceforge.net
+ *
+ * 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 2 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, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "sox.h"
+
+/* Concatenate audio files.  Note that the files must have the same number
+ * of channels and the same sample rate.
+ *
+ * Usage: example4 input-1 input-2 [... input-n] output
+ */
+
+#define check(x) do if (!(x)) { \
+  fprintf(stderr, "check failed: %s\n", #x); goto error; } while (0)
+
+int main(int argc, char * argv[])
+{
+  sox_format_t * output = NULL;
+  int i;
+
+  check(argc >= 1 + 2 + 1); /* Need at least 2 input files + 1 output file. */
+  check(sox_init() == SOX_SUCCESS);
+
+  /* Defer openning the output file as we want to set its characteristics
+   * based on those of the input files. */
+
+  for (i = 1; i < argc - 1; ++i) { /* For each input file... */
+    sox_format_t * input;
+    static sox_signalinfo_t signal; /* static quashes `uninitialised' warning.*/
+
+    /* The (maximum) number of samples that we shall read/write at a time;
+     * chosen as a rough match to typical operating system I/O buffer size: */
+    #define MAX_SAMPLES 2048
+    sox_sample_t samples[MAX_SAMPLES]; /* Temporary store whilst copying. */
+    size_t number_read;
+
+    /* Open this input file: */
+    check(input = sox_open_read(argv[i], NULL, NULL, NULL));
+
+    if (i == 1) { /* If this is the first input file... */
+
+      /* Open the output file using the same signal and encoding character-
+       * istics as the first input file.  Note that here, input->signal.length
+       * will not be equal to the output file length so we are relying on
+       * libSoX to set the output length correctly (i.e. non-seekable output
+       * is not catered for); an alternative would be to first calculate the
+       * output length by summing the lengths of the input files and modifying
+       * the second parameter to sox_open_write accordingly. */
+      check(output = sox_open_write(argv[argc - 1],
+            &input->signal, &input->encoding, NULL, NULL, NULL));
+      
+      /* Also, we'll store the signal characteristics of the first file
+       * so that we can check that these match those of the other inputs: */
+      signal = input->signal;
+    }
+    else { /* Second or subsequent input file... */
+
+      /* Check that this input file's signal matches that of the first file: */
+      check(input->signal.channels == signal.channels);
+      check(input->signal.rate == signal.rate);
+    }
+
+    /* Copy all of the audio from this input file to the output file: */
+    while ((number_read = sox_read(input, samples, MAX_SAMPLES)))
+      check(sox_write(output, samples, number_read) == number_read);
+
+    check(sox_close(input) == SOX_SUCCESS); /* Finished with this input file.*/
+  }
+
+  check(sox_close(output) == SOX_SUCCESS); /* Finished with the output file. */
+  output = NULL;
+  check(sox_quit() == SOX_SUCCESS);
+  return 0;
+
+error: /* Truncate output file on error: */
+  if (output) {
+    FILE * f;
+    sox_close(output);
+    if ((f = fopen(argv[argc - 1], "w"))) fclose(f);
+  }
+  return 1;
+}