ref: 92165d87a17ffe2a918a5e2c85f4523b9c2aaa53
parent: a26797de62057819c28732da90a48f18cb938e70
author: robs <robs>
date: Sun Mar 23 03:42:29 EDT 2008
added example2
--- a/src/.cvsignore
+++ b/src/.cvsignore
@@ -1,6 +1,6 @@
Makefile Makefile.in
sox_sample_test
-example1
+example1 example2
soxconfig.h.in soxconfig.h soxstdint.h
.deps
stamp-h1
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -43,17 +43,18 @@
cvsd htk s1-fmt u1-fmt
)
add_library(lib${PROJECT_NAME}
- effects ${formats_srcs} ${optional_srcs}
- effects_i getopt soxstdint
- ${effects_srcs} getopt1 xmalloc
- formats libsox
- formats_i libsox_i
+ effects formats_i ${optional_srcs}
+ effects_i ${formats_srcs} soxstdint
+ ${effects_srcs} libsox xmalloc
+ formats libsox_i
)
-add_executable(${PROJECT_NAME} ${PROJECT_NAME}.c)
+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(example1 example1.c)
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})
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
@@ -13,7 +13,7 @@
#########################
bin_PROGRAMS = sox
-EXTRA_PROGRAMS = example1 sox_sample_test
+EXTRA_PROGRAMS = example1 example2 sox_sample_test
lib_LTLIBRARIES = libsox.la libsfx.la
include_HEADERS = sox.h soxstdint.h
sox_SOURCES = sox.c
@@ -20,6 +20,8 @@
sox_LDADD = libsox.la libsfx.la
example1_SOURCES = example1.c
example1_LDADD = libsox.la libsfx.la
+example2_SOURCES = example2.c
+example2_LDADD = libsox.la
sox_sample_test_SOURCES = sox_sample_test.c sox_sample_test.h
#############################################
@@ -301,7 +303,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)
+all: sox$(EXEEXT) play rec soxi sox_sample_test$(EXEEXT) example1$(EXEEXT) example2$(EXEEXT)
play rec soxi: sox$(EXEEXT)
./sox --help > /dev/null
@@ -320,7 +322,7 @@
clean-local:
$(RM) play rec soxi
$(RM) sox_sample_test
- $(RM) example1
+ $(RM) example1 example2
distclean-local:
$(RM) soxstdint.h
--- /dev/null
+++ b/src/example2.c
@@ -1,0 +1,95 @@
+/*
+ * Simple example of using SoX libraries
+ *
+ * Copyright (c) 2008 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, Fifth
+ * Floor, 51 Franklin Street, Boston, MA 02111-1301, USA.
+ */
+
+#include "sox.h"
+#include "util.h"
+#include <stdio.h>
+#include <math.h>
+#ifdef NDEBUG /* N.B. assert used with active statements so enable always */
+#undef NDEBUG
+#endif
+#include <assert.h>
+
+
+/*
+ * Reads input file and displays a few seconds of wave-form, starting from
+ * a given time through the audio. E.g. example2 song2.au 30.75 1
+ */
+int main(int argc, char * argv[])
+{
+ sox_format_t * in;
+ sox_sample_t * buf;
+ size_t blocks, block_size;
+ static const double block_period = 0.025; /* seconds */
+ double start_secs = 0, period = 2;
+ char dummy;
+ sox_size_t seek;
+
+ assert(sox_format_init() == SOX_SUCCESS);
+
+ assert(argc > 1);
+ ++argv, --argc;
+
+ assert(in = sox_open_read(*argv, NULL, NULL, NULL));
+ ++argv, --argc;
+
+ if (argc) {
+ assert(sscanf(*argv, "%lf%c", &start_secs, &dummy) == 1);
+ ++argv, --argc;
+ }
+
+ if (argc) {
+ assert(sscanf(*argv, "%lf%c", &period, &dummy) == 1);
+ ++argv, --argc;
+ }
+
+ seek = start_secs * in->signal.rate * in->signal.channels + .5;
+ seek -= seek % in->signal.channels;
+ assert(sox_seek(in, seek, SOX_SEEK_SET) == SOX_SUCCESS);
+
+ block_size = block_period * in->signal.rate * in->signal.channels + .5;
+ block_size -= block_size % in->signal.channels;
+ assert(buf = malloc(sizeof(sox_sample_t) * block_size));
+
+ assert(in->signal.channels == 2);
+
+ for (blocks = 0; sox_read(in, buf, block_size) == block_size && blocks * block_period < period; ++blocks) {
+ double left = 0, right = 0;
+ size_t i, clips = 0;
+ static const char line[] = "===================================";
+ int l, r;
+
+ for (i = 0; i < block_size; ++i) {
+ double sample = SOX_SAMPLE_TO_FLOAT_64BIT(buf[i], clips);
+ if (i & 1)
+ right = max(right, fabs(sample));
+ else
+ left = max(left, fabs(sample));
+ }
+ l = (1 - left) * 35;
+ r = (1 - right) * 35;
+ printf("%8.3f%36s|%s\n", start_secs + blocks * block_period, line + l, line + r);
+ }
+
+ free(buf);
+ sox_close(in);
+ sox_format_quit();
+ return 0;
+}