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 static sox_format_t * in, * out; /* input and output files */
30
31 /* The function that will be called to input samples into the effects chain.
32 * In this example, we get samples to process from a SoX-openned audio file.
33 * In a different application, they might be generated or come from a different
34 * part of the application. */
input_drain(sox_effect_t * effp,sox_sample_t * obuf,size_t * osamp)35 static int input_drain(
36 sox_effect_t * effp, sox_sample_t * obuf, size_t * osamp)
37 {
38 (void)effp; /* This parameter is not needed in this example */
39
40 /* ensure that *osamp is a multiple of the number of channels. */
41 *osamp -= *osamp % effp->out_signal.channels;
42
43 /* Read up to *osamp samples into obuf; store the actual number read
44 * back to *osamp */
45 *osamp = sox_read(in, obuf, *osamp);
46
47 /* sox_read may return a number that is less than was requested; only if
48 * 0 samples is returned does it indicate that end-of-file has been reached
49 * or an error has occurred */
50 if (!*osamp && in->sox_errno)
51 fprintf(stderr, "%s: %s\n", in->filename, in->sox_errstr);
52 return *osamp? SOX_SUCCESS : SOX_EOF;
53 }
54
55 /* The function that will be called to output samples from the effects chain.
56 * In this example, we store the samples in a SoX-opened audio file.
57 * In a different application, they might perhaps be analysed in some way,
58 * or displayed as a wave-form */
output_flow(sox_effect_t * effp LSX_UNUSED,sox_sample_t const * ibuf,sox_sample_t * obuf LSX_UNUSED,size_t * isamp,size_t * osamp)59 static int output_flow(sox_effect_t *effp LSX_UNUSED, sox_sample_t const * ibuf,
60 sox_sample_t * obuf LSX_UNUSED, size_t * isamp, size_t * osamp)
61 {
62 /* Write out *isamp samples */
63 size_t len = sox_write(out, ibuf, *isamp);
64
65 /* len is the number of samples that were actually written out; if this is
66 * different to *isamp, then something has gone wrong--most often, it's
67 * out of disc space */
68 if (len != *isamp) {
69 fprintf(stderr, "%s: %s\n", out->filename, out->sox_errstr);
70 return SOX_EOF;
71 }
72
73 /* Outputting is the last `effect' in the effect chain so always passes
74 * 0 samples on to the next effect (as there isn't one!) */
75 *osamp = 0;
76
77 (void)effp; /* This parameter is not needed in this example */
78
79 return SOX_SUCCESS; /* All samples output successfully */
80 }
81
82 /* A `stub' effect handler to handle inputting samples to the effects
83 * chain; the only function needed for this example is `drain' */
input_handler(void)84 static sox_effect_handler_t const * input_handler(void)
85 {
86 static sox_effect_handler_t handler = {
87 "input", NULL, SOX_EFF_MCHAN, NULL, NULL, NULL, input_drain, NULL, NULL, 0
88 };
89 return &handler;
90 }
91
92 /* A `stub' effect handler to handle outputting samples from the effects
93 * chain; the only function needed for this example is `flow' */
output_handler(void)94 static sox_effect_handler_t const * output_handler(void)
95 {
96 static sox_effect_handler_t handler = {
97 "output", NULL, SOX_EFF_MCHAN, NULL, NULL, output_flow, NULL, NULL, NULL, 0
98 };
99 return &handler;
100 }
101
102 /*
103 * Reads input file, applies vol & flanger effects, stores in output file.
104 * E.g. example1 monkey.au monkey.aiff
105 */
main(int argc,char * argv[])106 int main(int argc, char * argv[])
107 {
108 sox_effects_chain_t * chain;
109 sox_effect_t * e;
110 char * vol[] = {"3dB"};
111
112 assert(argc == 3);
113
114 /* All libSoX applications must start by initialising the SoX library */
115 assert(sox_init() == SOX_SUCCESS);
116
117 /* Open the input file (with default parameters) */
118 assert(in = sox_open_read(argv[1], NULL, NULL, NULL));
119
120 /* Open the output file; we must specify the output signal characteristics.
121 * Since we are using only simple effects, they are the same as the input
122 * file characteristics */
123 assert(out = sox_open_write(argv[2], &in->signal, NULL, NULL, NULL, NULL));
124
125 /* Create an effects chain; some effects need to know about the input
126 * or output file encoding so we provide that information here */
127 chain = sox_create_effects_chain(&in->encoding, &out->encoding);
128
129 /* The first effect in the effect chain must be something that can source
130 * samples; in this case, we have defined an input handler that inputs
131 * data from an audio file */
132 e = sox_create_effect(input_handler());
133 /* This becomes the first `effect' in the chain */
134 assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
135 free(e);
136
137 /* Create the `vol' effect, and initialise it with the desired parameters: */
138 e = sox_create_effect(sox_find_effect("vol"));
139 assert(sox_effect_options(e, 1, vol) == SOX_SUCCESS);
140 /* Add the effect to the end of the effects processing chain: */
141 assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
142 free(e);
143
144 /* Create the `flanger' effect, and initialise it with default parameters: */
145 e = sox_create_effect(sox_find_effect("flanger"));
146 assert(sox_effect_options(e, 0, NULL) == SOX_SUCCESS);
147 /* Add the effect to the end of the effects processing chain: */
148 assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
149 free(e);
150
151 /* The last effect in the effect chain must be something that only consumes
152 * samples; in this case, we have defined an output handler that outputs
153 * data to an audio file */
154 e = sox_create_effect(output_handler());
155 assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
156 free(e);
157
158 /* Flow samples through the effects processing chain until EOF is reached */
159 sox_flow_effects(chain, NULL, NULL);
160
161 /* All done; tidy up: */
162 sox_delete_effects_chain(chain);
163 sox_close(out);
164 sox_close(in);
165 sox_quit();
166 return 0;
167 }
168