1 /* libSoX effect: Output audio to a file   (c) 2008 robs@users.sourceforge.net
2  *
3  * This library is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU Lesser General Public License as published by
5  * the Free Software Foundation; either version 2.1 of the License, or (at
6  * your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation,
15  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
16  */
17 
18 #include "sox_i.h"
19 
20 typedef struct {sox_format_t * file;} priv_t;
21 
getopts(sox_effect_t * effp,int argc,char ** argv)22 static int getopts(sox_effect_t * effp, int argc, char * * argv)
23 {
24   priv_t * p = (priv_t *)effp->priv;
25   if (argc != 2 || !(p->file = (sox_format_t *)argv[1]) || p->file->mode != 'w')
26     return SOX_EOF;
27   return SOX_SUCCESS;
28 }
29 
flow(sox_effect_t * effp,sox_sample_t const * ibuf,sox_sample_t * obuf,size_t * isamp,size_t * osamp)30 static int flow(sox_effect_t *effp, sox_sample_t const * ibuf,
31     sox_sample_t * obuf, size_t * isamp, size_t * osamp)
32 {
33   priv_t * p = (priv_t *)effp->priv;
34   /* Write out *isamp samples */
35   size_t len = sox_write(p->file, ibuf, *isamp);
36 
37   /* len is the number of samples that were actually written out; if this is
38    * different to *isamp, then something has gone wrong--most often, it's
39    * out of disc space */
40   if (len != *isamp) {
41     lsx_fail("%s: %s", p->file->filename, p->file->sox_errstr);
42     return SOX_EOF;
43   }
44 
45   /* Outputting is the last `effect' in the effect chain so always passes
46    * 0 samples on to the next effect (as there isn't one!) */
47   (void)obuf, *osamp = 0;
48   return SOX_SUCCESS; /* All samples output successfully */
49 }
50 
lsx_output_effect_fn(void)51 sox_effect_handler_t const * lsx_output_effect_fn(void)
52 {
53   static sox_effect_handler_t handler = {
54     "output", NULL, SOX_EFF_MCHAN | SOX_EFF_INTERNAL,
55     getopts, NULL, flow, NULL, NULL, NULL, sizeof(priv_t)
56   };
57   return &handler;
58 }
59 
60