formats: clarify setting of reverse_bytes
[sox.git] / src / downsample.c
blob36de6af6a63b7b5514d2786a72a627b5e4548c4b
1 /* libSoX effect: Downsample
3 * First version of this effect written 11/2011 by Ulrich Klauer.
5 * Copyright 2011 Chris Bagwell and SoX Contributors
7 * This library is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or (at
10 * your option) any later version.
12 * This library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
15 * General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "sox_i.h"
24 typedef struct {
25 unsigned int factor;
26 unsigned int carry; /* number of samples still to be discarded,
27 carried over from last block */
28 } priv_t;
30 static int create(sox_effect_t *effp, int argc, char **argv)
32 priv_t *p = (priv_t*)effp->priv;
33 p->factor = 2;
34 --argc, ++argv;
35 do { /* break-able block */
36 NUMERIC_PARAMETER(factor, 1, 16384)
37 } while (0);
38 return argc ? lsx_usage(effp) : SOX_SUCCESS;
41 static int start(sox_effect_t *effp)
43 priv_t *p = (priv_t*) effp->priv;
44 effp->out_signal.rate = effp->in_signal.rate / p->factor;
45 return p->factor == 1 ? SOX_EFF_NULL : SOX_SUCCESS;
48 static int flow(sox_effect_t *effp, const sox_sample_t *ibuf,
49 sox_sample_t *obuf, size_t *isamp, size_t *osamp)
51 priv_t *p = (priv_t*)effp->priv;
52 size_t ilen = *isamp, olen = *osamp;
53 size_t t;
55 t = min(p->carry, ilen);
56 p->carry -= t;
57 ibuf += t; ilen -= t;
59 /* NB: either p->carry (usually) or ilen is now zero; hence, a
60 non-zero value of ilen implies p->carry == 0, and there is no
61 need to test for this in the following while and if. */
63 while (ilen >= p->factor && olen) {
64 *obuf++ = *ibuf;
65 ibuf += p->factor;
66 olen--; ilen -= p->factor;
69 if (ilen && olen) {
70 *obuf++ = *ibuf;
71 p->carry = p->factor - ilen;
72 olen--; ilen = 0;
75 *isamp -= ilen, *osamp -= olen;
76 return SOX_SUCCESS;
79 sox_effect_handler_t const *lsx_downsample_effect_fn(void)
81 static sox_effect_handler_t handler = {"downsample", "[factor (2)]",
82 SOX_EFF_RATE | SOX_EFF_MODIFY,
83 create, start, flow, NULL, NULL, NULL, sizeof(priv_t)};
84 return &handler;