aboutsummaryrefslogtreecommitdiff
path: root/src/blockswap~.c
blob: d59f312d53c8dfe86ee7da76cb0d0b7e56db5083 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * blockswap~: swaps a signalblock around it's center
 *
 * (c) 1999-2011 IOhannes m zmölnig, forum::für::umläute, institute of electronic music and acoustics (iem)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "zexy.h"

/* ------------------------ blockswap~ ----------------------------- */

/* swaps a signalblock around it's center:
   {x[0], x[1], ... x[n-1]} --> {x[n-1], x[n-2], ... x[0]}
*/

static t_class *blockswap_class;

typedef struct _blockswap {
  t_object x_obj;
  int doit;
  int blocksize;
  t_sample *blockbuffer;
} t_blockswap;

static void blockswap_float(t_blockswap *x, t_floatarg f)
{
  x->doit = (f != 0);
}

static t_int *blockswap_perform(t_int *w)
{
  t_blockswap	*x = (t_blockswap *)(w[1]);
  t_sample *in = (t_sample *)(w[2]);
  t_sample *out = (t_sample *)(w[3]);
  int N = (int)(w[4]);
  int N2=N/2;
  if (x->doit) {
    int n=N2;
    t_sample *dummy=x->blockbuffer;
    while(n--) {
      *dummy++=*in++;
    }
    n=N-N2;
    while(n--) {
      *out++=*in++;
    }
    dummy=x->blockbuffer;
    n=N2;
    while(n--) {
      *out++=*dummy++;
    }
  } else while (N--) {
      *out++=*in++;
    }
  return (w+5);
}

static void blockswap_dsp(t_blockswap *x, t_signal **sp)
{
  if (x->blocksize*2<sp[0]->s_n) {
    if(x->blockbuffer) {
      freebytes(x->blockbuffer, sizeof(*x->blockbuffer)*x->blocksize);
    }
    x->blocksize = sp[0]->s_n/2;
    x->blockbuffer = getbytes(sizeof(*x->blockbuffer)*x->blocksize);
  }
  dsp_add(blockswap_perform, 4, x, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
}

static void blockswap_helper(t_blockswap *x)
{
  post("\n"HEARTSYMBOL" blockswap~-object for blockwise-swapping of a signal ");
  post("'help' : view this\n"
       "signal~");
  post("outlet : signal~");
}

static void blockswap_free(t_blockswap *x)
{
  if(x->blockbuffer) {
    freebytes(x->blockbuffer, sizeof(*x->blockbuffer)*x->blocksize);
  }
  x->blockbuffer=0;
}

static void *blockswap_new(void)
{
  t_blockswap *x = (t_blockswap *)pd_new(blockswap_class);
  outlet_new(&x->x_obj, gensym("signal"));
  x->doit = 1;
  x->blocksize=0;
  return (x);
}

void blockswap_tilde_setup(void)
{
  blockswap_class = class_new(gensym("blockswap~"),
                              (t_newmethod)blockswap_new, (t_method)blockswap_free,
                              sizeof(t_blockswap), 0, A_NULL);
  class_addmethod(blockswap_class, nullfn, gensym("signal"), 0);
  class_addmethod(blockswap_class, (t_method)blockswap_dsp, gensym("dsp"),
                  0);

  class_addfloat(blockswap_class, blockswap_float);

  class_addmethod(blockswap_class, (t_method)blockswap_helper,
                  gensym("help"), 0);
  zexy_register("blockswap~");
}