README.osx wasn't easily readable in Finder. Revert back to
[sox.git] / src / example3.c
blob6f0202aa8e007a615d0ee839fee00090a117d222
1 /* Simple example of using SoX libraries
3 * Copyright (c) 2007-9 robs@users.sourceforge.net
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 * Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #ifdef NDEBUG /* N.B. assert used with active statements so enable always. */
21 #undef NDEBUG /* Must undef above assert.h or other that might include it. */
22 #endif
24 #include "sox.h"
25 #include "util.h"
26 #include <stdio.h>
27 #include <assert.h>
30 * Example of a custom output message handler.
32 static void output_message(unsigned level, const char *filename, const char *fmt, va_list ap)
34 char const * const str[] = {"FAIL", "WARN", "INFO", "DBUG"};
35 if (sox_globals.verbosity >= level) {
36 fprintf(stderr, "%s ", str[min(level - 1, 3)]);
37 sox_output_message(stderr, filename, fmt, ap);
38 fprintf(stderr, "\n");
43 * On an alsa capable system, plays an audio file starting 10 seconds in.
44 * Copes with sample-rate and channel change if necessary since its
45 * common for audio drivers to to support subset of rates and channel
46 * counts..
47 * E.g. example3 song2.ogg
49 * Can easily be changed to work with other audio device drivers supported
50 * by libSoX; e.g. "oss", "ao", "coreaudio", etc.
51 * See the soxformat(7) manual page.
53 int main(int argc, char * argv[])
55 static sox_format_t * in, * out; /* input and output files */
56 sox_effects_chain_t * chain;
57 sox_effect_t * e;
58 char * args[10];
60 assert(argc == 2);
61 sox_globals.output_message_handler = output_message;
62 sox_globals.verbosity = 1;
64 assert(sox_init() == SOX_SUCCESS);
65 assert(in = sox_open_read(argv[1], NULL, NULL, NULL));
66 /* Change "alsa" in this line to use an alternative audio device driver: */
67 assert(out= sox_open_write("default", &in->signal, NULL, "alsa", NULL, NULL));
69 chain = sox_create_effects_chain(&in->encoding, &out->encoding);
71 e = sox_create_effect(sox_find_effect("input"));
72 args[0] = (char *)in, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
73 assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
75 e = sox_create_effect(sox_find_effect("trim"));
76 args[0] = "10", assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
77 assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
79 if (in->signal.rate != out->signal.rate) {
80 e = sox_create_effect(sox_find_effect("rate"));
81 assert(sox_effect_options(e, 0, NULL) == SOX_SUCCESS);
82 assert(sox_add_effect(chain, e, &in->signal, &out->signal) == SOX_SUCCESS);
85 if (in->signal.channels != out->signal.channels) {
86 e = sox_create_effect(sox_find_effect("channels"));
87 assert(sox_effect_options(e, 0, NULL) == SOX_SUCCESS);
88 assert(sox_add_effect(chain, e, &in->signal, &out->signal) == SOX_SUCCESS);
91 e = sox_create_effect(sox_find_effect("output"));
92 args[0] = (char *)out, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
93 assert(sox_add_effect(chain, e, &in->signal, &out->signal) == SOX_SUCCESS);
95 sox_flow_effects(chain, NULL, NULL);
97 sox_delete_effects_chain(chain);
98 sox_close(out);
99 sox_close(in);
100 sox_quit();
102 return 0;