shithub: sox

Download patch

ref: 9093df96336027c346b45383d47074a62e2e6c97
parent: 18722d0fc8bb401e66e00f16590ef0d530bb3797
author: robs <robs>
date: Tue Mar 31 02:54:40 EDT 2009

add example of playing audio with libsox

--- a/ChangeLog
+++ b/ChangeLog
@@ -118,7 +118,8 @@
     option (where supported).
   o [2003121] In many cases, no longer need to specify -t when inputing
     audio from a `pipe'.  (robs)
-  o Support Shoutcast more URL variants.  (robs)
+  o Support more Shoutcast URL variants.  (robs)
+  o Added libSoX example #3: playing audio.  (robs)
 
 Other bug fixes:
 
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -74,6 +74,8 @@
 target_link_libraries(example1 lib${PROJECT_NAME} lpc10 ${optional_libs})
 add_executable(example2 example2.c)
 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})
 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 sox_sample_test
+EXTRA_PROGRAMS = example0 example1 example2 example3 sox_sample_test
 lib_LTLIBRARIES = libsox.la
 include_HEADERS = sox.h
 nodist_include_HEADERS = soxstdint.h
@@ -39,6 +39,7 @@
 example0_SOURCES = example0.c
 example1_SOURCES = example1.c
 example2_SOURCES = example2.c
+example3_SOURCES = example3.c
 sox_sample_test_SOURCES = sox_sample_test.c sox_sample_test.h
 
 
@@ -119,12 +120,13 @@
 example0_LDADD = ${sox_LDADD}
 example1_LDADD = ${sox_LDADD}
 example2_LDADD = ${sox_LDADD}
+example3_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)
+all: sox$(EXEEXT) play rec soxi sox_sample_test$(EXEEXT) example0$(EXEEXT) example1$(EXEEXT) example2$(EXEEXT) example3$(EXEEXT)
 
 play rec: sox$(EXEEXT)
 	if test "$(PLAYRECLINKS)" = "yes"; then	\
@@ -155,7 +157,7 @@
 clean-local:
 	$(RM) play rec soxi
 	$(RM) sox_sample_test$(EXEEXT)
-	$(RM) example0$(EXEEXT) example1$(EXEEXT) example2$(EXEEXT)
+	$(RM) example0$(EXEEXT) example1$(EXEEXT) example2$(EXEEXT) example3$(EXEEXT)
 
 distclean-local:
 	$(RM) soxstdint.h
--- /dev/null
+++ b/src/example3.c
@@ -1,0 +1,92 @@
+/* Simple example of using SoX libraries
+ *
+ * Copyright (c) 2007-9 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>
+
+#ifdef min
+#undef min
+#endif
+#define min(a, b) ((a) <= (b) ? (a) : (b))
+
+/*
+ * Example of a custom output message handler.
+ */
+static void output_message(unsigned level, const char *filename, const char *fmt, va_list ap)
+{
+  char const * const str[] = {"FAIL", "WARN", "INFO", "DBUG"};
+  if (sox_globals.verbosity >= level) {
+    fprintf(stderr, "%s ", str[min(level - 1, 3)]);
+    sox_output_message(stderr, filename, fmt, ap);
+    fprintf(stderr, "\n");
+  }
+}
+
+/*
+ * On an alsa capable system, plays an audio file starting 10 seconds in.
+ * Copes with sample-rate change if necessary.  E.g. example3 song2.ogg
+ */
+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 == 2);
+  sox_globals.output_message_handler = output_message;
+  sox_globals.verbosity = 1;
+
+  assert(sox_init() == SOX_SUCCESS);
+  assert(in = sox_open_read(argv[1], NULL, NULL, NULL));
+  assert(out= sox_open_write("default", &in->signal, NULL, "alsa", NULL, NULL));
+
+  chain = sox_create_effects_chain(&in->encoding, &out->encoding);
+
+  e = sox_create_effect(sox_find_effect("input"));
+  args[0] = (char *)in, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
+  assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
+
+  e = sox_create_effect(sox_find_effect("trim"));
+  args[0] = "10", assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
+  assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
+
+  if (in->signal.rate != out->signal.rate) {
+    e = sox_create_effect(sox_find_effect("rate"));
+    assert(sox_effect_options(e, 0, NULL) == SOX_SUCCESS);
+    assert(sox_add_effect(chain, e, &in->signal, &out->signal) == SOX_SUCCESS);
+  }
+
+  e = sox_create_effect(sox_find_effect("output"));
+  args[0] = (char *)out, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
+  assert(sox_add_effect(chain, e, &in->signal, &out->signal) == SOX_SUCCESS);
+
+  sox_flow_effects(chain, NULL, NULL);
+
+  sox_delete_effects_chain(chain);
+  sox_close(out);
+  sox_close(in);
+  sox_format_quit();
+
+  return 0;
+}