formats: clarify setting of reverse_bytes
[sox.git] / src / example3.c
blobe49d12689832a422c0ad09ac2ddea96464d955e4
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 char base_name[128];
37 sox_basename(base_name, sizeof(base_name), filename);
38 fprintf(stderr, "%s %s: ", str[min(level - 1, 3)], base_name);
39 vfprintf(stderr, fmt, ap);
40 fprintf(stderr, "\n");
45 * On an alsa capable system, plays an audio file starting 10 seconds in.
46 * Copes with sample-rate and channel change if necessary since its
47 * common for audio drivers to support a subset of rates and channel
48 * counts.
49 * E.g. example3 song2.ogg
51 * Can easily be changed to work with other audio device drivers supported
52 * by libSoX; e.g. "oss", "ao", "coreaudio", etc.
53 * See the soxformat(7) manual page.
55 int main(int argc, char * argv[])
57 static sox_format_t * in, * out; /* input and output files */
58 sox_effects_chain_t * chain;
59 sox_effect_t * e;
60 sox_signalinfo_t interm_signal;
61 char * args[10];
63 assert(argc == 2);
64 sox_globals.output_message_handler = output_message;
65 sox_globals.verbosity = 1;
67 assert(sox_init() == SOX_SUCCESS);
68 assert((in = sox_open_read(argv[1], NULL, NULL, NULL)));
69 /* Change "alsa" in this line to use an alternative audio device driver: */
70 assert((out= sox_open_write("default", &in->signal, NULL, "alsa", NULL, NULL)));
72 chain = sox_create_effects_chain(&in->encoding, &out->encoding);
74 interm_signal = in->signal; /* NB: deep copy */
76 e = sox_create_effect(sox_find_effect("input"));
77 args[0] = (char *)in, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
78 assert(sox_add_effect(chain, e, &interm_signal, &in->signal) == SOX_SUCCESS);
79 free(e);
81 e = sox_create_effect(sox_find_effect("trim"));
82 args[0] = "10", assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
83 assert(sox_add_effect(chain, e, &interm_signal, &in->signal) == SOX_SUCCESS);
84 free(e);
86 if (in->signal.rate != out->signal.rate) {
87 e = sox_create_effect(sox_find_effect("rate"));
88 assert(sox_effect_options(e, 0, NULL) == SOX_SUCCESS);
89 assert(sox_add_effect(chain, e, &interm_signal, &out->signal) == SOX_SUCCESS);
90 free(e);
93 if (in->signal.channels != out->signal.channels) {
94 e = sox_create_effect(sox_find_effect("channels"));
95 assert(sox_effect_options(e, 0, NULL) == SOX_SUCCESS);
96 assert(sox_add_effect(chain, e, &interm_signal, &out->signal) == SOX_SUCCESS);
97 free(e);
100 e = sox_create_effect(sox_find_effect("output"));
101 args[0] = (char *)out, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
102 assert(sox_add_effect(chain, e, &interm_signal, &out->signal) == SOX_SUCCESS);
103 free(e);
105 sox_flow_effects(chain, NULL, NULL);
107 sox_delete_effects_chain(chain);
108 sox_close(out);
109 sox_close(in);
110 sox_quit();
112 return 0;