1 /* Simple example of using SoX libraries
2  *
3  * Copyright (c) 2007-14 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 "util.h"
26 #include <stdio.h>
27 #include <assert.h>
28 
29 /*
30  * Shows how to explicitly specify output file signal and encoding attributes.
31  *
32  * The example converts a given input file of any type to mono mu-law at 8kHz
33  * sampling-rate (providing that this is supported by the given output file
34  * type).
35  *
36  * For example:
37  *
38  *   sox -r 16k -n input.wav synth 8 sin 0:8k sin 8k:0 gain -1
39  *   ./example6 input.wav output.wav
40  *   soxi input.wav output.wav
41  *
42  * gives:
43  *
44  * Input File     : 'input.wav'
45  * Channels       : 2
46  * Sample Rate    : 16000
47  * Precision      : 32-bit
48  * Duration       : 00:00:08.00 = 128000 samples ~ 600 CDDA sectors
49  * File Size      : 1.02M
50  * Bit Rate       : 1.02M
51  * Sample Encoding: 32-bit Signed Integer PCM
52  *
53  * Input File     : 'output.wav'
54  * Channels       : 1
55  * Sample Rate    : 8000
56  * Precision      : 14-bit
57  * Duration       : 00:00:08.00 = 64000 samples ~ 600 CDDA sectors
58  * File Size      : 64.1k
59  * Bit Rate       : 64.1k
60  * Sample Encoding: 8-bit u-law
61  */
62 
main(int argc,char * argv[])63 int main(int argc, char * argv[])
64 {
65   static sox_format_t * in, * out; /* input and output files */
66   sox_effects_chain_t * chain;
67   sox_effect_t * e;
68   char * args[10];
69   sox_signalinfo_t interm_signal; /* @ intermediate points in the chain. */
70   sox_encodinginfo_t out_encoding = {
71     SOX_ENCODING_ULAW,
72     8,
73     0,
74     sox_option_default,
75     sox_option_default,
76     sox_option_default,
77     sox_false
78   };
79   sox_signalinfo_t out_signal = {
80     8000,
81     1,
82     0,
83     0,
84     NULL
85   };
86 
87   assert(argc == 3);
88   assert(sox_init() == SOX_SUCCESS);
89   assert((in = sox_open_read(argv[1], NULL, NULL, NULL)));
90   assert((out = sox_open_write(argv[2], &out_signal, &out_encoding, NULL, NULL, NULL)));
91 
92   chain = sox_create_effects_chain(&in->encoding, &out->encoding);
93 
94   interm_signal = in->signal; /* NB: deep copy */
95 
96   e = sox_create_effect(sox_find_effect("input"));
97   args[0] = (char *)in, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
98   assert(sox_add_effect(chain, e, &interm_signal, &in->signal) == SOX_SUCCESS);
99   free(e);
100 
101   if (in->signal.rate != out->signal.rate) {
102     e = sox_create_effect(sox_find_effect("rate"));
103     assert(sox_effect_options(e, 0, NULL) == SOX_SUCCESS);
104     assert(sox_add_effect(chain, e, &interm_signal, &out->signal) == SOX_SUCCESS);
105     free(e);
106   }
107 
108   if (in->signal.channels != out->signal.channels) {
109     e = sox_create_effect(sox_find_effect("channels"));
110     assert(sox_effect_options(e, 0, NULL) == SOX_SUCCESS);
111     assert(sox_add_effect(chain, e, &interm_signal, &out->signal) == SOX_SUCCESS);
112     free(e);
113   }
114 
115   e = sox_create_effect(sox_find_effect("output"));
116   args[0] = (char *)out, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
117   assert(sox_add_effect(chain, e, &interm_signal, &out->signal) == SOX_SUCCESS);
118   free(e);
119 
120   sox_flow_effects(chain, NULL, NULL);
121 
122   sox_delete_effects_chain(chain);
123   sox_close(out);
124   sox_close(in);
125   sox_quit();
126 
127   return 0;
128 }
129