1 /* Simple example of using SoX libraries
2  *
3  * Copyright (c) 2007-8 robs@users.sourceforge.net
4  *
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.
9  *
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.
14  *
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.
18  */
19 
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
23 
24 #include "sox.h"
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <assert.h>
28 
29 /*
30  * Reads input file, applies vol & flanger effects, stores in output file.
31  * E.g. example1 monkey.au monkey.aiff
32  */
main(int argc,char * argv[])33 int main(int argc, char * argv[])
34 {
35   static sox_format_t * in, * out; /* input and output files */
36   sox_effects_chain_t * chain;
37   sox_effect_t * e;
38   char * args[10];
39 
40   assert(argc == 3);
41 
42   /* All libSoX applications must start by initialising the SoX library */
43   assert(sox_init() == SOX_SUCCESS);
44 
45   /* Open the input file (with default parameters) */
46   assert((in = sox_open_read(argv[1], NULL, NULL, NULL)));
47 
48   /* Open the output file; we must specify the output signal characteristics.
49    * Since we are using only simple effects, they are the same as the input
50    * file characteristics */
51   assert((out = sox_open_write(argv[2], &in->signal, NULL, NULL, NULL, NULL)));
52 
53   /* Create an effects chain; some effects need to know about the input
54    * or output file encoding so we provide that information here */
55   chain = sox_create_effects_chain(&in->encoding, &out->encoding);
56 
57   /* The first effect in the effect chain must be something that can source
58    * samples; in this case, we use the built-in handler that inputs
59    * data from an audio file */
60   e = sox_create_effect(sox_find_effect("input"));
61   args[0] = (char *)in, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
62   /* This becomes the first `effect' in the chain */
63   assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
64   free(e);
65 
66   /* Create the `vol' effect, and initialise it with the desired parameters: */
67   e = sox_create_effect(sox_find_effect("vol"));
68   args[0] = "3dB", assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
69   /* Add the effect to the end of the effects processing chain: */
70   assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
71   free(e);
72 
73   /* Create the `flanger' effect, and initialise it with default parameters: */
74   e = sox_create_effect(sox_find_effect("flanger"));
75   assert(sox_effect_options(e, 0, NULL) == SOX_SUCCESS);
76   /* Add the effect to the end of the effects processing chain: */
77   assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
78   free(e);
79 
80   /* The last effect in the effect chain must be something that only consumes
81    * samples; in this case, we use the built-in handler that outputs
82    * data to an audio file */
83   e = sox_create_effect(sox_find_effect("output"));
84   args[0] = (char *)out, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
85   assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
86   free(e);
87 
88   /* Flow samples through the effects processing chain until EOF is reached */
89   sox_flow_effects(chain, NULL, NULL);
90 
91   /* All done; tidy up: */
92   sox_delete_effects_chain(chain);
93   sox_close(out);
94   sox_close(in);
95   sox_quit();
96   return 0;
97 }
98