shithub: sox

Download patch

ref: 3f660c88c51c414b45076ef3572e2bc6d63fcdef
parent: fee9a43775ad5aa0227119903bf1cd69e91aa328
author: robs <robs>
date: Wed Apr 16 03:08:05 EDT 2008

Added example0, with IO now in libSoX

--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -3,7 +3,7 @@
 include_directories(${CMAKE_CURRENT_BINARY_DIR})
 
 if(CMAKE_COMPILER_IS_GNUCC)
-  add_definitions(-Wconversion -Werror)
+  #add_definitions(-Wconversion -Werror)
   #add_definitions(-Wno-missing-field-initializers)
 endif(CMAKE_COMPILER_IS_GNUCC)
 
@@ -19,15 +19,16 @@
 
 # Format with: !xargs echo|tr ' ' '\n'|sort|column|expand|sed 's/^/  /'
 set(effects_srcs
-  biquad          earwax          mixer           remix           stat
-  biquads         echo            noiseprof       repeat          stretch
-  chorus          echos           noisered        resample        swap
-  compand         fade            normalise       reverb          synth
-  compandt        FFT             pad             reverse         tempo
-  contrast        filter          pan             silence         tremolo
-  dcshift         flanger         phaser          skeleff         trim
-  delay           key             pitch           speed           vol
-  dither          mcompand        polyphas        splice
+  biquad          echo            noiseprof       repeat          swap
+  biquads         echos           noisered        resample        synth
+  chorus          fade            normalise       reverb          tempo
+  compand         FFT             output          reverse         tremolo
+  compandt        filter          pad             silence         trim
+  contrast        flanger         pan             skeleff         vol
+  dcshift         input           phaser          speed
+  delay           key             pitch           splice
+  dither          mcompand        polyphas        stat
+  earwax          mixer           remix           stretch
 )
 set(formats_srcs
   8svx            cvsd-fmt        ima-fmt         s2-fmt          u2-fmt
@@ -46,11 +47,13 @@
   effects                 formats_i               ${optional_srcs}
   effects_i               ${formats_srcs}         soxstdint
   ${effects_srcs}         libsox                  xmalloc
-  formats                 libsox_i
+  formats                 libsox_i remez0
 )
 add_executable(${PROJECT_NAME} ${PROJECT_NAME}.c getopt getopt1)
 target_link_libraries(${PROJECT_NAME} lib${PROJECT_NAME} lpc10 ${optional_libs})
 add_executable(sox_sample_test sox_sample_test.c)
+add_executable(example0 example0.c)
+target_link_libraries(example0 lib${PROJECT_NAME} lpc10 ${optional_libs})
 add_executable(example1 example1.c)
 target_link_libraries(example1 lib${PROJECT_NAME} lpc10 ${optional_libs})
 add_executable(example2 example2.c)
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -13,11 +13,13 @@
 #########################
 
 bin_PROGRAMS = sox
-EXTRA_PROGRAMS = example1 example2 sox_sample_test
+EXTRA_PROGRAMS = example0 example1 example2 sox_sample_test
 lib_LTLIBRARIES = libsox.la libsfx.la
 include_HEADERS = sox.h soxstdint.h
 sox_SOURCES = sox.c
 sox_LDADD = libsox.la libsfx.la
+example0_SOURCES = example0.c
+example0_LDADD = libsox.la libsfx.la
 example1_SOURCES = example1.c
 example1_LDADD = libsox.la libsfx.la
 example2_SOURCES = example2.c
@@ -225,7 +227,7 @@
 	  pan.c phaser.c pitch.c polyphas.c rabbit.c remix.c repeat.c \
 	  resample.c reverb.c reverse.c silence.c skeleff.c speed.c	\
 	  splice.c stat.c stretch.c swap.c synth.c tempo.c tremolo.c trim.c \
-	  vol.c normalise.c delay.c contrast.c effects_i.c
+	  vol.c normalise.c delay.c contrast.c effects_i.c input.c output.c
 libsfx_la_CFLAGS = @SAMPLERATE_CFLAGS@
 libsfx_la_LIBADD = @SAMPLERATE_LIBS@ libsox.la
 
@@ -321,7 +323,7 @@
 	     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) example1$(EXEEXT) example2$(EXEEXT)
+all: sox$(EXEEXT) play rec soxi sox_sample_test$(EXEEXT) example0$(EXEEXT) example1$(EXEEXT) example2$(EXEEXT)
 
 play rec soxi: sox$(EXEEXT)
 	./sox --help > /dev/null
@@ -340,7 +342,7 @@
 clean-local:
 	$(RM) play rec soxi
 	$(RM) sox_sample_test
-	$(RM) example1 example2
+	$(RM) example0 example1 example2
 
 distclean-local:
 	$(RM) soxstdint.h
--- a/src/effects.h
+++ b/src/effects.h
@@ -36,6 +36,7 @@
   EFFECT(flanger)
   EFFECT(gain)
   EFFECT(highpass)
+  EFFECT(input)
   EFFECT(key)
 #ifdef HAVE_LADSPA_H
   EFFECT(ladspa)
@@ -47,6 +48,7 @@
   EFFECT(noisered)
   EFFECT(norm)
   EFFECT(oops)
+  EFFECT(output)
   EFFECT(pad)
   EFFECT(pan)
   EFFECT(phaser)
--- /dev/null
+++ b/src/example0.c
@@ -1,0 +1,92 @@
+/* Simple example of using SoX libraries
+ *
+ * Copyright (c) 2007-8 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"
+#include <stdio.h>
+#ifdef NDEBUG /* N.B. assert used with active statements so enable always */
+#undef NDEBUG
+#endif
+#include <assert.h>
+
+/*
+ * Reads input file, applies vol & flanger effects, stores in output file.
+ * E.g. example1 monkey.au monkey.aiff
+ */
+int main(int argc, char * argv[])
+{
+  static sox_format_t * in, * out; /* input and output files */
+  sox_effects_chain_t * chain;
+  sox_effect_t * e;
+  char * args[10];
+
+  assert(argc == 3);
+
+  /* All libSoX applications must start by initialising the SoX library */
+  assert(sox_format_init() == SOX_SUCCESS);
+
+  /* Open the input file (with default parameters) */
+  assert(in = sox_open_read(argv[1], NULL, NULL, NULL));
+
+  /* Open the output file; we must specify the output signal characteristics.
+   * Since we are using only simple effects, they are the same as the input
+   * file characteristics */
+  assert(out = sox_open_write(argv[2], &in->signal, NULL, NULL, NULL, NULL));
+
+  /* Create an effects chain; some effects need to know about the input
+   * or output file encoding so we provide that information here */
+  chain = sox_create_effects_chain(&in->encoding, &out->encoding);
+
+  /* The first effect in the effect chain must be something that can source
+   * samples; in this case, we have defined an input handler that inputs
+   * data from an audio file */
+  e = sox_create_effect(sox_find_effect("input"));
+  args[0] = (char *)in, assert(e->handler.getopts(e, 1, args) == SOX_SUCCESS);
+  /* This becomes the first `effect' in the chain */
+  assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
+
+  /* Create the `vol' effect, and initialise it with the desired parameters: */
+  e = sox_create_effect(sox_find_effect("vol"));
+  args[0] = "3dB", assert(e->handler.getopts(e, 1, args) == SOX_SUCCESS);
+  /* Add the effect to the end of the effects processing chain: */
+  assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
+
+  /* Create the `flanger' effect, and initialise it with default parameters: */
+  e = sox_create_effect(sox_find_effect("flanger"));
+  assert(e->handler.getopts(e, 0, NULL) == SOX_SUCCESS);
+  /* Add the effect to the end of the effects processing chain: */
+  assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
+
+  /* The last effect in the effect chain must be something that only consumes
+   * samples; in this case, we have defined an output handler that outputs
+   * data to an audio file */
+  e = sox_create_effect(sox_find_effect("output"));
+  args[0] = (char *)out, assert(e->handler.getopts(e, 1, args) == SOX_SUCCESS);
+  assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
+
+  /* Flow samples through the effects processing chain until EOF is reached */
+  sox_flow_effects(chain, NULL);
+
+  /* All done; tidy up: */
+  sox_delete_effects(chain);
+  sox_close(out);
+  sox_close(in);
+  sox_format_quit();
+  free(chain);
+  return 0;
+}
--- /dev/null
+++ b/src/input.c
@@ -1,0 +1,58 @@
+/* libSoX effect: Input audio from a file   (c) 2008 robs@users.sourceforge.net
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or (at
+ * your option) any later version.
+ *
+ * This library 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 Lesser
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "sox_i.h"
+
+typedef struct {sox_format_t * file;} priv_t;
+
+static int getopts(sox_effect_t * effp, int argc, char * * argv)
+{
+  priv_t * p = (priv_t *)effp->priv;
+  if (argc != 1 || !(p->file = (sox_format_t *)argv[0]) || p->file->mode != 'r')
+    return SOX_EOF;
+  return SOX_SUCCESS;
+}
+
+static int drain(
+    sox_effect_t * effp, sox_sample_t * obuf, sox_size_t * osamp)
+{
+  priv_t * p = (priv_t *)effp->priv;
+
+  /* ensure that *osamp is a multiple of the number of channels. */
+  *osamp -= *osamp % effp->out_signal.channels;
+
+  /* Read up to *osamp samples into obuf; store the actual number read
+   * back to *osamp */
+  *osamp = sox_read(p->file, obuf, *osamp);
+
+  /* sox_read may return a number that is less than was requested; only if
+   * 0 samples is returned does it indicate that end-of-file has been reached
+   * or an error has occurred */
+  if (!*osamp && p->file->sox_errno)
+    sox_fail("%s: %s", p->file->filename, p->file->sox_errstr);
+  return *osamp? SOX_SUCCESS : SOX_EOF;
+}
+
+sox_effect_handler_t const * sox_input_effect_fn(void)
+{
+  static sox_effect_handler_t handler = {
+    "input", NULL, SOX_EFF_MCHAN | SOX_EFF_DEPRECATED,
+    getopts, NULL, NULL, drain, NULL, NULL, sizeof(priv_t)
+  };
+  return &handler;
+}
+
--- /dev/null
+++ b/src/output.c
@@ -1,0 +1,59 @@
+/* libSoX effect: Output audio to a file   (c) 2008 robs@users.sourceforge.net
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or (at
+ * your option) any later version.
+ *
+ * This library 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 Lesser
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "sox_i.h"
+
+typedef struct {sox_format_t * file;} priv_t;
+
+static int getopts(sox_effect_t * effp, int argc, char * * argv)
+{
+  priv_t * p = (priv_t *)effp->priv;
+  if (argc != 1 || !(p->file = (sox_format_t *)argv[0]) || p->file->mode != 'w')
+    return SOX_EOF;
+  return SOX_SUCCESS;
+}
+
+static int flow(sox_effect_t *effp, sox_sample_t const * ibuf,
+    sox_sample_t * obuf, sox_size_t * isamp, sox_size_t * osamp)
+{
+  priv_t * p = (priv_t *)effp->priv;
+  /* Write out *isamp samples */
+  size_t len = sox_write(p->file, ibuf, *isamp);
+
+  /* len is the number of samples that were actually written out; if this is
+   * different to *isamp, then something has gone wrong--most often, it's
+   * out of disc space */
+  if (len != *isamp) {
+    sox_fail("%s: %s", p->file->filename, p->file->sox_errstr);
+    return SOX_EOF;
+  }
+
+  /* Outputting is the last `effect' in the effect chain so always passes
+   * 0 samples on to the next effect (as there isn't one!) */
+  (void)obuf, *osamp = 0;
+  return SOX_SUCCESS; /* All samples output successfully */
+}
+
+sox_effect_handler_t const * sox_output_effect_fn(void)
+{
+  static sox_effect_handler_t handler = {
+    "output", NULL, SOX_EFF_MCHAN | SOX_EFF_DEPRECATED,
+    getopts, NULL, flow, NULL, NULL, NULL, sizeof(priv_t)
+  };
+  return &handler;
+}
+