1 /* libSoX effect: swap pairs of audio channels
2  *
3  * First version written 01/2012 by Ulrich Klauer.
4  * Replaces an older swap effect originally written by Chris Bagwell
5  * on March 16, 1999.
6  *
7  * Copyright 2012 Chris Bagwell and SoX Contributors
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or (at
12  * your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22  */
23 
24 #include "sox_i.h"
25 
start(sox_effect_t * effp)26 static int start(sox_effect_t *effp)
27 {
28   return effp->in_signal.channels >= 2 ? SOX_SUCCESS : SOX_EFF_NULL;
29 }
30 
flow(sox_effect_t * effp,const sox_sample_t * ibuf,sox_sample_t * obuf,size_t * isamp,size_t * osamp)31 static int flow(sox_effect_t *effp, const sox_sample_t *ibuf,
32     sox_sample_t *obuf, size_t *isamp, size_t *osamp)
33 {
34   size_t len = min(*isamp, *osamp);
35   size_t channels = effp->in_signal.channels;
36   len /= channels;
37   *isamp = *osamp = len * channels;
38 
39   while (len--) {
40     size_t i;
41     for (i = 0; i + 1 < channels; i += 2) {
42       *obuf++ = ibuf[1];
43       *obuf++ = ibuf[0];
44       ibuf += 2;
45     }
46     if (channels % 2)
47       *obuf++ = *ibuf++;
48   }
49 
50   return SOX_SUCCESS;
51 }
52 
lsx_swap_effect_fn(void)53 sox_effect_handler_t const *lsx_swap_effect_fn(void)
54 {
55   static sox_effect_handler_t handler = {
56     "swap", NULL,
57     SOX_EFF_MCHAN | SOX_EFF_MODIFY,
58     NULL, start, flow, NULL, NULL, NULL,
59     0
60   };
61   return &handler;
62 }
63