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
26 unsigned int carry
; /* number of samples still to be discarded,
27 carried over from last block */
30 static int create(sox_effect_t
*effp
, int argc
, char **argv
)
32 priv_t
*p
= (priv_t
*)effp
->priv
;
35 do { /* break-able block */
36 NUMERIC_PARAMETER(factor
, 1, 16384)
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
;
55 t
= min(p
->carry
, ilen
);
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
) {
66 olen
--; ilen
-= p
->factor
;
71 p
->carry
= p
->factor
- ilen
;
75 *isamp
-= ilen
, *osamp
-= olen
;
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
)};