aboutsummaryrefslogtreecommitdiff
path: root/src/blockshuffle~.c
blob: 772c792e5c2616432ad20ec0547314335e2be1d2 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/******************************************************
 *
 * zexy - implementation file
 *
 * copyleft (c) IOhannes m zmölnig
 *
 *   1999:forum::für::umläute:2004
 *
 *   institute of electronic music and acoustics (iem)
 *
 ******************************************************
 *
 * license: GNU General Public License v.2
 *
 ******************************************************/

/*
  1110:forum::für::umläute:1999
*/

#include "zexy.h"

/* ------------------------ blockshuffle~ ----------------------------- */

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

static t_class *blockshuffle_class;

typedef struct _blockshuffle
{
  t_object x_obj;

  t_sample*blockbuf;
  t_int*   indices;
  int      size;

  t_float *shuffle;
  int shufflesize;
} t_blockshuffle;

static void blockshuffle_buildindex(t_blockshuffle *x, int blocksize){
  int i=0;
  if(blocksize!=x->size){
    if(x->indices)freebytes(x->indices, x->size);
    if(x->blockbuf)freebytes(x->blockbuf, x->size);
    x->indices=getbytes (sizeof(t_int)*blocksize);
    x->blockbuf=getbytes(sizeof(t_sample)*blocksize);
    x->size=blocksize;
  }
  for(i=0;i<x->shufflesize&&i<blocksize; i++){
    int idx=x->shuffle[i];
    if(idx>=blocksize)idx=blocksize-1;
    if(idx<0)idx=0;
    x->indices[i]=idx;
  }
  for(;i<blocksize; i++){
    x->indices[i]=i;
  }
}

static void blockshuffle_list(t_blockshuffle *x, t_symbol*s, int argc, t_atom*argv)
{
  int i;
  if(x->shuffle){
    freebytes(x->shuffle, x->shufflesize);
    x->shuffle=0;
  }
  x->shufflesize=argc;
  x->shuffle=getbytes(sizeof(t_float)*argc);

  for(i=0; i<argc; i++){
    x->shuffle[i]=atom_getfloat(argv++);
  }
  blockshuffle_buildindex(x, x->size);
}

static t_int *blockshuffle_perform(t_int *w)
{
  t_blockshuffle*x = (t_blockshuffle *)(w[1]);
  t_sample *in = (t_sample *)(w[2]);
  t_sample *out = (t_sample *)(w[3]);
  int n = (int)(w[4]);
  int i=0;
  t_sample *temp=x->blockbuf;
  t_int    *idx =x->indices;

  if(idx){
  for(i=0; i<n; i++){
    temp[i]=in[idx[i]];
  }
  temp=x->blockbuf;
  for(i=0; i<n; i++){
    *out++=*temp++;
  }
  } else
    while(n--)*out++=*in++;

  return (w+5);
}

static void blockshuffle_dsp(t_blockshuffle *x, t_signal **sp)
{
  blockshuffle_buildindex(x, sp[0]->s_n);

  dsp_add(blockshuffle_perform, 4, x, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
}

static void blockshuffle_helper(void)
{
  post("\n%c blockshuffle~-object for shuffling the samples within a signal-block", HEARTSYMBOL);
  post("'help' : view this\n"
       "signal~");
  post("outlet : signal~");
}
static void blockshuffle_free(t_blockshuffle *x){
  if(x->indices)freebytes(x->indices, x->size);
  if(x->blockbuf)freebytes(x->blockbuf, x->size);
  if(x->shuffle)freebytes(x->shuffle, x->shufflesize);
}

static void *blockshuffle_new(void)
{
  t_blockshuffle *x = (t_blockshuffle *)pd_new(blockshuffle_class);
  outlet_new(&x->x_obj, gensym("signal"));
  x->size=0;
  x->blockbuf=0;
  x->indices=0;
  x->shuffle=0;
  x->shufflesize=0;
  return (x);
}

void blockshuffle_tilde_setup(void)
{
  blockshuffle_class = class_new(gensym("blockshuffle~"), (t_newmethod)blockshuffle_new, 
                                 (t_method)blockshuffle_free,
                                 sizeof(t_blockshuffle), 0, A_NULL);
  class_addmethod(blockshuffle_class, nullfn, gensym("signal"), 0);
  class_addmethod(blockshuffle_class, (t_method)blockshuffle_dsp, gensym("dsp"), 0);
  
  class_addlist(blockshuffle_class, blockshuffle_list);
  
  class_addmethod(blockshuffle_class, (t_method)blockshuffle_helper, gensym("help"), 0);
  class_sethelpsymbol(blockshuffle_class, gensym("zexy/blockshuffle~"));
  zexy_register("blockshuffle~");
}