History

New downsample effect

A new effect for reducing the sample rate without any decimation filtering. Can be useful if the signal is known to be bandlimited already, for special tasks where aliasing is desired, or after a custom anti-aliasing filter.

Ulrich Klauer committed 14 years ago

Commit 4ec68fa1 parent 47a25248 files · +110 −5Browse files

Showing the changes to src/downsample.c (1 of 8 files). Show all

Unified · Split

src/downsample.c+85−0
@@ -0,0 +1,85 @@
1/* libSoX effect: Downsample
2 *
3 * First version of this effect written 11/2011 by Ulrich Klauer.
4 *
5 * Copyright 2011 Chris Bagwell and SoX Contributors
6 *
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.
11 *
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.
16 *
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
20 */
21
22#include "sox_i.h"
23
24typedef 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;
29
30static int create(sox_effect_t *effp, int argc, char **argv)
31{
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;
39}
40
41static int start(sox_effect_t *effp)
42{
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;
46}
47
48static int flow(sox_effect_t *effp, const sox_sample_t *ibuf,
49 sox_sample_t *obuf, size_t *isamp, size_t *osamp)
50{
51 priv_t *p = (priv_t*)effp->priv;
52 size_t ilen = *isamp, olen = *osamp;
53 size_t t;
54
55 t = min(p->carry, ilen);
56 p->carry -= t;
57 ibuf += t; ilen -= t;
58
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. */
62
63 while (ilen >= p->factor && olen) {
64 *obuf++ = *ibuf;
65 ibuf += p->factor;
66 olen--; ilen -= p->factor;
67 }
68
69 if (ilen && olen) {
70 *obuf++ = *ibuf;
71 p->carry = p->factor - ilen;
72 olen--; ilen = 0;
73 }
74
75 *isamp -= ilen, *osamp -= olen;
76 return SOX_SUCCESS;
77}
78
79sox_effect_handler_t const *lsx_downsample_effect_fn(void)
80{
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;
85}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t