1 /* libSoX effect: Input audio from 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 != 'r')
26     return SOX_EOF;
27   return SOX_SUCCESS;
28 }
29 
drain(sox_effect_t * effp,sox_sample_t * obuf,size_t * osamp)30 static int drain(
31     sox_effect_t * effp, sox_sample_t * obuf, size_t * osamp)
32 {
33   priv_t * p = (priv_t *)effp->priv;
34 
35   /* ensure that *osamp is a multiple of the number of channels. */
36   *osamp -= *osamp % effp->out_signal.channels;
37 
38   /* Read up to *osamp samples into obuf; store the actual number read
39    * back to *osamp */
40   *osamp = sox_read(p->file, obuf, *osamp);
41 
42   /* sox_read may return a number that is less than was requested; only if
43    * 0 samples is returned does it indicate that end-of-file has been reached
44    * or an error has occurred */
45   if (!*osamp && p->file->sox_errno)
46     lsx_fail("%s: %s", p->file->filename, p->file->sox_errstr);
47   return *osamp? SOX_SUCCESS : SOX_EOF;
48 }
49 
lsx_input_effect_fn(void)50 sox_effect_handler_t const * lsx_input_effect_fn(void)
51 {
52   static sox_effect_handler_t handler = {
53     "input", NULL, SOX_EFF_MCHAN | SOX_EFF_LENGTH | SOX_EFF_INTERNAL,
54     getopts, NULL, NULL, drain, NULL, NULL, sizeof(priv_t)
55   };
56   return &handler;
57 }
58 
59