1 /* Simple example of using SoX libraries
3 * Copyright (c) 2007-14 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. */
30 * Shows how to explicitly specify output file signal and encoding attributes.
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
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
44 * Input File : 'input.wav'
48 * Duration : 00:00:08.00 = 128000 samples ~ 600 CDDA sectors
51 * Sample Encoding: 32-bit Signed Integer PCM
53 * Input File : 'output.wav'
57 * Duration : 00:00:08.00 = 64000 samples ~ 600 CDDA sectors
60 * Sample Encoding: 8-bit u-law
63 int main(int argc
, char * argv
[])
65 static sox_format_t
* in
, * out
; /* input and output files */
66 sox_effects_chain_t
* chain
;
69 sox_signalinfo_t interm_signal
; /* @ intermediate points in the chain. */
70 sox_encodinginfo_t out_encoding
= {
79 sox_signalinfo_t out_signal
= {
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
)));
92 chain
= sox_create_effects_chain(&in
->encoding
, &out
->encoding
);
94 interm_signal
= in
->signal
; /* NB: deep copy */
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
);
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
);
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
);
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
);
120 sox_flow_effects(chain
, NULL
, NULL
);
122 sox_delete_effects_chain(chain
);